Skip to content

no-unnecessary-components: Add support for Text size and weight, and improve spread object typechecking #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/rules/__tests__/no-unnecessary-components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ ruleTester.run('unnecessary-components', rule, {
filename,
},
]),
{
name: `Text with weight prop`,
code: `${prcImport}${jsx(`<Text weight='medium'>Hello World</Text>`)}`,
filename,
},
{
name: `Text with size prop`,
code: `${prcImport}${jsx(`<Text size='small'>Hello World</Text>`)}`,
filename,
},
],
invalid: Object.entries(components).flatMap(([component, {messageId, replacement}]) => [
{
Expand Down
27 changes: 16 additions & 11 deletions src/rules/no-unnecessary-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const components = {
replacement: 'div',
messageId: 'unecessaryBox',
message: 'Prefer plain HTML elements over `Box` when not using `sx` for styling.',
allowedProps: new Set(['sx']), // + styled-system props
},
Text: {
replacement: 'span',
messageId: 'unecessarySpan',
message: 'Prefer plain HTML elements over `Text` when not using `sx` for styling.',
allowedProps: new Set(['sx', 'size', 'weight']), // + styled-system props
},
}

Expand Down Expand Up @@ -68,33 +70,36 @@ const rule = ESLintUtils.RuleCreator.withoutDocs({
const isPrimer = skipImportCheck || isPrimerComponent(name, context.sourceCode.getScope(openingElement))
if (!isPrimer) return

// Validate the attributes and ensure an `sx` prop is present or spreaded in
/** @param {string} name */
const isAllowedProp = name => componentConfig.allowedProps.has(name) || isStyledSystemProp(name)

// Validate the attributes and ensure an allowed prop is present or spreaded in
/** @type {typeof attributes[number] | undefined | null} */
let asProp = undefined
for (const attribute of attributes) {
// If there is a spread type, check if the type of the spreaded value has an `sx` property
// If there is a spread type, check if the type of the spreaded value has an allowed property
if (attribute.type === 'JSXSpreadAttribute') {
const services = ESLintUtils.getParserServices(context)
const typeChecker = services.program.getTypeChecker()

const spreadType = services.getTypeAtLocation(attribute.argument)
if (typeChecker.getPropertyOfType(spreadType, 'sx') !== undefined) return

// Check if the spread type has a string index signature - this could hide an `sx` property
// Check if the spread type has a string index signature - this could hide an allowed property
if (typeChecker.getIndexTypeOfType(spreadType, IndexKind.String) !== undefined) return
Comment on lines +87 to 88
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched these checks around since theoretically getIndexOfType should be slightly faster than getPropertiesOfType so we can potentially short-circuit a little bit faster and save some time.


const spreadPropNames = typeChecker.getPropertiesOfType(spreadType).map(prop => prop.getName())

// If an allowed prop gets spread in, this is a valid use of the component
if (spreadPropNames.some(isAllowedProp)) return

// If there is an `as` inside the spread object, we can't autofix reliably
if (typeChecker.getPropertyOfType(spreadType, 'as') !== undefined) asProp = null
if (spreadPropNames.includes('as')) asProp = null

continue
}

// Has sx prop, so should keep using this component
if (
attribute.name.type === 'JSXIdentifier' &&
(attribute.name.name === 'sx' || isStyledSystemProp(attribute.name.name))
)
return
// Has an allowed prop, so should keep using this component
if (attribute.name.type === 'JSXIdentifier' && isAllowedProp(attribute.name.name)) return

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