-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathnoImportsFromDistRule.ts
More file actions
30 lines (25 loc) · 1 KB
/
noImportsFromDistRule.ts
File metadata and controls
30 lines (25 loc) · 1 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
import * as Lint from 'tslint';
import * as ts from 'typescript';
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING =
'importing from dist/ is prohibited. Please use public API';
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(
new NoImportsFromDistWalker(sourceFile, this.getOptions()));
}
}
class NoImportsFromDistWalker extends Lint.RuleWalker {
public visitImportDeclaration(node: ts.ImportDeclaration) {
const importFrom = node.moduleSpecifier.getText();
const reg = /@tensorflow\/tfjs[-a-z]*\/dist/;
if (importFrom.match(reg)) {
const fix = new Lint.Replacement(
node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(),
importFrom.replace(/\/dist[\/]*/, ''));
this.addFailure(this.createFailure(
node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(),
Rule.FAILURE_STRING, fix));
}
super.visitImportDeclaration(node);
}
}