diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 0b6646412201b..9ecd30e3a9e5c 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -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; } }