All files / src/nodes VariableDeclarationStatement.js

100% Statements 15/15
100% Branches 12/12
100% Functions 4/4
100% Lines 14/14

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        55x   55x   55x 645x   55x 645x   55x 55x     735x   645x                   645x 645x   645x                   55x  
const {
  doc: {
    builders: { group, indentIfBreak }
  }
} = require('prettier');
 
const printSeparatedList = require('./print-separated-list');
 
const embraceVariables = (doc, embrace) =>
  embrace ? ['(', printSeparatedList(doc), ')'] : doc;
 
const initialValue = (node, path, print) =>
  node.initialValue ? [' = ', path.call(print, 'initialValue')] : '';
 
let groupIndex = 0;
const VariableDeclarationStatement = {
  print: ({ node, path, print }) => {
    const startsWithVar =
      node.variables.filter((x) => x && x.typeName).length === 0;
 
    const declarationDoc = group(
      [
        startsWithVar ? 'var ' : '',
        embraceVariables(
          path.map(print, 'variables'),
          node.variables.length > 1 || startsWithVar
        )
      ],
      { id: `VariableDeclarationStatement.variables-${groupIndex}` }
    );
    groupIndex += 1;
    const initialValueDoc = initialValue(node, path, print);
 
    return group([
      declarationDoc,
      indentIfBreak(initialValueDoc, {
        groupId: declarationDoc.id
      }),
      node.omitSemicolon ? '' : ';'
    ]);
  }
};
 
module.exports = VariableDeclarationStatement;