Skip to content

Commit 96541fe

Browse files
committed
Use stack to handle new test case
1 parent 52a5adb commit 96541fe

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/rules/__tests__/direct-slot-children.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ruleTester.run('direct-slot-children', rule, {
1515
valid: [
1616
`import {PageLayout} from '@primer/react'; <PageLayout><PageLayout.Header>Header</PageLayout.Header><PageLayout.Footer>Footer</PageLayout.Footer></PageLayout>`,
1717
`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>`,
1819
`import {PageLayout} from './PageLayout'; <PageLayout.Header>Header</PageLayout.Header>`,
1920
{
2021
code: `import {Foo} from './Foo'; <Foo><div><Foo.Bar></Foo.Bar></div></Foo>`,

src/rules/direct-slot-children.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {isPrimerComponent} = require('../utils/is-primer-component')
2+
const {last} = require('lodash')
23

34
const slotParentToChildMap = {
45
PageLayout: ['PageLayout.Header', 'PageLayout.Footer'],
@@ -34,6 +35,7 @@ module.exports = {
3435
}
3536
},
3637
create(context) {
38+
const stack = []
3739
return {
3840
JSXOpeningElement(jsxNode) {
3941
const name = getJSXOpeningElementName(jsxNode)
@@ -48,18 +50,23 @@ module.exports = {
4850
(skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) &&
4951
slotChildToParentMap[name]
5052
) {
51-
const JSXElement = jsxNode.parent
52-
const parent = JSXElement.parent
53-
5453
const expectedParentName = slotChildToParentMap[name]
55-
if (parent.type !== 'JSXElement' || getJSXOpeningElementName(parent.openingElement) !== expectedParentName) {
54+
const parent = last(stack)
55+
if (parent !== expectedParentName) {
5656
context.report({
5757
node: jsxNode,
5858
messageId: 'directSlotChildren',
5959
data: {childName: name, parentName: expectedParentName}
6060
})
6161
}
6262
}
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()
6370
}
6471
}
6572
}

0 commit comments

Comments
 (0)