Skip to content

Commit ee0e5dd

Browse files
fix(no-topromise): don't try to fix zero import files
Likely someone is spoofing Observable or maybe RxJS has been embedded in the file? Weird edge case that we don't need to handle.
1 parent 6b4e306 commit ee0e5dd

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
lines changed

src/rules/no-topromise.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ export const noTopromiseRule = ruleCreator({
3535
conversion: 'lastValueFrom' | 'firstValueFrom',
3636
callExpression: es.CallExpression,
3737
observableNode: es.Node,
38+
importDeclarations: es.ImportDeclaration[],
3839
) {
3940
return function* fix(fixer: TSESLint.RuleFixer) {
4041
let namespace = '';
4142
let functionName: string = conversion;
4243

43-
const { body } = context.sourceCode.ast;
44-
const importDeclarations = body.filter(isImportDeclaration);
45-
4644
const rxjsImportDeclaration = importDeclarations.find(node => node.source.value === 'rxjs');
4745

4846
if (rxjsImportDeclaration?.specifiers?.every(isImportNamespaceSpecifier)) {
@@ -69,11 +67,8 @@ export const noTopromiseRule = ruleCreator({
6967
`\nimport { ${functionName} } from ${quote}rxjs${quote};`,
7068
);
7169
} else {
72-
// No imports. Add to top of file.
73-
yield fixer.insertTextBefore(
74-
body[0],
75-
`import { ${functionName} } from "rxjs";\n`,
76-
);
70+
console.warn('No import declarations found. Unable to suggest a fix.');
71+
return;
7772
}
7873

7974
yield fixer.replaceText(
@@ -91,17 +86,25 @@ export const noTopromiseRule = ruleCreator({
9186
if (!couldBeObservable(memberExpression.object)) {
9287
return;
9388
}
89+
90+
const { body } = context.sourceCode.ast;
91+
const importDeclarations = body.filter(isImportDeclaration);
92+
if (!importDeclarations.length) {
93+
// couldBeObservable yet no imports? Skip.
94+
return;
95+
}
96+
9497
context.report({
9598
messageId: 'forbidden',
9699
node: memberExpression.property,
97100
suggest: [
98101
{
99102
messageId: 'suggestLastValueFrom',
100-
fix: createFix('lastValueFrom', node, memberExpression.object),
103+
fix: createFix('lastValueFrom', node, memberExpression.object, importDeclarations),
101104
},
102105
{
103106
messageId: 'suggestFirstValueFrom',
104-
fix: createFix('firstValueFrom', node, memberExpression.object),
107+
fix: createFix('firstValueFrom', node, memberExpression.object, importDeclarations),
105108
},
106109
],
107110
});

tests/rules/no-topromise.test.ts

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ ruleTester({ types: true }).run('no-topromise', noTopromiseRule, {
2020
};
2121
a.toPromise().then(value => console.log(value));
2222
`,
23+
stripIndent`
24+
// no imports
25+
class Observable {
26+
toPromise() {
27+
return Promise.resolve("a");
28+
}
29+
}
30+
const a = new Observable();
31+
a.toPromise().then(value => console.log(value));
32+
`,
2333
],
2434
invalid: [
2535
fromFixture(
@@ -220,50 +230,5 @@ ruleTester({ types: true }).run('no-topromise', noTopromiseRule, {
220230
],
221231
},
222232
),
223-
fromFixture(
224-
stripIndent`
225-
// no imports
226-
class Observable {
227-
toPromise() {
228-
return Promise.resolve("a");
229-
}
230-
}
231-
const a = new Observable();
232-
a.toPromise().then(value => console.log(value));
233-
~~~~~~~~~ [forbidden suggest 0 1]
234-
`,
235-
{
236-
suggestions: [
237-
{
238-
messageId: 'suggestLastValueFrom',
239-
output: stripIndent`
240-
// no imports
241-
import { lastValueFrom } from "rxjs";
242-
class Observable {
243-
toPromise() {
244-
return Promise.resolve("a");
245-
}
246-
}
247-
const a = new Observable();
248-
lastValueFrom(a).then(value => console.log(value));
249-
`,
250-
},
251-
{
252-
messageId: 'suggestFirstValueFrom',
253-
output: stripIndent`
254-
// no imports
255-
import { firstValueFrom } from "rxjs";
256-
class Observable {
257-
toPromise() {
258-
return Promise.resolve("a");
259-
}
260-
}
261-
const a = new Observable();
262-
firstValueFrom(a).then(value => console.log(value));
263-
`,
264-
},
265-
],
266-
},
267-
),
268233
],
269234
});

0 commit comments

Comments
 (0)