@@ -12,11 +12,13 @@ const components = {
12
12
replacement : 'div' ,
13
13
messageId : 'unecessaryBox' ,
14
14
message : 'Prefer plain HTML elements over `Box` when not using `sx` for styling.' ,
15
+ allowedProps : [ 'sx' ] , // + styled-system props
15
16
} ,
16
17
Text : {
17
18
replacement : 'span' ,
18
19
messageId : 'unecessarySpan' ,
19
20
message : 'Prefer plain HTML elements over `Text` when not using `sx` for styling.' ,
21
+ allowedProps : [ 'sx' , 'size' , 'weight' ] , // + styled-system props
20
22
} ,
21
23
}
22
24
@@ -68,19 +70,20 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
68
70
const isPrimer = skipImportCheck || isPrimerComponent ( name , context . sourceCode . getScope ( openingElement ) )
69
71
if ( ! isPrimer ) return
70
72
71
- // Validate the attributes and ensure an `sx` prop is present or spreaded in
73
+ // Validate the attributes and ensure an allowed prop is present or spreaded in
72
74
/** @type {typeof attributes[number] | undefined | null } */
73
75
let asProp = undefined
74
76
for ( const attribute of attributes ) {
75
- // If there is a spread type, check if the type of the spreaded value has an `sx` property
77
+ // If there is a spread type, check if the type of the spreaded value has an allowed property
76
78
if ( attribute . type === 'JSXSpreadAttribute' ) {
77
79
const services = ESLintUtils . getParserServices ( context )
78
80
const typeChecker = services . program . getTypeChecker ( )
79
81
80
82
const spreadType = services . getTypeAtLocation ( attribute . argument )
81
- if ( typeChecker . getPropertyOfType ( spreadType , 'sx' ) !== undefined ) return
83
+ for ( const allowedProp of componentConfig . allowedProps )
84
+ if ( typeChecker . getPropertyOfType ( spreadType , allowedProp ) !== undefined ) return
82
85
83
- // Check if the spread type has a string index signature - this could hide an `sx` property
86
+ // Check if the spread type has a string index signature - this could hide an allowed property
84
87
if ( typeChecker . getIndexTypeOfType ( spreadType , IndexKind . String ) !== undefined ) return
85
88
86
89
// If there is an `as` inside the spread object, we can't autofix reliably
@@ -89,12 +92,13 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
89
92
continue
90
93
}
91
94
92
- // Has sx prop, so should keep using this component
93
- if (
94
- attribute . name . type === 'JSXIdentifier' &&
95
- ( attribute . name . name === 'sx' || isStyledSystemProp ( attribute . name . name ) )
96
- )
97
- return
95
+ // Has an allowed prop, so should keep using this component
96
+ for ( const allowedProp of componentConfig . allowedProps )
97
+ if (
98
+ attribute . name . type === 'JSXIdentifier' &&
99
+ ( attribute . name . name === allowedProp || isStyledSystemProp ( attribute . name . name ) )
100
+ )
101
+ return
98
102
99
103
// If there is an `as` prop we will need to account for that when autofixing
100
104
if ( attribute . name . type === 'JSXIdentifier' && attribute . name . name === 'as' ) asProp = attribute
0 commit comments