Skip to content

Commit

Permalink
Update noImportOtherModulesRule.ts
Browse files Browse the repository at this point in the history
Indent source as 2
  • Loading branch information
nadongguri committed Feb 14, 2018
1 parent b2e279b commit 94904f6
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lint_rules/noImportOtherModulesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import * as ts from "typescript";
import * as Lint from "tslint";

export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "should not import sub modules in other folder";
public static FAILURE_STRING = "should not import sub modules in other folder";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoImportOtherModulesWalker(sourceFile, this.getOptions()));
}
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoImportOtherModulesWalker(sourceFile, this.getOptions()));
}
}

// The walker takes care of all the work.
class NoImportOtherModulesWalker extends Lint.RuleWalker {
public visitImportDeclaration(node: ts.ImportDeclaration) {
// create a failure at the current position
const PROJECT_STRING = "bacardi";
const sourcePath = (node.parent as ts.SourceFile).fileName;
const importPath = (node.moduleSpecifier as any).text as string;
if (importPath.includes('/')) {
let importModuleName = importPath.split('/')[0];
let relativeSourcePath = sourcePath.split(PROJECT_STRING)[1];
let sourceModuleName = relativeSourcePath.split('/')[1];
if (importModuleName != sourceModuleName) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}
public visitImportDeclaration(node: ts.ImportDeclaration) {
// create a failure at the current position
const PROJECT_STRING = "bacardi";
const sourcePath = (node.parent as ts.SourceFile).fileName;
const importPath = (node.moduleSpecifier as any).text as string;
if (importPath.includes('/')) {
let importModuleName = importPath.split('/')[0];
let relativeSourcePath = sourcePath.split(PROJECT_STRING)[1];
let sourceModuleName = relativeSourcePath.split('/')[1];
if (importModuleName != sourceModuleName) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}

// call the base version of this visitor to actually parse this node
super.visitImportDeclaration(node);
}
// call the base version of this visitor to actually parse this node
super.visitImportDeclaration(node);
}
}

0 comments on commit 94904f6

Please sign in to comment.