refactor(core): move google3 migration rules into single directory (#30956)

Moves all google3 migration tslint rules into a single directory.
This makes it easier to wire up multiple migration rules in
google3 without having to update the rule directories each time
a new migration is available.

PR Close #30956
This commit is contained in:
Paul Gschwendtner
2019-06-18 11:28:23 +02:00
committed by Kara Erickson
parent 9f2ae5d6ff
commit f69e4e6f77
18 changed files with 33 additions and 56 deletions

View File

@ -4,7 +4,7 @@ ts_library(
name = "google3",
srcs = glob(["**/*.ts"]),
tsconfig = "//packages/core/schematics:tsconfig.json",
visibility = ["//packages/core/schematics/test:__pkg__"],
visibility = ["//packages/core/schematics/migrations/google3:__pkg__"],
deps = [
"//packages/core/schematics/migrations/missing-injectable",
"@npm//tslint",

View File

@ -1,64 +0,0 @@
/**
* @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;
}
}
}