feat(core): template-var-assignment update schematic (#29608)
Introduces a new update schematic called "template-var-assignment" that is responsible for analyzing template files in order to warn developers if template variables are assigned to values. The schematic also comes with a driver for `tslint` so that the check can be used wtihin Google. PR Close #29608
This commit is contained in:

committed by
Jason Aden

parent
15eb1e0ce1
commit
7c8f4e3202
@ -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/template-var-assignment",
|
||||
"//packages/core/schematics/utils/tslint",
|
||||
"@npm//tslint",
|
||||
],
|
||||
)
|
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @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 {createHtmlSourceFile} from '../../../utils/tslint/tslint_html_source_file';
|
||||
import {analyzeResolvedTemplate} from '../analyze_template';
|
||||
import {NgComponentTemplateVisitor} from '../angular/ng_component_template';
|
||||
|
||||
const FAILURE_MESSAGE = 'Found assignment to template variable. This does not work with Ivy and ' +
|
||||
'needs to be updated.';
|
||||
|
||||
/**
|
||||
* Rule that reports if an Angular template contains property assignments to template variables.
|
||||
*/
|
||||
export class Rule extends Rules.TypedRule {
|
||||
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
|
||||
const typeChecker = program.getTypeChecker();
|
||||
const templateVisitor = new NgComponentTemplateVisitor(typeChecker);
|
||||
const failures: RuleFailure[] = [];
|
||||
|
||||
// Analyze the current source files by detecting all referenced HTML templates.
|
||||
templateVisitor.visitNode(sourceFile);
|
||||
|
||||
const {resolvedTemplates} = templateVisitor;
|
||||
|
||||
// Analyze each resolved template and print a warning for property writes to
|
||||
// template variables.
|
||||
resolvedTemplates.forEach((template, filePath) => {
|
||||
const nodes = analyzeResolvedTemplate(filePath, template);
|
||||
const templateFile =
|
||||
template.inline ? sourceFile : createHtmlSourceFile(filePath, template.content);
|
||||
|
||||
if (!nodes) {
|
||||
return;
|
||||
}
|
||||
|
||||
nodes.forEach(n => {
|
||||
failures.push(new RuleFailure(
|
||||
templateFile, template.start + n.start, template.start + n.end, FAILURE_MESSAGE,
|
||||
this.ruleName));
|
||||
});
|
||||
});
|
||||
|
||||
return failures;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user