feat(ivy): introduce missing-injectable migration for google3 (#30956)
Introduces a new migration schematic for adding the "@Injectable()" decorator to provider classes which are currently not migrated. Previously in ViewEngine, classes which are declared as providers sometimes don't require the "@Injectable()" decorator (e.g. https://stackblitz.com/edit/angular-hpo7gw) With Ivy, provider classes need to be explicitly decorated with the "@Injectable()" decorator if they are declared as providers of a given module. This commit introduces a migration schematic which automatically adds the explicit decorator to places where the decorator is currently missing. The migration logic is designed in a CLI devkit and TSlint agnostic way so that we can also have this migration run as part of a public CLI migration w/ `ng update`. This will be handled as part of a follow-up to reiterate on console output etc. Resolves FW-1371 PR Close #30956
This commit is contained in:

committed by
Kara Erickson

parent
9eefe25e2f
commit
9f2ae5d6ff
@ -0,0 +1,13 @@
|
||||
load("//tools:defaults.bzl", "ts_library")
|
||||
|
||||
ts_library(
|
||||
name = "google3",
|
||||
srcs = glob(["**/*.ts"]),
|
||||
tsconfig = "//packages/core/schematics:tsconfig.json",
|
||||
visibility = ["//packages/core/schematics/test:__pkg__"],
|
||||
deps = [
|
||||
"//packages/core/schematics/migrations/missing-injectable",
|
||||
"@npm//tslint",
|
||||
"@npm//typescript",
|
||||
],
|
||||
)
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {RuleFailure, Rules} from 'tslint';
|
||||
import * as ts from 'typescript';
|
||||
import {NgModuleCollector} from '../module_collector';
|
||||
import {MissingInjectableTransform} from '../transform';
|
||||
import {TslintUpdateRecorder} from './tslint_update_recorder';
|
||||
|
||||
/**
|
||||
* TSLint rule that flags classes which are declared as providers in NgModules but
|
||||
* aren't decorated with any Angular decorator.
|
||||
*/
|
||||
export class Rule extends Rules.TypedRule {
|
||||
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
|
||||
const ruleName = this.ruleName;
|
||||
const typeChecker = program.getTypeChecker();
|
||||
const sourceFiles = program.getSourceFiles().filter(
|
||||
s => !s.isDeclarationFile && !program.isSourceFileFromExternalLibrary(s));
|
||||
const moduleCollector = new NgModuleCollector(typeChecker);
|
||||
const failures: RuleFailure[] = [];
|
||||
|
||||
// Analyze source files by detecting all NgModule definitions.
|
||||
sourceFiles.forEach(sourceFile => moduleCollector.visitNode(sourceFile));
|
||||
|
||||
const {resolvedModules} = moduleCollector;
|
||||
const transformer = new MissingInjectableTransform(typeChecker, getUpdateRecorder);
|
||||
const updateRecorders = new Map<ts.SourceFile, TslintUpdateRecorder>();
|
||||
|
||||
resolvedModules.forEach(module => {
|
||||
transformer.migrateModule(module).forEach(({message, node}) => {
|
||||
// Only report failures for the current source file that is visited.
|
||||
if (node.getSourceFile() === sourceFile) {
|
||||
failures.push(
|
||||
new RuleFailure(node.getSourceFile(), node.getStart(), 0, message, ruleName));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Record the changes collected in the import manager and NgModule manager.
|
||||
transformer.recordChanges();
|
||||
|
||||
if (updateRecorders.has(sourceFile)) {
|
||||
failures.push(...updateRecorders.get(sourceFile) !.failures);
|
||||
}
|
||||
|
||||
return failures;
|
||||
|
||||
/** Gets the update recorder for the specified source file. */
|
||||
function getUpdateRecorder(sourceFile: ts.SourceFile): TslintUpdateRecorder {
|
||||
if (updateRecorders.has(sourceFile)) {
|
||||
return updateRecorders.get(sourceFile) !;
|
||||
}
|
||||
const recorder = new TslintUpdateRecorder(ruleName, sourceFile);
|
||||
updateRecorders.set(sourceFile, recorder);
|
||||
return recorder;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Replacement, RuleFailure} from 'tslint';
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {UpdateRecorder} from '../update_recorder';
|
||||
|
||||
export class TslintUpdateRecorder implements UpdateRecorder {
|
||||
failures: RuleFailure[] = [];
|
||||
|
||||
constructor(private ruleName: string, private sourceFile: ts.SourceFile) {}
|
||||
|
||||
addClassDecorator(node: ts.ClassDeclaration, decoratorText: string, moduleName: string) {
|
||||
// Adding a decorator should be the last replacement. Replacements/rule failures
|
||||
// are handled in reverse and in case a decorator and import are inserted at
|
||||
// the start of the file, the class decorator should come after the import.
|
||||
this.failures.unshift(new RuleFailure(
|
||||
this.sourceFile, node.getStart(), 0, `Class needs to be decorated with ` +
|
||||
`"${decoratorText}" because it has been provided by "${moduleName}".`,
|
||||
this.ruleName, Replacement.appendText(node.getStart(), `${decoratorText}\n`)));
|
||||
}
|
||||
|
||||
addNewImport(start: number, importText: string) {
|
||||
this.failures.push(new RuleFailure(
|
||||
this.sourceFile, start, 0, `Source file needs to have import: "${importText}"`,
|
||||
this.ruleName, Replacement.appendText(start, importText)));
|
||||
}
|
||||
|
||||
updateExistingImport(namedBindings: ts.NamedImports, newNamedBindings: string): void {
|
||||
const fix = [
|
||||
Replacement.deleteText(namedBindings.getStart(), namedBindings.getWidth()),
|
||||
Replacement.appendText(namedBindings.getStart(), newNamedBindings),
|
||||
];
|
||||
this.failures.push(new RuleFailure(
|
||||
this.sourceFile, namedBindings.getStart(), namedBindings.getEnd(),
|
||||
`Import needs to be updated to import symbols: "${newNamedBindings}"`, this.ruleName, fix));
|
||||
}
|
||||
|
||||
replaceDecorator(decorator: ts.Node, newText: string, moduleName: string): void {
|
||||
const fix = [
|
||||
Replacement.deleteText(decorator.getStart(), decorator.getWidth()),
|
||||
Replacement.appendText(decorator.getStart(), newText),
|
||||
];
|
||||
this.failures.push(new RuleFailure(
|
||||
this.sourceFile, decorator.getStart(), decorator.getEnd(),
|
||||
`Decorator needs to be replaced with "${newText}" because it has been provided ` +
|
||||
`by "${moduleName}"`,
|
||||
this.ruleName, fix));
|
||||
}
|
||||
|
||||
commitUpdate() {}
|
||||
}
|
Reference in New Issue
Block a user