From e16ac09037b3f6d42c45311a34993be83550d982 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Tue, 24 Oct 2023 16:19:06 +0200 Subject: [PATCH] Improve S6544 (`no-misused-promises`): Report on the function's main token --- packages/jsts/src/rules/S6544/cb.fixture.ts | 1 + packages/jsts/src/rules/S6544/rule.ts | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/jsts/src/rules/S6544/cb.fixture.ts b/packages/jsts/src/rules/S6544/cb.fixture.ts index ffb45bfa2cd..536a7e7d056 100644 --- a/packages/jsts/src/rules/S6544/cb.fixture.ts +++ b/packages/jsts/src/rules/S6544/cb.fixture.ts @@ -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 => {}); diff --git a/packages/jsts/src/rules/S6544/rule.ts b/packages/jsts/src/rules/S6544/rule.ts index b796ab8f245..dbe8becd1b3 100644 --- a/packages/jsts/src/rules/S6544/rule.ts +++ b/packages/jsts/src/rules/S6544/rule.ts @@ -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' @@ -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); + } } } },