UNPKG

4.97 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.findTopLevelFunctions = exports.getPrefixFunctionsTransformer = void 0;
30const ts = __importStar(require("typescript"));
31const ast_utils_1 = require("../helpers/ast-utils");
32function getPrefixFunctionsTransformer() {
33 return (context) => {
34 const transformer = (sf) => {
35 const topLevelFunctions = findTopLevelFunctions(sf);
36 const visitor = (node) => {
37 // Add pure function comment to top level functions.
38 if (topLevelFunctions.has(node)) {
39 const newNode = ast_utils_1.addPureComment(node);
40 // Replace node with modified one.
41 return ts.visitEachChild(newNode, visitor, context);
42 }
43 // Otherwise return node as is.
44 return ts.visitEachChild(node, visitor, context);
45 };
46 return ts.visitNode(sf, visitor);
47 };
48 return transformer;
49 };
50}
51exports.getPrefixFunctionsTransformer = getPrefixFunctionsTransformer;
52function findTopLevelFunctions(parentNode) {
53 const topLevelFunctions = new Set();
54 function cb(node) {
55 // Stop recursing into this branch if it's a definition construct.
56 // These are function expression, function declaration, class, or arrow function (lambda).
57 // The body of these constructs will not execute when loading the module, so we don't
58 // need to mark function calls inside them as pure.
59 // Class static initializers in ES2015 are an exception we don't cover. They would need similar
60 // processing as enums to prevent property setting from causing the class to be retained.
61 if (ts.isFunctionLike(node) ||
62 ts.isClassLike(node) ||
63 ts.isArrowFunction(node) ||
64 ts.isMethodDeclaration(node)) {
65 return;
66 }
67 let noPureComment = !ast_utils_1.hasPureComment(node);
68 let innerNode = node;
69 while (innerNode && ts.isParenthesizedExpression(innerNode)) {
70 innerNode = innerNode.expression;
71 noPureComment = noPureComment && !ast_utils_1.hasPureComment(innerNode);
72 }
73 if (!innerNode) {
74 return;
75 }
76 if ((ts.isFunctionExpression(innerNode) || ts.isArrowFunction(innerNode)) &&
77 ts.isParenthesizedExpression(node)) {
78 // pure functions can be wrapped in parentizes
79 // we should not add pure comments to this sort of syntax.
80 // example var foo = (() => x)
81 return;
82 }
83 if (noPureComment) {
84 if (ts.isNewExpression(innerNode)) {
85 topLevelFunctions.add(node);
86 }
87 else if (ts.isCallExpression(innerNode)) {
88 let expression = innerNode.expression;
89 if (ts.isIdentifier(expression) && ast_utils_1.getCleanHelperName(expression.text)) {
90 return;
91 }
92 while (expression && ts.isParenthesizedExpression(expression)) {
93 expression = expression.expression;
94 }
95 if (expression) {
96 if (ts.isFunctionExpression(expression)) {
97 // Skip IIFE's with arguments
98 // This could be improved to check if there are any references to variables
99 if (innerNode.arguments.length === 0) {
100 topLevelFunctions.add(node);
101 }
102 }
103 else {
104 topLevelFunctions.add(node);
105 }
106 }
107 }
108 }
109 ts.forEachChild(innerNode, cb);
110 }
111 ts.forEachChild(parentNode, cb);
112 return topLevelFunctions;
113}
114exports.findTopLevelFunctions = findTopLevelFunctions;