From f89261eea6f4e302ee6e97117766ab0a0d71f34b Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sat, 24 Jun 2017 14:45:44 +0200 Subject: [PATCH] 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. --- tools/tslint-rules/noRxjsPatchImportsRule.js | 35 ++++++++++++++++++++ tslint.json | 5 +++ 2 files changed, 40 insertions(+) create mode 100644 tools/tslint-rules/noRxjsPatchImportsRule.js diff --git a/tools/tslint-rules/noRxjsPatchImportsRule.js b/tools/tslint-rules/noRxjsPatchImportsRule.js new file mode 100644 index 00000000000..8d7e33c74e1 --- /dev/null +++ b/tools/tslint-rules/noRxjsPatchImportsRule.js @@ -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; diff --git a/tslint.json b/tslint.json index ec9c7ecc83d..16948fdd442 100644 --- a/tslint.json +++ b/tslint.json @@ -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",