UNPKG

7.58 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.getPrefixClassesTransformer = exports.testPrefixClasses = void 0;
30const ts = __importStar(require("typescript"));
31const ast_utils_1 = require("../helpers/ast-utils");
32function testPrefixClasses(content) {
33 const exportVarSetter = /(?:export )?(?:var|const)\s+(?:\S+)\s*=\s*/;
34 const multiLineComment = /\s*(?:\/\*[\s\S]*?\*\/)?\s*/;
35 const newLine = /\s*\r?\n\s*/;
36 const regexes = [
37 [
38 /^/,
39 exportVarSetter,
40 multiLineComment,
41 /\(/,
42 multiLineComment,
43 /\s*function \(\) {/,
44 newLine,
45 multiLineComment,
46 /function (?:\S+)\([^)]*\) \{/,
47 newLine,
48 ],
49 [
50 /^/,
51 exportVarSetter,
52 multiLineComment,
53 /\(/,
54 multiLineComment,
55 /\s*function \(_super\) {/,
56 newLine,
57 /\S*\.?__extends\(\S+, _super\);/,
58 ],
59 ].map((arr) => new RegExp(arr.map((x) => x.source).join(''), 'm'));
60 return regexes.some((regex) => regex.test(content));
61}
62exports.testPrefixClasses = testPrefixClasses;
63const superParameterName = '_super';
64const extendsHelperName = '__extends';
65function getPrefixClassesTransformer() {
66 return (context) => {
67 const transformer = (sf) => {
68 const visitor = (node) => {
69 // Add pure comment to downleveled classes.
70 if (ts.isVariableStatement(node) && isDownleveledClass(node)) {
71 const varDecl = node.declarationList.declarations[0];
72 const varInitializer = varDecl.initializer;
73 // Update node with the pure comment before the variable declaration initializer.
74 const newNode = ts.updateVariableStatement(node, node.modifiers, ts.updateVariableDeclarationList(node.declarationList, [
75 ts.updateVariableDeclaration(varDecl, varDecl.name, varDecl.type, (0, ast_utils_1.addPureComment)(varInitializer)),
76 ]));
77 // Replace node with modified one.
78 return ts.visitEachChild(newNode, visitor, context);
79 }
80 // Otherwise return node as is.
81 return ts.visitEachChild(node, visitor, context);
82 };
83 return ts.visitEachChild(sf, visitor, context);
84 };
85 return transformer;
86 };
87}
88exports.getPrefixClassesTransformer = getPrefixClassesTransformer;
89// Determine if a node matched the structure of a downleveled TS class.
90function isDownleveledClass(node) {
91 if (!ts.isVariableStatement(node)) {
92 return false;
93 }
94 if (node.declarationList.declarations.length !== 1) {
95 return false;
96 }
97 const variableDeclaration = node.declarationList.declarations[0];
98 if (!ts.isIdentifier(variableDeclaration.name) || !variableDeclaration.initializer) {
99 return false;
100 }
101 let potentialClass = variableDeclaration.initializer;
102 // TS 2.3 has an unwrapped class IIFE
103 // TS 2.4 uses a function expression wrapper
104 // TS 2.5 uses an arrow function wrapper
105 if (ts.isParenthesizedExpression(potentialClass)) {
106 potentialClass = potentialClass.expression;
107 }
108 if (!ts.isCallExpression(potentialClass) || potentialClass.arguments.length > 1) {
109 return false;
110 }
111 let wrapperBody;
112 if (ts.isFunctionExpression(potentialClass.expression)) {
113 wrapperBody = potentialClass.expression.body;
114 }
115 else if (ts.isArrowFunction(potentialClass.expression) &&
116 ts.isBlock(potentialClass.expression.body)) {
117 wrapperBody = potentialClass.expression.body;
118 }
119 else {
120 return false;
121 }
122 if (wrapperBody.statements.length === 0) {
123 return false;
124 }
125 const functionExpression = potentialClass.expression;
126 const functionStatements = wrapperBody.statements;
127 // need a minimum of two for a function declaration and return statement
128 if (functionStatements.length < 2) {
129 return false;
130 }
131 const firstStatement = functionStatements[0];
132 // find return statement - may not be last statement
133 let returnStatement;
134 for (let i = functionStatements.length - 1; i > 0; i--) {
135 if (ts.isReturnStatement(functionStatements[i])) {
136 returnStatement = functionStatements[i];
137 break;
138 }
139 }
140 if (returnStatement == undefined ||
141 returnStatement.expression == undefined ||
142 !ts.isIdentifier(returnStatement.expression)) {
143 return false;
144 }
145 if (functionExpression.parameters.length === 0) {
146 // potential non-extended class or wrapped es2015 class
147 return ((ts.isFunctionDeclaration(firstStatement) || ts.isClassDeclaration(firstStatement)) &&
148 firstStatement.name !== undefined &&
149 returnStatement.expression.text === firstStatement.name.text);
150 }
151 else if (functionExpression.parameters.length !== 1) {
152 return false;
153 }
154 // Potential extended class
155 const functionParameter = functionExpression.parameters[0];
156 if (!ts.isIdentifier(functionParameter.name) ||
157 functionParameter.name.text !== superParameterName) {
158 return false;
159 }
160 if (functionStatements.length < 3 || !ts.isExpressionStatement(firstStatement)) {
161 return false;
162 }
163 if (!ts.isCallExpression(firstStatement.expression)) {
164 return false;
165 }
166 const extendCallExpression = firstStatement.expression;
167 let functionName;
168 if (ts.isIdentifier(extendCallExpression.expression)) {
169 functionName = extendCallExpression.expression.text;
170 }
171 else if (ts.isPropertyAccessExpression(extendCallExpression.expression)) {
172 functionName = extendCallExpression.expression.name.text;
173 }
174 if (!functionName || !functionName.endsWith(extendsHelperName)) {
175 return false;
176 }
177 if (extendCallExpression.arguments.length === 0) {
178 return false;
179 }
180 const lastArgument = extendCallExpression.arguments[extendCallExpression.arguments.length - 1];
181 if (!ts.isIdentifier(lastArgument) || lastArgument.text !== functionParameter.name.text) {
182 return false;
183 }
184 const secondStatement = functionStatements[1];
185 return (ts.isFunctionDeclaration(secondStatement) &&
186 secondStatement.name !== undefined &&
187 returnStatement.expression.text === secondStatement.name.text);
188}