2019-03-13 11:29:25 -04:00
|
|
|
/**
|
|
|
|
* @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, Rules} from 'tslint';
|
|
|
|
import * as ts from 'typescript';
|
2019-04-08 10:16:56 -04:00
|
|
|
|
2019-06-18 05:28:23 -04:00
|
|
|
import {NgComponentTemplateVisitor} from '../../utils/ng_component_template';
|
|
|
|
import {NgQueryResolveVisitor} from '../static-queries/angular/ng_query_visitor';
|
|
|
|
import {QueryTiming} from '../static-queries/angular/query-definition';
|
|
|
|
import {QueryUsageStrategy} from '../static-queries/strategies/usage_strategy/usage_strategy';
|
|
|
|
import {getTransformedQueryCallExpr} from '../static-queries/transform';
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-03-14 18:59:44 -04:00
|
|
|
const FAILURE_MESSAGE = 'Query does not explicitly specify its timing. Read more here: ' +
|
2019-03-13 11:46:06 -04:00
|
|
|
'https://github.com/angular/angular/pull/28810';
|
|
|
|
|
2019-03-13 11:29:25 -04:00
|
|
|
/**
|
|
|
|
* Rule that reports if an Angular "ViewChild" or "ContentChild" query is not explicitly
|
|
|
|
* specifying its timing. The rule also provides TSLint automatic replacements that can
|
|
|
|
* be applied in order to automatically migrate to the explicit query timing API.
|
|
|
|
*/
|
|
|
|
export class Rule extends Rules.TypedRule {
|
|
|
|
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
|
|
|
|
const typeChecker = program.getTypeChecker();
|
|
|
|
const queryVisitor = new NgQueryResolveVisitor(program.getTypeChecker());
|
2019-04-08 10:16:56 -04:00
|
|
|
const templateVisitor = new NgComponentTemplateVisitor(typeChecker);
|
2019-03-13 11:29:25 -04:00
|
|
|
const rootSourceFiles = program.getRootFileNames().map(f => program.getSourceFile(f) !);
|
|
|
|
const printer = ts.createPrinter();
|
|
|
|
const failures: RuleFailure[] = [];
|
|
|
|
|
2019-04-08 10:16:56 -04:00
|
|
|
// Analyze source files by detecting queries, class relations and component templates.
|
|
|
|
rootSourceFiles.forEach(sourceFile => {
|
2019-05-03 09:02:08 -04:00
|
|
|
queryVisitor.visitNode(sourceFile);
|
|
|
|
templateVisitor.visitNode(sourceFile);
|
2019-04-08 10:16:56 -04:00
|
|
|
});
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
const {resolvedQueries, classMetadata} = queryVisitor;
|
2019-04-08 10:16:56 -04:00
|
|
|
|
|
|
|
// Add all resolved templates to the class metadata so that we can also
|
|
|
|
// check component templates for static query usage.
|
|
|
|
templateVisitor.resolvedTemplates.forEach(template => {
|
|
|
|
if (classMetadata.has(template.container)) {
|
|
|
|
classMetadata.get(template.container) !.template = template;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-13 11:29:25 -04:00
|
|
|
const queries = resolvedQueries.get(sourceFile);
|
2019-04-08 13:37:31 -04:00
|
|
|
const usageStrategy = new QueryUsageStrategy(classMetadata, typeChecker);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
// No queries detected for the given source file.
|
|
|
|
if (!queries) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the query usage for all resolved queries and update the
|
|
|
|
// query definitions to explicitly declare the query timing (static or dynamic)
|
|
|
|
queries.forEach(q => {
|
|
|
|
const queryExpr = q.decorator.node.expression;
|
2019-04-12 11:55:51 -04:00
|
|
|
const {timing, message} = usageStrategy.detectTiming(q);
|
2019-04-28 10:40:59 -04:00
|
|
|
const result = getTransformedQueryCallExpr(q, timing, !!message);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
2019-04-28 10:40:59 -04:00
|
|
|
if (!result) {
|
2019-03-13 11:29:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-28 10:40:59 -04:00
|
|
|
const newText = printer.printNode(ts.EmitHint.Unspecified, result.node, sourceFile);
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
// Replace the existing query decorator call expression with the
|
|
|
|
// updated call expression node.
|
|
|
|
const fix = new Replacement(queryExpr.getStart(), queryExpr.getWidth(), newText);
|
2019-03-13 11:46:06 -04:00
|
|
|
const failureMessage = `${FAILURE_MESSAGE}. Based on analysis of the query it can be ` +
|
2019-03-14 18:59:44 -04:00
|
|
|
`marked as "{static: ${(timing === QueryTiming.STATIC).toString()}}".`;
|
2019-03-13 11:29:25 -04:00
|
|
|
|
|
|
|
failures.push(new RuleFailure(
|
2019-04-09 15:21:28 -04:00
|
|
|
sourceFile, queryExpr.getStart(), queryExpr.getEnd(), failureMessage, this.ruleName,
|
2019-03-13 11:46:06 -04:00
|
|
|
fix));
|
2019-03-13 11:29:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
}
|