mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-15 01:54:04 +08:00
refactor: create helper functions for pure comments
This commit is contained in:
parent
6b2699d530
commit
e12adf4dcb
@ -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[] = [];
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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<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.
|
||||
@ -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),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
@ -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> => {
|
||||
@ -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);
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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<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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user