Skip to content

Commit cf978cd

Browse files
alan-agius4filipesilva
authored andcommitted
refactor(@ngtools/webpack): replace deprecated TypeScript methods
TypeScript provides a set of pure functions for producing syntax tree nodes. Since TypeScript 4.0 these methods have been deprecated in favor of the `factory` API. See: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-40
1 parent 4453bb1 commit cf978cd

File tree

7 files changed

+68
-62
lines changed

7 files changed

+68
-62
lines changed

packages/ngtools/webpack/src/transformers/export_ngfactory.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ export function exportNgFactory(
5858
const factoryClassName = entryModule.className + 'NgFactory';
5959
const factoryModulePath = normalizedEntryModulePath + '.ngfactory';
6060

61-
const namedExports = ts.createNamedExports([ts.createExportSpecifier(undefined,
62-
ts.createIdentifier(factoryClassName))]);
63-
const newImport = ts.createExportDeclaration(undefined, undefined, namedExports,
64-
ts.createLiteral(factoryModulePath));
61+
const namedExports = ts.factory.createNamedExports([ts.factory.createExportSpecifier(undefined,
62+
ts.factory.createIdentifier(factoryClassName))]);
63+
const newImport = ts.factory.createExportDeclaration(undefined, undefined, false, namedExports,
64+
ts.factory.createStringLiteral(factoryModulePath));
6565

6666
const firstNode = getFirstNode(sourceFile);
6767
if (firstNode) {

packages/ngtools/webpack/src/transformers/import_factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ function replaceImport(
227227
const replacementVisitor: ts.Visitor = (node: ts.Node) => {
228228
if (node === importStringLit) {
229229
// Transform the import string.
230-
return ts.createStringLiteral(newImportString);
230+
return ts.factory.createStringLiteral(newImportString);
231231
} else if (node === exportNameId) {
232232
// Transform the export name.
233-
return ts.createIdentifier(exportNameId.text + 'NgFactory');
233+
return ts.factory.createIdentifier(exportNameId.text + 'NgFactory');
234234
}
235235

236236
return ts.visitEachChild(node, replacementVisitor, context);

packages/ngtools/webpack/src/transformers/insert_import.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export function insertStarImport(
2323
// We don't need to verify if the symbol is already imported, star imports should be unique.
2424

2525
// Create the new import node.
26-
const namespaceImport = ts.createNamespaceImport(identifier);
27-
const importClause = ts.createImportClause(undefined, namespaceImport);
28-
const newImport = ts.createImportDeclaration(undefined, undefined, importClause,
29-
ts.createLiteral(modulePath));
26+
const namespaceImport = ts.factory.createNamespaceImport(identifier);
27+
const importClause = ts.factory.createImportClause(false, undefined, namespaceImport);
28+
const newImport = ts.factory.createImportDeclaration(undefined, undefined, importClause,
29+
ts.factory.createStringLiteral(modulePath));
3030

3131
if (target) {
3232
ops.push(new AddNodeOperation(
@@ -106,15 +106,15 @@ export function insertImport(
106106
sourceFile,
107107
maybeImports[0].elements[maybeImports[0].elements.length - 1],
108108
undefined,
109-
ts.createImportSpecifier(undefined, ts.createIdentifier(symbolName)),
109+
ts.factory.createImportSpecifier(undefined, ts.factory.createIdentifier(symbolName)),
110110
));
111111
} else {
112112
// Create the new import node.
113-
const namedImports = ts.createNamedImports([ts.createImportSpecifier(undefined,
114-
ts.createIdentifier(symbolName))]);
115-
const importClause = ts.createImportClause(undefined, namedImports);
116-
const newImport = ts.createImportDeclaration(undefined, undefined, importClause,
117-
ts.createLiteral(modulePath));
113+
const namedImports = ts.factory.createNamedImports([ts.factory.createImportSpecifier(undefined,
114+
ts.factory.createIdentifier(symbolName))]);
115+
const importClause = ts.factory.createImportClause(false, undefined, namedImports);
116+
const newImport = ts.factory.createImportDeclaration(undefined, undefined, importClause,
117+
ts.factory.createStringLiteral(modulePath));
118118

119119
if (allImports.length > 0) {
120120
// Find the last import and insert after.

packages/ngtools/webpack/src/transformers/register_locale_data.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function registerLocaleData(
6666
}
6767

6868
// Create the import node for the locale.
69-
const localeNamespaceId = ts.createUniqueName('__NgCli_locale_');
69+
const localeNamespaceId = ts.factory.createUniqueName('__NgCli_locale_');
7070
ops.push(...insertStarImport(
7171
sourceFile,
7272
localeNamespaceId,
@@ -76,19 +76,19 @@ export function registerLocaleData(
7676
));
7777

7878
// Create the import node for the registerLocaleData function.
79-
const regIdentifier = ts.createIdentifier(`registerLocaleData`);
80-
const regNamespaceId = ts.createUniqueName('__NgCli_locale_');
79+
const regIdentifier = ts.factory.createIdentifier(`registerLocaleData`);
80+
const regNamespaceId = ts.factory.createUniqueName('__NgCli_locale_');
8181
ops.push(
8282
...insertStarImport(sourceFile, regNamespaceId, '@angular/common', firstNode, true),
8383
);
8484

8585
// Create the register function call
86-
const registerFunctionCall = ts.createCall(
87-
ts.createPropertyAccess(regNamespaceId, regIdentifier),
86+
const registerFunctionCall = ts.factory.createCallExpression(
87+
ts.factory.createPropertyAccessExpression(regNamespaceId, regIdentifier),
8888
undefined,
89-
[ts.createPropertyAccess(localeNamespaceId, 'default')],
89+
[ts.factory.createPropertyAccessExpression(localeNamespaceId, 'default')],
9090
);
91-
const registerFunctionStatement = ts.createStatement(registerFunctionCall);
91+
const registerFunctionStatement = ts.factory.createExpressionStatement(registerFunctionCall);
9292

9393
ops.push(new AddNodeOperation(
9494
sourceFile,

packages/ngtools/webpack/src/transformers/replace_bootstrap.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ export function replaceBootstrap(
7474

7575
const platformBrowserDynamicIdentifier = innerCallExpr.expression as ts.Identifier;
7676

77-
const idPlatformBrowser = ts.createUniqueName('__NgCli_bootstrap_');
78-
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');
77+
const idPlatformBrowser = ts.factory.createUniqueName('__NgCli_bootstrap_');
78+
const idNgFactory = ts.factory.createUniqueName('__NgCli_bootstrap_');
7979

8080
// Add the transform operations.
8181
const relativeEntryModulePath = relative(dirname(sourceFile.fileName), entryModule.path);
@@ -93,13 +93,13 @@ export function replaceBootstrap(
9393
// Replace the entry module import.
9494
...insertStarImport(sourceFile, idNgFactory, modulePath),
9595
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
96-
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(className))),
96+
ts.factory.createPropertyAccessExpression(idNgFactory, ts.factory.createIdentifier(className))),
9797
// Replace the platformBrowserDynamic import.
9898
...insertStarImport(sourceFile, idPlatformBrowser, '@angular/platform-browser'),
9999
new ReplaceNodeOperation(sourceFile, platformBrowserDynamicIdentifier,
100-
ts.createPropertyAccess(idPlatformBrowser, 'platformBrowser')),
100+
ts.factory.createPropertyAccessExpression(idPlatformBrowser, 'platformBrowser')),
101101
new ReplaceNodeOperation(sourceFile, bootstrapModuleIdentifier,
102-
ts.createIdentifier(bootstrapIdentifier)),
102+
ts.factory.createIdentifier(bootstrapIdentifier)),
103103
);
104104
});
105105

packages/ngtools/webpack/src/transformers/replace_resources.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ export function replaceResources(
1616
const typeChecker = getTypeChecker();
1717
const resourceImportDeclarations: ts.ImportDeclaration[] = [];
1818
const moduleKind = context.getCompilerOptions().module;
19+
const nodeFactory = context.factory;
20+
1921
const visitNode: ts.Visitor = (node: ts.Node) => {
2022
if (ts.isClassDeclaration(node)) {
2123
const decorators = ts.visitNodes(node.decorators, node =>
2224
ts.isDecorator(node)
23-
? visitDecorator(node, typeChecker, directTemplateLoading, resourceImportDeclarations, moduleKind)
25+
? visitDecorator(nodeFactory, node, typeChecker, directTemplateLoading, resourceImportDeclarations, moduleKind)
2426
: node,
2527
);
2628

27-
return ts.updateClassDeclaration(
29+
return nodeFactory.updateClassDeclaration(
2830
node,
2931
decorators,
3032
node.modifiers,
@@ -46,10 +48,10 @@ export function replaceResources(
4648
const updatedSourceFile = ts.visitNode(sourceFile, visitNode);
4749
if (resourceImportDeclarations.length) {
4850
// Add resource imports
49-
return ts.updateSourceFileNode(
51+
return context.factory.updateSourceFile(
5052
updatedSourceFile,
5153
ts.setTextRange(
52-
ts.createNodeArray([
54+
context.factory.createNodeArray([
5355
...resourceImportDeclarations,
5456
...updatedSourceFile.statements,
5557
]),
@@ -64,6 +66,7 @@ export function replaceResources(
6466
}
6567

6668
function visitDecorator(
69+
nodeFactory: ts.NodeFactory,
6770
node: ts.Decorator,
6871
typeChecker: ts.TypeChecker,
6972
directTemplateLoading: boolean,
@@ -91,29 +94,30 @@ function visitDecorator(
9194
// visit all properties
9295
let properties = ts.visitNodes(objectExpression.properties, node =>
9396
ts.isObjectLiteralElementLike(node)
94-
? visitComponentMetadata(node, styleReplacements, directTemplateLoading, resourceImportDeclarations, moduleKind)
97+
? visitComponentMetadata(nodeFactory, node, styleReplacements, directTemplateLoading, resourceImportDeclarations, moduleKind)
9598
: node,
9699
);
97100

98101
// replace properties with updated properties
99102
if (styleReplacements.length > 0) {
100-
const styleProperty = ts.createPropertyAssignment(
101-
ts.createIdentifier('styles'),
102-
ts.createArrayLiteral(styleReplacements),
103+
const styleProperty = nodeFactory.createPropertyAssignment(
104+
nodeFactory.createIdentifier('styles'),
105+
nodeFactory.createArrayLiteralExpression(styleReplacements),
103106
);
104107

105-
properties = ts.createNodeArray([...properties, styleProperty]);
108+
properties = nodeFactory.createNodeArray([...properties, styleProperty]);
106109
}
107110

108-
return ts.updateDecorator(
111+
return nodeFactory.updateDecorator(
109112
node,
110-
ts.updateCall(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
111-
ts.updateObjectLiteral(objectExpression, properties),
113+
nodeFactory.updateCallExpression(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
114+
nodeFactory.updateObjectLiteralExpression(objectExpression, properties),
112115
]),
113116
);
114117
}
115118

116119
function visitComponentMetadata(
120+
nodeFactory: ts.NodeFactory,
117121
node: ts.ObjectLiteralElementLike,
118122
styleReplacements: ts.Expression[],
119123
directTemplateLoading: boolean,
@@ -131,6 +135,7 @@ function visitComponentMetadata(
131135

132136
case 'templateUrl':
133137
const importName = createResourceImport(
138+
nodeFactory,
134139
node.initializer,
135140
directTemplateLoading ? '!raw-loader!' : '',
136141
resourceImportDeclarations,
@@ -140,9 +145,9 @@ function visitComponentMetadata(
140145
return node;
141146
}
142147

143-
return ts.updatePropertyAssignment(
148+
return nodeFactory.updatePropertyAssignment(
144149
node,
145-
ts.createIdentifier('template'),
150+
nodeFactory.createIdentifier('template'),
146151
importName,
147152
);
148153
case 'styles':
@@ -158,10 +163,10 @@ function visitComponentMetadata(
158163
}
159164

160165
if (isInlineStyles) {
161-
return ts.createLiteral(node.text);
166+
return nodeFactory.createStringLiteral(node.text);
162167
}
163168

164-
return createResourceImport(node, undefined, resourceImportDeclarations, moduleKind) || node;
169+
return createResourceImport(nodeFactory, node, undefined, resourceImportDeclarations, moduleKind) || node;
165170
});
166171

167172
// Styles should be placed first
@@ -200,6 +205,7 @@ function isComponentDecorator(node: ts.Node, typeChecker: ts.TypeChecker): node
200205
}
201206

202207
function createResourceImport(
208+
nodeFactory: ts.NodeFactory,
203209
node: ts.Node,
204210
loader: string | undefined,
205211
resourceImportDeclarations: ts.ImportDeclaration[],
@@ -210,23 +216,23 @@ function createResourceImport(
210216
return null;
211217
}
212218

213-
const urlLiteral = ts.createLiteral(url);
219+
const urlLiteral = nodeFactory.createStringLiteral(url);
214220

215221
if (moduleKind < ts.ModuleKind.ES2015) {
216-
return ts.createPropertyAccess(
217-
ts.createCall(
218-
ts.createIdentifier('require'),
222+
return nodeFactory.createPropertyAccessExpression(
223+
nodeFactory.createCallExpression(
224+
nodeFactory.createIdentifier('require'),
219225
[],
220226
[urlLiteral],
221227
),
222228
'default',
223229
);
224230
} else {
225-
const importName = ts.createIdentifier(`__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`);
226-
resourceImportDeclarations.push(ts.createImportDeclaration(
231+
const importName = nodeFactory.createIdentifier(`__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`);
232+
resourceImportDeclarations.push(nodeFactory.createImportDeclaration(
227233
undefined,
228234
undefined,
229-
ts.createImportClause(importName, undefined),
235+
nodeFactory.createImportClause(false, importName, undefined),
230236
urlLiteral,
231237
));
232238

packages/ngtools/webpack/src/transformers/replace_server_bootstrap.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ export function replaceServerBootstrap(
7979

8080
const platformDynamicServerIdentifier = innerCallExpr.expression as ts.Identifier;
8181

82-
const idPlatformServer = ts.createUniqueName('__NgCli_bootstrap_');
83-
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');
82+
const idPlatformServer = ts.factory.createUniqueName('__NgCli_bootstrap_');
83+
const idNgFactory = ts.factory.createUniqueName('__NgCli_bootstrap_');
8484

8585
// Add the transform operations.
8686
ops.push(
8787
// Replace the entry module import.
8888
...insertStarImport(sourceFile, idNgFactory, factoryModulePath),
8989
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
90-
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))),
90+
ts.factory.createPropertyAccessExpression(idNgFactory, ts.factory.createIdentifier(factoryClassName))),
9191
// Replace the platformBrowserDynamic import.
9292
...insertStarImport(sourceFile, idPlatformServer, '@angular/platform-server'),
9393
new ReplaceNodeOperation(sourceFile, platformDynamicServerIdentifier,
94-
ts.createPropertyAccess(idPlatformServer, 'platformServer')),
94+
ts.factory.createPropertyAccessExpression(idPlatformServer, 'platformServer')),
9595
new ReplaceNodeOperation(sourceFile, bootstrapModuleIdentifier,
96-
ts.createIdentifier('bootstrapModuleFactory')),
96+
ts.factory.createIdentifier('bootstrapModuleFactory')),
9797
);
9898
} else if (callExpr.expression.kind === ts.SyntaxKind.Identifier) {
9999
// Figure out if it is renderModule
@@ -106,30 +106,30 @@ export function replaceServerBootstrap(
106106

107107
const renderModuleIdentifier = identifierExpr as ts.Identifier;
108108

109-
const idPlatformServer = ts.createUniqueName('__NgCli_bootstrap_');
110-
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');
109+
const idPlatformServer = ts.factory.createUniqueName('__NgCli_bootstrap_');
110+
const idNgFactory = ts.factory.createUniqueName('__NgCli_bootstrap_');
111111

112112
ops.push(
113113
// Replace the entry module import.
114114
...insertStarImport(sourceFile, idNgFactory, factoryModulePath),
115115
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
116-
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))),
116+
ts.factory.createPropertyAccessExpression(idNgFactory, ts.factory.createIdentifier(factoryClassName))),
117117
// Replace the renderModule import.
118118
...insertStarImport(sourceFile, idPlatformServer, '@angular/platform-server'),
119119
new ReplaceNodeOperation(sourceFile, renderModuleIdentifier,
120-
ts.createPropertyAccess(idPlatformServer, 'renderModuleFactory')),
120+
ts.factory.createPropertyAccessExpression(idPlatformServer, 'renderModuleFactory')),
121121
);
122122
}
123123
} else if (entryModuleIdentifier.parent.kind === ts.SyntaxKind.PropertyAssignment) {
124124
// This is for things that accept a module as a property in a config object
125125
// .ie the express engine
126126

127-
const idNgFactory = ts.createUniqueName('__NgCli_bootstrap_');
127+
const idNgFactory = ts.factory.createUniqueName('__NgCli_bootstrap_');
128128

129129
ops.push(
130130
...insertStarImport(sourceFile, idNgFactory, factoryModulePath),
131131
new ReplaceNodeOperation(sourceFile, entryModuleIdentifier,
132-
ts.createPropertyAccess(idNgFactory, ts.createIdentifier(factoryClassName))),
132+
ts.factory.createPropertyAccessExpression(idNgFactory, ts.factory.createIdentifier(factoryClassName))),
133133
);
134134
}
135135
});

0 commit comments

Comments
 (0)