Skip to content

Commit ada59c0

Browse files
committed
feat(rule): rxjs-no-deep-operators.
1 parent 823e447 commit ada59c0

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ The package includes the following rules (none of which are enabled by default):
7474
| `rxjs-finnish` | Enforces the use of [Finnish notation](https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b). | None |
7575
| `rxjs-no-add` | Disallows the importation of patched observables and operators. | See below |
7676
| `rxjs-no-create` | Disallows the calling of `Observable.create`. Useful as a warning. | None |
77+
| `rxjs-no-deep-operators` | Disallows deep importation from 'rxjs/operators'. Deep imports won't be in available in RxJS v6. | None |
7778
| `rxjs-no-do` | I do without `do` operators. [Do you not?](https://youtu.be/spG-Yj0zEyc) Well, `do` isn't always a code smell, but this rule can be useful as a warning. | None |
7879
| `rxjs-no-finnish` | Disallows the use of [Finnish notation](https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b). | None |
7980
| `rxjs-no-ignored-error` | Disallows the calling of `subscribe` without specifying an error handler. | None |
8081
| `rxjs-no-ignored-subscribe` | Disallows the calling of subscribe without specifying arguments. | None |
81-
| `rxjs-no-operator` | Disallows importation from the `operator` directory. Useful if you prefer ['lettable' operators](https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md) - which are located in the `operators` directory. | None |
82+
| `rxjs-no-operator` | Disallows importation from 'rxjs/operator'. Useful if you prefer ['lettable' operators](https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md) - which are located in the `operators` directory. | None |
8283
| `rxjs-no-patched` | Disallows the calling of patched methods. Methods must be imported and called explicitly - not via `Observable` or `Observable.prototype`. | See below |
8384
| `rxjs-no-subject-unsubscribe` | Disallows calling the `unsubscribe` method of a `Subject` instance. For an explanation of why this can be a problem, see [this](https://stackoverflow.com/a/45112125/6680611) Stack Overflow answer. | None |
8485
| `rxjs-no-subject-value` | Disallows accessing the `value` property of a `BehaviorSubject` instance. | None |

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ The package includes the following rules (none of which are enabled by default):
1414
| `rxjs-finnish` | Enforces the use of [Finnish notation](https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b). | None |
1515
| `rxjs-no-add` | Disallows the importation of patched observables and operators. | See below |
1616
| `rxjs-no-create` | Disallows the calling of `Observable.create`. Useful as a warning. | None |
17+
| `rxjs-no-deep-operators` | Disallows deep importation from 'rxjs/operators'. Deep imports won't be in available in RxJS v6. | None |
1718
| `rxjs-no-do` | I do without `do` operators. [Do you not?](https://youtu.be/spG-Yj0zEyc) Well, `do` isn't always a code smell, but this rule can be useful as a warning. | None |
1819
| `rxjs-no-finnish` | Disallows the use of [Finnish notation](https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b). | None |
1920
| `rxjs-no-ignored-error` | Disallows the calling of `subscribe` without specifying an error handler. | None |
2021
| `rxjs-no-ignored-subscribe` | Disallows the calling of subscribe without specifying arguments. | None |
21-
| `rxjs-no-operator` | Disallows importation from the `operator` directory. Useful if you prefer ['lettable' operators](https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md) - which are located in the `operators` directory. | None |
22+
| `rxjs-no-operator` | Disallows importation from 'rxjs/operator'. Useful if you prefer ['lettable' operators](https://github.com/ReactiveX/rxjs/blob/master/doc/lettable-operators.md) - which are located in the `operators` directory. | None |
2223
| `rxjs-no-patched` | Disallows the calling of patched methods. Methods must be imported and called explicitly - not via `Observable` or `Observable.prototype`. | See below |
2324
| `rxjs-no-subject-unsubscribe` | Disallows calling the `unsubscribe` method of a `Subject` instance. For an explanation of why this can be a problem, see [this](https://stackoverflow.com/a/45112125/6680611) Stack Overflow answer. | None |
2425
| `rxjs-no-subject-value` | Disallows accessing the `value` property of a `BehaviorSubject` instance. | None |

fixtures/no-deep-operators/fixture.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Observable } from "rxjs";
2+
import { map } from "rxjs/operators/map";
3+
import { tap } from "rxjs/operators";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"lib": ["es2015"],
5+
"noEmit": true,
6+
"paths": {
7+
"rxjs": ["../node_modules/rxjs"]
8+
},
9+
"skipLibCheck": true,
10+
"target": "es5"
11+
},
12+
"include": ["fixture.ts"]
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"defaultSeverity": "error",
3+
"jsRules": {},
4+
"rules": {
5+
"rxjs-no-deep-operators": { "severity": "error" }
6+
},
7+
"rulesDirectory": "../../build/rules"
8+
}

source/fixtures-spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ describe("fixtures", function (): void {
181181
});
182182
});
183183

184+
describe("no-deep-operators", () => {
185+
186+
it("should effect 'rxjs-no-deep-operators' errors", () => {
187+
188+
const result = lint("no-deep-operators", "tslint.json");
189+
190+
expect(result).to.have.property("errorCount", 1);
191+
result.failures.forEach(failure => expect(failure).to.have.property("ruleName", "rxjs-no-deep-operators"));
192+
});
193+
});
194+
184195
describe("no-do", () => {
185196

186197
it("should effect 'rxjs-no-do' errors", () => {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Use of this source code is governed by an MIT-style license that
3+
* can be found in the LICENSE file at https://github.com/cartant/rxjs-tslint-rules
4+
*/
5+
/*tslint:disable:no-use-before-declare*/
6+
7+
import * as Lint from "tslint";
8+
import * as ts from "typescript";
9+
10+
export class Rule extends Lint.Rules.AbstractRule {
11+
12+
public static metadata: Lint.IRuleMetadata = {
13+
description: "Disallows deep importation from 'rxjs/operators'.",
14+
options: null,
15+
optionsDescription: "Not configurable.",
16+
requiresTypeInfo: false,
17+
ruleName: "rxjs-no-deep-operators",
18+
type: "functionality",
19+
typescriptOnly: false
20+
};
21+
22+
public static FAILURE_STRING = "Deep importation from 'rxjs/operators' is forbidden";
23+
24+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
25+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
26+
}
27+
}
28+
29+
class Walker extends Lint.RuleWalker {
30+
31+
public visitImportDeclaration(node: ts.ImportDeclaration): void {
32+
33+
const moduleSpecifier = node.moduleSpecifier.getText();
34+
35+
if (/^['"]rxjs\/operators\/\w+/.test(moduleSpecifier)) {
36+
this.addFailureAtNode(node, Rule.FAILURE_STRING);
37+
}
38+
39+
super.visitImportDeclaration(node);
40+
}
41+
}

source/rules/rxjsNoOperatorRule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as ts from "typescript";
1010
export class Rule extends Lint.Rules.AbstractRule {
1111

1212
public static metadata: Lint.IRuleMetadata = {
13-
description: "Disallows importation from the 'operator' directory.",
13+
description: "Disallows importation from 'rxjs/operator'.",
1414
options: null,
1515
optionsDescription: "Not configurable.",
1616
requiresTypeInfo: false,
@@ -19,7 +19,7 @@ export class Rule extends Lint.Rules.AbstractRule {
1919
typescriptOnly: false
2020
};
2121

22-
public static FAILURE_STRING = "RxJS importation from the 'operator' directory is forbidden";
22+
public static FAILURE_STRING = "Importation from 'rxjs/operator' is forbidden";
2323

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

0 commit comments

Comments
 (0)