Skip to content

Commit 1406c77

Browse files
feat(no-import-operators): new rule to ban rxjs/operators imports
1 parent 05862a3 commit 1406c77

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ The package includes the following rules.
8888
| [no-ignored-subscription](docs/rules/no-ignored-subscription.md) | Disallow ignoring the subscription returned by `subscribe`. | | | | 💭 | |
8989
| [no-ignored-takewhile-value](docs/rules/no-ignored-takewhile-value.md) | Disallow ignoring the value within `takeWhile`. || | | | |
9090
| [no-implicit-any-catch](docs/rules/no-implicit-any-catch.md) | Disallow implicit `any` error parameters in `catchError` operators. || 🔧 | 💡 | 💭 | |
91+
| [no-import-operators](docs/rules/no-import-operators.md) | Disallow importing operators from `rxjs/operators`. | | | | | |
9192
| [no-index](docs/rules/no-index.md) | Disallow importing index modules. || | | | |
9293
| [no-internal](docs/rules/no-internal.md) | Disallow importing internal modules. || 🔧 | 💡 | | |
9394
| [no-nested-subscribe](docs/rules/no-nested-subscribe.md) | Disallow calling `subscribe` within a `subscribe` callback. || | | 💭 | |

docs/rules/no-import-operators.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Disallow importing operators from `rxjs/operators` (`rxjs-x/no-import-operators`)
2+
3+
<!-- end auto-generated rule header -->
4+
5+
This rule prevents importing from the `rxjs/operators` export site.
6+
Most operators were moved to the `rxjs` export site in RxJS v7.2.0
7+
(excepting a couple of old and deprecated operators).
8+
The `rxjs/operators` export site has since been deprecated and will be removed in a future major version.
9+
10+
## Rule details
11+
12+
Examples of **incorrect** code for this rule:
13+
14+
```ts
15+
import { map } from 'rxjs/operators';
16+
```
17+
18+
Examples of **correct** code for this rule:
19+
20+
```ts
21+
import { map } from 'rxjs';
22+
```
23+
24+
## Further reading
25+
26+
- [Importing instructions](https://rxjs.dev/guide/importing)

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { noIgnoredSubscribeRule } from './rules/no-ignored-subscribe';
2424
import { noIgnoredSubscriptionRule } from './rules/no-ignored-subscription';
2525
import { noIgnoredTakewhileValueRule } from './rules/no-ignored-takewhile-value';
2626
import { noImplicitAnyCatchRule } from './rules/no-implicit-any-catch';
27+
import { noImportOperatorsRule } from './rules/no-import-operators';
2728
import { noIndexRule } from './rules/no-index';
2829
import { noInternalRule } from './rules/no-internal';
2930
import { noNestedSubscribeRule } from './rules/no-nested-subscribe';
@@ -70,6 +71,7 @@ const plugin = {
7071
'no-ignored-subscription': noIgnoredSubscriptionRule,
7172
'no-ignored-takewhile-value': noIgnoredTakewhileValueRule,
7273
'no-implicit-any-catch': noImplicitAnyCatchRule,
74+
'no-import-operators': noImportOperatorsRule,
7375
'no-index': noIndexRule,
7476
'no-internal': noInternalRule,
7577
'no-nested-subscribe': noNestedSubscribeRule,

src/rules/no-import-operators.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { TSESTree as es } from '@typescript-eslint/utils';
2+
import { ruleCreator } from '../utils';
3+
4+
export const noImportOperatorsRule = ruleCreator({
5+
defaultOptions: [],
6+
meta: {
7+
docs: {
8+
description: 'Disallow importing operators from `rxjs/operators`.',
9+
},
10+
messages: {
11+
forbidden: 'RxJS imports from `rxjs/operators` are forbidden.',
12+
},
13+
schema: [],
14+
type: 'problem',
15+
},
16+
name: 'no-import-operators',
17+
create: (context) => {
18+
return {
19+
'ImportDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
20+
context.report({
21+
messageId: 'forbidden',
22+
node,
23+
});
24+
},
25+
'ImportExpression Literal[value="rxjs/operators"]': (node: es.Literal) => {
26+
context.report({
27+
messageId: 'forbidden',
28+
node,
29+
});
30+
},
31+
'ExportNamedDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
32+
context.report({
33+
messageId: 'forbidden',
34+
node,
35+
});
36+
},
37+
'ExportAllDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
38+
context.report({
39+
messageId: 'forbidden',
40+
node,
41+
});
42+
},
43+
};
44+
},
45+
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { stripIndent } from 'common-tags';
2+
import { noImportOperatorsRule } from '../../src/rules/no-import-operators';
3+
import { fromFixture } from '../etc';
4+
import { ruleTester } from '../rule-tester';
5+
6+
ruleTester({ types: false }).run('no-import-operators', noImportOperatorsRule, {
7+
valid: [
8+
stripIndent`
9+
// import declaration named
10+
import { concat } from "rxjs";
11+
import { concat } from 'rxjs';
12+
`,
13+
stripIndent`
14+
// import declaration namespace
15+
import * as Rx from "rxjs";
16+
`,
17+
stripIndent`
18+
// import expression
19+
const { concat } = await import("rxjs");
20+
`,
21+
stripIndent`
22+
// import expression with identifier is not supported
23+
const path = "rxjs/operators";
24+
const { concat } = await import(path);
25+
`,
26+
stripIndent`
27+
// export named
28+
export { concat } from "rxjs";
29+
`,
30+
stripIndent`
31+
// export all
32+
export * from "rxjs";
33+
`,
34+
stripIndent`
35+
// unrelated import
36+
import { ajax } from "rxjs/ajax";
37+
import { fromFetch } from "rxjs/fetch";
38+
import { TestScheduler } from "rxjs/testing";
39+
import { webSocket } from "rxjs/webSocket";
40+
import * as prefixedPackage from "rxjs-prefixed-package";
41+
`,
42+
],
43+
invalid: [
44+
fromFixture(
45+
stripIndent`
46+
// import declaration named
47+
import { concat } from "rxjs/operators";
48+
~~~~~~~~~~~~~~~~ [forbidden]
49+
import { concat } from 'rxjs/operators';
50+
~~~~~~~~~~~~~~~~ [forbidden]
51+
`,
52+
),
53+
fromFixture(
54+
stripIndent`
55+
// import declaration namespace
56+
import * as RxOperators from "rxjs/operators";
57+
~~~~~~~~~~~~~~~~ [forbidden]
58+
`,
59+
),
60+
fromFixture(
61+
stripIndent`
62+
// import expression
63+
const { concat } = await import("rxjs/operators");
64+
~~~~~~~~~~~~~~~~ [forbidden]
65+
`,
66+
),
67+
fromFixture(
68+
stripIndent`
69+
// export named
70+
export { concat } from "rxjs/operators";
71+
~~~~~~~~~~~~~~~~ [forbidden]
72+
`,
73+
),
74+
fromFixture(
75+
stripIndent`
76+
// export all
77+
export * from "rxjs/operators";
78+
~~~~~~~~~~~~~~~~ [forbidden]
79+
`,
80+
),
81+
],
82+
});

0 commit comments

Comments
 (0)