|
1 | 1 | const {isPrimerComponent} = require('../utils/is-primer-component')
|
2 |
| -const {last} = require('lodash') |
3 | 2 |
|
4 | 3 | const slotParentToChildMap = {
|
5 | 4 | PageLayout: ['PageLayout.Header', 'PageLayout.Footer'],
|
@@ -27,31 +26,25 @@ module.exports = {
|
27 | 26 | }
|
28 | 27 | },
|
29 | 28 | create(context) {
|
30 |
| - const stack = [] |
31 | 29 | return {
|
32 | 30 | JSXOpeningElement(jsxNode) {
|
33 | 31 | const name = getJSXOpeningElementName(jsxNode)
|
34 | 32 |
|
35 | 33 | // If component is a Primer component and a slot child,
|
36 | 34 | // check if it's a direct child of the slot parent
|
37 | 35 | 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) { |
41 | 41 | context.report({
|
42 | 42 | node: jsxNode,
|
43 | 43 | messageId: 'directSlotChildren',
|
44 |
| - data: {childName: name, parentName} |
| 44 | + data: {childName: name, parentName: expectedParentName} |
45 | 45 | })
|
46 | 46 | }
|
47 | 47 | }
|
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() |
55 | 48 | }
|
56 | 49 | }
|
57 | 50 | }
|
|
0 commit comments