-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(eslint-plugin): [no-floating-promises] check top-level type assertions (and more) #9043
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,9 +140,8 @@ async function test() { | |
} | ||
`, | ||
` | ||
declare const promiseValue: Promise<number>; | ||
async function test() { | ||
declare const promiseValue: Promise<number>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are just because |
||
|
||
await promiseValue; | ||
promiseValue.then( | ||
() => {}, | ||
|
@@ -158,9 +157,8 @@ async function test() { | |
} | ||
`, | ||
` | ||
declare const promiseUnion: Promise<number> | number; | ||
async function test() { | ||
declare const promiseUnion: Promise<number> | number; | ||
|
||
await promiseUnion; | ||
promiseUnion.then( | ||
() => {}, | ||
|
@@ -177,9 +175,8 @@ async function test() { | |
} | ||
`, | ||
` | ||
declare const promiseIntersection: Promise<number> & number; | ||
async function test() { | ||
declare const promiseIntersection: Promise<number> & number; | ||
|
||
await promiseIntersection; | ||
promiseIntersection.then( | ||
() => {}, | ||
|
@@ -210,12 +207,12 @@ async function test() { | |
} | ||
`, | ||
` | ||
declare const intersectionPromise: Promise<number> & number; | ||
async function test() { | ||
await (Math.random() > 0.5 ? numberPromise : 0); | ||
await (Math.random() > 0.5 ? foo : 0); | ||
await (Math.random() > 0.5 ? bar : 0); | ||
|
||
declare const intersectionPromise: Promise<number> & number; | ||
await intersectionPromise; | ||
} | ||
`, | ||
|
@@ -308,8 +305,8 @@ async function test() { | |
|
||
// optional chaining | ||
` | ||
declare const returnsPromise: () => Promise<void> | null; | ||
async function test() { | ||
declare const returnsPromise: () => Promise<void> | null; | ||
await returnsPromise?.(); | ||
returnsPromise()?.then( | ||
() => {}, | ||
|
@@ -516,6 +513,29 @@ declare const myTag: (strings: TemplateStringsArray) => string; | |
myTag\`abc\`; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
declare let x: any; | ||
declare const promiseArray: Array<Promise<unknown>>; | ||
x = promiseArray; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. surprisingly, assignments without a declaration were not tested for |
||
`, | ||
}, | ||
{ | ||
code: ` | ||
declare const promiseArray: Array<Promise<unknown>>; | ||
async function f() { | ||
return promiseArray; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
declare const promiseArray: Array<Promise<unknown>>; | ||
async function* generator() { | ||
yield promiseArray; | ||
} | ||
`, | ||
}, | ||
], | ||
|
||
invalid: [ | ||
|
@@ -972,9 +992,9 @@ async function test() { | |
}, | ||
{ | ||
code: ` | ||
async function test() { | ||
declare const promiseValue: Promise<number>; | ||
declare const promiseValue: Promise<number>; | ||
|
||
async function test() { | ||
promiseValue; | ||
promiseValue.then(() => {}); | ||
promiseValue.catch(); | ||
|
@@ -1002,9 +1022,9 @@ async function test() { | |
}, | ||
{ | ||
code: ` | ||
async function test() { | ||
declare const promiseUnion: Promise<number> | number; | ||
declare const promiseUnion: Promise<number> | number; | ||
|
||
async function test() { | ||
promiseUnion; | ||
} | ||
`, | ||
|
@@ -1017,9 +1037,9 @@ async function test() { | |
}, | ||
{ | ||
code: ` | ||
async function test() { | ||
declare const promiseIntersection: Promise<number> & number; | ||
declare const promiseIntersection: Promise<number> & number; | ||
|
||
async function test() { | ||
promiseIntersection; | ||
promiseIntersection.then(() => {}); | ||
promiseIntersection.catch(); | ||
|
@@ -1227,13 +1247,13 @@ async function test() { | |
}, | ||
{ | ||
code: ` | ||
(async function () { | ||
declare const promiseIntersection: Promise<number> & number; | ||
promiseIntersection; | ||
promiseIntersection.then(() => {}); | ||
promiseIntersection.catch(); | ||
promiseIntersection.finally(); | ||
})(); | ||
declare const promiseIntersection: Promise<number> & number; | ||
(async function () { | ||
promiseIntersection; | ||
promiseIntersection.then(() => {}); | ||
promiseIntersection.catch(); | ||
promiseIntersection.finally(); | ||
})(); | ||
`, | ||
options: [{ ignoreIIFE: true }], | ||
errors: [ | ||
|
@@ -1762,6 +1782,16 @@ void promiseArray; | |
}, | ||
{ | ||
code: ` | ||
declare const promiseArray: Array<Promise<unknown>>; | ||
async function f() { | ||
await promiseArray; | ||
} | ||
`, | ||
options: [{ ignoreVoid: false }], | ||
errors: [{ line: 4, messageId: 'floatingPromiseArray' }], | ||
}, | ||
{ | ||
code: ` | ||
[1, 2, Promise.reject(), 3]; | ||
`, | ||
errors: [{ line: 2, messageId: 'floatingPromiseArrayVoid' }], | ||
|
@@ -1855,5 +1885,47 @@ cursed(); | |
`, | ||
errors: [{ line: 3, messageId: 'floatingPromiseArrayVoid' }], | ||
}, | ||
{ | ||
code: ` | ||
declare const x: any; | ||
function* generator(): Generator<number, void, Promise<number>> { | ||
yield x; | ||
} | ||
`, | ||
errors: [{ messageId: 'floatingVoid' }], | ||
}, | ||
{ | ||
code: ` | ||
declare const x: Generator<number, Promise<number>, void>; | ||
function* generator(): Generator<number, void, void> { | ||
yield* x; | ||
} | ||
`, | ||
errors: [{ messageId: 'floatingVoid' }], | ||
}, | ||
{ | ||
code: ` | ||
3 as Promise<number>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a community maintainer, but I'm very much against these tests. Either these tests be removed or should be replaced with valid ts code. |
||
`, | ||
errors: [{ messageId: 'floatingVoid', line: 2 }], | ||
}, | ||
{ | ||
code: ` | ||
3 as Promise<number> & number; | ||
`, | ||
errors: [{ messageId: 'floatingVoid', line: 2 }], | ||
}, | ||
{ | ||
code: ` | ||
3 as Promise<number> & { yolo?: string }; | ||
`, | ||
errors: [{ messageId: 'floatingVoid', line: 2 }], | ||
}, | ||
{ | ||
code: ` | ||
<Promise<number>>3; | ||
`, | ||
errors: [{ messageId: 'floatingVoid', line: 2 }], | ||
}, | ||
], | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typescript gives a warning
Lets not include example of those cases for which the ts compiler gives us a warning/error (?)
Minimum repro for the warning , hover over
await
keyword in line 2.