Skip to content
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;
}
}