-
-
Notifications
You must be signed in to change notification settings - Fork 801
feat(lint/html): add useHtmlLang
#8262
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Added the `useHtmlLang` lint rule for HTML. The rule enforces that the `html` element has a `lang` attribute. | ||
|
|
||
| Invalid: | ||
|
|
||
| ```html | ||
| <html></html> | ||
| <html lang></html> | ||
| <html lang=""></html> | ||
| ``` | ||
|
|
||
| Valid: | ||
|
|
||
| ```html | ||
| <html lang="en"></html> | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_html_syntax::AnyHtmlElement; | ||
| use biome_rowan::{AstNode, TextRange}; | ||
| use biome_rule_options::use_html_lang::UseHtmlLangOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Enforce that `html` element has `lang` attribute. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <html></html> | ||
| /// ``` | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <html lang=""></html> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```html | ||
| /// <html lang="en"></html> | ||
| /// ``` | ||
| /// | ||
| /// ## Accessibility guidelines | ||
| /// | ||
| /// - [WCAG 3.1.1](https://www.w3.org/WAI/WCAG21/Understanding/language-of-page) | ||
| /// | ||
| pub UseHtmlLang { | ||
| version: "next", | ||
| name: "useHtmlLang", | ||
| language: "html", | ||
| sources: &[RuleSource::EslintJsxA11y("html-has-lang").same()], | ||
| recommended: true, | ||
| severity: Severity::Error, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseHtmlLang { | ||
| type Query = Ast<AnyHtmlElement>; | ||
| type State = TextRange; | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseHtmlLangOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let element = ctx.query(); | ||
|
|
||
| if !is_html_element(element) { | ||
| return None; | ||
| } | ||
|
|
||
| if let Some(lang_attribute) = element.find_attribute_by_name("lang") | ||
| && let Some(initializer) = lang_attribute.initializer() | ||
| && let Ok(value) = initializer.value() | ||
| && let Some(value) = value.string_value() | ||
| && !value.trim_ascii().is_empty() | ||
| { | ||
| return None; | ||
| } | ||
|
|
||
| Some(element.syntax().text_trimmed_range()) | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some(RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state, | ||
| markup! { | ||
| "Provide a "<Emphasis>"lang"</Emphasis>" attribute when using the "<Emphasis>"html"</Emphasis>" element." | ||
| } | ||
| ).note( | ||
| markup! { | ||
| "Setting a "<Emphasis>"lang"</Emphasis>" attribute on HTML document elements configures the language" | ||
| "used by screen readers when no user default is specified." | ||
| } | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| fn is_html_element(element: &AnyHtmlElement) -> bool { | ||
| element | ||
| .name() | ||
| .is_some_and(|token_text| token_text.eq_ignore_ascii_case("html")) | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
crates/biome_html_analyze/tests/specs/a11y/useHtmlLang/invalid.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <!-- should generate diagnostics --> | ||
| <html /> | ||
| <html></html> | ||
| <html lang></html> | ||
| <html lang=""></html> |
80 changes: 80 additions & 0 deletions
80
crates/biome_html_analyze/tests/specs/a11y/useHtmlLang/invalid.html.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: invalid.html | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should generate diagnostics --> | ||
| <html /> | ||
| <html></html> | ||
| <html lang></html> | ||
| <html lang=""></html> | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.html:2:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a lang attribute when using the html element. | ||
|
|
||
| 1 │ <!-- should generate diagnostics --> | ||
| > 2 │ <html /> | ||
| │ ^^^^^^^^ | ||
| 3 │ <html></html> | ||
| 4 │ <html lang></html> | ||
|
|
||
| i Setting a lang attribute on HTML document elements configures the languageused by screen readers when no user default is specified. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:3:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a lang attribute when using the html element. | ||
|
|
||
| 1 │ <!-- should generate diagnostics --> | ||
| 2 │ <html /> | ||
| > 3 │ <html></html> | ||
| │ ^^^^^^^^^^^^^ | ||
| 4 │ <html lang></html> | ||
| 5 │ <html lang=""></html> | ||
|
|
||
| i Setting a lang attribute on HTML document elements configures the languageused by screen readers when no user default is specified. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:4:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a lang attribute when using the html element. | ||
|
|
||
| 2 │ <html /> | ||
| 3 │ <html></html> | ||
| > 4 │ <html lang></html> | ||
| │ ^^^^^^^^^^^^^^^^^^ | ||
| 5 │ <html lang=""></html> | ||
| 6 │ | ||
|
|
||
| i Setting a lang attribute on HTML document elements configures the languageused by screen readers when no user default is specified. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.html:5:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Provide a lang attribute when using the html element. | ||
|
|
||
| 3 │ <html></html> | ||
| 4 │ <html lang></html> | ||
| > 5 │ <html lang=""></html> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^ | ||
| 6 │ | ||
|
|
||
| i Setting a lang attribute on HTML document elements configures the languageused by screen readers when no user default is specified. | ||
|
|
||
|
|
||
| ``` |
2 changes: 2 additions & 0 deletions
2
crates/biome_html_analyze/tests/specs/a11y/useHtmlLang/valid.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <!-- should not generate diagnostics --> | ||
| <html lang="en"></html> |
10 changes: 10 additions & 0 deletions
10
crates/biome_html_analyze/tests/specs/a11y/useHtmlLang/valid.html.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: valid.html | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should not generate diagnostics --> | ||
| <html lang="en"></html> | ||
|
|
||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.