Skip to content

Commit

Permalink
Improve S6544 (no-misused-promises): Report on the function's main …
Browse files Browse the repository at this point in the history
…token
  • Loading branch information
yassin-kammoun-sonarsource committed Oct 24, 2023
1 parent 5a2a1c3 commit e16ac09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit e16ac09

Please sign in to comment.