Skip to content

Commit 4e09837

Browse files
Pybsamacamc314
andauthored
fix(linter/jsx-a11y/lang): validate lang expression strings (#27304)
`jsx-a11y/lang` validates `<html lang="foo" />` but accepts the same invalid value in `<html lang={"foo"} />` or a static template. Apply the existing BCP 47 predicate to known expression strings and expression-free templates. Use cooked template values and unwrap TypeScript/parenthesized wrappers; preserve unknown dynamic expressions and the existing non-string behavior. No new language-tag dependency or broader constant evaluation. Validation: - Eight regression cases failed on current main before the fix. - Both focused lang-rule tests pass, with 20 new regression/control cases including empty/invalid strings, valid languages, dynamic templates, component settings, escaped cooked templates and TypeScript assertions. - Independent AI code review found no blocking issues; suggested template/wrapper coverage was added. - Full post-commit `just ready` passed: 98 test targets, 5,061 passed / 161 ignored / 0 failed; formatting, code generation, checks, Clippy and documentation completed with a clean worktree. Fixes #27303 AI-assisted with Codex. --------- Co-authored-by: Pybsama <[email protected]> Co-authored-by: Cameron Clark <[email protected]>
1 parent 1f917f4 commit 4e09837

2 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎crates/oxc_linter/src/rules/jsx_a11y/lang.rs‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use language_tags::LanguageTag;
22
use oxc_ast::{
33
AstKind,
4-
ast::{JSXAttributeItem, JSXAttributeValue},
4+
ast::{Expression, JSXAttributeItem, JSXAttributeValue},
55
};
66
use oxc_diagnostics::OxcDiagnostic;
77
use oxc_macros::declare_oxc_lint;
@@ -87,15 +87,30 @@ impl Rule for Lang {
8787
fn is_valid_lang_prop(item: &JSXAttributeItem) -> bool {
8888
match get_prop_value(item) {
8989
Some(JSXAttributeValue::ExpressionContainer(container)) => {
90-
!container.expression.is_expression() || !container.expression.is_undefined()
91-
}
92-
Some(JSXAttributeValue::StringLiteral(str)) => {
93-
LanguageTag::parse(str.value.as_str()).as_ref().is_ok_and(LanguageTag::is_valid)
90+
let Some(expression) = container.expression.as_expression() else {
91+
return true;
92+
};
93+
match expression.get_inner_expression() {
94+
Expression::StringLiteral(literal) => is_valid_language_tag(&literal.value),
95+
Expression::TemplateLiteral(template) if template.expressions.is_empty() => {
96+
template
97+
.quasis
98+
.first()
99+
.and_then(|quasi| quasi.value.cooked.as_ref())
100+
.is_none_or(|value| is_valid_language_tag(value))
101+
}
102+
_ => !expression.is_undefined(),
103+
}
94104
}
105+
Some(JSXAttributeValue::StringLiteral(literal)) => is_valid_language_tag(&literal.value),
95106
_ => true,
96107
}
97108
}
98109

110+
fn is_valid_language_tag(value: &str) -> bool {
111+
LanguageTag::parse(value).as_ref().is_ok_and(LanguageTag::is_valid)
112+
}
113+
99114
#[test]
100115
fn test() {
101116
use crate::tester::Tester;
@@ -126,6 +141,16 @@ fn test() {
126141
("<Foo lang={undefined} />", None, None),
127142
("<Foo lang='en' />", None, Some(settings())),
128143
("<Box as='html' lang='en' />", None, Some(settings())),
144+
(r#"<html lang={"en"} />"#, None, None),
145+
(r#"<html lang={"en-US"} />"#, None, None),
146+
(r"<html lang={`zh-Hans`} />", None, None),
147+
(r"<html lang={`\u0065n`} />", None, None),
148+
(r#"<html lang={("en" as const)} />"#, None, None),
149+
(r"<html lang={language} />", None, None),
150+
(r"<html lang={`${language}`} />", None, None),
151+
(r#"<div lang={"foo"} />"#, None, None),
152+
(r#"<Foo lang={"en"} />"#, None, Some(settings())),
153+
(r#"<Box as="html" lang={`en-US`} />"#, None, Some(settings())),
129154
];
130155

131156
let fail = vec![
@@ -135,6 +160,16 @@ fn test() {
135160
("<html lang={undefined} />", None, None),
136161
("<Foo lang={undefined} />", None, Some(settings())),
137162
("<Box as='html' lang='foo' />", None, Some(settings())),
163+
(r#"<html lang={"foo"} />"#, None, None),
164+
(r#"<html lang={""} />"#, None, None),
165+
(r#"<html lang={"zz-LL"} />"#, None, None),
166+
(r"<html lang={``} />", None, None),
167+
(r"<html lang={`foo`} />", None, None),
168+
(r"<html lang={`\u0066oo`} />", None, None),
169+
(r#"<html lang={("foo" as const)} />"#, None, None),
170+
(r"<html lang={`zz-LL`} />", None, None),
171+
(r#"<Foo lang={"foo"} />"#, None, Some(settings())),
172+
(r#"<Box as="html" lang={`foo`} />"#, None, Some(settings())),
138173
];
139174

140175
Tester::new(Lang::NAME, Lang::PLUGIN, pass, fail).test_and_snapshot();

‎crates/oxc_linter/src/snapshots/jsx_a11y_lang.snap‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,73 @@ source: crates/oxc_linter/src/tester.rs
4343
· ──────────
4444
╰────
4545
help: Set a valid value for `lang` attribute.
46+
47+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
48+
╭─[lang.tsx:1:7]
49+
1 │ <html lang={"foo"} />
50+
· ────────────
51+
╰────
52+
help: Set a valid value for `lang` attribute.
53+
54+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
55+
╭─[lang.tsx:1:7]
56+
1 │ <html lang={""} />
57+
· ─────────
58+
╰────
59+
help: Set a valid value for `lang` attribute.
60+
61+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
62+
╭─[lang.tsx:1:7]
63+
1 │ <html lang={"zz-LL"} />
64+
· ──────────────
65+
╰────
66+
help: Set a valid value for `lang` attribute.
67+
68+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
69+
╭─[lang.tsx:1:7]
70+
1 │ <html lang={``} />
71+
· ─────────
72+
╰────
73+
help: Set a valid value for `lang` attribute.
74+
75+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
76+
╭─[lang.tsx:1:7]
77+
1 │ <html lang={`foo`} />
78+
· ────────────
79+
╰────
80+
help: Set a valid value for `lang` attribute.
81+
82+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
83+
╭─[lang.tsx:1:7]
84+
1 │ <html lang={`\u0066oo`} />
85+
· ─────────────────
86+
╰────
87+
help: Set a valid value for `lang` attribute.
88+
89+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
90+
╭─[lang.tsx:1:7]
91+
1 │ <html lang={("foo" as const)} />
92+
· ───────────────────────
93+
╰────
94+
help: Set a valid value for `lang` attribute.
95+
96+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
97+
╭─[lang.tsx:1:7]
98+
1 │ <html lang={`zz-LL`} />
99+
· ──────────────
100+
╰────
101+
help: Set a valid value for `lang` attribute.
102+
103+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
104+
╭─[lang.tsx:1:6]
105+
1 │ <Foo lang={"foo"} />
106+
· ────────────
107+
╰────
108+
help: Set a valid value for `lang` attribute.
109+
110+
⚠ jsx-a11y(lang): `lang` attribute must have a valid value.
111+
╭─[lang.tsx:1:16]
112+
1 │ <Box as="html" lang={`foo`} />
113+
· ────────────
114+
╰────
115+
help: Set a valid value for `lang` attribute.

0 commit comments

Comments
 (0)