Skip to content

build: reintroduce static query rule #17360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tools/tslint-rules/staticQueryRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

/**
* Rule which enforces that all queries are explicitly marked as static or non-static.
* TODO(crisbeto): we can remove this once we don't support 8.0.0 anymore.
*/
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {
visitPropertyDeclaration(node: ts.PropertyDeclaration) {
const childQueryDecorator = node.decorators && node.decorators.find(decorator => {
const expression = (decorator.expression as ts.CallExpression);
const name = expression && expression.expression.getText();
return name === 'ViewChild' || name === 'ContentChild';
});

if (childQueryDecorator) {
const options = (childQueryDecorator.expression as ts.CallExpression).arguments[1];

if (!options || !ts.isObjectLiteralExpression(options) ||
!this._getObjectProperty(options, 'static')) {
this.addFailureAtNode(childQueryDecorator,
'Queries have to explicitly set the `static` option.');
}
}

super.visitPropertyDeclaration(node);
}

/** Gets the node of an object property by name. */
private _getObjectProperty(node: ts.ObjectLiteralExpression, name: string) {
return node.properties.find(property => (property.name as ts.Identifier).getText() === name);
}
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"ng-on-changes-property-access": true,
"rxjs-imports": true,
"require-breaking-change-version": true,
"static-query": true,
"no-host-decorator-in-concrete": [
true,
"HostBinding",
Expand Down