refactor(@angular-devkit/build-optimizer): replace deprecated TypeScript methods

This commit is contained in:
Alan Agius 2021-01-20 11:07:21 +01:00 committed by Filipe Silva
parent 74a1c2a2b9
commit 4453bb1ab9

View File

@ -10,10 +10,10 @@ import { addPureComment } from '../helpers/ast-utils';
function isBlockLike(node: ts.Node): node is ts.BlockLike {
return node.kind === ts.SyntaxKind.Block
|| node.kind === ts.SyntaxKind.ModuleBlock
|| node.kind === ts.SyntaxKind.CaseClause
|| node.kind === ts.SyntaxKind.DefaultClause
|| node.kind === ts.SyntaxKind.SourceFile;
|| node.kind === ts.SyntaxKind.ModuleBlock
|| node.kind === ts.SyntaxKind.CaseClause
|| node.kind === ts.SyntaxKind.DefaultClause
|| node.kind === ts.SyntaxKind.SourceFile;
}
export function getWrapEnumsTransformer(): ts.TransformerFactory<ts.SourceFile> {
@ -21,7 +21,7 @@ export function getWrapEnumsTransformer(): ts.TransformerFactory<ts.SourceFile>
const transformer: ts.Transformer<ts.SourceFile> = sf => {
const result = visitBlockStatements(sf.statements, context);
return ts.updateSourceFileNode(sf, ts.setTextRange(result, sf.statements));
return context.factory.updateSourceFile(sf, ts.setTextRange(result, sf.statements));
};
return transformer;
@ -32,9 +32,9 @@ function visitBlockStatements(
statements: ts.NodeArray<ts.Statement>,
context: ts.TransformationContext,
): ts.NodeArray<ts.Statement> {
// copy of statements to modify; lazy initialized
let updatedStatements: Array<ts.Statement> | undefined;
const nodeFactory = context.factory;
const visitor: ts.Visitor = (node) => {
if (isBlockLike(node)) {
@ -45,13 +45,13 @@ function visitBlockStatements(
result = ts.setTextRange(result, node.statements);
switch (node.kind) {
case ts.SyntaxKind.Block:
return ts.updateBlock(node, result);
return nodeFactory.updateBlock(node, result);
case ts.SyntaxKind.ModuleBlock:
return ts.updateModuleBlock(node, result);
return nodeFactory.updateModuleBlock(node, result);
case ts.SyntaxKind.CaseClause:
return ts.updateCaseClause(node, node.expression, result);
return nodeFactory.updateCaseClause(node, node.expression, result);
case ts.SyntaxKind.DefaultClause:
return ts.updateDefaultClause(node, result);
return nodeFactory.updateDefaultClause(node, result);
default:
return node;
}
@ -97,6 +97,7 @@ function visitBlockStatements(
// update IIFE and replace variable statement and old IIFE
oldStatementsLength = 2;
newStatement = updateEnumIife(
nodeFactory,
currentStatement,
iife[0],
iife[1],
@ -118,6 +119,7 @@ function visitBlockStatements(
oldStatementsLength = classStatements.length;
newStatement = createWrappedClass(
nodeFactory,
variableDeclaration,
classStatements,
);
@ -134,11 +136,12 @@ function visitBlockStatements(
oldStatementsLength = classStatements.length;
newStatement = createWrappedClass(
nodeFactory,
currentStatement,
classStatements,
);
oIndex += classStatements.length - 1;
oIndex += oldStatementsLength - 1;
}
if (newStatement && newStatement.length > 0) {
@ -163,7 +166,7 @@ function visitBlockStatements(
// if changes, return updated statements
// otherwise, return original array instance
return updatedStatements ? ts.createNodeArray(updatedStatements) : statements;
return updatedStatements ? nodeFactory.createNodeArray(updatedStatements) : statements;
}
// TS 2.3 enums have statements that are inside a IIFE.
@ -251,20 +254,22 @@ function findEnumIife(
}
function updateHostNode(
nodeFactory: ts.NodeFactory,
hostNode: ts.VariableStatement,
expression: ts.Expression,
): ts.Statement {
// Update existing host node with the pure comment before the variable declaration initializer.
const variableDeclaration = hostNode.declarationList.declarations[0];
const outerVarStmt = ts.updateVariableStatement(
const outerVarStmt = nodeFactory.updateVariableStatement(
hostNode,
hostNode.modifiers,
ts.updateVariableDeclarationList(
nodeFactory.updateVariableDeclarationList(
hostNode.declarationList,
[
ts.updateVariableDeclaration(
nodeFactory.updateVariableDeclaration(
variableDeclaration,
variableDeclaration.name,
variableDeclaration.exclamationToken,
variableDeclaration.type,
expression,
),
@ -353,23 +358,24 @@ function findStatements(
}
function updateEnumIife(
nodeFactory: ts.NodeFactory,
hostNode: ts.VariableStatement,
iife: ts.CallExpression,
exportAssignment?: ts.Expression,
): ts.Statement[] {
if (!ts.isParenthesizedExpression(iife.expression)
|| !ts.isFunctionExpression(iife.expression.expression)) {
|| !ts.isFunctionExpression(iife.expression.expression)) {
throw new Error('Invalid IIFE Structure');
}
// Ignore export assignment if variable is directly exported
if (hostNode.modifiers
&& hostNode.modifiers.findIndex(m => m.kind == ts.SyntaxKind.ExportKeyword) != -1) {
&& hostNode.modifiers.findIndex(m => m.kind == ts.SyntaxKind.ExportKeyword) != -1) {
exportAssignment = undefined;
}
const expression = iife.expression.expression;
const updatedFunction = ts.updateFunctionExpression(
const updatedFunction = nodeFactory.updateFunctionExpression(
expression,
expression.modifiers,
expression.asteriskToken,
@ -377,22 +383,22 @@ function updateEnumIife(
expression.typeParameters,
expression.parameters,
expression.type,
ts.updateBlock(
nodeFactory.updateBlock(
expression.body,
[
...expression.body.statements,
ts.createReturn(expression.parameters[0].name as ts.Identifier),
nodeFactory.createReturnStatement(expression.parameters[0].name as ts.Identifier),
],
),
);
let arg: ts.Expression = ts.createObjectLiteral();
let arg: ts.Expression = nodeFactory.createObjectLiteralExpression();
if (exportAssignment) {
arg = ts.createBinary(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
arg = nodeFactory.createBinaryExpression(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
}
const updatedIife = ts.updateCall(
const updatedIife = nodeFactory.updateCallExpression(
iife,
ts.updateParen(
nodeFactory.updateParenthesizedExpression(
iife.expression,
updatedFunction,
),
@ -402,49 +408,17 @@ function updateEnumIife(
let value: ts.Expression = addPureComment(updatedIife);
if (exportAssignment) {
value = ts.createBinary(
value = nodeFactory.createBinaryExpression(
exportAssignment,
ts.SyntaxKind.FirstAssignment,
updatedIife);
}
return [updateHostNode(hostNode, value)];
}
function createWrappedEnum(
name: string,
hostNode: ts.VariableStatement,
statements: Array<ts.Statement>,
literalInitializer: ts.ObjectLiteralExpression = ts.createObjectLiteral(),
addExportModifier = false,
): ts.Statement[] {
const node = addExportModifier
? ts.updateVariableStatement(
hostNode,
[ts.createToken(ts.SyntaxKind.ExportKeyword)],
hostNode.declarationList,
)
: hostNode;
const innerVarStmt = ts.createVariableStatement(
undefined,
ts.createVariableDeclarationList([
ts.createVariableDeclaration(name, undefined, literalInitializer),
]),
);
const innerReturn = ts.createReturn(ts.createIdentifier(name));
const iife = ts.createImmediatelyInvokedFunctionExpression([
innerVarStmt,
...statements,
innerReturn,
]);
return [updateHostNode(node, addPureComment(ts.createParen(iife)))];
return [updateHostNode(nodeFactory, hostNode, value)];
}
function createWrappedClass(
nodeFactory: ts.NodeFactory,
hostNode: ts.ClassDeclaration | ts.VariableDeclaration,
statements: ts.Statement[],
): ts.Statement[] {
@ -453,7 +427,7 @@ function createWrappedClass(
const updatedStatements = [...statements];
if (ts.isClassDeclaration(hostNode)) {
updatedStatements[0] = ts.createClassDeclaration(
updatedStatements[0] = nodeFactory.createClassDeclaration(
hostNode.decorators,
undefined,
hostNode.name,
@ -464,9 +438,9 @@ function createWrappedClass(
}
const pureIife = addPureComment(
ts.createImmediatelyInvokedArrowFunction([
nodeFactory.createImmediatelyInvokedArrowFunction([
...updatedStatements,
ts.createReturn(ts.createIdentifier(name)),
nodeFactory.createReturnStatement(nodeFactory.createIdentifier(name)),
]),
);
@ -476,22 +450,22 @@ function createWrappedClass(
const newStatement: ts.Statement[] = [];
newStatement.push(
ts.createVariableStatement(
nodeFactory.createVariableStatement(
isDefault ? undefined : modifiers,
ts.createVariableDeclarationList([
ts.createVariableDeclaration(name, undefined, pureIife),
nodeFactory.createVariableDeclarationList([
nodeFactory.createVariableDeclaration(name, undefined, undefined, pureIife),
],
ts.NodeFlags.Let,
ts.NodeFlags.Let,
),
));
if (isDefault) {
newStatement.push(
ts.createExportAssignment(
nodeFactory.createExportAssignment(
undefined,
undefined,
false,
ts.createIdentifier(name),
nodeFactory.createIdentifier(name),
));
}