All files / src/nodes FunctionCall.js

100% Statements 23/23
100% Branches 14/14
100% Functions 4/4
100% Lines 23/23

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66        55x   55x   55x 45x 45x         115x                 55x 1733x       55x 55x   2098x 2098x   2098x 1778x 45x   1733x           2098x 405x       405x   405x         405x     1693x       55x  
const {
  doc: {
    builders: { group, indentIfBreak, label, line, softline }
  }
} = require('prettier');
 
const printSeparatedList = require('./print-separated-list');
 
const printObject = (path, print, options) => {
  const identifiers = path.map(print, 'identifiers');
  return [
    '{',
    printSeparatedList(
      path
        .map(print, 'arguments')
        .map((arg, index) => [identifiers[index], ': ', arg]),
      {
        firstSeparator: options.bracketSpacing ? line : softline,
        lastSeparator: [options.bracketSpacing ? line : softline, '})']
      }
    )
  ];
};
 
const printArguments = (path, print) =>
  printSeparatedList(path.map(print, 'arguments'), {
    lastSeparator: [softline, ')']
  });
 
let groupIndex = 0;
const FunctionCall = {
  print: ({ node, path, print, options }) => {
    let expressionDoc = path.call(print, 'expression');
    let argumentsDoc = ')';
 
    if (node.arguments && node.arguments.length > 0) {
      if (node.identifiers && node.identifiers.length > 0) {
        argumentsDoc = printObject(path, print, options);
      } else {
        argumentsDoc = printArguments(path, print);
      }
    }
 
    // If we are at the end of a MemberAccessChain we should indent the
    // arguments accordingly.
    if (expressionDoc.label === 'MemberAccessChain') {
      expressionDoc = group(expressionDoc.contents, {
        id: `FunctionCall.expression-${groupIndex}`
      });
 
      groupIndex += 1;
 
      argumentsDoc = indentIfBreak(argumentsDoc, {
        groupId: expressionDoc.id
      });
      // We wrap the expression in a label in case there is an IndexAccess or
      // a FunctionCall following this IndexAccess.
      return label('MemberAccessChain', [expressionDoc, '(', argumentsDoc]);
    }
 
    return [expressionDoc, '(', argumentsDoc];
  }
};
 
module.exports = FunctionCall;