build: add tslint rule to enforce consistent rxjs imports (#11629)

Adds a small tslint rule that will help us catch cases like [this one](https://github.com/angular/material2/blob/master/src/demo-app/virtual-scroll/virtual-scroll-demo.ts#L10), which can come up when the IDE auto-imports something from rjxs.
This commit is contained in:
Kristiyan Kostadinov
2018-06-07 07:55:16 +02:00
committed by Andrew Seguin
parent 4e548f68fa
commit 46e6116d80
3 changed files with 25 additions and 1 deletions
@@ -7,7 +7,7 @@
*/
import {Component, ViewEncapsulation} from '@angular/core';
import {BehaviorSubject} from 'rxjs/index';
import {BehaviorSubject} from 'rxjs';
type State = {
+23
View File
@@ -0,0 +1,23 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
/**
* Rule that ensures that all rxjs imports come only from `rxjs` and `rxjs/operators`.
*/
export class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile: ts.SourceFile) {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}
class Walker extends Lint.RuleWalker {
visitImportDeclaration(node: ts.ImportDeclaration) {
const specifier = node.moduleSpecifier.getText().slice(1, -1);
if (specifier.startsWith('rxjs') && specifier !== 'rxjs' && specifier !== 'rxjs/operators') {
this.addFailureAtNode(node, 'RxJS imports are only allowed from `rxjs` or `rxjs/operators`.');
}
super.visitImportDeclaration(node);
}
}
+1
View File
@@ -98,6 +98,7 @@
"ts-loader": true,
"no-exposed-todo": true,
"setters-after-getters": true,
"rxjs-imports": true,
"no-host-decorator-in-concrete": [
true,
"HostBinding",