From 80af2e0a05ebd63ecdcdbd5c7af258321aeb656b Mon Sep 17 00:00:00 2001 From: mrkldshv Date: Sat, 17 Dec 2022 10:06:13 +0000 Subject: [PATCH] feat(rome_js_analyzer): handle `JsTemplateExpression` --- .../src/analyzers/a11y/use_html_lang.rs | 25 ++++++++++++------- .../tests/specs/a11y/useHtmlLang.jsx | 1 + .../tests/specs/a11y/useHtmlLang.jsx.snap | 1 + 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/crates/rome_js_analyze/src/analyzers/a11y/use_html_lang.rs b/crates/rome_js_analyze/src/analyzers/a11y/use_html_lang.rs index c60a4191f5a5..95796feafc3d 100644 --- a/crates/rome_js_analyze/src/analyzers/a11y/use_html_lang.rs +++ b/crates/rome_js_analyze/src/analyzers/a11y/use_html_lang.rs @@ -1,7 +1,8 @@ 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, + jsx_ext::AnyJsxElement, AnyJsExpression, AnyJsxAttribute, AnyJsxAttributeValue, JsxAttribute, + TextRange, }; use rome_rowan::AstNode; @@ -113,24 +114,30 @@ fn is_valid_lang_attribute(attr: JsxAttribute) -> Option<()> { if let AnyJsxAttributeValue::JsxExpressionAttributeValue(expression) = attribute_value { let expression = expression.expression().ok()?; - if let Some(identifier_expression) = expression.as_js_identifier_expression() { - if !identifier_expression.text().is_empty() { + 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(()); - } - return None; + }; } - let bool_expression = expression + expression .as_any_js_literal_expression()? .as_js_boolean_literal_expression(); - if bool_expression.is_some() { - return None; - } 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; } diff --git a/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx b/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx index 44a53eb04000..dcf50664a2ab 100644 --- a/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx +++ b/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx @@ -13,6 +13,7 @@ {/* valid */} + diff --git a/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx.snap b/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx.snap index 6819ee0690f8..1b3c63b4503a 100644 --- a/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx.snap +++ b/crates/rome_js_analyze/tests/specs/a11y/useHtmlLang.jsx.snap @@ -19,6 +19,7 @@ expression: useHtmlLang.jsx {/* valid */} +