Skip to content

Commit

Permalink
refactor: create helper functions for pure comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 authored and mgechev committed Jan 23, 2019
1 parent 6b2699d commit e12adf4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 46 deletions.
21 changes: 21 additions & 0 deletions packages/angular_devkit/build_optimizer/src/helpers/ast-utils.ts
Expand Up @@ -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<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T[] {
const nodes: T[] = [];
Expand All @@ -20,3 +22,22 @@ export function collectDeepNodes<T extends ts.Node>(node: ts.Node, kind: ts.Synt

return nodes;
}

export function addPureComment<T extends ts.Node>(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);
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -42,9 +43,6 @@ const extendsHelperName = '__extends';
export function getPrefixClassesTransformer(): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
const transformer: ts.Transformer<ts.SourceFile> = (sf: ts.SourceFile) => {

const pureFunctionComment = '@__PURE__';

const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {

// Add pure comment to downleveled classes.
Expand All @@ -63,12 +61,7 @@ export function getPrefixClassesTransformer(): ts.TransformerFactory<ts.SourceFi
varDecl,
varDecl.name,
varDecl.type,
ts.addSyntheticLeadingComment(
varInitializer,
ts.SyntaxKind.MultiLineCommentTrivia,
pureFunctionComment,
false,
),
addPureComment(varInitializer),
),
],
),
Expand Down
Expand Up @@ -6,9 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';


const pureFunctionComment = '@__PURE__';
import { addPureComment, hasPureComment } from '../helpers/ast-utils';

export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
Expand All @@ -19,8 +17,7 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.Source
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
// 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);
Expand Down Expand Up @@ -96,12 +93,3 @@ export function findTopLevelFunctions(parentNode: ts.Node): Set<ts.Node> {

return topLevelFunctions;
}

function hasPureComment(node: ts.Node) {
if (!node) {
return false;
}
const leadingComment = ts.getSyntheticLeadingComments(node);

return leadingComment && leadingComment.some((comment) => comment.text === pureFunctionComment);
}
Expand Up @@ -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
Expand Down Expand Up @@ -384,17 +385,6 @@ function findEnumNameStatements(
return enumStatements;
}

function addPureComment<T extends ts.Node>(node: T): T {
const pureFunctionComment = '@__PURE__';

return ts.addSyntheticLeadingComment(
node,
ts.SyntaxKind.MultiLineCommentTrivia,
pureFunctionComment,
false,
);
}

function updateHostNode(
hostNode: ts.VariableStatement,
expression: ts.Expression,
Expand Down

0 comments on commit e12adf4

Please sign in to comment.