Skip to content

Commit 127a33c

Browse files
committed
Add support for multiple accepted parents
1 parent b263cc9 commit 127a33c

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ ruleTester.run('direct-slot-children', rule, {
2020
`import {FormControl, Radio} from '@primer/react'; <FormControl><Radio value="one" /><FormControl.Label>Choice one</FormControl.Label></FormControl>`,
2121
`import {ActionList} from '@primer/react';
2222
<ActionList.Item><ActionList.LeadingVisual> <Avatar src="https://github.com/mona.png" /></ActionList.LeadingVisual>mona<ActionList.Description>Monalisa Octocat</ActionList.Description></ActionList.Item>`,
23+
`import {ActionList} from '@primer/react';
24+
<ActionList.LinkItem><ActionList.LeadingVisual></ActionList.LeadingVisual>mona<ActionList.Description>Monalisa Octocat</ActionList.Description></ActionList.LinkItem>`,
2325
{code: `import {Foo} from './Foo'; <Foo><div><Foo.Bar></Foo.Bar></div></Foo>`, options: [{skipImportCheck: true}]}
2426
],
2527
invalid: [
@@ -86,6 +88,15 @@ ruleTester.run('direct-slot-children', rule, {
8688
data: {childName: 'PageLayout.Header', parentName: 'PageLayout'}
8789
}
8890
]
91+
},
92+
{
93+
code: `import {ActionList} from '@primer/react'; <ActionList.Item><div><ActionList.LeadingVisual>Visual</ActionList.LeadingVisual></div></ActionList.Item>`,
94+
errors: [
95+
{
96+
messageId: 'directSlotChildren',
97+
data: {childName: 'ActionList.LeadingVisual', parentName: 'ActionList.Item or ActionList.LinkItem'}
98+
}
99+
]
89100
}
90101
]
91102
})

src/rules/direct-slot-children.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const slotParentToChildMap = {
66
SplitPageLayout: ['SplitPageLayout.Header', 'SplitPageLayout.Footer'],
77
FormControl: ['FormControl.Label', 'FormControl.Caption', 'FormControl.LeadingVisual', 'FormControl.TrailingVisual'],
88
'ActionList.Item': ['ActionList.LeadingVisual', 'ActionList.TrailingVisual', 'ActionList.Description'],
9+
'ActionList.LinkItem': ['ActionList.LeadingVisual', 'ActionList.TrailingVisual', 'ActionList.Description'],
910
'NavList.Item': ['NavList.LeadingVisual', 'NavList.TrailingVisual'],
1011
'TreeView.Item': ['TreeView.LeadingVisual', 'TreeView.TrailingVisual'],
1112
RadioGroup: ['RadioGroup.Label', 'RadioGroup.Caption', 'RadioGroup.Validation'],
@@ -16,7 +17,11 @@ const slotParentToChildMap = {
1617

1718
const slotChildToParentMap = Object.entries(slotParentToChildMap).reduce((acc, [parent, children]) => {
1819
for (const child of children) {
19-
acc[child] = parent
20+
if (acc[child]) {
21+
acc[child].push(parent)
22+
} else {
23+
acc[child] = [parent]
24+
}
2025
}
2126
return acc
2227
}, {})
@@ -53,13 +58,16 @@ module.exports = {
5358
(skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) &&
5459
slotChildToParentMap[name]
5560
) {
56-
const expectedParentName = slotChildToParentMap[name]
61+
const expectedParentNames = slotChildToParentMap[name]
5762
const parent = last(stack)
58-
if (parent !== expectedParentName) {
63+
if (!expectedParentNames.includes(parent)) {
5964
context.report({
6065
node: jsxNode,
6166
messageId: 'directSlotChildren',
62-
data: {childName: name, parentName: expectedParentName}
67+
data: {
68+
childName: name,
69+
parentName: expectedParentNames.length > 1 ? expectedParentNames.join(' or ') : expectedParentNames[0]
70+
}
6371
})
6472
}
6573
}

0 commit comments

Comments
 (0)