mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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.
This commit is contained in:
committed by
Jeremy Elbourn
parent
6e0b848486
commit
f89261eea6
@@ -0,0 +1,35 @@
|
||||
const Lint = require('tslint');
|
||||
const ERROR_MESSAGE = 'Uses of RxJS patch imports are forbidden.';
|
||||
|
||||
/**
|
||||
* Rule that prevents uses of RxJS patch imports (e.g. `import 'rxjs/add/operator/map').
|
||||
* Supports whitelisting via `"no-patch-imports": [true, "\.spec\.ts$"]`.
|
||||
*/
|
||||
class Rule extends Lint.Rules.AbstractRule {
|
||||
apply(file) {
|
||||
return this.applyWithWalker(new Walker(file, this.getOptions()));
|
||||
}
|
||||
}
|
||||
|
||||
class Walker extends Lint.RuleWalker {
|
||||
constructor(file, options) {
|
||||
super(...arguments);
|
||||
|
||||
// Whitelist with regular expressions to use when determining which files to lint.
|
||||
const whitelist = options.ruleArguments;
|
||||
|
||||
// Whether the file should be checked at all.
|
||||
this._enabled = !whitelist.length || whitelist.some(p => new RegExp(p).test(file.fileName));
|
||||
}
|
||||
|
||||
visitImportDeclaration(node) {
|
||||
// Walk through the imports and check if they start with `rxjs/add`.
|
||||
if (this._enabled && node.moduleSpecifier.getText().startsWith('rxjs/add', 1)) {
|
||||
this.addFailureAtNode(node, ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
super.visitImportDeclaration(node);
|
||||
}
|
||||
}
|
||||
|
||||
exports.Rule = Rule;
|
||||
@@ -29,6 +29,11 @@
|
||||
"no-var-keyword": true,
|
||||
"no-exposed-todo": true,
|
||||
"no-debugger": true,
|
||||
"no-rxjs-patch-imports": [
|
||||
true,
|
||||
"src/lib",
|
||||
"src/cdk"
|
||||
],
|
||||
"one-line": [
|
||||
true,
|
||||
"check-catch",
|
||||
|
||||
Reference in New Issue
Block a user