Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-floating-promise] add option to ignore IIFEs#1799

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 18 commits intotypescript-eslint:masterfromanikethsaha:fixes-647
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
ff3ce3d
fix: ignores iife for no-floating-promise (fixes #647)
anikethsahaMar 25, 2020
f52b591
feat: ignoreIIFE options and tests
anikethsahaMar 26, 2020
7b754bb
chore: docs and typing updates
anikethsahaMar 27, 2020
f705053
chore: docs and types update
anikethsahaMar 29, 2020
03b18c7
chore: formatting and docs refactore
anikethsahaMar 29, 2020
6986405
chore: docs formatting
anikethsahaMar 29, 2020
325d9d8
chore: test refactore and typecheck fix
anikethsahaApr 1, 2020
4b69717
docs: docs refactore
anikethsahaApr 1, 2020
2c8315f
chore: linting fixes
anikethsahaApr 4, 2020
920ebdd
chore: test fix
anikethsahaApr 4, 2020
ed4f9b4
Merge branch 'master' into fixes-647
anikethsahaApr 4, 2020
836a7e8
Merge branch 'master' into fixes-647
anikethsahaApr 6, 2020
a26861b
Update no-floating-promises.md
bradzacherApr 20, 2020
6548b1c
Update no-floating-promises.md
bradzacherApr 20, 2020
2c2155e
Update no-floating-promises.md
bradzacherApr 20, 2020
00d9312
Merge branch 'master' into fixes-647
bradzacherApr 20, 2020
b741c77
Update no-floating-promises.md
bradzacherApr 20, 2020
05ed5f6
Update no-floating-promises.md
bradzacherApr 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletionspackages/eslint-plugin/docs/rules/no-floating-promises.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,6 +51,8 @@ The rule accepts an options object with the following properties:
type Options = {
// if true, checking void expressions will be skipped
ignoreVoid?: boolean;
// if true, checking for async iife will be skipped
ignoreIIFE?: boolean;
};

const defaults = {
Expand All@@ -60,7 +62,8 @@ const defaults = {

### `ignoreVoid`

This allows to easily suppress false-positives with void operator.
This allows you to stop the rule reporting promises consumed with void operator.
This can be a good way to explicitly mark a promise as intentionally not awaited.

Examples of **correct** code for this rule with `{ ignoreVoid: true }`:

Expand All@@ -73,10 +76,25 @@ void returnsPromise();
void Promise.reject('value');
```

### `ignoreIIFE`

This allows you to skip checking of async iife

Examples of **correct** code for this rule with `{ ignoreIIFE: true }`:

```ts
await(async function() {
await res(1);
})();

(async function() {
await res(1);
})();
```

## When Not To Use It

If you do not use Promise-like values in your codebase or want to allow them to
remain unhandled.
If you do not use Promise-like values in your codebase, or want to allow them to remain unhandled.

## Related to

Expand Down
26 changes: 25 additions & 1 deletionpackages/eslint-plugin/src/rules/no-floating-promises.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import {
TSESLint,
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';

import * as util from '../util';

type Options = [
{
ignoreVoid?: boolean;
ignoreIIFE?: boolean;
},
];

Expand All@@ -33,6 +38,7 @@ export default util.createRule<Options, MessageId>({
type: 'object',
properties: {
ignoreVoid: { type: 'boolean' },
ignoreIIFE: { type: 'boolean' },
},
additionalProperties: false,
},
Expand All@@ -42,6 +48,7 @@ export default util.createRule<Options, MessageId>({
defaultOptions: [
{
ignoreVoid: false,
ignoreIIFE: false,
},
],

Expand All@@ -54,6 +61,10 @@ export default util.createRule<Options, MessageId>({
ExpressionStatement(node): void {
const { expression } = parserServices.esTreeNodeToTSNodeMap.get(node);

if (options.ignoreIIFE && isAsyncIife(node)) {
return;
}

if (isUnhandledPromise(checker, expression)) {
if (options.ignoreVoid) {
context.report({
Expand All@@ -80,6 +91,19 @@ export default util.createRule<Options, MessageId>({
},
};

function isAsyncIife(node: TSESTree.ExpressionStatement): boolean {
if (node.expression.type !== AST_NODE_TYPES.CallExpression) {
return false;
}

return (
node.expression.type === AST_NODE_TYPES.CallExpression &&
(node.expression.callee.type ===
AST_NODE_TYPES.ArrowFunctionExpression ||
node.expression.callee.type === AST_NODE_TYPES.FunctionExpression)
);
}

function isUnhandledPromise(
checker: ts.TypeChecker,
node: ts.Node,
Expand Down
169 changes: 169 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-floating-promises.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -335,6 +335,54 @@ async function test() {
return returnsPromise();
}
`,
// ignoreIIFE
{
code: `
(async () => {
await something();
})();
`,
options: [{ ignoreIIFE: true }],
},
{
code: `
(async () => {
something();
})();
`,
options: [{ ignoreIIFE: true }],
},
{
code: '(async function foo() {})();',
options: [{ ignoreIIFE: true }],
},
{
code: `
function foo() {
(async function bar() {})();
}
`,
options: [{ ignoreIIFE: true }],
},
{
code: `
const foo = () =>
new Promise(res => {
(async function() {
await res(1);
})();
});
`,
options: [{ ignoreIIFE: true }],
},
{
code: `
(async function() {
await res(1);
})();
`,
options: [{ ignoreIIFE: true }],
},
],

invalid: [
Expand DownExpand Up@@ -726,5 +774,126 @@ async function test() {
},
],
},
{
code: `
(async () => {
await something();
})();
`,
errors: [
{
line: 2,
messageId: 'floating',
},
],
},
{
code: `
(async () => {
something();
})();
`,
errors: [
{
line: 2,
messageId: 'floating',
},
],
},
{
code: '(async function foo() {})();',
errors: [
{
line: 1,
messageId: 'floating',
},
],
},
{
code: `
function foo() {
(async function bar() {})();
}
`,
errors: [
{
line: 3,
messageId: 'floating',
},
],
},
{
code: `
const foo = () =>
new Promise(res => {
(async function() {
await res(1);
})();
});
`,
errors: [
{
line: 4,
messageId: 'floating',
},
],
},
{
code: `
(async function() {
await res(1);
})();
`,
errors: [
{
line: 2,
messageId: 'floating',
},
],
},
{
code: `
(async function() {
Promise.resolve();
})();
`,
options: [{ ignoreIIFE: true }],
errors: [
{
line: 3,
messageId: 'floating',
},
],
},
{
code: `
(async function() {
const promiseIntersection: Promise<number> & number;
promiseIntersection;
promiseIntersection.then(() => {});
promiseIntersection.catch();
promiseIntersection.finally();
})();
`,
options: [{ ignoreIIFE: true }],
errors: [
{
line: 4,
messageId: 'floating',
},
{
line: 5,
messageId: 'floating',
},
{
line: 6,
messageId: 'floating',
},
{
line: 7,
messageId: 'floating',
},
],
},
],
});
3 changes: 2 additions & 1 deletionpackages/typescript-estree/src/ts-estree/ts-estree.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -392,7 +392,8 @@ export type LeftHandSideExpression =
| PrimaryExpression
| TaggedTemplateExpression
| TSNonNullExpression
| TSAsExpression;
| TSAsExpression
| ArrowFunctionExpression;
export type Literal =
| BooleanLiteral
| NumberLiteral
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp