Last active
January 22, 2018 13:17
-
-
Save rokerkony/695c23a68aef19318b36a1e283ec09f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ImportDeclaration, SourceFile} from 'typescript'; | |
import {RuleFailure, Rules, RuleWalker} from 'tslint'; | |
const disallowedImports: string[] = [ | |
// lettable import | |
'\'rxjs/operators\'', | |
'"rxjs/operators"', | |
// not-lettable import | |
'rxjs/add/operator', | |
]; | |
export class Rule extends Rules.AbstractRule { | |
public apply (sourceFile: SourceFile): RuleFailure[] { | |
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | |
} | |
} | |
class Walker extends RuleWalker { | |
public visitImportDeclaration (importDeclaration: ImportDeclaration): void { | |
const importDeclarationText: string = importDeclaration.getText(); | |
if (this.hasDisallowedImport(importDeclarationText)) { | |
this.addFailureAt( | |
importDeclaration.getStart(), | |
importDeclaration.getWidth(), | |
'RxJS operators should be imported from their own scope. ', | |
); | |
} | |
} | |
private hasDisallowedImport (importDeclarationText: string): boolean { | |
return disallowedImports.some((i: string): boolean => importDeclarationText.indexOf(i) !== -1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment