diff --git a/packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts b/packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts index 131dfbf8d4..8c34506d32 100644 --- a/packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts +++ b/packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts @@ -7,6 +7,8 @@ */ import * as ts from 'typescript'; +const pureFunctionComment = '@__PURE__'; + // Find all nodes from the AST in the subtree of node of SyntaxKind kind. export function collectDeepNodes(node: ts.Node, kind: ts.SyntaxKind): T[] { const nodes: T[] = []; @@ -20,3 +22,22 @@ export function collectDeepNodes(node: ts.Node, kind: ts.Synt return nodes; } + +export function addPureComment(node: T): T { + return ts.addSyntheticLeadingComment( + node, + ts.SyntaxKind.MultiLineCommentTrivia, + pureFunctionComment, + false, + ); +} + +export function hasPureComment(node: ts.Node): boolean { + if (!node) { + return false; + } + + const leadingComment = ts.getSyntheticLeadingComments(node); + + return !!leadingComment && leadingComment.some(comment => comment.text === pureFunctionComment); +} diff --git a/packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts b/packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts index ecc49220ab..206a1b4ab4 100644 --- a/packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts +++ b/packages/angular_devkit/build_optimizer/src/transforms/class-fold.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ import * as ts from 'typescript'; +import { addPureComment } from '../helpers/ast-utils'; interface ClassData { name: string; @@ -81,18 +82,12 @@ export function getFoldFileTransformer(program: ts.Program): ts.TransformerFacto const classStatement = clazz.declaration as ts.ClassDeclaration; const innerReturn = ts.createReturn(ts.createIdentifier(clazz.name)); - const iife = ts.createImmediatelyInvokedFunctionExpression([ - classStatement, - ...clazz.statements.map(st => st.expressionStatement), - innerReturn, - ]); - - const pureIife = ts.addSyntheticLeadingComment( - iife, - ts.SyntaxKind.MultiLineCommentTrivia, - '@__PURE__', - false, - ); + const pureIife = addPureComment( + ts.createImmediatelyInvokedFunctionExpression([ + classStatement, + ...clazz.statements.map(st => st.expressionStatement), + innerReturn, + ])); // Move the original class modifiers to the var statement. const newNode = ts.createVariableStatement( diff --git a/packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts b/packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts index cc8797d775..0ade653c54 100644 --- a/packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts +++ b/packages/angular_devkit/build_optimizer/src/transforms/prefix-classes.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ import * as ts from 'typescript'; +import { addPureComment } from '../helpers/ast-utils'; /** * @deprecated From 0.9.0 @@ -42,9 +43,6 @@ const extendsHelperName = '__extends'; export function getPrefixClassesTransformer(): ts.TransformerFactory { return (context: ts.TransformationContext): ts.Transformer => { const transformer: ts.Transformer = (sf: ts.SourceFile) => { - - const pureFunctionComment = '@__PURE__'; - const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult => { // Add pure comment to downleveled classes. @@ -63,12 +61,7 @@ export function getPrefixClassesTransformer(): ts.TransformerFactory { return (context: ts.TransformationContext): ts.Transformer => { @@ -19,8 +17,7 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory { // Add pure function comment to top level functions. if (topLevelFunctions.has(node)) { - const newNode = ts.addSyntheticLeadingComment( - node, ts.SyntaxKind.MultiLineCommentTrivia, pureFunctionComment, false); + const newNode = addPureComment(node); // Replace node with modified one. return ts.visitEachChild(newNode, visitor, context); @@ -96,12 +93,3 @@ export function findTopLevelFunctions(parentNode: ts.Node): Set { return topLevelFunctions; } - -function hasPureComment(node: ts.Node) { - if (!node) { - return false; - } - const leadingComment = ts.getSyntheticLeadingComments(node); - - return leadingComment && leadingComment.some((comment) => comment.text === pureFunctionComment); -} diff --git a/packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts b/packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts index 394c122ffb..8b2341cdd0 100644 --- a/packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts +++ b/packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ import * as ts from 'typescript'; +import { addPureComment } from '../helpers/ast-utils'; function isBlockLike(node: ts.Node): node is ts.BlockLike { return node.kind === ts.SyntaxKind.Block @@ -384,17 +385,6 @@ function findEnumNameStatements( return enumStatements; } -function addPureComment(node: T): T { - const pureFunctionComment = '@__PURE__'; - - return ts.addSyntheticLeadingComment( - node, - ts.SyntaxKind.MultiLineCommentTrivia, - pureFunctionComment, - false, - ); -} - function updateHostNode( hostNode: ts.VariableStatement, expression: ts.Expression,