Skip to content
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

Improve S6544 (no-misused-promises): Report on the function's main token #4313

Merged
merged 1 commit into from
Oct 25, 2023
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 packages/jsts/src/rules/S6544/cb.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// otherwise rule 'no-misused-promises' gets triggered second
new Promise(async (resolve) => { // Noncompliant {{Promise returned in function argument where a void return was expected.}}
// ^^
const a = await Promise.resolve(12);
resolve(a);
}).catch(error => {});
Expand Down
17 changes: 14 additions & 3 deletions packages/jsts/src/rules/S6544/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import { Rule } from 'eslint';
import { tsEslintRules } from '../typescript-eslint';
import { eslintRules } from '../core';
import { interceptReport, mergeRules } from '../helpers';
import { FUNCTION_NODES, RuleContext, interceptReport, mergeRules } from '../helpers';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { getMainFunctionTokenLocation } from 'eslint-plugin-sonarjs/lib/utils/locations';

/**
* We keep a single occurence of issues raised by both rules, discarding the ones raised by 'no-async-promise-executor'
Expand All @@ -41,10 +42,20 @@ const decoratedNoMisusedPromisesRule = interceptReport(
noMisusedPromisesRule,
(context, descriptor) => {
if ('node' in descriptor) {
const start = (descriptor.node as TSESTree.Node).range[0];
const node = descriptor.node as TSESTree.Node;
const start = node.range[0];
if (!flaggedNodeStarts.get(start)) {
flaggedNodeStarts.set(start, true);
context.report(descriptor);
if (FUNCTION_NODES.includes(node.type)) {
const loc = getMainFunctionTokenLocation(
node as TSESTree.FunctionLike,
node.parent,
context as unknown as RuleContext,
);
context.report({ ...descriptor, loc });
} else {
context.report(descriptor);
}
}
}
},
Expand Down