@@ -2,12 +2,16 @@ use std::ops::Deref;
22
33use oxc_ast:: {
44 AstKind ,
5- ast:: { JSXAttributeItem , JSXAttributeValue , JSXChild , JSXElement } ,
5+ ast:: { Expression , JSXAttributeItem , JSXAttributeValue , JSXChild , JSXElement } ,
66} ;
77use oxc_diagnostics:: OxcDiagnostic ;
8+ use oxc_ecmascript:: { StringToNumber , ToInt32 } ;
89use oxc_macros:: declare_oxc_lint;
910use oxc_span:: Span ;
1011use oxc_str:: CompactStr ;
12+ use oxc_syntax:: {
13+ identifier:: is_white_space, line_terminator:: is_line_terminator, operator:: UnaryOperator ,
14+ } ;
1115use schemars:: JsonSchema ;
1216use serde:: { Deserialize , Serialize } ;
1317
@@ -203,13 +207,17 @@ impl Rule for LabelHasAssociatedControl {
203207 return ;
204208 }
205209
206- let has_html_for = if let Some ( attributes) = ctx. settings ( ) . jsx_a11y . attributes . get ( "for" ) {
207- attributes
208- . iter ( )
209- . any ( |attr| has_jsx_prop ( & element. opening_element , attr. as_str ( ) ) . is_some ( ) )
210+ // Upstream validates the first present alias, even when its value is falsy.
211+ let html_for_attribute = if let Some ( attributes) =
212+ ctx. settings ( ) . jsx_a11y . attributes . get ( "for" )
213+ {
214+ attributes. iter ( ) . find_map ( |attr| has_jsx_prop ( & element. opening_element , attr. as_str ( ) ) )
210215 } else {
211- has_jsx_prop ( & element. opening_element , "htmlFor" ) . is_some ( )
216+ has_jsx_prop ( & element. opening_element , "htmlFor" )
212217 } ;
218+ let has_html_for = html_for_attribute
219+ . and_then ( JSXAttributeItem :: as_attribute)
220+ . is_some_and ( |attr| has_attribute_value ( attr. value . as_ref ( ) , false ) ) ;
213221
214222 let has_control = self . has_nested_control ( element, ctx) ;
215223
@@ -257,13 +265,7 @@ impl LabelHasAssociatedControl {
257265 }
258266
259267 fn has_accessible_label < ' a > ( & self , root : & JSXElement < ' a > , ctx : & LintContext < ' a > ) -> bool {
260- if root. opening_element . attributes . iter ( ) . any ( |attribute| match attribute {
261- JSXAttributeItem :: Attribute ( attr) => {
262- let attr_name = get_jsx_attribute_name ( & attr. name ) ;
263- self . label_attributes . binary_search ( & attr_name. into ( ) ) . is_ok ( )
264- }
265- JSXAttributeItem :: SpreadAttribute ( _) => true ,
266- } ) {
268+ if self . has_labelling_prop ( root) {
267269 return true ;
268270 }
269271
@@ -276,6 +278,17 @@ impl LabelHasAssociatedControl {
276278 false
277279 }
278280
281+ fn has_labelling_prop ( & self , element : & JSXElement < ' _ > ) -> bool {
282+ element. opening_element . attributes . iter ( ) . any ( |attribute| match attribute {
283+ JSXAttributeItem :: Attribute ( attr) => {
284+ let attr_name = get_jsx_attribute_name ( & attr. name ) ;
285+ self . label_attributes . binary_search ( & attr_name. into ( ) ) . is_ok ( )
286+ && has_attribute_value ( attr. value . as_ref ( ) , true )
287+ }
288+ JSXAttributeItem :: SpreadAttribute ( _) => true ,
289+ } )
290+ }
291+
279292 fn has_nested_control < ' a > ( & self , root : & JSXElement < ' a > , ctx : & LintContext < ' a > ) -> bool {
280293 for child in & root. children {
281294 if self . search_for_nested_control ( child, 1 , ctx) {
@@ -339,25 +352,7 @@ impl LabelHasAssociatedControl {
339352 JSXChild :: ExpressionContainer ( _) => true ,
340353 JSXChild :: Text ( text) => !text. value . as_str ( ) . trim ( ) . is_empty ( ) ,
341354 JSXChild :: Element ( element) => {
342- let has_labelling_prop =
343- element. opening_element . attributes . iter ( ) . any ( |attr| match attr {
344- JSXAttributeItem :: Attribute ( attribute) => {
345- self . label_attributes . iter ( ) . any ( |labelling_prop| {
346- attribute. is_identifier ( labelling_prop)
347- && attribute. value . as_ref ( ) . is_some_and ( |attribute_value| {
348- match attribute_value {
349- JSXAttributeValue :: StringLiteral ( literal) => {
350- !literal. value . as_str ( ) . trim ( ) . is_empty ( )
351- }
352- _ => true ,
353- }
354- } )
355- } )
356- }
357- JSXAttributeItem :: SpreadAttribute ( _) => true ,
358- } ) ;
359-
360- if has_labelling_prop {
355+ if self . has_labelling_prop ( element) {
361356 return true ;
362357 }
363358
@@ -392,6 +387,111 @@ impl LabelHasAssociatedControl {
392387 }
393388}
394389
390+ fn has_attribute_value ( value : Option < & JSXAttributeValue < ' _ > > , trim_strings : bool ) -> bool {
391+ match value {
392+ Some ( JSXAttributeValue :: StringLiteral ( literal) ) => {
393+ AttributeValue :: from_literal ( & literal. value ) . has_value ( trim_strings)
394+ }
395+ Some ( JSXAttributeValue :: ExpressionContainer ( container) ) => container
396+ . expression
397+ . as_expression ( )
398+ . and_then ( get_attribute_expression_value)
399+ . is_none_or ( |value| value. has_value ( trim_strings) ) ,
400+ // Bare attributes and unknown values may provide a label, as in the upstream rule.
401+ _ => true ,
402+ }
403+ }
404+
405+ // Match jsx-ast-utils' primitive value extraction rather than runtime string truthiness.
406+ #[ derive( Clone , Copy ) ]
407+ enum AttributeValue < ' a > {
408+ String ( & ' a str ) ,
409+ Number ( f64 ) ,
410+ BigInt ( bool ) ,
411+ }
412+
413+ impl < ' a > AttributeValue < ' a > {
414+ fn from_literal ( text : & ' a str ) -> Self {
415+ if text. eq_ignore_ascii_case ( "false" ) {
416+ Self :: Number ( 0.0 )
417+ } else if text. eq_ignore_ascii_case ( "true" ) {
418+ Self :: Number ( 1.0 )
419+ } else {
420+ Self :: String ( text)
421+ }
422+ }
423+
424+ fn has_value ( self , trim_strings : bool ) -> bool {
425+ match self {
426+ Self :: String ( text) => {
427+ let text = if trim_strings {
428+ text. trim_matches ( |c| is_white_space ( c) || is_line_terminator ( c) )
429+ } else {
430+ text
431+ } ;
432+ !text. is_empty ( )
433+ }
434+ Self :: Number ( value) => value != 0.0 && !value. is_nan ( ) ,
435+ Self :: BigInt ( nonzero) => nonzero,
436+ }
437+ }
438+
439+ fn to_number ( self ) -> Option < f64 > {
440+ match self {
441+ Self :: String ( text) => Some ( text. string_to_number ( ) ) ,
442+ Self :: Number ( value) => Some ( value) ,
443+ Self :: BigInt ( _) => None ,
444+ }
445+ }
446+ }
447+
448+ fn get_attribute_expression_value < ' a > (
449+ expression : & ' a Expression < ' _ > ,
450+ ) -> Option < AttributeValue < ' a > > {
451+ let value = match expression. get_inner_expression ( ) {
452+ Expression :: StringLiteral ( literal) => AttributeValue :: from_literal ( & literal. value ) ,
453+ Expression :: TemplateLiteral ( template) if template. expressions . is_empty ( ) => {
454+ // jsx-ast-utils uses raw template text, including escape sequences.
455+ AttributeValue :: String ( template. quasis . first ( ) ?. value . raw . as_str ( ) )
456+ }
457+ Expression :: NullLiteral ( _) => AttributeValue :: Number ( 0.0 ) ,
458+ Expression :: BooleanLiteral ( literal) => AttributeValue :: Number ( f64:: from ( literal. value ) ) ,
459+ Expression :: NumericLiteral ( literal) => AttributeValue :: Number ( literal. value ) ,
460+ Expression :: BigIntLiteral ( literal) => AttributeValue :: BigInt ( !literal. is_zero ( ) ) ,
461+ Expression :: Identifier ( identifier) => match identifier. name . as_str ( ) {
462+ "undefined" => AttributeValue :: Number ( f64:: NAN ) ,
463+ "Infinity" => AttributeValue :: Number ( f64:: INFINITY ) ,
464+ // Upstream represents unknown identifiers by their name.
465+ name => AttributeValue :: String ( name) ,
466+ } ,
467+ Expression :: UnaryExpression ( unary) => {
468+ let value = match unary. operator {
469+ // jsx-ast-utils returns undefined for both operators.
470+ UnaryOperator :: Void | UnaryOperator :: Typeof => f64:: NAN ,
471+ UnaryOperator :: Delete => 1.0 ,
472+ operator => {
473+ let argument = get_attribute_expression_value ( & unary. argument ) ?;
474+ if unary. operator == UnaryOperator :: UnaryNegation
475+ && matches ! ( argument, AttributeValue :: BigInt ( _) )
476+ {
477+ return Some ( argument) ;
478+ }
479+ match operator {
480+ UnaryOperator :: UnaryNegation => -argument. to_number ( ) ?,
481+ UnaryOperator :: UnaryPlus => argument. to_number ( ) ?,
482+ UnaryOperator :: LogicalNot => f64:: from ( !argument. has_value ( false ) ) ,
483+ UnaryOperator :: BitwiseNot => f64:: from ( !argument. to_number ( ) ?. to_int_32 ( ) ) ,
484+ _ => unreachable ! ( ) ,
485+ }
486+ }
487+ } ;
488+ AttributeValue :: Number ( value)
489+ }
490+ _ => return None ,
491+ } ;
492+ Some ( value)
493+ }
494+
395495#[ test]
396496fn test ( ) {
397497 use crate :: tester:: Tester ;
@@ -421,7 +521,7 @@ fn test() {
421521 } )
422522 }
423523
424- let pass = vec ! [
524+ let mut pass = vec ! [
425525 (
426526 r#"<label htmlFor="js_id"><span><span><span>A label</span></span></span></label>"# ,
427527 Some ( serde_json:: json!( [ { "depth" : 4 , "assert" : "htmlFor" } ] ) ) ,
@@ -977,7 +1077,7 @@ fn test() {
9771077 ) ,
9781078 ] ;
9791079
980- let fail = vec ! [
1080+ let mut fail = vec ! [
9811081 (
9821082 r#"<label htmlFor="js_id"><span><span><span>A label</span></span></span></label>"# ,
9831083 Some ( serde_json:: json!( [ {
@@ -1664,6 +1764,161 @@ fn test() {
16641764 ) ,
16651765 ] ;
16661766
1767+ {
1768+ pass. extend ( vec ! [
1769+ ( r#"<label aria-label="Name" htmlFor="name" />"# , None , None ) ,
1770+ ( r"<label aria-label={label} htmlFor={id} />" , None , None ) ,
1771+ ( r#"<label aria-label htmlFor="name" />"# , None , None ) ,
1772+ ( r#"<label htmlFor="name"><span aria-label /></label>"# , None , None ) ,
1773+ ( r#"<label htmlFor="name"><span {...props} /></label>"# , None , None ) ,
1774+ ( r#"<label {...props} htmlFor="name" />"# , None , None ) ,
1775+ ( r#"<label htmlFor="">Name<input /></label>"# , None , None ) ,
1776+ ( r#"<label aria-label="">Name<input /></label>"# , None , None ) ,
1777+ ( r#"<label htmlFor=" ">Name</label>"# , None , None ) ,
1778+ ( r"<label htmlFor>Name</label>" , None , None ) ,
1779+ ( r#"<label aria-label={`Name`} htmlFor="name" />"# , None , None ) ,
1780+ ( r#"<label aria-label={`${label}`} htmlFor="name" />"# , None , None ) ,
1781+ ( r#"<label aria-label={1n} htmlFor="name" />"# , None , None ) ,
1782+ ( r"<label htmlFor={1n}>Name</label>" , None , None ) ,
1783+ ( r#"<label aria-label={"\u0085"} htmlFor="name" />"# , None , None ) ,
1784+ ( r#"<label htmlFor="name"><span aria-label={"\u0085"} /></label>"# , None , None ) ,
1785+ ( r#"<label htmlFor={"\uFEFF"}>Name</label>"# , None , None ) ,
1786+ ] ) ;
1787+ fail. extend ( vec ! [
1788+ ( r#"<label aria-label="" htmlFor="name" />"# , None , None ) ,
1789+ ( r#"<label aria-label=" " htmlFor="name" />"# , None , None ) ,
1790+ ( r#"<label aria-labelledby="" htmlFor="name" />"# , None , None ) ,
1791+ ( r#"<label alt="" htmlFor="name" />"# , None , None ) ,
1792+ ( r#"<label aria-label={""} htmlFor="name" />"# , None , None ) ,
1793+ ( r#"<label aria-label={" "} htmlFor="name" />"# , None , None ) ,
1794+ ( r#"<label aria-label={``} htmlFor="name" />"# , None , None ) ,
1795+ ( r#"<label aria-label={null} htmlFor="name" />"# , None , None ) ,
1796+ ( r#"<label aria-label={false} htmlFor="name" />"# , None , None ) ,
1797+ ( r#"<label aria-label={0} htmlFor="name" />"# , None , None ) ,
1798+ ( r#"<label aria-label={0n} htmlFor="name" />"# , None , None ) ,
1799+ ( r#"<label aria-label={"\uFEFF"} htmlFor="name" />"# , None , None ) ,
1800+ ( r#"<label htmlFor="name"><span aria-label={"\uFEFF"} /></label>"# , None , None ) ,
1801+ ( r#"<label aria-label={undefined} htmlFor="name" />"# , None , None ) ,
1802+ ( r#"<label htmlFor="name"><span aria-label={""} /></label>"# , None , None ) ,
1803+ ( r#"<label htmlFor="">Name</label>"# , None , None ) ,
1804+ ( r#"<label htmlFor={""}>Name</label>"# , None , None ) ,
1805+ ( r"<label htmlFor={null}>Name</label>" , None , None ) ,
1806+ ( r"<label htmlFor={false}>Name</label>" , None , None ) ,
1807+ ( r"<label htmlFor={0n}>Name</label>" , None , None ) ,
1808+ (
1809+ r#"<label htmlFor="">Name<input /></label>"# ,
1810+ Some ( serde_json:: json!( [ { "assert" : "both" } ] ) ) ,
1811+ None ,
1812+ ) ,
1813+ (
1814+ r#"<label label="" htmlFor="name" />"# ,
1815+ Some ( serde_json:: json!( [ {
1816+ "labelAttributes" : [ "label" ]
1817+ } ] ) ) ,
1818+ None ,
1819+ ) ,
1820+ (
1821+ r#"<label for="">Name</label>"# ,
1822+ None ,
1823+ Some ( serde_json:: json!( { "settings" : { "jsx-a11y" : {
1824+ "attributes" : { "for" : [ "for" ] }
1825+ } } } ) ) ,
1826+ ) ,
1827+ ] ) ;
1828+ }
1829+
1830+ {
1831+ pass. extend (
1832+ vec ! [
1833+ r#"<label aria-label="true" htmlFor="name" />"# ,
1834+ r#"<label aria-label=" false " htmlFor="name" />"# ,
1835+ r#"<label aria-label={-1} htmlFor="name" />"# ,
1836+ r#"<label aria-label={!false} htmlFor="name" />"# ,
1837+ r#"<label aria-label={!!true} htmlFor="name" />"# ,
1838+ r#"<label aria-label={~0} htmlFor="name" />"# ,
1839+ r#"<label aria-label={+"true"} htmlFor="name" />"# ,
1840+ r#"<label aria-label={delete object.label} htmlFor="name" />"# ,
1841+ ]
1842+ . into_iter ( )
1843+ . map ( |code| ( code, None , None ) ) ,
1844+ ) ;
1845+ fail. extend (
1846+ vec ! [
1847+ r#"<label aria-label="false" htmlFor="name" />"# ,
1848+ r#"<label aria-label="FaLsE" htmlFor="name" />"# ,
1849+ r#"<label aria-label={"false"} htmlFor="name" />"# ,
1850+ r#"<label htmlFor="false">Name</label>"# ,
1851+ r#"<label htmlFor="name"><span aria-label="false" /></label>"# ,
1852+ r#"<label aria-label={-unknown} htmlFor="name" />"# ,
1853+ r#"<label aria-label={-0} htmlFor="name" />"# ,
1854+ r#"<label aria-label={-0n} htmlFor="name" />"# ,
1855+ r#"<label aria-label={+0} htmlFor="name" />"# ,
1856+ r#"<label aria-label={!true} htmlFor="name" />"# ,
1857+ r#"<label aria-label={!!false} htmlFor="name" />"# ,
1858+ r#"<label aria-label={~(-1)} htmlFor="name" />"# ,
1859+ r#"<label aria-label={+"false"} htmlFor="name" />"# ,
1860+ r"<label htmlFor={void 0}>Name</label>" ,
1861+ r#"<label aria-label={typeof value} htmlFor="name" />"# ,
1862+ ]
1863+ . into_iter ( )
1864+ . map ( |code| ( code, None , None ) ) ,
1865+ ) ;
1866+ }
1867+
1868+ {
1869+ pass. extend (
1870+ vec ! [
1871+ r#"<label aria-label={`\uFEFF`} htmlFor="name" />"# ,
1872+ r#"<label htmlFor="name"><span aria-label={`\uFEFF`} /></label>"# ,
1873+ r#"<label aria-label={`\n`} htmlFor="name" />"# ,
1874+ r#"<label aria-label={`false`} htmlFor="name" />"# ,
1875+ r"<label htmlFor={`\uFEFF`}>Name</label>" ,
1876+ ]
1877+ . into_iter ( )
1878+ . map ( |code| ( code, None , None ) ) ,
1879+ ) ;
1880+ fail. extend (
1881+ vec ! [
1882+ r#"<label aria-label={``} htmlFor="name" />"# ,
1883+ r#"<label aria-label={` `} htmlFor="name" />"# ,
1884+ r"<label htmlFor={``}>Name</label>" ,
1885+ ]
1886+ . into_iter ( )
1887+ . map ( |code| ( code, None , None ) ) ,
1888+ ) ;
1889+ }
1890+
1891+ {
1892+ let settings = Some ( serde_json:: json!( { "settings" : { "jsx-a11y" : {
1893+ "attributes" : { "for" : [ "htmlFor" , "for" ] }
1894+ } } } ) ) ;
1895+ pass. extend ( vec ! [
1896+ ( r#"<label for="name">Name</label>"# , None , settings. clone( ) ) ,
1897+ ( r#"<label htmlFor="name" for="">Name</label>"# , None , settings. clone( ) ) ,
1898+ ( r#"<label for="" htmlFor="name">Name</label>"# , None , settings. clone( ) ) ,
1899+ ( r#"<label htmlFor={id} for="name">Name</label>"# , None , settings. clone( ) ) ,
1900+ ( r#"<label htmlFor="" for="name">Name<input /></label>"# , None , settings. clone( ) ) ,
1901+ (
1902+ r#"<label htmlFor="" for="name">Name</label>"# ,
1903+ None ,
1904+ Some ( serde_json:: json!( { "settings" : { "jsx-a11y" : {
1905+ "attributes" : { "for" : [ "for" , "htmlFor" ] }
1906+ } } } ) ) ,
1907+ ) ,
1908+ ] ) ;
1909+ fail. extend ( vec ! [
1910+ ( r#"<label htmlFor="" for="name">Name</label>"# , None , settings. clone( ) ) ,
1911+ ( r#"<label for="name" htmlFor="">Name</label>"# , None , settings. clone( ) ) ,
1912+ ( r#"<label htmlFor={null} for="name">Name</label>"# , None , settings. clone( ) ) ,
1913+ ( r#"<label htmlFor={false} for="name">Name</label>"# , None , settings. clone( ) ) ,
1914+ (
1915+ r#"<label htmlFor="" for="name">Name<input /></label>"# ,
1916+ Some ( serde_json:: json!( [ { "assert" : "both" } ] ) ) ,
1917+ settings,
1918+ ) ,
1919+ ] ) ;
1920+ }
1921+
16671922 Tester :: new ( LabelHasAssociatedControl :: NAME , LabelHasAssociatedControl :: PLUGIN , pass, fail)
16681923 . test_and_snapshot ( ) ;
16691924}
0 commit comments