Skip to content

Commit 46e6116

Browse files
crisbetoandrewseguin
authored andcommitted
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.
1 parent 4e548f6 commit 46e6116

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/demo-app/virtual-scroll/virtual-scroll-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Component, ViewEncapsulation} from '@angular/core';
10-
import {BehaviorSubject} from 'rxjs/index';
10+
import {BehaviorSubject} from 'rxjs';
1111

1212

1313
type State = {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as ts from 'typescript';
2+
import * as Lint from 'tslint';
3+
4+
/**
5+
* Rule that ensures that all rxjs imports come only from `rxjs` and `rxjs/operators`.
6+
*/
7+
export class Rule extends Lint.Rules.AbstractRule {
8+
apply(sourceFile: ts.SourceFile) {
9+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
10+
}
11+
}
12+
13+
class Walker extends Lint.RuleWalker {
14+
visitImportDeclaration(node: ts.ImportDeclaration) {
15+
const specifier = node.moduleSpecifier.getText().slice(1, -1);
16+
17+
if (specifier.startsWith('rxjs') && specifier !== 'rxjs' && specifier !== 'rxjs/operators') {
18+
this.addFailureAtNode(node, 'RxJS imports are only allowed from `rxjs` or `rxjs/operators`.');
19+
}
20+
21+
super.visitImportDeclaration(node);
22+
}
23+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"ts-loader": true,
9999
"no-exposed-todo": true,
100100
"setters-after-getters": true,
101+
"rxjs-imports": true,
101102
"no-host-decorator-in-concrete": [
102103
true,
103104
"HostBinding",

0 commit comments

Comments
 (0)