forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoForwardRefRule.ts
More file actions
54 lines (47 loc) · 1.84 KB
/
noForwardRefRule.ts
File metadata and controls
54 lines (47 loc) · 1.84 KB
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
52
53
54
import * as Lint from 'tslint';
import * as ts from 'typescript';
import { sprintf } from 'sprintf-js';
import SyntaxKind = require('./util/syntaxKind');
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
ruleName: 'no-forward-ref',
type: 'maintainability',
description: `Disallows usage of forward references for DI.`,
rationale: `The flow of DI is disrupted by using \`forwardRef\` and might make code more difficult to understand.`,
options: null,
optionsDescription: `Not configurable.`,
typescriptOnly: true,
};
static FAILURE_IN_CLASS: string = 'Avoid using forwardRef in class "%s"';
static FAILURE_IN_VARIABLE: string = 'Avoid using forwardRef in variable "%s"';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new ExpressionCallMetadataWalker(sourceFile,
this.getOptions()));
}
}
export class ExpressionCallMetadataWalker extends Lint.RuleWalker {
visitCallExpression(node: ts.CallExpression) {
this.validateCallExpression(node);
super.visitCallExpression(node);
}
private validateCallExpression(callExpression) {
if (callExpression.expression.text === 'forwardRef') {
let currentNode: any = callExpression;
while (currentNode.parent.parent) {
currentNode = currentNode.parent;
}
let failureConfig: string[] = [];
if (currentNode.kind === SyntaxKind.current().VariableStatement) {
failureConfig = [Rule.FAILURE_IN_VARIABLE, currentNode.declarationList.declarations[0].name.text];
} else {
failureConfig = [Rule.FAILURE_IN_CLASS, currentNode.name.text];
}
this.addFailure(
this.createFailure(
callExpression.getStart(),
callExpression.getWidth(),
sprintf.apply(this, failureConfig)));
}
}
}