Skip to content

Simplify suppressLeadingAndTrailingTrivia #22356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Mar 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,37 +1386,30 @@ namespace ts {
*/
/* @internal */
export function suppressLeadingAndTrailingTrivia(node: Node) {
Debug.assert(node !== undefined);

suppressLeading(node);
suppressTrailing(node);

function suppressLeading(node: Node) {
addEmitFlags(node, EmitFlags.NoLeadingComments);

const firstChild = forEachChild(node, child => child);
if (firstChild) {
suppressLeading(firstChild);
}
Debug.assertDefined(node);
suppress(node, EmitFlags.NoLeadingComments, getFirstChild);
suppress(node, EmitFlags.NoTrailingComments, getLastChild);
function suppress(node: Node, flag: EmitFlags, getChild: (n: Node) => Node) {
addEmitFlags(node, flag);
const child = getChild(node);
if (child) suppress(child, flag, getChild);
}
}

function suppressTrailing(node: Node) {
addEmitFlags(node, EmitFlags.NoTrailingComments);

let lastChild: Node;
forEachChild(
node,
child => (lastChild = child, undefined),
children => {
// As an optimization, jump straight to the end of the list.
if (children.length) {
lastChild = last(children);
}
return undefined;
});
if (lastChild) {
suppressTrailing(lastChild);
}
}
function getFirstChild(node: Node): Node | undefined {
return node.forEachChild(child => child);
}

function getLastChild(node: Node): Node | undefined {
let lastChild: Node | undefined;
node.forEachChild(
child => { lastChild = child; },
children => {
// As an optimization, jump straight to the end of the list.
if (children.length) {
lastChild = last(children);
}
});
return lastChild;
}
}