Skip to content

Commit 0bda0ed

Browse files
committed
use node.parent instead
1 parent 96a675d commit 0bda0ed

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/rules/direct-slot-children.js

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

43
const slotParentToChildMap = {
54
PageLayout: ['PageLayout.Header', 'PageLayout.Footer'],
@@ -27,31 +26,25 @@ module.exports = {
2726
}
2827
},
2928
create(context) {
30-
const stack = []
3129
return {
3230
JSXOpeningElement(jsxNode) {
3331
const name = getJSXOpeningElementName(jsxNode)
3432

3533
// If component is a Primer component and a slot child,
3634
// check if it's a direct child of the slot parent
3735
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && slotChildToParentMap[name]) {
38-
const parentName = slotChildToParentMap[name]
39-
const parent = last(stack)
40-
if (parent !== parentName) {
36+
const JSXElement = jsxNode.parent
37+
const parent = JSXElement.parent
38+
39+
const expectedParentName = slotChildToParentMap[name]
40+
if (parent.type !== 'JSXElement' || getJSXOpeningElementName(parent.openingElement) !== expectedParentName) {
4141
context.report({
4242
node: jsxNode,
4343
messageId: 'directSlotChildren',
44-
data: {childName: name, parentName}
44+
data: {childName: name, parentName: expectedParentName}
4545
})
4646
}
4747
}
48-
49-
// Push the current element onto the stack
50-
stack.push(name)
51-
},
52-
JSXClosingElement() {
53-
// Pop the current element off the stack
54-
stack.pop()
5548
}
5649
}
5750
}

0 commit comments

Comments
 (0)