Skip to content

Commit 7360e60

Browse files
feat(no-import-operators): add suggestion
We're only adding a suggestion right now because an auto-fixer might break if the operator is old and not exported from `rxjs` too.
1 parent 51ed5f6 commit 7360e60

File tree

4 files changed

+103
-19
lines changed

4 files changed

+103
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +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`. | | | | | |
91+
| [no-import-operators](docs/rules/no-import-operators.md) | Disallow importing operators from `rxjs/operators`. | | | 💡 | | |
9292
| [no-index](docs/rules/no-index.md) | Disallow importing index modules. || | | | |
9393
| [no-internal](docs/rules/no-internal.md) | Disallow importing internal modules. || 🔧 | 💡 | | |
9494
| [no-nested-subscribe](docs/rules/no-nested-subscribe.md) | Disallow calling `subscribe` within a `subscribe` callback. || | | 💭 | |

docs/rules/no-import-operators.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow importing operators from `rxjs/operators` (`rxjs-x/no-import-operators`)
22

3+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
4+
35
<!-- end auto-generated rule header -->
46

57
This rule prevents importing from the `rxjs/operators` export site.

src/rules/no-import-operators.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,56 @@ export const noImportOperatorsRule = ruleCreator({
77
docs: {
88
description: 'Disallow importing operators from `rxjs/operators`.',
99
},
10+
hasSuggestions: true,
1011
messages: {
1112
forbidden: 'RxJS imports from `rxjs/operators` are forbidden.',
13+
suggest: 'Import from `rxjs` instead.',
1214
},
1315
schema: [],
1416
type: 'problem',
1517
},
1618
name: 'no-import-operators',
1719
create: (context) => {
18-
return {
19-
'ImportDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
20+
function getReplacement(rawLocation: string) {
21+
const match = /^\s*('|")/.exec(rawLocation);
22+
if (!match) {
23+
return undefined;
24+
}
25+
const [, quote] = match;
26+
if (/^['"]rxjs\/operators/.test(rawLocation)) {
27+
return `${quote}rxjs${quote}`;
28+
}
29+
return undefined;
30+
}
31+
32+
function reportNode(node: es.Literal) {
33+
const replacement = getReplacement(node.raw);
34+
if (replacement) {
2035
context.report({
2136
messageId: 'forbidden',
2237
node,
38+
suggest: [{ messageId: 'suggest', fix: (fixer) => fixer.replaceText(node, replacement) }],
2339
});
24-
},
25-
'ImportExpression Literal[value="rxjs/operators"]': (node: es.Literal) => {
40+
} else {
2641
context.report({
2742
messageId: 'forbidden',
2843
node,
2944
});
45+
}
46+
}
47+
48+
return {
49+
'ImportDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
50+
reportNode(node);
51+
},
52+
'ImportExpression Literal[value="rxjs/operators"]': (node: es.Literal) => {
53+
reportNode(node);
3054
},
3155
'ExportNamedDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
32-
context.report({
33-
messageId: 'forbidden',
34-
node,
35-
});
56+
reportNode(node);
3657
},
3758
'ExportAllDeclaration Literal[value="rxjs/operators"]': (node: es.Literal) => {
38-
context.report({
39-
messageId: 'forbidden',
40-
node,
41-
});
59+
reportNode(node);
4260
},
4361
};
4462
},

tests/rules/no-import-operators.test.ts

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,102 @@ ruleTester({ types: false }).run('no-import-operators', noImportOperatorsRule, {
4545
stripIndent`
4646
// import declaration named
4747
import { concat } from "rxjs/operators";
48-
~~~~~~~~~~~~~~~~ [forbidden]
48+
~~~~~~~~~~~~~~~~ [forbidden suggest 0]
4949
import { concat } from 'rxjs/operators';
50-
~~~~~~~~~~~~~~~~ [forbidden]
50+
~~~~~~~~~~~~~~~~ [forbidden suggest 1]
5151
`,
52+
{
53+
suggestions: [
54+
{
55+
messageId: 'suggest',
56+
output: stripIndent`
57+
// import declaration named
58+
import { concat } from "rxjs";
59+
import { concat } from 'rxjs/operators';
60+
`,
61+
},
62+
{
63+
messageId: 'suggest',
64+
output: stripIndent`
65+
// import declaration named
66+
import { concat } from "rxjs/operators";
67+
import { concat } from 'rxjs';
68+
`,
69+
},
70+
],
71+
},
5272
),
5373
fromFixture(
5474
stripIndent`
5575
// import declaration namespace
5676
import * as RxOperators from "rxjs/operators";
57-
~~~~~~~~~~~~~~~~ [forbidden]
77+
~~~~~~~~~~~~~~~~ [forbidden suggest]
5878
`,
79+
{
80+
suggestions: [
81+
{
82+
messageId: 'suggest',
83+
output: stripIndent`
84+
// import declaration namespace
85+
import * as RxOperators from "rxjs";
86+
`,
87+
},
88+
],
89+
},
5990
),
6091
fromFixture(
6192
stripIndent`
6293
// import expression
6394
const { concat } = await import("rxjs/operators");
64-
~~~~~~~~~~~~~~~~ [forbidden]
95+
~~~~~~~~~~~~~~~~ [forbidden suggest]
6596
`,
97+
{
98+
suggestions: [
99+
{
100+
messageId: 'suggest',
101+
output: stripIndent`
102+
// import expression
103+
const { concat } = await import("rxjs");
104+
`,
105+
},
106+
],
107+
},
66108
),
67109
fromFixture(
68110
stripIndent`
69111
// export named
70112
export { concat } from "rxjs/operators";
71-
~~~~~~~~~~~~~~~~ [forbidden]
113+
~~~~~~~~~~~~~~~~ [forbidden suggest]
72114
`,
115+
{
116+
suggestions: [
117+
{
118+
messageId: 'suggest',
119+
output: stripIndent`
120+
// export named
121+
export { concat } from "rxjs";
122+
`,
123+
},
124+
],
125+
},
73126
),
74127
fromFixture(
75128
stripIndent`
76129
// export all
77130
export * from "rxjs/operators";
78-
~~~~~~~~~~~~~~~~ [forbidden]
131+
~~~~~~~~~~~~~~~~ [forbidden suggest]
79132
`,
133+
{
134+
suggestions: [
135+
{
136+
messageId: 'suggest',
137+
output: stripIndent`
138+
// export all
139+
export * from "rxjs";
140+
`,
141+
},
142+
],
143+
},
80144
),
81145
],
82146
});

0 commit comments

Comments
 (0)