eslint-plugin-primer-react 2.0.0 → 2.0.1

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,5 +1,11 @@
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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,6 +15,7 @@ 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 '@primer/react'; <PageLayout>{true ? <PageLayout.Header>Header</PageLayout.Header> : null}</PageLayout>`,
18
19
  `import {PageLayout} from './PageLayout'; <PageLayout.Header>Header</PageLayout.Header>`,
19
20
  {
20
21
  code: `import {Foo} from './Foo'; <Foo><div><Foo.Bar></Foo.Bar></div></Foo>`,
@@ -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'],
@@ -34,6 +35,7 @@ module.exports = {
34
35
  }
35
36
  },
36
37
  create(context) {
38
+ const stack = []
37
39
  return {
38
40
  JSXOpeningElement(jsxNode) {
39
41
  const name = getJSXOpeningElementName(jsxNode)
@@ -48,11 +50,9 @@ module.exports = {
48
50
  (skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) &&
49
51
  slotChildToParentMap[name]
50
52
  ) {
51
- const JSXElement = jsxNode.parent
52
- const parent = JSXElement.parent
53
-
54
53
  const expectedParentName = slotChildToParentMap[name]
55
- if (parent.type !== 'JSXElement' || getJSXOpeningElementName(parent.openingElement) !== expectedParentName) {
54
+ const parent = last(stack)
55
+ if (parent !== expectedParentName) {
56
56
  context.report({
57
57
  node: jsxNode,
58
58
  messageId: 'directSlotChildren',
@@ -60,6 +60,13 @@ module.exports = {
60
60
  })
61
61
  }
62
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()
63
70
  }
64
71
  }
65
72
  }