This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 653
feat(rome_js_analyzer): rule useHtmlLang
#4052
Merged
Merged
Changes from all commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
159 changes: 159 additions & 0 deletions
159
crates/rome_js_analyze/src/analyzers/a11y/use_html_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,159 @@ | ||
use rome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_syntax::{ | ||
jsx_ext::AnyJsxElement, AnyJsxAttribute, AnyJsxAttributeValue, JsxAttribute, TextRange, | ||
}; | ||
use rome_rowan::AstNode; | ||
|
||
declare_rule! { | ||
/// Enforce that `html` element has `lang` attribute. | ||
/// This allows users to choose a language other than the default. | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <html></html> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <html lang={""}></html> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <html lang={null}></html> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <html lang={undefined}></html> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <html lang={true}></html> | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// <html lang="en"></html> | ||
/// ``` | ||
/// | ||
/// ```jsx | ||
/// <html lang={language}></html> | ||
/// ``` | ||
/// | ||
/// ```jsx | ||
/// <html {...props}></html> | ||
/// ``` | ||
/// | ||
/// ```jsx | ||
/// <html lang={""} {...props}></html> | ||
/// ``` | ||
/// | ||
/// ## Accessibility guidelines | ||
/// | ||
/// [WCAG 3.1.1](https://www.w3.org/WAI/WCAG21/Understanding/language-of-page) | ||
pub(crate) UseHtmlLang { | ||
version: "12.0.0", | ||
name: "useHtmlLang", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for UseHtmlLang { | ||
type Query = Ast<AnyJsxElement>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let element = ctx.query(); | ||
let name = element.name().ok()?; | ||
let name = name.as_jsx_name()?.value_token().ok()?; | ||
let name_trimmed = name.text_trimmed(); | ||
if name_trimmed == "html" { | ||
if let Some(lang_attribute) = element.find_attribute_by_name("lang") { | ||
if element.has_trailing_spread_prop(lang_attribute.clone()) | ||
|| is_valid_lang_attribute(lang_attribute).is_some() | ||
{ | ||
return None; | ||
} | ||
return Some(element.syntax().text_trimmed_range()); | ||
} | ||
if !has_spread_prop(element) { | ||
return Some(element.syntax().text_trimmed_range()); | ||
} | ||
} | ||
None | ||
} | ||
|
||
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_valid_lang_attribute(attr: JsxAttribute) -> Option<()> { | ||
if attr.is_value_undefined_or_null() { | ||
return None; | ||
} | ||
|
||
let attribute_value = attr.initializer()?.value().ok()?; | ||
|
||
if let AnyJsxAttributeValue::JsxExpressionAttributeValue(expression) = attribute_value { | ||
let expression = expression.expression().ok()?; | ||
|
||
if expression.as_js_identifier_expression().is_some() { | ||
return Some(()); | ||
} | ||
|
||
if let Some(template_expression) = expression.as_js_template_expression() { | ||
let template_element = template_expression | ||
.elements() | ||
.into_iter() | ||
.find(|element| element.as_js_template_chunk_element().is_some()); | ||
|
||
if template_element.is_some() { | ||
return Some(()); | ||
}; | ||
} | ||
|
||
expression | ||
.as_any_js_literal_expression()? | ||
.as_js_boolean_literal_expression(); | ||
|
||
let string_expression = expression | ||
.as_any_js_literal_expression()? | ||
.as_js_string_literal_expression()?; | ||
let string_expression_text = string_expression.inner_string_text().ok()?; | ||
|
||
if string_expression_text.is_empty() { | ||
return None; | ||
} | ||
|
||
return Some(()); | ||
leops marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
let string_text = attribute_value.as_jsx_string()?.inner_string_text().ok()?; | ||
if string_text.is_empty() { | ||
return None; | ||
} | ||
|
||
Some(()) | ||
} | ||
|
||
fn has_spread_prop(element: &AnyJsxElement) -> bool { | ||
element | ||
.attributes() | ||
.into_iter() | ||
.any(|attribute| matches!(attribute, AnyJsxAttribute::JsxSpreadAttribute(_))) | ||
} |
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,20 @@ | ||
<> | ||
{/* invalid */} | ||
<html /> | ||
<html></html> | ||
<html lang=""></html> | ||
<html lang={""}></html> | ||
mrkldshv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<html lang={``}></html> | ||
<html lang={true}></html> | ||
<html lang={false}></html> | ||
<html lang={undefined}></html> | ||
<html lang={null}></html> | ||
<html {...props} lang=""></html> | ||
{/* valid */} | ||
<html lang="en"></html> | ||
<html lang={"en"}></html> | ||
<html lang={`en`}></html> | ||
<html lang={lang}></html> | ||
<html {...props}></html> | ||
<html lang="" {...props}></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.