/
noTemplateCallExpressionRule.ts
51 lines (41 loc) · 1.88 KB
/
noTemplateCallExpressionRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as e from '@angular/compiler/src/expression_parser/ast';
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { NgWalker, NgWalkerConfig } from './angular/ngWalker';
import { BasicTemplateAstVisitor } from './angular/templates/basicTemplateAstVisitor';
import { RecursiveAngularExpressionVisitor } from './angular/templates/recursiveAngularExpressionVisitor';
export class Rule extends Lint.Rules.AbstractRule {
static readonly metadata: Lint.IRuleMetadata = {
description: 'Call expressions are not allowed in templates except in output handlers.',
options: null,
optionsDescription: 'Not configurable.',
rationale: 'The change detector will call functions used in templates very often. Use an observable instead.',
ruleName: 'no-template-call-expression',
type: 'maintainability',
typescriptOnly: true
};
static readonly FAILURE_STRING = 'Call expressions are not allowed in templates except in output handlers';
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const walkerConfig: NgWalkerConfig = {
expressionVisitorCtrl: ExpressionVisitor,
templateVisitorCtrl: TemplateVisitor
};
return this.applyWithWalker(new NgWalker(sourceFile, this.getOptions(), walkerConfig));
}
}
class TemplateVisitor extends BasicTemplateAstVisitor {
visitEvent() {}
}
class ExpressionVisitor extends RecursiveAngularExpressionVisitor {
private allowedMethodNames = ['$any'];
visitFunctionCall(node: e.FunctionCall, context: any) {
this.addFailureFromStartToEnd(node.span.start, node.span.end, Rule.FAILURE_STRING);
super.visitFunctionCall(node, context);
}
visitMethodCall(node: e.MethodCall, context: any) {
if (this.allowedMethodNames.indexOf(node.name) === -1) {
this.addFailureFromStartToEnd(node.span.start, node.span.end, Rule.FAILURE_STRING);
}
super.visitMethodCall(node, context);
}
}