Skip to content

Commit cbc5e8e

Browse files
authored
Merge pull request #18 from primer/jfuchs/no-styled-system-props
jfuchs/no styled system props
2 parents c93e31e + f0c7a3d commit cbc5e8e

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

.changeset/few-shoes-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-primer-react': minor
3+
---
4+
5+
Introduced an option on no-system-props to include utility components (includeUtilityComponents).

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ESLint rules for Primer React
1212
npm install --save-dev eslint-plugin-primer-react
1313

1414
# or
15-
15+
1616
yarn add --dev eslint-plugin-primer-react
1717
```
1818

@@ -30,3 +30,4 @@ ESLint rules for Primer React
3030
## Rules
3131

3232
- [no-deprecated-colors](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/no-deprecated-colors.md)
33+
- [no-system-props](https://github.com/primer/eslint-plugin-primer-react/blob/main/docs/rules/no-system-props.md)

docs/rules/no-system-props.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ import {Avatar} from 'some-other-library'
3838
// System props passed to non-Primer components are allowed
3939
<Avatar mr={2} />
4040
```
41+
42+
## Options
43+
44+
- `includeUtilityComponents` (default: `false`)
45+
46+
By default, `Box` and `Text` are excluded because styled system props are not deprecated in our utility components. If you prefer to avoid styled system props there as well for consistency, you can set `includeUtilityComponents` to `true`.
47+
48+
```js
49+
/* eslint primer-react/no-system-props: ["warn", {"includeUtilityComponents": true}] */
50+
import {Box} from '@primer/components'
51+
52+
function App() {
53+
// Enabling `includeUtilityComponents` will find system prop usage on utility components like this:
54+
return <Box width={200} />
55+
}
56+
```

src/rules/__tests__/no-system-props.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ ruleTester.run('no-system-props', rule, {
120120
data: {propNames: 'width', componentName: 'Label'}
121121
}
122122
]
123+
},
124+
{
125+
code: `import {Box} from '@primer/components'; <Box width={200} />`,
126+
output: `import {Box} from '@primer/components'; <Box sx={{width: 200}} />`,
127+
options: [{includeUtilityComponents: true}],
128+
errors: [
129+
{
130+
messageId: 'noSystemProps',
131+
data: {propNames: 'width', componentName: 'Box'}
132+
}
133+
]
134+
},
135+
{
136+
code: `import {Text} from '@primer/components'; <Text width={200} />`,
137+
output: `import {Text} from '@primer/components'; <Text sx={{width: 200}} />`,
138+
options: [{includeUtilityComponents: true}],
139+
errors: [
140+
{
141+
messageId: 'noSystemProps',
142+
data: {propNames: 'width', componentName: 'Text'}
143+
}
144+
]
123145
}
124146
]
125147
})

src/rules/no-system-props.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ const {pick} = require('@styled-system/props')
33
const {some, last} = require('lodash')
44

55
// Components for which we allow all styled system props
6-
const excludedComponents = new Set([
7-
'Box',
8-
'Text',
6+
const alwaysExcludedComponents = new Set([
97
'BaseStyles' // BaseStyles will be deprecated eventually
108
])
119

10+
// Excluded by default, but optionally included:
11+
const utilityComponents = new Set(['Box', 'Text'])
12+
1213
// Components for which we allow a set of prop names
1314
const excludedComponentProps = new Map([
1415
['AnchoredOverlay', new Set(['width', 'height'])],
@@ -25,12 +26,27 @@ module.exports = {
2526
meta: {
2627
type: 'suggestion',
2728
fixable: 'code',
28-
schema: [],
29+
schema: [
30+
{
31+
properties: {
32+
includeUtilityComponents: {
33+
type: 'boolean'
34+
}
35+
}
36+
}
37+
],
2938
messages: {
3039
noSystemProps: 'Styled-system props are deprecated ({{ componentName }} called with props: {{ propNames }})'
3140
}
3241
},
3342
create(context) {
43+
const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false
44+
45+
const excludedComponents = new Set([
46+
...alwaysExcludedComponents,
47+
...(includeUtilityComponents ? [] : utilityComponents)
48+
])
49+
3450
return {
3551
JSXOpeningElement(jsxNode) {
3652
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return

0 commit comments

Comments
 (0)