Skip to content

Commit 146a7a2

Browse files
authored
refactor(material/schematics): replace usages of deprecated TypeScript APIs (#24454)
Replaces all of our usages of deprecated `ts.create*` and `ts.update*` APIs with their non-deprecated counterparts.
1 parent 29b0a61 commit 146a7a2

File tree

3 files changed

+26
-38
lines changed

3 files changed

+26
-38
lines changed

src/material/schematics/ng-update/migrations/hammer-gestures-v9/hammer-gestures-migration.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,9 @@ export class HammerGesturesMigration extends DevkitMigration<null> {
762762
HAMMER_CONFIG_TOKEN_NAME,
763763
HAMMER_CONFIG_TOKEN_MODULE,
764764
);
765-
const newProviderNode = ts.createObjectLiteral([
766-
ts.createPropertyAssignment('provide', hammerConfigTokenExpr),
767-
ts.createPropertyAssignment('useClass', gestureConfigExpr),
765+
const newProviderNode = ts.factory.createObjectLiteralExpression([
766+
ts.factory.createPropertyAssignment('provide', hammerConfigTokenExpr),
767+
ts.factory.createPropertyAssignment('useClass', gestureConfigExpr),
768768
]);
769769

770770
// If the providers field exists and already contains references to the hammer gesture

src/material/schematics/ng-update/migrations/hammer-gestures-v9/import-manager.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,23 @@ export class ImportManager {
210210
// If no symbol name has been specified, the default import is requested. In that
211211
// case we search for non-namespace and non-specifier imports.
212212
if (!symbolName && !importData.namespace && !importData.specifiers) {
213-
return ts.createIdentifier(importData.name!.text);
213+
return ts.factory.createIdentifier(importData.name!.text);
214214
}
215215

216216
// In case a "Type" symbol is imported, we can't use namespace imports
217217
// because these only export symbols available at runtime (no types)
218218
if (importData.namespace && !typeImport) {
219-
return ts.createPropertyAccess(
220-
ts.createIdentifier(importData.name!.text),
221-
ts.createIdentifier(symbolName || 'default'),
219+
return ts.factory.createPropertyAccessExpression(
220+
ts.factory.createIdentifier(importData.name!.text),
221+
ts.factory.createIdentifier(symbolName || 'default'),
222222
);
223223
} else if (importData.specifiers && symbolName) {
224224
const existingSpecifier = importData.specifiers.find(s =>
225225
s.propertyName ? s.propertyName.text === symbolName : s.name.text === symbolName,
226226
);
227227

228228
if (existingSpecifier) {
229-
return ts.createIdentifier(existingSpecifier.name.text);
229+
return ts.factory.createIdentifier(existingSpecifier.name.text);
230230
}
231231

232232
// In case the symbol could not be found in an existing import, we
@@ -239,7 +239,7 @@ export class ImportManager {
239239
// If there is an existing import that matches the specified module, we
240240
// just update the import specifiers to also import the requested symbol.
241241
if (existingImport) {
242-
const propertyIdentifier = ts.createIdentifier(symbolName!);
242+
const propertyIdentifier = ts.factory.createIdentifier(symbolName!);
243243
const generatedUniqueIdentifier = this._getUniqueIdentifier(
244244
sourceFile,
245245
symbolName!,
@@ -267,7 +267,7 @@ export class ImportManager {
267267
let newImport: AnalyzedImport | null = null;
268268

269269
if (symbolName) {
270-
const propertyIdentifier = ts.createIdentifier(symbolName);
270+
const propertyIdentifier = ts.factory.createIdentifier(symbolName);
271271
const generatedUniqueIdentifier = this._getUniqueIdentifier(
272272
sourceFile,
273273
symbolName,
@@ -276,11 +276,11 @@ export class ImportManager {
276276
const needsGeneratedUniqueName = generatedUniqueIdentifier.text !== symbolName;
277277
identifier = needsGeneratedUniqueName ? generatedUniqueIdentifier : propertyIdentifier;
278278

279-
const newImportDecl = ts.createImportDeclaration(
279+
const newImportDecl = ts.factory.createImportDeclaration(
280280
undefined,
281281
undefined,
282-
ts.createImportClause(undefined, ts.createNamedImports([])),
283-
ts.createStringLiteral(moduleName),
282+
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([])),
283+
ts.factory.createStringLiteral(moduleName),
284284
);
285285

286286
newImport = {
@@ -300,11 +300,11 @@ export class ImportManager {
300300
'defaultExport',
301301
ignoreIdentifierCollisions,
302302
);
303-
const newImportDecl = ts.createImportDeclaration(
303+
const newImportDecl = ts.factory.createImportDeclaration(
304304
undefined,
305305
undefined,
306-
ts.createImportClause(identifier, undefined),
307-
ts.createStringLiteral(moduleName),
306+
ts.factory.createImportClause(false, identifier, undefined),
307+
ts.factory.createStringLiteral(moduleName),
308308
);
309309
newImport = {
310310
moduleName,
@@ -351,19 +351,19 @@ export class ImportManager {
351351
const importSpecifiers = importData.specifiers.map(s =>
352352
createImportSpecifier(s.propertyName, s.name),
353353
);
354-
const updatedBindings = ts.updateNamedImports(namedBindings, importSpecifiers);
354+
const updatedBindings = ts.factory.updateNamedImports(namedBindings, importSpecifiers);
355355

356356
// In case an import has been added newly, we need to print the whole import
357357
// declaration and insert it at the import start index. Otherwise, we just
358358
// update the named bindings to not re-print the whole import (which could
359359
// cause unnecessary formatting changes)
360360
if (hasFlag(importData, ImportState.ADDED)) {
361-
const updatedImport = ts.updateImportDeclaration(
361+
const updatedImport = ts.factory.updateImportDeclaration(
362362
importData.node,
363363
undefined,
364364
undefined,
365-
ts.createImportClause(undefined, updatedBindings),
366-
ts.createStringLiteral(importData.moduleName),
365+
ts.factory.createImportClause(false, undefined, updatedBindings),
366+
ts.factory.createStringLiteral(importData.moduleName),
367367
undefined,
368368
);
369369
const newImportText = this._printer.printNode(
@@ -452,7 +452,7 @@ export class ImportManager {
452452
): ts.Identifier {
453453
if (this._isUniqueIdentifierName(sourceFile, symbolName, ignoreIdentifierCollisions)) {
454454
this._recordUsedIdentifier(sourceFile, symbolName);
455-
return ts.createIdentifier(symbolName);
455+
return ts.factory.createIdentifier(symbolName);
456456
}
457457

458458
let name: string | null = null;
@@ -462,7 +462,7 @@ export class ImportManager {
462462
} while (!this._isUniqueIdentifierName(sourceFile, name, ignoreIdentifierCollisions));
463463

464464
this._recordUsedIdentifier(sourceFile, name!);
465-
return ts.createIdentifier(name!);
465+
return ts.factory.createIdentifier(name!);
466466
}
467467

468468
/**
@@ -535,6 +535,6 @@ function createImportSpecifier(
535535
name: ts.Identifier,
536536
): ts.ImportSpecifier {
537537
return PARSED_TS_VERSION > 4.4
538-
? ts.createImportSpecifier(false, propertyName, name)
538+
? ts.factory.createImportSpecifier(false, propertyName, name)
539539
: (ts.createImportSpecifier as any)(propertyName, name);
540540
}

src/material/schematics/ng-update/migrations/package-imports-v8/secondary-entry-points-migration.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ export class SecondaryEntryPointsMigration extends Migration<null> {
121121
const newImportStatements = Array.from(importMap.entries())
122122
.sort()
123123
.map(([name, elements]) => {
124-
const newImport = ts.createImportDeclaration(
124+
const newImport = ts.factory.createImportDeclaration(
125125
undefined,
126126
undefined,
127-
ts.createImportClause(undefined, ts.createNamedImports(elements)),
128-
createStringLiteral(`${materialModuleSpecifier}/${name}`, singleQuoteImport),
127+
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(elements)),
128+
ts.factory.createStringLiteral(`${materialModuleSpecifier}/${name}`, singleQuoteImport),
129129
);
130130
return this.printer.printNode(
131131
ts.EmitHint.Unspecified,
@@ -153,18 +153,6 @@ export class SecondaryEntryPointsMigration extends Migration<null> {
153153
}
154154
}
155155

156-
/**
157-
* Creates a string literal from the specified text.
158-
* @param text Text of the string literal.
159-
* @param singleQuotes Whether single quotes should be used when printing the literal node.
160-
*/
161-
function createStringLiteral(text: string, singleQuotes: boolean): ts.StringLiteral {
162-
const literal = ts.createStringLiteral(text);
163-
// See: https://github.com/microsoft/TypeScript/blob/master/src/compiler/utilities.ts#L584-L590
164-
(literal as any).singleQuote = singleQuotes;
165-
return literal;
166-
}
167-
168156
/** Gets the symbol that contains the value declaration of the given node. */
169157
function getDeclarationSymbolOfNode(node: ts.Node, checker: ts.TypeChecker): ts.Symbol | undefined {
170158
const symbol = checker.getSymbolAtLocation(node);

0 commit comments

Comments
 (0)