Simplify suppressLeadingAndTrailingTrivia (#22356)
This commit is contained in:
parent
a138985448
commit
2fb7e643f4
1 changed files with 24 additions and 31 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue