From 50ef80703c47d965043f5fc9cc4cb1c44e19e1c3 Mon Sep 17 00:00:00 2001 From: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:12:17 +0100 Subject: [PATCH] Improve S6849 (`html-has-lang`): raise issue on `html` tag name instead of whole `` tag if the `lang` attribute is missing (#4395) --- packages/jsts/src/rules/S6849/cb.fixture.tsx | 6 ++++-- packages/jsts/src/rules/S6849/rule.ts | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/jsts/src/rules/S6849/cb.fixture.tsx b/packages/jsts/src/rules/S6849/cb.fixture.tsx index 425e795ed3a..5bb6b926239 100644 --- a/packages/jsts/src/rules/S6849/cb.fixture.tsx +++ b/packages/jsts/src/rules/S6849/cb.fixture.tsx @@ -1,2 +1,4 @@ -; // Noncompliant {{ elements must have the lang prop.}} -; // Noncompliant {{lang attribute must have a valid value.}} + ; // Noncompliant {{ elements must have the lang prop.}} +// ^^^^ + ; // Noncompliant {{lang attribute must have a valid value.}} +// ^^^^^^^^^^^ diff --git a/packages/jsts/src/rules/S6849/rule.ts b/packages/jsts/src/rules/S6849/rule.ts index adfeeb80790..cbb9d79376b 100644 --- a/packages/jsts/src/rules/S6849/rule.ts +++ b/packages/jsts/src/rules/S6849/rule.ts @@ -19,25 +19,35 @@ */ // https://sonarsource.github.io/rspec/#/rspec/S6849/javascript +import { TSESTree } from '@typescript-eslint/experimental-utils'; import { Rule } from 'eslint'; import { rules as jsxA11yRules } from 'eslint-plugin-jsx-a11y'; -import { mergeRules } from '../helpers'; +import { interceptReport, mergeRules } from '../helpers'; const langRule = jsxA11yRules['lang']; const htmlHasLangRule = jsxA11yRules['html-has-lang']; +const decoratedHasLangRule = decorate(htmlHasLangRule); + +export function decorate(rule: Rule.RuleModule): Rule.RuleModule { + return interceptReport(rule, (context, reportDescriptor) => { + const node = (reportDescriptor as any).node as TSESTree.JSXOpeningElement; + (reportDescriptor as any).node = node.name; + context.report(reportDescriptor); + }); +} export const rule: Rule.RuleModule = { meta: { hasSuggestions: true, messages: { ...langRule.meta!.messages, - ...htmlHasLangRule.meta!.messages, + ...decoratedHasLangRule.meta!.messages, }, }, create(context: Rule.RuleContext) { const langListener: Rule.RuleListener = langRule.create(context); - const htmlHasLangListener: Rule.RuleListener = htmlHasLangRule.create(context); + const htmlHasLangListener: Rule.RuleListener = decoratedHasLangRule.create(context); return mergeRules(langListener, htmlHasLangListener); },