Skip to content

Commit 0073358

Browse files
authored
Add new size and weight allowed props for Text component
1 parent baff792 commit 0073358

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/rules/__tests__/no-unnecessary-components.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ ruleTester.run('unnecessary-components', rule, {
6565
filename,
6666
},
6767
]),
68+
{
69+
name: `Text with weight prop`,
70+
code: `${prcImport}${jsx(`<Text weight='medium'>Hello World</Text>`)}`,
71+
filename,
72+
},
73+
{
74+
name: `Text with size prop`,
75+
code: `${prcImport}${jsx(`<Text size='small'>Hello World</Text>`)}`,
76+
filename,
77+
},
6878
],
6979
invalid: Object.entries(components).flatMap(([component, {messageId, replacement}]) => [
7080
{

src/rules/no-unnecessary-components.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ const components = {
1212
replacement: 'div',
1313
messageId: 'unecessaryBox',
1414
message: 'Prefer plain HTML elements over `Box` when not using `sx` for styling.',
15+
allowedProps: ['sx'], // + styled-system props
1516
},
1617
Text: {
1718
replacement: 'span',
1819
messageId: 'unecessarySpan',
1920
message: 'Prefer plain HTML elements over `Text` when not using `sx` for styling.',
21+
allowedProps: ['sx', 'size', 'weight'], // + styled-system props
2022
},
2123
}
2224

@@ -68,19 +70,20 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
6870
const isPrimer = skipImportCheck || isPrimerComponent(name, context.sourceCode.getScope(openingElement))
6971
if (!isPrimer) return
7072

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
7274
/** @type {typeof attributes[number] | undefined | null} */
7375
let asProp = undefined
7476
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
7678
if (attribute.type === 'JSXSpreadAttribute') {
7779
const services = ESLintUtils.getParserServices(context)
7880
const typeChecker = services.program.getTypeChecker()
7981

8082
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
8285

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
8487
if (typeChecker.getIndexTypeOfType(spreadType, IndexKind.String) !== undefined) return
8588

8689
// If there is an `as` inside the spread object, we can't autofix reliably
@@ -89,12 +92,13 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
8992
continue
9093
}
9194

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
98102

99103
// If there is an `as` prop we will need to account for that when autofixing
100104
if (attribute.name.type === 'JSXIdentifier' && attribute.name.name === 'as') asProp = attribute

0 commit comments

Comments
 (0)