-
-
Notifications
You must be signed in to change notification settings - Fork 884
feat(html/analyze): add useValidLang #8690
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 all commits
Commits
Show all changes
4 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,5 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Added the rule [`useValidLang`](https://biomejs.dev/linter/rules/use-valid-lang) to the HTML language. | ||
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
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
195 changes: 195 additions & 0 deletions
195
crates/biome_html_analyze/src/lint/a11y/use_valid_lang.rs
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,195 @@ | ||
| use biome_analyze::context::RuleContext; | ||
| use biome_analyze::{Ast, Rule, RuleDiagnostic, RuleSource, declare_lint_rule}; | ||
| use biome_aria_metadata::{is_valid_country, is_valid_language, is_valid_script}; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_html_syntax::HtmlFileSource; | ||
| use biome_html_syntax::element_ext::AnyHtmlTagElement; | ||
| use biome_rowan::{AstNode, TextRange}; | ||
| use biome_rule_options::use_valid_lang::UseValidLangOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Ensure that the attribute passed to the `lang` attribute is a correct ISO language and/or country. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <html lang="lorem" /> | ||
| /// ``` | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <html lang="en-babab" /> | ||
| /// ``` | ||
| /// | ||
| /// ```html,expect_diagnostic | ||
| /// <html lang="en-GB-typo" /> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```html | ||
| /// <html lang="en-GB" /> | ||
| /// ``` | ||
| pub UseValidLang { | ||
| version: "next", | ||
| name: "useValidLang", | ||
| language: "html", | ||
| sources: &[RuleSource::EslintJsxA11y("lang").same()], | ||
| recommended: true, | ||
| severity: Severity::Error, | ||
| } | ||
| } | ||
|
|
||
| enum InvalidKind { | ||
| Language, | ||
| Country, | ||
| Script, | ||
| Value, | ||
| } | ||
|
|
||
| pub struct UseValidLangState { | ||
| invalid_kind: InvalidKind, | ||
| attribute_range: TextRange, | ||
| } | ||
|
|
||
| impl Rule for UseValidLang { | ||
| type Query = Ast<AnyHtmlTagElement>; | ||
| type State = UseValidLangState; | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseValidLangOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let node = ctx.query(); | ||
| let element_text = node.name().ok()?.value_token().ok()?; | ||
| let source_type = ctx.source_type::<HtmlFileSource>(); | ||
| let matches_tag = if source_type.is_html() { | ||
| element_text.text_trimmed().eq_ignore_ascii_case("html") | ||
| } else { | ||
| element_text.text_trimmed() == "html" | ||
| }; | ||
| if !matches_tag { | ||
| return None; | ||
| } | ||
|
|
||
| let attribute = node.find_attribute_by_name("lang")?; | ||
| let attribute_value = attribute.initializer()?.value().ok()?; | ||
| let attribute_static_value = attribute_value.as_static_value()?; | ||
| let attribute_text = attribute_static_value.text(); | ||
| let mut split_value = attribute_text.split('-'); | ||
| match (split_value.next(), split_value.next(), split_value.next()) { | ||
| (Some(language), Some(script), Some(country)) => { | ||
| if split_value.next().is_some() { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Value, | ||
| }); | ||
| } else if !is_valid_language(language) { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Language, | ||
| }); | ||
| } else if !is_valid_script(script) { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Script, | ||
| }); | ||
| } else if !is_valid_country(country) { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Country, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| (Some(language), Some(script_or_country), None) => { | ||
| if !is_valid_language(language) { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Language, | ||
| }); | ||
| } else if !is_valid_script(script_or_country) | ||
| && !is_valid_country(script_or_country) | ||
| { | ||
| match script_or_country.len() { | ||
| 4 => { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Script, | ||
| }); | ||
| } | ||
| 2 | 3 => { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Country, | ||
| }); | ||
| } | ||
| _ => { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Value, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| (Some(language), None, None) => { | ||
| if !is_valid_language(language) { | ||
| return Some(UseValidLangState { | ||
| attribute_range: attribute_value.range(), | ||
| invalid_kind: InvalidKind::Language, | ||
| }); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| let mut diagnostic = RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state.attribute_range, | ||
| markup! { | ||
| "Provide a valid value for the "<Emphasis>"lang"</Emphasis>" attribute." | ||
| }, | ||
| ); | ||
| diagnostic = match state.invalid_kind { | ||
| InvalidKind::Language => { | ||
| let languages = biome_aria_metadata::languages(); | ||
| let languages = if languages.len() > 15 { | ||
| &languages[..15] | ||
| } else { | ||
| languages | ||
| }; | ||
|
|
||
| diagnostic.footer_list("Some of valid languages:", languages) | ||
| } | ||
| InvalidKind::Country => { | ||
| let countries = biome_aria_metadata::countries(); | ||
| let countries = if countries.len() > 15 { | ||
| &countries[..15] | ||
| } else { | ||
| countries | ||
| }; | ||
|
|
||
| diagnostic.footer_list("Some of valid countries:", countries) | ||
| } | ||
| InvalidKind::Script => { | ||
| let scripts = biome_aria_metadata::scripts(); | ||
| let scripts = if scripts.len() > 15 { | ||
| &scripts[..15] | ||
| } else { | ||
| scripts | ||
| }; | ||
|
|
||
| diagnostic.footer_list("Some of valid scripts:", scripts) | ||
| } | ||
| InvalidKind::Value => diagnostic, | ||
| }; | ||
| Some(diagnostic) | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
crates/biome_html_analyze/tests/specs/a11y/useValidLang/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,7 @@ | ||
| <!-- should generate diagnostics --> | ||
| <html lang="lorem"></html> | ||
| <html lang="en-babab"></html> | ||
| <html lang="en-GB-something"></html> | ||
| <html lang="zh-Xxxx"></html> | ||
| <html lang="zh-Hans-ZZ"></html> | ||
| <html lang="en-US-GB-Extra"></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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplify "HTML language" to "HTML".
The phrase is redundant since the 'L' in HTML already stands for 'language'.
🔎 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[style] ~5-~5: This phrase is redundant (‘L’ stands for ‘language’). Use simply “HTML”.
Context: ...ejs.dev/linter/rules/use-valid-lang) to the HTML language.
(ACRONYM_TAUTOLOGY)
🤖 Prompt for AI Agents