Skip to content

Commit b1cc317

Browse files
committed
Address comments
1 parent df26962 commit b1cc317

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

docs/rules/no-system-props.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Disallow use of style system props (no-system-colors)
1+
# Disallow use of styled-system props (no-system-colors)
22

33
🔧 The `--fix` option on the [ESLint CLI](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
44

5-
System props are deprecated in Primer components (excluding utility components).
5+
[Styled-system](https://styled-system.com/table) props are deprecated in Primer components (excluding utility components).
66

77
## Rule details
88

9-
This rule disallows use of any styled system prop on a Primer component.
9+
This rule disallows the use of any styled-system prop on Primer components.
1010

1111
\*The two non-deprecated utility components (`Box` and `Text`) are allowed to use system props.
1212

@@ -26,13 +26,15 @@ import {Button} from '@primer/components'
2626
/* eslint primer-react/no-system-props: "error" */
2727
import {Box, Button, ProgressBar} from '@primer/components'
2828
import {Avatar} from 'some-other-library'
29-
30-
<Button sx={{width: 200}} />,
31-
<Button someOtherProp="foo" />,
32-
33-
<ProgressBar bg="howdy" />
34-
35-
<Box width={200} />,
36-
37-
<Avatar width={200} />,
29+
// Non-system props are allowed
30+
<Button someOtherProp="foo" />
31+
// If you need to override styles, use the `sx` prop instead of system props
32+
<Button sx={{mr: 2}} />
33+
// Some component prop names overlap with styled-system prop names.
34+
// These props are still allowed
35+
<ProgressBar bg="success.emphasis" />
36+
// Utility components like Box and Text still accept system props
37+
<Box mr={2} />
38+
// System props passed to non-Primer components are allowed
39+
<Avatar mr={2} />
3840
```

src/rules/no-system-props.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const excludedComponentProps = new Map([
1414
['AnchoredOverlay', new Set(['width', 'height'])],
1515
['Avatar', new Set(['size'])],
1616
['Dialog', new Set(['width', 'height'])],
17+
['Flash', new Set(['variant'])],
1718
['Label', new Set(['variant'])],
1819
['ProgressBar', new Set(['bg'])],
1920
['Spinner', new Set(['size'])],
@@ -26,7 +27,7 @@ module.exports = {
2627
fixable: 'code',
2728
schema: [],
2829
messages: {
29-
noSystemProps: 'Styled-system props are deprecated ({{componentName}} called with props: {{ propNames }})'
30+
noSystemProps: 'Styled-system props are deprecated ({{ componentName }} called with props: {{ propNames }})'
3031
}
3132
},
3233
create(context) {
@@ -36,61 +37,61 @@ module.exports = {
3637
if (excludedComponents.has(jsxNode.name.name)) return
3738

3839
// Create an object mapping from prop name to the AST node for that attribute
39-
const propsByNameObject = jsxNode.attributes.reduce((object, cur) => {
40-
// We don't do anything about spreads for now — only named attributes:
41-
if (cur.type === 'JSXAttribute') {
42-
object[cur.name.name] = cur
40+
const propsByNameObject = jsxNode.attributes.reduce((object, attribute) => {
41+
// We don't do anything about spreads for now — only named attributes
42+
if (attribute.type === 'JSXAttribute') {
43+
object[attribute.name.name] = attribute
4344
}
4445

4546
return object
4647
}, {})
4748

48-
// Create an array of bad prop attribute nodes
49-
let badProps = Object.values(pick(propsByNameObject))
49+
// Create an array of system prop attribute nodes
50+
let systemProps = Object.values(pick(propsByNameObject))
5051

5152
// Filter out our exceptional props
52-
badProps = badProps.filter(prop => {
53+
systemProps = systemProps.filter(prop => {
5354
const excludedProps = excludedComponentProps.get(jsxNode.name.name)
5455
if (!excludedProps) {
5556
return true
5657
}
5758
return !excludedProps.has(prop.name.name)
5859
})
5960

60-
if (badProps.length !== 0) {
61+
if (systemProps.length !== 0) {
6162
context.report({
6263
node: jsxNode,
6364
messageId: 'noSystemProps',
6465
data: {
6566
componentName: jsxNode.name.name,
66-
propNames: badProps.map(a => a.name.name).join(', ')
67+
propNames: systemProps.map(a => a.name.name).join(', ')
6768
},
6869
fix(fixer) {
6970
const existingSxProp = jsxNode.attributes.find(
7071
attribute => attribute.type === 'JSXAttribute' && attribute.name.name === 'sx'
7172
)
72-
const badPropStylesMap = stylesMapFromPropNodes(badProps, context)
73+
const systemPropstylesMap = stylesMapFromPropNodes(systemProps, context)
7374
if (existingSxProp && existingSxProp.value.expression.type !== 'ObjectExpression') {
7475
return
7576
}
7677

7778
const stylesToAdd = existingSxProp
78-
? excludeSxEntriesFromStyleMap(badPropStylesMap, existingSxProp)
79-
: badPropStylesMap
79+
? excludeSxEntriesFromStyleMap(systemPropstylesMap, existingSxProp)
80+
: systemPropstylesMap
8081

8182
return [
82-
// Remove the bad props:
83-
...badProps.map(node => fixer.remove(node)),
83+
// Remove the bad props
84+
...systemProps.map(node => fixer.remove(node)),
8485
...(stylesToAdd.size > 0
8586
? [
8687
existingSxProp
87-
? // Update an existing sx prop:
88+
? // Update an existing sx prop
8889
fixer.insertTextAfter(
8990
last(existingSxProp.value.expression.properties),
9091
`, ${objectEntriesStringFromStylesMap(stylesToAdd)}`
9192
)
9293
: // Insert new sx prop
93-
fixer.insertTextAfter(last(jsxNode.attributes), sxPropTextFromStylesMap(badPropStylesMap))
94+
fixer.insertTextAfter(last(jsxNode.attributes), sxPropTextFromStylesMap(systemPropstylesMap))
9495
]
9596
: [])
9697
]
@@ -111,9 +112,9 @@ const objectEntriesStringFromStylesMap = styles => {
111112
}
112113

113114
// Given an array of styled prop attributes, return a mapping from attribute to expression
114-
const stylesMapFromPropNodes = (badProps, context) => {
115+
const stylesMapFromPropNodes = (systemProps, context) => {
115116
return new Map(
116-
badProps.map(a => [
117+
systemProps.map(a => [
117118
a.name.name,
118119
a.value === null ? 'true' : a.value.raw || context.getSourceCode().getText(a.value.expression)
119120
])

0 commit comments

Comments
 (0)