Skip to content

Commit

Permalink
Improve S6849 (html-has-lang): raise issue on html tag name inste…
Browse files Browse the repository at this point in the history
…ad of whole `<html ...>` tag if the `lang` attribute is missing (#4395)
  • Loading branch information
ilia-kebets-sonarsource authored Nov 16, 2023
1 parent 16a9b9e commit 50ef807
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/jsts/src/rules/S6849/cb.fixture.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<html></html>; // Noncompliant {{<html> elements must have the lang prop.}}
<html lang="yolo"></html>; // Noncompliant {{lang attribute must have a valid value.}}
<html></html>; // Noncompliant {{<html> elements must have the lang prop.}}
// ^^^^
<html lang="yolo"></html>; // Noncompliant {{lang attribute must have a valid value.}}
// ^^^^^^^^^^^
16 changes: 13 additions & 3 deletions packages/jsts/src/rules/S6849/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit 50ef807

Please sign in to comment.