Skip to content

Commit 8041452

Browse files
authored
Merge pull request #21 from SferaDev/skip-import-system-props
Add Skip import option to system props rule
2 parents 015237e + a89654d commit 8041452

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

.changeset/gorgeous-dots-promise.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+
`no-system-props`: Add `skipImportCheck` option

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/react`. 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: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ const utilityComponents = new Set(['Box', 'Text'])
1313
// Components for which we allow a set of prop names
1414
const excludedComponentProps = new Map([
1515
['AnchoredOverlay', new Set(['width', 'height'])],
16+
['Avatar', new Set(['size'])],
17+
['AvatarToken', new Set(['size'])],
18+
['CircleOcticon', new Set(['size'])],
1619
['Dialog', new Set(['width', 'height'])],
20+
['IssueLabelToken', new Set(['size'])],
21+
['ProgressBar', new Set(['bg'])],
22+
['Spinner', new Set(['size'])],
23+
['StyledOcticon', new Set(['size'])],
24+
['PointerBox', new Set(['bg'])],
25+
['Token', new Set(['size'])],
1726
['PageLayout', new Set(['padding'])],
1827
['ProgressBar', new Set(['bg'])],
1928
['PointerBox', new Set(['bg'])]
@@ -28,6 +37,9 @@ module.exports = {
2837
schema: [
2938
{
3039
properties: {
40+
skipImportCheck: {
41+
type: 'boolean'
42+
},
3143
includeUtilityComponents: {
3244
type: 'boolean'
3345
}
@@ -39,6 +51,10 @@ module.exports = {
3951
}
4052
},
4153
create(context) {
54+
// If `skipImportCheck` is true, this rule will check for deprecated styled system props
55+
// used in any components (not just ones that are imported from `@primer/components`).
56+
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
57+
4258
const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false
4359

4460
const excludedComponents = new Set([
@@ -48,7 +64,7 @@ module.exports = {
4864

4965
return {
5066
JSXOpeningElement(jsxNode) {
51-
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
67+
if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
5268
if (excludedComponents.has(jsxNode.name.name)) return
5369

5470
// Create an object mapping from prop name to the AST node for that attribute
@@ -64,7 +80,7 @@ module.exports = {
6480
// Create an array of system prop attribute nodes
6581
let systemProps = Object.values(pick(propsByNameObject))
6682

67-
let excludedProps = excludedComponentProps.has(jsxNode.name.name)
83+
const excludedProps = excludedComponentProps.has(jsxNode.name.name)
6884
? new Set([...alwaysExcludedProps, ...excludedComponentProps.get(jsxNode.name.name)])
6985
: alwaysExcludedProps
7086

@@ -142,12 +158,12 @@ const excludeSxEntriesFromStyleMap = (stylesMap, sxProp) => {
142158
if (
143159
!sxProp.value ||
144160
sxProp.value.type !== 'JSXExpressionContainer' ||
145-
sxProp.value.expression.type != 'ObjectExpression'
161+
sxProp.value.expression.type !== 'ObjectExpression'
146162
) {
147163
return stylesMap
148164
}
149165
return new Map(
150-
[...stylesMap].filter(([key, _value]) => {
166+
[...stylesMap].filter(([key]) => {
151167
return !some(sxProp.value.expression.properties, p => p.type === 'Property' && p.key.name === key)
152168
})
153169
)

0 commit comments

Comments
 (0)