@angular-devkit/build-optimizer 0.1101.4 → 0.1102.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/transforms/wrap-enums.js +29 -43
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@ function getWrapEnumsTransformer() {
|
|
|
21
21
|
return (context) => {
|
|
22
22
|
const transformer = sf => {
|
|
23
23
|
const result = visitBlockStatements(sf.statements, context);
|
|
24
|
-
return
|
|
24
|
+
return context.factory.updateSourceFile(sf, ts.setTextRange(result, sf.statements));
|
|
25
25
|
};
|
|
26
26
|
return transformer;
|
|
27
27
|
};
|
|
@@ -30,6 +30,7 @@ exports.getWrapEnumsTransformer = getWrapEnumsTransformer;
|
|
|
30
30
|
function visitBlockStatements(statements, context) {
|
|
31
31
|
// copy of statements to modify; lazy initialized
|
|
32
32
|
let updatedStatements;
|
|
33
|
+
const nodeFactory = context.factory;
|
|
33
34
|
const visitor = (node) => {
|
|
34
35
|
if (isBlockLike(node)) {
|
|
35
36
|
let result = visitBlockStatements(node.statements, context);
|
|
@@ -39,13 +40,13 @@ function visitBlockStatements(statements, context) {
|
|
|
39
40
|
result = ts.setTextRange(result, node.statements);
|
|
40
41
|
switch (node.kind) {
|
|
41
42
|
case ts.SyntaxKind.Block:
|
|
42
|
-
return
|
|
43
|
+
return nodeFactory.updateBlock(node, result);
|
|
43
44
|
case ts.SyntaxKind.ModuleBlock:
|
|
44
|
-
return
|
|
45
|
+
return nodeFactory.updateModuleBlock(node, result);
|
|
45
46
|
case ts.SyntaxKind.CaseClause:
|
|
46
|
-
return
|
|
47
|
+
return nodeFactory.updateCaseClause(node, node.expression, result);
|
|
47
48
|
case ts.SyntaxKind.DefaultClause:
|
|
48
|
-
return
|
|
49
|
+
return nodeFactory.updateDefaultClause(node, result);
|
|
49
50
|
default:
|
|
50
51
|
return node;
|
|
51
52
|
}
|
|
@@ -85,7 +86,7 @@ function visitBlockStatements(statements, context) {
|
|
|
85
86
|
if (iife) {
|
|
86
87
|
// update IIFE and replace variable statement and old IIFE
|
|
87
88
|
oldStatementsLength = 2;
|
|
88
|
-
newStatement = updateEnumIife(currentStatement, iife[0], iife[1]);
|
|
89
|
+
newStatement = updateEnumIife(nodeFactory, currentStatement, iife[0], iife[1]);
|
|
89
90
|
// skip IIFE statement
|
|
90
91
|
oIndex++;
|
|
91
92
|
}
|
|
@@ -98,7 +99,7 @@ function visitBlockStatements(statements, context) {
|
|
|
98
99
|
continue;
|
|
99
100
|
}
|
|
100
101
|
oldStatementsLength = classStatements.length;
|
|
101
|
-
newStatement = createWrappedClass(variableDeclaration, classStatements);
|
|
102
|
+
newStatement = createWrappedClass(nodeFactory, variableDeclaration, classStatements);
|
|
102
103
|
oIndex += classStatements.length - 1;
|
|
103
104
|
}
|
|
104
105
|
}
|
|
@@ -110,8 +111,8 @@ function visitBlockStatements(statements, context) {
|
|
|
110
111
|
continue;
|
|
111
112
|
}
|
|
112
113
|
oldStatementsLength = classStatements.length;
|
|
113
|
-
newStatement = createWrappedClass(currentStatement, classStatements);
|
|
114
|
-
oIndex +=
|
|
114
|
+
newStatement = createWrappedClass(nodeFactory, currentStatement, classStatements);
|
|
115
|
+
oIndex += oldStatementsLength - 1;
|
|
115
116
|
}
|
|
116
117
|
if (newStatement && newStatement.length > 0) {
|
|
117
118
|
if (!updatedStatements) {
|
|
@@ -132,7 +133,7 @@ function visitBlockStatements(statements, context) {
|
|
|
132
133
|
}
|
|
133
134
|
// if changes, return updated statements
|
|
134
135
|
// otherwise, return original array instance
|
|
135
|
-
return updatedStatements ?
|
|
136
|
+
return updatedStatements ? nodeFactory.createNodeArray(updatedStatements) : statements;
|
|
136
137
|
}
|
|
137
138
|
// TS 2.3 enums have statements that are inside a IIFE.
|
|
138
139
|
function findEnumIife(name, statement) {
|
|
@@ -196,11 +197,11 @@ function findEnumIife(name, statement) {
|
|
|
196
197
|
}
|
|
197
198
|
return [callExpression, exportExpression];
|
|
198
199
|
}
|
|
199
|
-
function updateHostNode(hostNode, expression) {
|
|
200
|
+
function updateHostNode(nodeFactory, hostNode, expression) {
|
|
200
201
|
// Update existing host node with the pure comment before the variable declaration initializer.
|
|
201
202
|
const variableDeclaration = hostNode.declarationList.declarations[0];
|
|
202
|
-
const outerVarStmt =
|
|
203
|
-
|
|
203
|
+
const outerVarStmt = nodeFactory.updateVariableStatement(hostNode, hostNode.modifiers, nodeFactory.updateVariableDeclarationList(hostNode.declarationList, [
|
|
204
|
+
nodeFactory.updateVariableDeclaration(variableDeclaration, variableDeclaration.name, variableDeclaration.exclamationToken, variableDeclaration.type, expression),
|
|
204
205
|
]));
|
|
205
206
|
return outerVarStmt;
|
|
206
207
|
}
|
|
@@ -265,7 +266,7 @@ function findStatements(name, statements, statementIndex, offset = 0) {
|
|
|
265
266
|
}
|
|
266
267
|
return undefined;
|
|
267
268
|
}
|
|
268
|
-
function updateEnumIife(hostNode, iife, exportAssignment) {
|
|
269
|
+
function updateEnumIife(nodeFactory, hostNode, iife, exportAssignment) {
|
|
269
270
|
if (!ts.isParenthesizedExpression(iife.expression)
|
|
270
271
|
|| !ts.isFunctionExpression(iife.expression.expression)) {
|
|
271
272
|
throw new Error('Invalid IIFE Structure');
|
|
@@ -276,55 +277,40 @@ function updateEnumIife(hostNode, iife, exportAssignment) {
|
|
|
276
277
|
exportAssignment = undefined;
|
|
277
278
|
}
|
|
278
279
|
const expression = iife.expression.expression;
|
|
279
|
-
const updatedFunction =
|
|
280
|
+
const updatedFunction = nodeFactory.updateFunctionExpression(expression, expression.modifiers, expression.asteriskToken, expression.name, expression.typeParameters, expression.parameters, expression.type, nodeFactory.updateBlock(expression.body, [
|
|
280
281
|
...expression.body.statements,
|
|
281
|
-
|
|
282
|
+
nodeFactory.createReturnStatement(expression.parameters[0].name),
|
|
282
283
|
]));
|
|
283
|
-
let arg =
|
|
284
|
+
let arg = nodeFactory.createObjectLiteralExpression();
|
|
284
285
|
if (exportAssignment) {
|
|
285
|
-
arg =
|
|
286
|
+
arg = nodeFactory.createBinaryExpression(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
|
|
286
287
|
}
|
|
287
|
-
const updatedIife =
|
|
288
|
+
const updatedIife = nodeFactory.updateCallExpression(iife, nodeFactory.updateParenthesizedExpression(iife.expression, updatedFunction), iife.typeArguments, [arg]);
|
|
288
289
|
let value = ast_utils_1.addPureComment(updatedIife);
|
|
289
290
|
if (exportAssignment) {
|
|
290
|
-
value =
|
|
291
|
+
value = nodeFactory.createBinaryExpression(exportAssignment, ts.SyntaxKind.FirstAssignment, updatedIife);
|
|
291
292
|
}
|
|
292
|
-
return [updateHostNode(hostNode, value)];
|
|
293
|
+
return [updateHostNode(nodeFactory, hostNode, value)];
|
|
293
294
|
}
|
|
294
|
-
function
|
|
295
|
-
const node = addExportModifier
|
|
296
|
-
? ts.updateVariableStatement(hostNode, [ts.createToken(ts.SyntaxKind.ExportKeyword)], hostNode.declarationList)
|
|
297
|
-
: hostNode;
|
|
298
|
-
const innerVarStmt = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([
|
|
299
|
-
ts.createVariableDeclaration(name, undefined, literalInitializer),
|
|
300
|
-
]));
|
|
301
|
-
const innerReturn = ts.createReturn(ts.createIdentifier(name));
|
|
302
|
-
const iife = ts.createImmediatelyInvokedFunctionExpression([
|
|
303
|
-
innerVarStmt,
|
|
304
|
-
...statements,
|
|
305
|
-
innerReturn,
|
|
306
|
-
]);
|
|
307
|
-
return [updateHostNode(node, ast_utils_1.addPureComment(ts.createParen(iife)))];
|
|
308
|
-
}
|
|
309
|
-
function createWrappedClass(hostNode, statements) {
|
|
295
|
+
function createWrappedClass(nodeFactory, hostNode, statements) {
|
|
310
296
|
const name = hostNode.name.text;
|
|
311
297
|
const updatedStatements = [...statements];
|
|
312
298
|
if (ts.isClassDeclaration(hostNode)) {
|
|
313
|
-
updatedStatements[0] =
|
|
299
|
+
updatedStatements[0] = nodeFactory.createClassDeclaration(hostNode.decorators, undefined, hostNode.name, hostNode.typeParameters, hostNode.heritageClauses, hostNode.members);
|
|
314
300
|
}
|
|
315
|
-
const pureIife = ast_utils_1.addPureComment(
|
|
301
|
+
const pureIife = ast_utils_1.addPureComment(nodeFactory.createImmediatelyInvokedArrowFunction([
|
|
316
302
|
...updatedStatements,
|
|
317
|
-
|
|
303
|
+
nodeFactory.createReturnStatement(nodeFactory.createIdentifier(name)),
|
|
318
304
|
]));
|
|
319
305
|
const modifiers = hostNode.modifiers;
|
|
320
306
|
const isDefault = !!modifiers
|
|
321
307
|
&& modifiers.some(x => x.kind === ts.SyntaxKind.DefaultKeyword);
|
|
322
308
|
const newStatement = [];
|
|
323
|
-
newStatement.push(
|
|
324
|
-
|
|
309
|
+
newStatement.push(nodeFactory.createVariableStatement(isDefault ? undefined : modifiers, nodeFactory.createVariableDeclarationList([
|
|
310
|
+
nodeFactory.createVariableDeclaration(name, undefined, undefined, pureIife),
|
|
325
311
|
], ts.NodeFlags.Let)));
|
|
326
312
|
if (isDefault) {
|
|
327
|
-
newStatement.push(
|
|
313
|
+
newStatement.push(nodeFactory.createExportAssignment(undefined, undefined, false, nodeFactory.createIdentifier(name)));
|
|
328
314
|
}
|
|
329
315
|
return newStatement;
|
|
330
316
|
}
|