-
Notifications
You must be signed in to change notification settings - Fork 12k
/
prefix-classes.ts
209 lines (169 loc) · 6.27 KB
/
prefix-classes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* 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
*/
export function testPrefixClasses(content: string) {
const exportVarSetter = /(?:export )?(?:var|const)\s+(?:\S+)\s*=\s*/;
const multiLineComment = /\s*(?:\/\*[\s\S]*?\*\/)?\s*/;
const newLine = /\s*\r?\n\s*/;
const regexes = [
[
/^/,
exportVarSetter, multiLineComment,
/\(/, multiLineComment,
/\s*function \(\) {/, newLine,
multiLineComment,
/function (?:\S+)\([^\)]*\) \{/, newLine,
],
[
/^/,
exportVarSetter, multiLineComment,
/\(/, multiLineComment,
/\s*function \(_super\) {/, newLine,
/\S*\.?__extends\(\S+, _super\);/,
],
].map(arr => new RegExp(arr.map(x => x.source).join(''), 'm'));
return regexes.some((regex) => regex.test(content));
}
const superParameterName = '_super';
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 visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
// Add pure comment to downleveled classes.
if (ts.isVariableStatement(node) && isDownleveledClass(node)) {
const varDecl = node.declarationList.declarations[0];
const varInitializer = varDecl.initializer as ts.Expression;
// Update node with the pure comment before the variable declaration initializer.
const newNode = ts.updateVariableStatement(
node,
node.modifiers,
ts.updateVariableDeclarationList(
node.declarationList,
[
ts.updateVariableDeclaration(
varDecl,
varDecl.name,
varDecl.type,
addPureComment(varInitializer),
),
],
),
);
// Replace node with modified one.
return ts.visitEachChild(newNode, visitor, context);
}
// Otherwise return node as is.
return ts.visitEachChild(node, visitor, context);
};
return ts.visitEachChild(sf, visitor, context);
};
return transformer;
};
}
// Determine if a node matched the structure of a downleveled TS class.
function isDownleveledClass(node: ts.Node): boolean {
if (!ts.isVariableStatement(node)) {
return false;
}
if (node.declarationList.declarations.length !== 1) {
return false;
}
const variableDeclaration = node.declarationList.declarations[0];
if (!ts.isIdentifier(variableDeclaration.name)
|| !variableDeclaration.initializer) {
return false;
}
let potentialClass = variableDeclaration.initializer;
// TS 2.3 has an unwrapped class IIFE
// TS 2.4 uses a function expression wrapper
// TS 2.5 uses an arrow function wrapper
if (ts.isParenthesizedExpression(potentialClass)) {
potentialClass = potentialClass.expression;
}
if (!ts.isCallExpression(potentialClass) || potentialClass.arguments.length > 1) {
return false;
}
let wrapperBody: ts.Block;
if (ts.isFunctionExpression(potentialClass.expression)) {
wrapperBody = potentialClass.expression.body;
} else if (ts.isArrowFunction(potentialClass.expression)
&& ts.isBlock(potentialClass.expression.body)) {
wrapperBody = potentialClass.expression.body;
} else {
return false;
}
if (wrapperBody.statements.length === 0) {
return false;
}
const functionExpression = potentialClass.expression;
const functionStatements = wrapperBody.statements;
// need a minimum of two for a function declaration and return statement
if (functionStatements.length < 2) {
return false;
}
const firstStatement = functionStatements[0];
// find return statement - may not be last statement
let returnStatement: ts.ReturnStatement | undefined;
for (let i = functionStatements.length - 1; i > 0; i--) {
if (ts.isReturnStatement(functionStatements[i])) {
returnStatement = functionStatements[i] as ts.ReturnStatement;
break;
}
}
if (returnStatement == undefined
|| returnStatement.expression == undefined
|| !ts.isIdentifier(returnStatement.expression)) {
return false;
}
if (functionExpression.parameters.length === 0) {
// potential non-extended class or wrapped es2015 class
return (ts.isFunctionDeclaration(firstStatement) || ts.isClassDeclaration(firstStatement))
&& firstStatement.name !== undefined
&& returnStatement.expression.text === firstStatement.name.text;
} else if (functionExpression.parameters.length !== 1) {
return false;
}
// Potential extended class
const functionParameter = functionExpression.parameters[0];
if (!ts.isIdentifier(functionParameter.name)
|| functionParameter.name.text !== superParameterName) {
return false;
}
if (functionStatements.length < 3 || !ts.isExpressionStatement(firstStatement)) {
return false;
}
if (!ts.isCallExpression(firstStatement.expression)) {
return false;
}
const extendCallExpression = firstStatement.expression;
let functionName;
if (ts.isIdentifier(extendCallExpression.expression)) {
functionName = extendCallExpression.expression.text;
} else if (ts.isPropertyAccessExpression(extendCallExpression.expression)) {
functionName = extendCallExpression.expression.name.text;
}
if (!functionName || !functionName.endsWith(extendsHelperName)) {
return false;
}
if (extendCallExpression.arguments.length === 0) {
return false;
}
const lastArgument = extendCallExpression.arguments[extendCallExpression.arguments.length - 1];
if (!ts.isIdentifier(lastArgument) || lastArgument.text !== functionParameter.name.text) {
return false;
}
const secondStatement = functionStatements[1];
return ts.isFunctionDeclaration(secondStatement)
&& secondStatement.name !== undefined
&& returnStatement.expression.text === secondStatement.name.text;
}