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 S6849 (html-has-lang): raise issue on html tag name instead of whole <html ...> tag if the lang attribute is missing #4395

Merged
merged 2 commits into from
Nov 16, 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
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