diff --git a/src/common/printer-helpers.js b/src/common/printer-helpers.js index e3d2ef111..0d768e4c6 100644 --- a/src/common/printer-helpers.js +++ b/src/common/printer-helpers.js @@ -37,35 +37,27 @@ export const printComments = (node, path, options, filter = () => true) => { }; export function printPreservingEmptyLines(path, key, options, print) { - const parts = []; - path.each((childPath, index) => { + return path.map((childPath, index) => { const node = childPath.getValue(); - const nodeType = node.type; - if ( - // Avoid adding a hardline at the beginning of the document. - parts.length !== 0 && + return [ + // Only attempt to prepend an empty line if `node` is not the first item + index > 0 && // LabelDefinition adds a dedented line so we don't have to prepend a // hardline. - nodeType !== 'LabelDefinition' - ) { - parts.push(hardline); - } - - parts.push(print(childPath)); - - // Only attempt to append an empty line if `node` is not the last item - if ( + node.type !== 'LabelDefinition' + ? hardline + : '', + print(childPath), + // Only attempt to append an empty line if `node` is not the last item !isLast(childPath, key, index) && + // Append an empty line if the original text already had an one after the + // current `node` isNextLineEmpty(options.originalText, options.locEnd(node) + 1) - ) { - // Append an empty line if the original text already had an one after - // the current `node` - parts.push(hardline); - } + ? hardline + : '' + ]; }, key); - - return parts; } // This function will add an indentation to the `item` and separate it from the @@ -77,10 +69,10 @@ export const printSeparatedItem = ( lastSeparator = firstSeparator, grouped = true } = {} -) => { - const document = [indent([firstSeparator, item]), lastSeparator]; - return grouped ? group(document) : document; -}; +) => + grouped + ? group([indent([firstSeparator, item]), lastSeparator]) + : [indent([firstSeparator, item]), lastSeparator]; // This function will add an indentation to the `list` and separate it from the // rest of the `doc` in most cases by a `softline`. diff --git a/src/nodes/AssemblyLocalDefinition.js b/src/nodes/AssemblyLocalDefinition.js index 9e2c17e76..ef3a76439 100644 --- a/src/nodes/AssemblyLocalDefinition.js +++ b/src/nodes/AssemblyLocalDefinition.js @@ -4,17 +4,9 @@ import { printSeparatedList } from '../common/printer-helpers.js'; const { line } = doc.builders; export const AssemblyLocalDefinition = { - print: ({ node, path, print }) => { - const parts = [ - 'let', - printSeparatedList(path.map(print, 'names'), { firstSeparator: line }) - ]; - - if (node.expression !== null) { - parts.push(':= '); - parts.push(path.call(print, 'expression')); - } - - return parts; - } + print: ({ node, path, print }) => [ + 'let', + printSeparatedList(path.map(print, 'names'), { firstSeparator: line }), + node.expression ? [':= ', path.call(print, 'expression')] : '' + ] }; diff --git a/src/nodes/ExpressionStatement.js b/src/nodes/ExpressionStatement.js index 6149028c4..b4866bc06 100644 --- a/src/nodes/ExpressionStatement.js +++ b/src/nodes/ExpressionStatement.js @@ -5,23 +5,15 @@ const { hardline } = doc.builders; export const ExpressionStatement = { print: ({ node, options, path, print }) => { - const parts = []; + const comments = + path.getParentNode().type === 'IfStatement' + ? printComments(node, path, options) + : []; - const parent = path.getParentNode(); - - if (parent.type === 'IfStatement') { - if (node.comments?.length) { - const comments = printComments(node, path, options); - if (comments?.length) { - parts.push(comments); - parts.push(hardline); - } - } - } - - parts.push(path.call(print, 'expression')); - parts.push(node.omitSemicolon ? [] : ';'); - - return parts; + return [ + comments.length ? [comments, hardline] : '', + path.call(print, 'expression'), + node.omitSemicolon ? '' : ';' + ]; } };