Simplify suppressLeadingAndTrailingTrivia (#22356)

This commit is contained in:
Andy 2018-03-06 11:34:33 -08:00 committed by GitHub
parent a138985448
commit 2fb7e643f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
}
}
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);
}
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 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;
}
}