eslint-plugin-primer-react 2.0.0-rc.9730bcf → 2.0.1-rc.a74c9bd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,11 +1,21 @@
1
1
  # eslint-plugin-primer-react
2
2
 
3
+ ## 2.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#43](https://github.com/primer/eslint-plugin-primer-react/pull/43) [`943e49a`](https://github.com/primer/eslint-plugin-primer-react/commit/943e49a62ada544b73320791858398705ed8208e) Thanks [@colebemis](https://github.com/colebemis)! - `direct-slot-children` fixes
8
+
3
9
  ## 2.0.0
4
10
 
5
11
  ### Major Changes
6
12
 
7
13
  - [#39](https://github.com/primer/eslint-plugin-primer-react/pull/39) [`96a675d`](https://github.com/primer/eslint-plugin-primer-react/commit/96a675d8865450d3be556135947a29181a56551c) Thanks [@colebemis](https://github.com/colebemis)! - Add `direct-slot-children` rule
8
14
 
15
+ ### Minor Changes
16
+
17
+ - [#21](https://github.com/primer/eslint-plugin-primer-react/pull/21) [`5f68eb7`](https://github.com/primer/eslint-plugin-primer-react/commit/5f68eb7e222e2a41c527b5036b1308ff293e7a0d) Thanks [@SferaDev](https://github.com/SferaDev)! - `no-system-props`: Add `skipImportCheck` option
18
+
9
19
  ## 1.0.1
10
20
 
11
21
  ### Patch Changes
@@ -37,3 +37,10 @@ const App = () => (
37
37
  </PageLayout>
38
38
  )
39
39
  ```
40
+
41
+ ## Options
42
+
43
+ - `skipImportCheck` (default: `false`)
44
+
45
+ By default, the `direct-slot-children` rule will only check for direct slot children in components that are imported from `@primer/react`. You can disable this behavior by setting `skipImportCheck` to `true`. This is used for internal linting in the [primer/react](https://github.com/prime/react) repository.
46
+
@@ -1,4 +1,4 @@
1
- # Disallow use of styled-system props (no-system-colors)
1
+ # Disallow use of styled-system props (no-system-props)
2
2
 
3
3
  🔧 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.
4
4
 
@@ -41,6 +41,10 @@ import {Avatar} from 'some-other-library'
41
41
 
42
42
  ## Options
43
43
 
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
+
44
48
  - `includeUtilityComponents` (default: `false`)
45
49
 
46
50
  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`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "2.0.0-rc.9730bcf",
3
+ "version": "2.0.1-rc.a74c9bd",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -8,14 +8,15 @@ module.exports = {
8
8
  plugins: ['primer-react', 'github'],
9
9
  extends: ['plugin:github/react'],
10
10
  rules: {
11
+ 'primer-react/direct-slot-children': 'error',
11
12
  'primer-react/no-deprecated-colors': 'warn',
12
13
  'primer-react/no-system-props': 'warn'
13
14
  },
14
15
  settings: {
15
16
  github: {
16
17
  components: {
17
- Link: { props: { as: { undefined: 'a', 'a': 'a', 'button': 'button'}}},
18
- Button: { default: 'button' },
18
+ Link: {props: {as: {undefined: 'a', a: 'a', button: 'button'}}},
19
+ Button: {default: 'button'}
19
20
  }
20
21
  },
21
22
  'jsx-a11y': {
package/src/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  module.exports = {
2
2
  rules: {
3
+ 'direct-slot-children': require('./rules/direct-slot-children'),
3
4
  'no-deprecated-colors': require('./rules/no-deprecated-colors'),
4
5
  'no-system-props': require('./rules/no-system-props')
5
6
  },
@@ -15,7 +15,12 @@ ruleTester.run('direct-slot-children', rule, {
15
15
  valid: [
16
16
  `import {PageLayout} from '@primer/react'; <PageLayout><PageLayout.Header>Header</PageLayout.Header><PageLayout.Footer>Footer</PageLayout.Footer></PageLayout>`,
17
17
  `import {PageLayout} from '@primer/react'; <PageLayout><div><PageLayout.Pane>Header</PageLayout.Pane></div></PageLayout>`,
18
- `import {PageLayout} from 'some-library'; <PageLayout.Header>Header</PageLayout.Header>`
18
+ `import {PageLayout} from '@primer/react'; <PageLayout>{true ? <PageLayout.Header>Header</PageLayout.Header> : null}</PageLayout>`,
19
+ `import {PageLayout} from './PageLayout'; <PageLayout.Header>Header</PageLayout.Header>`,
20
+ {
21
+ code: `import {Foo} from './Foo'; <Foo><div><Foo.Bar></Foo.Bar></div></Foo>`,
22
+ options: [{skipImportCheck: true}]
23
+ }
19
24
  ],
20
25
  invalid: [
21
26
  {
@@ -62,6 +67,16 @@ ruleTester.run('direct-slot-children', rule, {
62
67
  data: {childName: 'TreeView.LeadingVisual', parentName: 'TreeView.Item'}
63
68
  }
64
69
  ]
70
+ },
71
+ {
72
+ code: `import {PageLayout} from './PageLayout'; <PageLayout><div><PageLayout.Header>Header</PageLayout.Header></div></PageLayout>`,
73
+ options: [{skipImportCheck: true}],
74
+ errors: [
75
+ {
76
+ messageId: 'directSlotChildren',
77
+ data: {childName: 'PageLayout.Header', parentName: 'PageLayout'}
78
+ }
79
+ ]
65
80
  }
66
81
  ]
67
82
  })
@@ -20,7 +20,7 @@ ruleTester.run('no-system-props', rule, {
20
20
  `import {ProgressBar} from '@primer/react'; <ProgressBar bg="howdy" />`,
21
21
  `import {Button} from '@primer/react'; <Button {...someExpression()} />`,
22
22
  `import {Button} from '@primer/react'; <Button variant="large" />`,
23
- `import {Button} from '@primer/react'; <Button size="large" />`,
23
+ `import {Button} from '@primer/react'; <Button size="large" />`
24
24
  ],
25
25
  invalid: [
26
26
  {
@@ -144,6 +144,28 @@ ruleTester.run('no-system-props', rule, {
144
144
  data: {propNames: 'width', componentName: 'Text'}
145
145
  }
146
146
  ]
147
+ },
148
+ {
149
+ code: `import {Button} from '../Button'; <Button width={200} />`,
150
+ options: [{skipImportCheck: true}],
151
+ output: `import {Button} from '../Button'; <Button sx={{width: 200}} />`,
152
+ errors: [
153
+ {
154
+ messageId: 'noSystemProps',
155
+ data: {propNames: 'width', componentName: 'Button'}
156
+ }
157
+ ]
158
+ },
159
+ {
160
+ code: `import {Foo} from '../Foo'; <Foo width={200} />`,
161
+ options: [{skipImportCheck: true}],
162
+ output: `import {Foo} from '../Foo'; <Foo sx={{width: 200}} />`,
163
+ errors: [
164
+ {
165
+ messageId: 'noSystemProps',
166
+ data: {propNames: 'width', componentName: 'Foo'}
167
+ }
168
+ ]
147
169
  }
148
170
  ]
149
171
  })
@@ -1,4 +1,5 @@
1
1
  const {isPrimerComponent} = require('../utils/is-primer-component')
2
+ const {last} = require('lodash')
2
3
 
3
4
  const slotParentToChildMap = {
4
5
  PageLayout: ['PageLayout.Header', 'PageLayout.Footer'],
@@ -20,24 +21,38 @@ const slotChildToParentMap = Object.entries(slotParentToChildMap).reduce((acc, [
20
21
  module.exports = {
21
22
  meta: {
22
23
  type: 'problem',
23
- schema: [],
24
+ schema: [
25
+ {
26
+ properties: {
27
+ skipImportCheck: {
28
+ type: 'boolean'
29
+ }
30
+ }
31
+ }
32
+ ],
24
33
  messages: {
25
34
  directSlotChildren: '{{childName}} must be a direct child of {{parentName}}.'
26
35
  }
27
36
  },
28
37
  create(context) {
38
+ const stack = []
29
39
  return {
30
40
  JSXOpeningElement(jsxNode) {
31
41
  const name = getJSXOpeningElementName(jsxNode)
32
42
 
43
+ // If `skipImportCheck` is true, this rule will check for direct slot children
44
+ // in any components (not just ones that are imported from `@primer/react`).
45
+ const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
46
+
33
47
  // If component is a Primer component and a slot child,
34
48
  // check if it's a direct child of the slot parent
35
- if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && slotChildToParentMap[name]) {
36
- const JSXElement = jsxNode.parent
37
- const parent = JSXElement.parent
38
-
49
+ if (
50
+ (skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) &&
51
+ slotChildToParentMap[name]
52
+ ) {
39
53
  const expectedParentName = slotChildToParentMap[name]
40
- if (parent.type !== 'JSXElement' || getJSXOpeningElementName(parent.openingElement) !== expectedParentName) {
54
+ const parent = last(stack)
55
+ if (parent !== expectedParentName) {
41
56
  context.report({
42
57
  node: jsxNode,
43
58
  messageId: 'directSlotChildren',
@@ -45,6 +60,13 @@ module.exports = {
45
60
  })
46
61
  }
47
62
  }
63
+
64
+ // Push the current element onto the stack
65
+ stack.push(name)
66
+ },
67
+ JSXClosingElement() {
68
+ // Pop the current element off the stack
69
+ stack.pop()
48
70
  }
49
71
  }
50
72
  }
@@ -13,7 +13,16 @@ const utilityComponents = new Set(['Box', 'Text'])
13
13
  // Components for which we allow a set of prop names
14
14
  const excludedComponentProps = new Map([
15
15
  ['AnchoredOverlay', new Set(['width', 'height'])],
16
+ ['Avatar', new Set(['size'])],
17
+ ['AvatarToken', new Set(['size'])],
18
+ ['CircleOcticon', new Set(['size'])],
16
19
  ['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'])],
17
26
  ['PageLayout', new Set(['padding'])],
18
27
  ['ProgressBar', new Set(['bg'])],
19
28
  ['PointerBox', new Set(['bg'])]
@@ -28,6 +37,9 @@ module.exports = {
28
37
  schema: [
29
38
  {
30
39
  properties: {
40
+ skipImportCheck: {
41
+ type: 'boolean'
42
+ },
31
43
  includeUtilityComponents: {
32
44
  type: 'boolean'
33
45
  }
@@ -39,6 +51,10 @@ module.exports = {
39
51
  }
40
52
  },
41
53
  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/react`).
56
+ const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
57
+
42
58
  const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false
43
59
 
44
60
  const excludedComponents = new Set([
@@ -48,7 +64,7 @@ module.exports = {
48
64
 
49
65
  return {
50
66
  JSXOpeningElement(jsxNode) {
51
- if (!isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
67
+ if (!skipImportCheck && !isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) return
52
68
  if (excludedComponents.has(jsxNode.name.name)) return
53
69
 
54
70
  // Create an object mapping from prop name to the AST node for that attribute
@@ -64,7 +80,7 @@ module.exports = {
64
80
  // Create an array of system prop attribute nodes
65
81
  let systemProps = Object.values(pick(propsByNameObject))
66
82
 
67
- let excludedProps = excludedComponentProps.has(jsxNode.name.name)
83
+ const excludedProps = excludedComponentProps.has(jsxNode.name.name)
68
84
  ? new Set([...alwaysExcludedProps, ...excludedComponentProps.get(jsxNode.name.name)])
69
85
  : alwaysExcludedProps
70
86
 
@@ -142,12 +158,12 @@ const excludeSxEntriesFromStyleMap = (stylesMap, sxProp) => {
142
158
  if (
143
159
  !sxProp.value ||
144
160
  sxProp.value.type !== 'JSXExpressionContainer' ||
145
- sxProp.value.expression.type != 'ObjectExpression'
161
+ sxProp.value.expression.type !== 'ObjectExpression'
146
162
  ) {
147
163
  return stylesMap
148
164
  }
149
165
  return new Map(
150
- [...stylesMap].filter(([key, _value]) => {
166
+ [...stylesMap].filter(([key]) => {
151
167
  return !some(sxProp.value.expression.properties, p => p.type === 'Property' && p.key.name === key)
152
168
  })
153
169
  )