Skip to content

Commit

Permalink
feat: Add rxjs-no-index rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Sep 7, 2019
1 parent f3e8222 commit 9db9385
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"test": "yarn run lint && yarn run test:build && yarn run test:mocha && yarn run test:tslint-v5 && yarn run test:tslint-v6 && yarn run test:tslint-v6-compat",
"test:build": "yarn run test:clean && tsc -p tsconfig.json",
"test:clean": "rimraf build",
"test:debug": "tslint --test \"./test/v6/fixtures/no-internal/**/tslint.json\"",
"test:debug": "tslint --test \"./test/v6/fixtures/no-index/**/tslint.json\"",
"test:issues": "yarn run test:clean && tsc -p tsconfig.json && tslint --test \"./test/v6/fixtures/issues/**/tslint.json\"",
"test:mocha": "mocha \"./build/**/*-spec.js\"",
"test:tslint-v5": "yarn --cwd ./test/v5 install && yarn --cwd ./test/v5 upgrade && tslint --test \"./test/v5/fixtures/**/tslint.json\"",
Expand Down
42 changes: 42 additions & 0 deletions source/rules/rxjsNoIndexRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-tslint-rules
*/
/*tslint:disable:no-use-before-declare*/

import * as Lint from "tslint";
import * as ts from "typescript";

export class Rule extends Lint.Rules.AbstractRule {
public static metadata: Lint.IRuleMetadata = {
deprecationMessage: undefined,
description: "Disallows the importation from index modules.",
options: null,
optionsDescription: "Not configurable.",
requiresTypeInfo: false,
ruleName: "rxjs-no-index",
type: "functionality",
typescriptOnly: false
};

public static FAILURE_STRING =
"RxJS imports from index modules are forbidden";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
}
}

class Walker extends Lint.RuleWalker {
public visitImportDeclaration(node: ts.ImportDeclaration): void {
const moduleSpecifier = node.moduleSpecifier.getText();
const match = moduleSpecifier.match(/^['"]rxjs(\/\w+)?\/index/);
if (match) {
this.addFailureAtNode(
node.moduleSpecifier,
"RxJS imports from index are forbidden - see https://github.com/ReactiveX/rxjs/issues/4230"
);
}
super.visitImportDeclaration(node);
}
}
10 changes: 10 additions & 0 deletions test/v6/fixtures/no-index/default/fixture.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Observable } from "rxjs/index";
~~~~~~~~~~~~ [no-index]
import { map } from "rxjs/operators/index";
~~~~~~~~~~~~~~~~~~~~~~ [no-index]
import { TestScheduler } from "rxjs/testing/index";
~~~~~~~~~~~~~~~~~~~~ [no-index]
import { WebSocketSubject } from "rxjs/webSocket/index";
~~~~~~~~~~~~~~~~~~~~~~ [no-index]

[no-index]: RxJS imports from index are forbidden - see https://github.com/ReactiveX/rxjs/issues/4230
13 changes: 13 additions & 0 deletions test/v6/fixtures/no-index/default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"baseUrl": ".",
"lib": ["es2015"],
"noEmit": true,
"paths": {
"rxjs": ["../../node_modules/rxjs"]
},
"skipLibCheck": true,
"target": "es5"
},
"include": ["fixture.ts"]
}
8 changes: 8 additions & 0 deletions test/v6/fixtures/no-index/default/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaultSeverity": "error",
"jsRules": {},
"rules": {
"rxjs-no-index": { "severity": "error" }
},
"rulesDirectory": "../../../../../build/rules"
}

0 comments on commit 9db9385

Please sign in to comment.