Skip to content

Commit dfac605

Browse files
author
Andy Hanson
committed
Simplify suppressLeadingAndTrailingTrivia
1 parent e5f91f5 commit dfac605

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/services/utilities.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,37 +1386,30 @@ namespace ts {
13861386
*/
13871387
/* @internal */
13881388
export function suppressLeadingAndTrailingTrivia(node: Node) {
1389-
Debug.assert(node !== undefined);
1390-
1391-
suppressLeading(node);
1392-
suppressTrailing(node);
1393-
1394-
function suppressLeading(node: Node) {
1395-
addEmitFlags(node, EmitFlags.NoLeadingComments);
1396-
1397-
const firstChild = forEachChild(node, child => child);
1398-
if (firstChild) {
1399-
suppressLeading(firstChild);
1400-
}
1389+
Debug.assertDefined(node);
1390+
suppress(node, EmitFlags.NoLeadingComments, getFirstChild);
1391+
suppress(node, EmitFlags.NoTrailingComments, getLastChild);
1392+
function suppress(node: Node, flag: EmitFlags, getChild: (n: Node) => Node) {
1393+
addEmitFlags(node, flag);
1394+
const child = getChild(node);
1395+
if (child) suppress(child, flag, getChild);
14011396
}
1397+
}
14021398

1403-
function suppressTrailing(node: Node) {
1404-
addEmitFlags(node, EmitFlags.NoTrailingComments);
1405-
1406-
let lastChild: Node;
1407-
forEachChild(
1408-
node,
1409-
child => (lastChild = child, undefined),
1410-
children => {
1411-
// As an optimization, jump straight to the end of the list.
1412-
if (children.length) {
1413-
lastChild = last(children);
1414-
}
1415-
return undefined;
1416-
});
1417-
if (lastChild) {
1418-
suppressTrailing(lastChild);
1419-
}
1420-
}
1399+
function getFirstChild(node: Node): Node | undefined {
1400+
return node.forEachChild(child => child);
1401+
}
1402+
1403+
function getLastChild(node: Node): Node | undefined {
1404+
let lastChild: Node | undefined;
1405+
node.forEachChild(
1406+
child => { lastChild = child; },
1407+
children => {
1408+
// As an optimization, jump straight to the end of the list.
1409+
if (children.length) {
1410+
lastChild = last(children);
1411+
}
1412+
});
1413+
return lastChild;
14211414
}
14221415
}

0 commit comments

Comments
 (0)