All files / src/nodes print-preserving-empty-lines.js

100% Statements 17/17
100% Branches 18/18
100% Functions 2/2
100% Lines 17/17

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          55x     4150x 4150x 11021x 11021x   11021x             6936x     11021x 6961x       40x       11021x   11021x             3823x       4150x 597x     4150x     55x  
const {
  doc: {
    builders: { hardline }
  },
  util: { isNextLineEmptyAfterIndex }
} = require('prettier');
 
function printPreservingEmptyLines(path, key, options, print) {
  const parts = [];
  path.each((childPath) => {
    const node = childPath.getValue();
    const nodeType = node.type;
 
    if (
      // Avoid adding a hardline at the beginning of the document.
      parts.length !== 0 &&
      // LabelDefinition adds a dedented line so we don't have to prepend a
      // hardline.
      nodeType !== 'LabelDefinition'
    ) {
      parts.push(hardline);
    }
 
    if (childPath.getName() > 0) {
      if (
        ['ContractDefinition', 'FunctionDefinition'].includes(nodeType) &&
        parts[parts.length - 2] !== hardline
      ) {
        parts.push(hardline);
      }
    }
 
    parts.push(print(childPath));
 
    if (
      isNextLineEmptyAfterIndex(
        options.originalText,
        options.locEnd(node) + 1
      ) ||
      nodeType === 'FunctionDefinition'
    ) {
      parts.push(hardline);
    }
  }, key);
 
  if (parts.length > 1 && parts[parts.length - 1] === hardline) {
    parts.pop();
  }
 
  return parts;
}
 
module.exports = printPreservingEmptyLines;