Skip to content

Commit f89261e

Browse files
crisbetojelbourn
authored andcommitted
build: add lint rule to prevent rxjs patch imports (#5344)
Adds a rule that will ensure that we don't use patch imports in the library and cdk files.
1 parent 6e0b848 commit f89261e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Lint = require('tslint');
2+
const ERROR_MESSAGE = 'Uses of RxJS patch imports are forbidden.';
3+
4+
/**
5+
* Rule that prevents uses of RxJS patch imports (e.g. `import 'rxjs/add/operator/map').
6+
* Supports whitelisting via `"no-patch-imports": [true, "\.spec\.ts$"]`.
7+
*/
8+
class Rule extends Lint.Rules.AbstractRule {
9+
apply(file) {
10+
return this.applyWithWalker(new Walker(file, this.getOptions()));
11+
}
12+
}
13+
14+
class Walker extends Lint.RuleWalker {
15+
constructor(file, options) {
16+
super(...arguments);
17+
18+
// Whitelist with regular expressions to use when determining which files to lint.
19+
const whitelist = options.ruleArguments;
20+
21+
// Whether the file should be checked at all.
22+
this._enabled = !whitelist.length || whitelist.some(p => new RegExp(p).test(file.fileName));
23+
}
24+
25+
visitImportDeclaration(node) {
26+
// Walk through the imports and check if they start with `rxjs/add`.
27+
if (this._enabled && node.moduleSpecifier.getText().startsWith('rxjs/add', 1)) {
28+
this.addFailureAtNode(node, ERROR_MESSAGE);
29+
}
30+
31+
super.visitImportDeclaration(node);
32+
}
33+
}
34+
35+
exports.Rule = Rule;

tslint.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"no-var-keyword": true,
3030
"no-exposed-todo": true,
3131
"no-debugger": true,
32+
"no-rxjs-patch-imports": [
33+
true,
34+
"src/lib",
35+
"src/cdk"
36+
],
3237
"one-line": [
3338
true,
3439
"check-catch",

0 commit comments

Comments
 (0)