Skip to content

Instantly share code, notes, and snippets.

@rokerkony
Last active January 22, 2018 13:17
Show Gist options
  • Save rokerkony/695c23a68aef19318b36a1e283ec09f7 to your computer and use it in GitHub Desktop.
Save rokerkony/695c23a68aef19318b36a1e283ec09f7 to your computer and use it in GitHub Desktop.
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