feat(core): add dynamic queries schematic (#32231)
Adds a schematic that will remove the explicit `static: false` flag from dynamic queries. E.g. ```ts import { Directive, ViewChild, ContentChild, ElementRef } from '@angular/core'; @Directive() export class MyDirective { @ViewChild('child', { static: false }) child: any; @ViewChild('secondChild', { read: ElementRef, static: false }) secondChild: ElementRef; @ContentChild('thirdChild', { static: false }) thirdChild: any; } ``` ```ts import { Directive, ViewChild, ContentChild, ElementRef } from '@angular/core'; @Directive() export class MyDirective { @ViewChild('child') child: any; @ViewChild('secondChild', { read: ElementRef }) secondChild: ElementRef; @ContentChild('thirdChild') thirdChild: any; } ``` PR Close #32231
This commit is contained in:
@ -6,6 +6,7 @@ ts_library(
|
||||
tsconfig = "//packages/core/schematics:tsconfig.json",
|
||||
visibility = ["//packages/core/schematics/test/google3:__pkg__"],
|
||||
deps = [
|
||||
"//packages/core/schematics/migrations/dynamic-queries",
|
||||
"//packages/core/schematics/migrations/missing-injectable",
|
||||
"//packages/core/schematics/migrations/missing-injectable/google3",
|
||||
"//packages/core/schematics/migrations/renderer-to-renderer2",
|
||||
|
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @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';
|
||||
|
||||
import {identifyDynamicQueryNodes, removeOptionsParameter, removeStaticFlag} from '../dynamic-queries/util';
|
||||
|
||||
const RULE_NAME = 'dynamic-queries';
|
||||
const FAILURE_MESSAGE =
|
||||
'The static flag defaults to false, so setting it false manually is unnecessary.';
|
||||
|
||||
/**
|
||||
* TSLint rule that removes the `static` flag from dynamic queries.
|
||||
*/
|
||||
export class Rule extends Rules.TypedRule {
|
||||
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): RuleFailure[] {
|
||||
const printer = ts.createPrinter();
|
||||
const failures: RuleFailure[] = [];
|
||||
const result = identifyDynamicQueryNodes(program.getTypeChecker(), sourceFile);
|
||||
|
||||
result.removeProperty.forEach(node => {
|
||||
failures.push(new RuleFailure(
|
||||
sourceFile, node.getStart(), node.getEnd(), FAILURE_MESSAGE, RULE_NAME,
|
||||
new Replacement(
|
||||
node.getStart(), node.getWidth(),
|
||||
printer.printNode(ts.EmitHint.Unspecified, removeStaticFlag(node), sourceFile))));
|
||||
});
|
||||
|
||||
result.removeParameter.forEach(node => {
|
||||
failures.push(new RuleFailure(
|
||||
sourceFile, node.getStart(), node.getEnd(), FAILURE_MESSAGE, RULE_NAME,
|
||||
new Replacement(
|
||||
node.getStart(), node.getWidth(),
|
||||
printer.printNode(
|
||||
ts.EmitHint.Unspecified, removeOptionsParameter(node), sourceFile))));
|
||||
});
|
||||
|
||||
return failures;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user