|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import * as ts from 'typescript'; |
| 10 | + |
| 11 | +import {getImportSpecifier} from '../../utils/typescript/imports'; |
| 12 | +import {isReferenceToImport} from '../../utils/typescript/symbol'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Configures the methods that the migration should be looking for |
| 16 | + * and the properties from `NavigationExtras` that should be preserved. |
| 17 | + */ |
| 18 | +const methodConfig = new Set<string>(['navigate', 'createUrlTree']); |
| 19 | + |
| 20 | +const preserveQueryParamsKey = 'preserveQueryParams'; |
| 21 | + |
| 22 | +export function migrateLiteral( |
| 23 | + methodName: string, node: ts.ObjectLiteralExpression): ts.ObjectLiteralExpression { |
| 24 | + const isMigratableMethod = methodConfig.has(methodName); |
| 25 | + |
| 26 | + if (!isMigratableMethod) { |
| 27 | + throw Error(`Attempting to migrate unconfigured method called ${methodName}.`); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + const propertiesToKeep: ts.ObjectLiteralElementLike[] = []; |
| 32 | + let propertyToMigrate: ts.PropertyAssignment|ts.ShorthandPropertyAssignment|undefined = undefined; |
| 33 | + |
| 34 | + for (const property of node.properties) { |
| 35 | + // Only look for regular and shorthand property assignments since resolving things |
| 36 | + // like spread operators becomes too complicated for this migration. |
| 37 | + if ((ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) && |
| 38 | + (ts.isStringLiteralLike(property.name) || ts.isNumericLiteral(property.name) || |
| 39 | + ts.isIdentifier(property.name)) && |
| 40 | + (property.name.text === preserveQueryParamsKey)) { |
| 41 | + propertyToMigrate = property; |
| 42 | + continue; |
| 43 | + } |
| 44 | + propertiesToKeep.push(property); |
| 45 | + } |
| 46 | + |
| 47 | + // Don't modify the node if there's nothing to migrate. |
| 48 | + if (propertyToMigrate === undefined) { |
| 49 | + return node; |
| 50 | + } |
| 51 | + |
| 52 | + if ((ts.isShorthandPropertyAssignment(propertyToMigrate) && |
| 53 | + propertyToMigrate.objectAssignmentInitializer?.kind === ts.SyntaxKind.TrueKeyword) || |
| 54 | + (ts.isPropertyAssignment(propertyToMigrate) && |
| 55 | + propertyToMigrate.initializer.kind === ts.SyntaxKind.TrueKeyword)) { |
| 56 | + return ts.updateObjectLiteral( |
| 57 | + node, |
| 58 | + propertiesToKeep.concat( |
| 59 | + ts.createPropertyAssignment('queryParamsHandler', ts.createIdentifier(`'preserve'`)))); |
| 60 | + } |
| 61 | + |
| 62 | + return ts.updateObjectLiteral(node, propertiesToKeep); |
| 63 | +} |
| 64 | + |
| 65 | +export function findLiteralsToMigrate(sourceFile: ts.SourceFile, typeChecker: ts.TypeChecker) { |
| 66 | + const results = new Map<string, Set<ts.ObjectLiteralExpression>>( |
| 67 | + Array.from(methodConfig.keys(), key => [key, new Set()])); |
| 68 | + const routerImport = getImportSpecifier(sourceFile, '@angular/router', 'Router'); |
| 69 | + const seenLiterals = new Map<ts.ObjectLiteralExpression, string>(); |
| 70 | + |
| 71 | + if (routerImport) { |
| 72 | + sourceFile.forEachChild(function visitNode(node: ts.Node) { |
| 73 | + // Look for calls that look like `foo.<method to migrate>` with more than one parameter. |
| 74 | + if (ts.isCallExpression(node) && node.arguments.length > 1 && |
| 75 | + ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.name) && |
| 76 | + methodConfig.has(node.expression.name.text)) { |
| 77 | + // Check whether the type of the object on which the |
| 78 | + // function is called refers to the Router import. |
| 79 | + if (isReferenceToImport(typeChecker, node.expression.expression, routerImport)) { |
| 80 | + const methodName = node.expression.name.text; |
| 81 | + const parameterDeclaration = |
| 82 | + typeChecker.getTypeAtLocation(node.arguments[1]).getSymbol()?.valueDeclaration; |
| 83 | + |
| 84 | + // Find the source of the object literal. |
| 85 | + if (parameterDeclaration && ts.isObjectLiteralExpression(parameterDeclaration)) { |
| 86 | + if (!seenLiterals.has(parameterDeclaration)) { |
| 87 | + results.get(methodName)!.add(parameterDeclaration); |
| 88 | + seenLiterals.set(parameterDeclaration, methodName); |
| 89 | + // If the same literal has been passed into multiple different methods, we can't |
| 90 | + // migrate it, because the supported properties are different. When we detect such |
| 91 | + // a case, we drop it from the results so that it gets ignored. If it's used multiple |
| 92 | + // times for the same method, it can still be migrated. |
| 93 | + } else if (seenLiterals.get(parameterDeclaration) !== methodName) { |
| 94 | + results.forEach(literals => literals.delete(parameterDeclaration)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } else { |
| 99 | + node.forEachChild(visitNode); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + return results; |
| 105 | +} |
0 commit comments