Skip to content

Commit

Permalink
Merge pull request #21 from SferaDev/skip-import-system-props
Browse files Browse the repository at this point in the history
Add Skip import option to system props rule
  • Loading branch information
colebemis authored Mar 22, 2023
2 parents 015237e + a89654d commit 8041452
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-dots-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-primer-react": minor
---

`no-system-props`: Add `skipImportCheck` option
6 changes: 5 additions & 1 deletion docs/rules/no-system-props.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Disallow use of styled-system props (no-system-colors)
# Disallow use of styled-system props (no-system-props)

🔧 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.

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

## Options

- `skipImportCheck` (default: `false`)

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.

- `includeUtilityComponents` (default: `false`)

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`.
Expand Down
24 changes: 20 additions & 4 deletions src/rules/no-system-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ const utilityComponents = new Set(['Box', 'Text'])
// Components for which we allow a set of prop names
const excludedComponentProps = new Map([
['AnchoredOverlay', new Set(['width', 'height'])],
['Avatar', new Set(['size'])],
['AvatarToken', new Set(['size'])],
['CircleOcticon', new Set(['size'])],
['Dialog', new Set(['width', 'height'])],
['IssueLabelToken', new Set(['size'])],
['ProgressBar', new Set(['bg'])],
['Spinner', new Set(['size'])],
['StyledOcticon', new Set(['size'])],
['PointerBox', new Set(['bg'])],
['Token', new Set(['size'])],
['PageLayout', new Set(['padding'])],
['ProgressBar', new Set(['bg'])],
['PointerBox', new Set(['bg'])]
Expand All @@ -28,6 +37,9 @@ module.exports = {
schema: [
{
properties: {
skipImportCheck: {
type: 'boolean'
},
includeUtilityComponents: {
type: 'boolean'
}
Expand All @@ -39,6 +51,10 @@ module.exports = {
}
},
create(context) {
// If `skipImportCheck` is true, this rule will check for deprecated styled system props
// used in any components (not just ones that are imported from `@primer/components`).
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false

const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false

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

return {
JSXOpeningElement(jsxNode) {
if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
if (excludedComponents.has(jsxNode.name.name)) return

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

let excludedProps = excludedComponentProps.has(jsxNode.name.name)
const excludedProps = excludedComponentProps.has(jsxNode.name.name)
? new Set([...alwaysExcludedProps, ...excludedComponentProps.get(jsxNode.name.name)])
: alwaysExcludedProps

Expand Down Expand Up @@ -142,12 +158,12 @@ const excludeSxEntriesFromStyleMap = (stylesMap, sxProp) => {
if (
!sxProp.value ||
sxProp.value.type !== 'JSXExpressionContainer' ||
sxProp.value.expression.type != 'ObjectExpression'
sxProp.value.expression.type !== 'ObjectExpression'
) {
return stylesMap
}
return new Map(
[...stylesMap].filter(([key, _value]) => {
[...stylesMap].filter(([key]) => {
return !some(sxProp.value.expression.properties, p => p.type === 'Property' && p.key.name === key)
})
)
Expand Down

0 comments on commit 8041452

Please sign in to comment.