Skip to content

Commit

Permalink
removing array manipulations
Browse files Browse the repository at this point in the history
  • Loading branch information
Janther committed Dec 16, 2023
1 parent 556a7f1 commit b966c71
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
44 changes: 18 additions & 26 deletions src/common/printer-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down
18 changes: 5 additions & 13 deletions src/nodes/AssemblyLocalDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')] : ''
]
};
26 changes: 9 additions & 17 deletions src/nodes/ExpressionStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? '' : ';'
];
}
};

0 comments on commit b966c71

Please sign in to comment.