Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"oxlint",
"packagespecifier",
"parameterised",
"parenthesization",
"performant",
"pluggable",
"postprocess",
Expand Down
28 changes: 12 additions & 16 deletions packages/eslint-plugin/src/rules/no-floating-promises.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type * as ts from 'typescript';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import type { TypeOrValueSpecifier } from '../util';

import {
createRule,
getOperatorPrecedence,
getOperatorPrecedenceForNode,
getParserServices,
isBuiltinSymbolLike,
isParenthesized,
OperatorPrecedence,
readonlynessOptionsDefaults,
readonlynessOptionsSchema,
Expand Down Expand Up @@ -168,10 +169,11 @@ export default createRule<Options, MessageId>({
{
messageId: 'floatingFixVoid',
fix(fixer): TSESLint.RuleFix | TSESLint.RuleFix[] {
const tsNode = services.esTreeNodeToTSNodeMap.get(
node.expression,
);
if (isHigherPrecedenceThanUnary(tsNode)) {
if (
isParenthesized(expression, context.sourceCode) ||
getOperatorPrecedenceForNode(expression) >
OperatorPrecedence.Unary
) {
return fixer.insertTextBefore(node, 'void ');
}
return [
Expand Down Expand Up @@ -223,8 +225,10 @@ export default createRule<Options, MessageId>({
'await',
);
}
const tsNode = services.esTreeNodeToTSNodeMap.get(node.expression);
if (isHigherPrecedenceThanUnary(tsNode)) {
if (
isParenthesized(expression, context.sourceCode) ||
getOperatorPrecedenceForNode(expression) > OperatorPrecedence.Unary
) {
return fixer.insertTextBefore(node, 'await ');
}
return [
Expand Down Expand Up @@ -261,14 +265,6 @@ export default createRule<Options, MessageId>({
);
}

function isHigherPrecedenceThanUnary(node: ts.Node): boolean {
const operator = ts.isBinaryExpression(node)
? node.operatorToken.kind
: ts.SyntaxKind.Unknown;
const nodePrecedence = getOperatorPrecedence(node.kind, operator);
return nodePrecedence > OperatorPrecedence.Unary;
}

function isAsyncIife(node: TSESTree.ExpressionStatement): boolean {
if (node.expression.type !== AST_NODE_TYPES.CallExpression) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/util/getOperatorPrecedence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ export enum OperatorPrecedence {
Invalid = -1,
}

/**
* Note that this does not take into account parenthesization. You should check
* for parenthesization separately if it's relevant to your usage.
*/
export function getOperatorPrecedenceForNode(
node: TSESTree.Node,
): OperatorPrecedence {
Expand Down
18 changes: 9 additions & 9 deletions packages/eslint-plugin/tests/rules/no-floating-promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ async function test() {
messageId: 'floatingFixVoid',
output: `
async function test() {
void ((Promise.resolve(), 123));
void (Promise.resolve(), 123);
(123, Promise.resolve());
(123, Promise.resolve(), 123);
}
Expand All @@ -2054,7 +2054,7 @@ async function test() {
messageId: 'floatingFixAwait',
output: `
async function test() {
await ((Promise.resolve(), 123));
await (Promise.resolve(), 123);
(123, Promise.resolve());
(123, Promise.resolve(), 123);
}
Expand All @@ -2071,7 +2071,7 @@ async function test() {
output: `
async function test() {
(Promise.resolve(), 123);
void ((123, Promise.resolve()));
void (123, Promise.resolve());
(123, Promise.resolve(), 123);
}
`,
Expand All @@ -2081,7 +2081,7 @@ async function test() {
output: `
async function test() {
(Promise.resolve(), 123);
await ((123, Promise.resolve()));
await (123, Promise.resolve());
(123, Promise.resolve(), 123);
}
`,
Expand All @@ -2098,7 +2098,7 @@ async function test() {
async function test() {
(Promise.resolve(), 123);
(123, Promise.resolve());
void ((123, Promise.resolve(), 123));
void (123, Promise.resolve(), 123);
}
`,
},
Expand All @@ -2108,7 +2108,7 @@ async function test() {
async function test() {
(Promise.resolve(), 123);
(123, Promise.resolve());
await ((123, Promise.resolve(), 123));
await (123, Promise.resolve(), 123);
}
`,
},
Expand Down Expand Up @@ -2237,7 +2237,7 @@ async function returnsPromise() {
async function returnsPromise() {
return 'value';
}
await ((1, returnsPromise()));
await (1, returnsPromise());
`,
},
],
Expand Down Expand Up @@ -4556,13 +4556,13 @@ await promiseIntersection.finally(() => {});
{
messageId: 'floatingFixVoid',
output: `
void ((Promise.resolve().finally(() => {}), 123));
void (Promise.resolve().finally(() => {}), 123);
`,
},
{
messageId: 'floatingFixAwait',
output: `
await ((Promise.resolve().finally(() => {}), 123));
await (Promise.resolve().finally(() => {}), 123);
`,
},
],
Expand Down