All files / src/nodes IndexAccess.js

100% Statements 12/12
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39        55x   55x 55x   290x 290x               290x 65x       65x   65x         65x     225x       55x  
const {
  doc: {
    builders: { group, indent, indentIfBreak, label, softline }
  }
} = require('prettier');
 
let groupIndex = 0;
const IndexAccess = {
  print: ({ path, print }) => {
    let baseDoc = path.call(print, 'base');
    let indexDoc = group([
      indent([softline, path.call(print, 'index')]),
      softline,
      ']'
    ]);
 
    // If we are at the end of a MemberAccessChain we should indent the
    // arguments accordingly.
    if (baseDoc.label === 'MemberAccessChain') {
      baseDoc = group(baseDoc.contents, {
        id: `IndexAccess.base-${groupIndex}`
      });
 
      groupIndex += 1;
 
      indexDoc = indentIfBreak(indexDoc, {
        groupId: baseDoc.id
      });
      // We wrap the expression in a label in case there is an IndexAccess or
      // a FunctionCall following this IndexAccess.
      return label('MemberAccessChain', [baseDoc, '[', indexDoc]);
    }
 
    return [baseDoc, '[', indexDoc];
  }
};
 
module.exports = IndexAccess;