Skip to content

refactor(material/schematics): replace usages of deprecated TypeScript APIs #24454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,9 @@ export class HammerGesturesMigration extends DevkitMigration<null> {
HAMMER_CONFIG_TOKEN_NAME,
HAMMER_CONFIG_TOKEN_MODULE,
);
const newProviderNode = ts.createObjectLiteral([
ts.createPropertyAssignment('provide', hammerConfigTokenExpr),
ts.createPropertyAssignment('useClass', gestureConfigExpr),
const newProviderNode = ts.factory.createObjectLiteralExpression([
ts.factory.createPropertyAssignment('provide', hammerConfigTokenExpr),
ts.factory.createPropertyAssignment('useClass', gestureConfigExpr),
]);

// If the providers field exists and already contains references to the hammer gesture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,23 +210,23 @@ export class ImportManager {
// If no symbol name has been specified, the default import is requested. In that
// case we search for non-namespace and non-specifier imports.
if (!symbolName && !importData.namespace && !importData.specifiers) {
return ts.createIdentifier(importData.name!.text);
return ts.factory.createIdentifier(importData.name!.text);
}

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

if (existingSpecifier) {
return ts.createIdentifier(existingSpecifier.name.text);
return ts.factory.createIdentifier(existingSpecifier.name.text);
}

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

if (symbolName) {
const propertyIdentifier = ts.createIdentifier(symbolName);
const propertyIdentifier = ts.factory.createIdentifier(symbolName);
const generatedUniqueIdentifier = this._getUniqueIdentifier(
sourceFile,
symbolName,
Expand All @@ -276,11 +276,11 @@ export class ImportManager {
const needsGeneratedUniqueName = generatedUniqueIdentifier.text !== symbolName;
identifier = needsGeneratedUniqueName ? generatedUniqueIdentifier : propertyIdentifier;

const newImportDecl = ts.createImportDeclaration(
const newImportDecl = ts.factory.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(undefined, ts.createNamedImports([])),
ts.createStringLiteral(moduleName),
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports([])),
ts.factory.createStringLiteral(moduleName),
);

newImport = {
Expand All @@ -300,11 +300,11 @@ export class ImportManager {
'defaultExport',
ignoreIdentifierCollisions,
);
const newImportDecl = ts.createImportDeclaration(
const newImportDecl = ts.factory.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(identifier, undefined),
ts.createStringLiteral(moduleName),
ts.factory.createImportClause(false, identifier, undefined),
ts.factory.createStringLiteral(moduleName),
);
newImport = {
moduleName,
Expand Down Expand Up @@ -351,19 +351,19 @@ export class ImportManager {
const importSpecifiers = importData.specifiers.map(s =>
createImportSpecifier(s.propertyName, s.name),
);
const updatedBindings = ts.updateNamedImports(namedBindings, importSpecifiers);
const updatedBindings = ts.factory.updateNamedImports(namedBindings, importSpecifiers);

// In case an import has been added newly, we need to print the whole import
// declaration and insert it at the import start index. Otherwise, we just
// update the named bindings to not re-print the whole import (which could
// cause unnecessary formatting changes)
if (hasFlag(importData, ImportState.ADDED)) {
const updatedImport = ts.updateImportDeclaration(
const updatedImport = ts.factory.updateImportDeclaration(
importData.node,
undefined,
undefined,
ts.createImportClause(undefined, updatedBindings),
ts.createStringLiteral(importData.moduleName),
ts.factory.createImportClause(false, undefined, updatedBindings),
ts.factory.createStringLiteral(importData.moduleName),
undefined,
);
const newImportText = this._printer.printNode(
Expand Down Expand Up @@ -452,7 +452,7 @@ export class ImportManager {
): ts.Identifier {
if (this._isUniqueIdentifierName(sourceFile, symbolName, ignoreIdentifierCollisions)) {
this._recordUsedIdentifier(sourceFile, symbolName);
return ts.createIdentifier(symbolName);
return ts.factory.createIdentifier(symbolName);
}

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

this._recordUsedIdentifier(sourceFile, name!);
return ts.createIdentifier(name!);
return ts.factory.createIdentifier(name!);
}

/**
Expand Down Expand Up @@ -535,6 +535,6 @@ function createImportSpecifier(
name: ts.Identifier,
): ts.ImportSpecifier {
return PARSED_TS_VERSION > 4.4
? ts.createImportSpecifier(false, propertyName, name)
? ts.factory.createImportSpecifier(false, propertyName, name)
: (ts.createImportSpecifier as any)(propertyName, name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ export class SecondaryEntryPointsMigration extends Migration<null> {
const newImportStatements = Array.from(importMap.entries())
.sort()
.map(([name, elements]) => {
const newImport = ts.createImportDeclaration(
const newImport = ts.factory.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(undefined, ts.createNamedImports(elements)),
createStringLiteral(`${materialModuleSpecifier}/${name}`, singleQuoteImport),
ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(elements)),
ts.factory.createStringLiteral(`${materialModuleSpecifier}/${name}`, singleQuoteImport),
);
return this.printer.printNode(
ts.EmitHint.Unspecified,
Expand Down Expand Up @@ -153,18 +153,6 @@ export class SecondaryEntryPointsMigration extends Migration<null> {
}
}

/**
* Creates a string literal from the specified text.
* @param text Text of the string literal.
* @param singleQuotes Whether single quotes should be used when printing the literal node.
*/
function createStringLiteral(text: string, singleQuotes: boolean): ts.StringLiteral {
const literal = ts.createStringLiteral(text);
// See: https://github.com/microsoft/TypeScript/blob/master/src/compiler/utilities.ts#L584-L590
(literal as any).singleQuote = singleQuotes;
return literal;
}

/** Gets the symbol that contains the value declaration of the given node. */
function getDeclarationSymbolOfNode(node: ts.Node, checker: ts.TypeChecker): ts.Symbol | undefined {
const symbol = checker.getSymbolAtLocation(node);
Expand Down