Skip to content

Commit 7a0f948

Browse files
committed
Add skip import check
1 parent b086a62 commit 7a0f948

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

docs/rules/no-system-props.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Disallow use of styled-system props (no-system-colors)
1+
# Disallow use of styled-system props (no-system-props)
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

@@ -41,6 +41,10 @@ import {Avatar} from 'some-other-library'
4141

4242
## Options
4343

44+
- `skipImportCheck` (default: `false`)
45+
46+
By default, the `no-system-props` rule will only check for styled system props used in functions and components that are imported from `@primer/components`. You can disable this behavior by setting `skipImportCheck` to `true`. This is useful for linting custom components that pass styled system props down to Primer React components.
47+
4448
- `includeUtilityComponents` (default: `false`)
4549

4650
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`.

src/rules/no-system-props.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const {isPrimerComponent} = require('../utils/is-primer-component')
2-
const {pick} = require('@styled-system/props')
3-
const {some, last} = require('lodash')
1+
const { isPrimerComponent } = require('../utils/is-primer-component')
2+
const { pick } = require('@styled-system/props')
3+
const { some, last } = require('lodash')
44

55
// Components for which we allow all styled system props
66
const alwaysExcludedComponents = new Set([
@@ -29,6 +29,9 @@ module.exports = {
2929
schema: [
3030
{
3131
properties: {
32+
skipImportCheck: {
33+
type: 'boolean'
34+
},
3235
includeUtilityComponents: {
3336
type: 'boolean'
3437
}
@@ -40,6 +43,10 @@ module.exports = {
4043
}
4144
},
4245
create(context) {
46+
// If `skipImportCheck` is true, this rule will check for deprecated colors
47+
// used in any components (not just ones that are imported from `@primer/components`).
48+
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
49+
4350
const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false
4451

4552
const excludedComponents = new Set([
@@ -49,7 +56,7 @@ module.exports = {
4956

5057
return {
5158
JSXOpeningElement(jsxNode) {
52-
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
59+
if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
5360
if (excludedComponents.has(jsxNode.name.name)) return
5461

5562
// Create an object mapping from prop name to the AST node for that attribute
@@ -65,7 +72,7 @@ module.exports = {
6572
// Create an array of system prop attribute nodes
6673
let systemProps = Object.values(pick(propsByNameObject))
6774

68-
let excludedProps = excludedComponentProps.has(jsxNode.name.name)
75+
const excludedProps = excludedComponentProps.has(jsxNode.name.name)
6976
? new Set([...alwaysExcludedProps, ...excludedComponentProps.get(jsxNode.name.name)])
7077
: alwaysExcludedProps
7178

@@ -100,15 +107,15 @@ module.exports = {
100107
...systemProps.map(node => fixer.remove(node)),
101108
...(stylesToAdd.size > 0
102109
? [
103-
existingSxProp
104-
? // Update an existing sx prop
105-
fixer.insertTextAfter(
106-
last(existingSxProp.value.expression.properties),
107-
`, ${objectEntriesStringFromStylesMap(stylesToAdd)}`
108-
)
109-
: // Insert new sx prop
110-
fixer.insertTextAfter(last(jsxNode.attributes), sxPropTextFromStylesMap(systemPropstylesMap))
111-
]
110+
existingSxProp
111+
? // Update an existing sx prop
112+
fixer.insertTextAfter(
113+
last(existingSxProp.value.expression.properties),
114+
`, ${objectEntriesStringFromStylesMap(stylesToAdd)}`
115+
)
116+
: // Insert new sx prop
117+
fixer.insertTextAfter(last(jsxNode.attributes), sxPropTextFromStylesMap(systemPropstylesMap))
118+
]
112119
: [])
113120
]
114121
}
@@ -143,12 +150,12 @@ const excludeSxEntriesFromStyleMap = (stylesMap, sxProp) => {
143150
if (
144151
!sxProp.value ||
145152
sxProp.value.type !== 'JSXExpressionContainer' ||
146-
sxProp.value.expression.type != 'ObjectExpression'
153+
sxProp.value.expression.type !== 'ObjectExpression'
147154
) {
148155
return stylesMap
149156
}
150157
return new Map(
151-
[...stylesMap].filter(([key, _value]) => {
158+
[...stylesMap].filter(([key]) => {
152159
return !some(sxProp.value.expression.properties, p => p.type === 'Property' && p.key.name === key)
153160
})
154161
)

0 commit comments

Comments
 (0)