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
2019-08-21 01:40:30 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
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
2019-08-21 01:40:30 -04:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|