1 | import * as Lint from "tslint";
|
2 | import * as ts from "typescript";
|
3 |
|
4 | export class Rule extends Lint.Rules.AbstractRule {
|
5 |
|
6 | static FAILURE_STRING: string = "do not import from invalid folder like 'DISALLOWED_TEXT' (tsf-folders-import-from-disallowed-folders)";
|
7 |
|
8 | apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
9 | const walker = new ImportsWalker(sourceFile, this.getOptions());
|
10 | this.applyWithWalker(walker);
|
11 |
|
12 | return walker.getFailures();
|
13 | }
|
14 | }
|
15 |
|
16 | class ImportsWalker extends Lint.RuleWalker {
|
17 | visitImportDeclaration(node: ts.ImportDeclaration) {
|
18 | this.validate(node, node.moduleSpecifier.getText());
|
19 | }
|
20 |
|
21 | visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {
|
22 | this.validate(node, node.moduleReference.getText());
|
23 |
|
24 | super.visitImportEqualsDeclaration(node);
|
25 | }
|
26 |
|
27 | private validate(node: ts.Node, text: string) {
|
28 | const options = this.getOptions();
|
29 | if (options.length !== 1) {
|
30 | throw new Error("tslint rule is misconfigured (tsf-folders-import-from-disallowed-folders)");
|
31 | }
|
32 |
|
33 | const disallowedTexts: string[] = this.getOptions()[0].disallowed;
|
34 |
|
35 | for (const disallowed of disallowedTexts) {
|
36 | if (text.indexOf(disallowed) >= 0) {
|
37 | this.addFailureAtNode(node, Rule.FAILURE_STRING.replace("DISALLOWED_TEXT", disallowed));
|
38 |
|
39 |
|
40 | return;
|
41 | }
|
42 | }
|
43 | }
|
44 | }
|