Skip to content

Commit e12adf4

Browse files
alan-agius4mgechev
authored andcommitted
refactor: create helper functions for pure comments
1 parent 6b2699d commit e12adf4

File tree

5 files changed

+33
-46
lines changed

5 files changed

+33
-46
lines changed

packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
import * as ts from 'typescript';
99

10+
const pureFunctionComment = '@__PURE__';
11+
1012
// Find all nodes from the AST in the subtree of node of SyntaxKind kind.
1113
export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T[] {
1214
const nodes: T[] = [];
@@ -20,3 +22,22 @@ export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.Synt
2022

2123
return nodes;
2224
}
25+
26+
export function addPureComment<T extends ts.Node>(node: T): T {
27+
return ts.addSyntheticLeadingComment(
28+
node,
29+
ts.SyntaxKind.MultiLineCommentTrivia,
30+
pureFunctionComment,
31+
false,
32+
);
33+
}
34+
35+
export function hasPureComment(node: ts.Node): boolean {
36+
if (!node) {
37+
return false;
38+
}
39+
40+
const leadingComment = ts.getSyntheticLeadingComments(node);
41+
42+
return !!leadingComment && leadingComment.some(comment => comment.text === pureFunctionComment);
43+
}

packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import * as ts from 'typescript';
9+
import { addPureComment } from '../helpers/ast-utils';
910

1011
interface ClassData {
1112
name: string;
@@ -81,18 +82,12 @@ export function getFoldFileTransformer(program: ts.Program): ts.TransformerFacto
8182
const classStatement = clazz.declaration as ts.ClassDeclaration;
8283
const innerReturn = ts.createReturn(ts.createIdentifier(clazz.name));
8384

84-
const iife = ts.createImmediatelyInvokedFunctionExpression([
85-
classStatement,
86-
...clazz.statements.map(st => st.expressionStatement),
87-
innerReturn,
88-
]);
89-
90-
const pureIife = ts.addSyntheticLeadingComment(
91-
iife,
92-
ts.SyntaxKind.MultiLineCommentTrivia,
93-
'@__PURE__',
94-
false,
95-
);
85+
const pureIife = addPureComment(
86+
ts.createImmediatelyInvokedFunctionExpression([
87+
classStatement,
88+
...clazz.statements.map(st => st.expressionStatement),
89+
innerReturn,
90+
]));
9691

9792
// Move the original class modifiers to the var statement.
9893
const newNode = ts.createVariableStatement(

packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import * as ts from 'typescript';
9+
import { addPureComment } from '../helpers/ast-utils';
910

1011
/**
1112
* @deprecated From 0.9.0
@@ -42,9 +43,6 @@ const extendsHelperName = '__extends';
4243
export function getPrefixClassesTransformer(): ts.TransformerFactory<ts.SourceFile> {
4344
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
4445
const transformer: ts.Transformer<ts.SourceFile> = (sf: ts.SourceFile) => {
45-
46-
const pureFunctionComment = '@__PURE__';
47-
4846
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
4947

5048
// Add pure comment to downleveled classes.
@@ -63,12 +61,7 @@ export function getPrefixClassesTransformer(): ts.TransformerFactory<ts.SourceFi
6361
varDecl,
6462
varDecl.name,
6563
varDecl.type,
66-
ts.addSyntheticLeadingComment(
67-
varInitializer,
68-
ts.SyntaxKind.MultiLineCommentTrivia,
69-
pureFunctionComment,
70-
false,
71-
),
64+
addPureComment(varInitializer),
7265
),
7366
],
7467
),

packages/angular_devkit/build_optimizer/src/transforms/prefix-functions.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import * as ts from 'typescript';
9-
10-
11-
const pureFunctionComment = '@__PURE__';
9+
import { addPureComment, hasPureComment } from '../helpers/ast-utils';
1210

1311
export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.SourceFile> {
1412
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
@@ -19,8 +17,7 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.Source
1917
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
2018
// Add pure function comment to top level functions.
2119
if (topLevelFunctions.has(node)) {
22-
const newNode = ts.addSyntheticLeadingComment(
23-
node, ts.SyntaxKind.MultiLineCommentTrivia, pureFunctionComment, false);
20+
const newNode = addPureComment(node);
2421

2522
// Replace node with modified one.
2623
return ts.visitEachChild(newNode, visitor, context);
@@ -96,12 +93,3 @@ export function findTopLevelFunctions(parentNode: ts.Node): Set<ts.Node> {
9693

9794
return topLevelFunctions;
9895
}
99-
100-
function hasPureComment(node: ts.Node) {
101-
if (!node) {
102-
return false;
103-
}
104-
const leadingComment = ts.getSyntheticLeadingComments(node);
105-
106-
return leadingComment && leadingComment.some((comment) => comment.text === pureFunctionComment);
107-
}

packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import * as ts from 'typescript';
9+
import { addPureComment } from '../helpers/ast-utils';
910

1011
function isBlockLike(node: ts.Node): node is ts.BlockLike {
1112
return node.kind === ts.SyntaxKind.Block
@@ -384,17 +385,6 @@ function findEnumNameStatements(
384385
return enumStatements;
385386
}
386387

387-
function addPureComment<T extends ts.Node>(node: T): T {
388-
const pureFunctionComment = '@__PURE__';
389-
390-
return ts.addSyntheticLeadingComment(
391-
node,
392-
ts.SyntaxKind.MultiLineCommentTrivia,
393-
pureFunctionComment,
394-
false,
395-
);
396-
}
397-
398388
function updateHostNode(
399389
hostNode: ts.VariableStatement,
400390
expression: ts.Expression,

0 commit comments

Comments
 (0)