Skip to content

Commit 32097eb

Browse files
committed
Add skipImportCheck option to direct-slot-children
1 parent ec9a5d0 commit 32097eb

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

docs/rules/direct-slot-children.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,9 @@ const App = () => (
3737
</PageLayout>
3838
)
3939
```
40+
41+
## Options
42+
43+
- `skipImportCheck` (default: `false`)
44+
45+
By default, the `direct-children` rule will only check for direct slot children in components that are imported from `@primer/react`. You can disable this behavior by setting `skipImportCheck` to `true`. This is used for internal linting in the [primer/react](https://github.com/prime/react) repository.

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ 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 'some-library'; <PageLayout.Header>Header</PageLayout.Header>`
18+
`import {PageLayout} from './PageLayout'; <PageLayout.Header>Header</PageLayout.Header>`,
19+
{
20+
code: `import {Foo} from './Foo'; <Foo><div><Foo.Bar></Foo.Bar></div></Foo>`,
21+
options: [{skipImportCheck: true}]
22+
}
1923
],
2024
invalid: [
2125
{
@@ -62,6 +66,16 @@ ruleTester.run('direct-slot-children', rule, {
6266
data: {childName: 'TreeView.LeadingVisual', parentName: 'TreeView.Item'}
6367
}
6468
]
69+
},
70+
{
71+
code: `import {PageLayout} from './PageLayout'; <PageLayout><div><PageLayout.Header>Header</PageLayout.Header></div></PageLayout>`,
72+
options: [{skipImportCheck: true}],
73+
errors: [
74+
{
75+
messageId: 'directSlotChildren',
76+
data: {childName: 'PageLayout.Header', parentName: 'PageLayout'}
77+
}
78+
]
6579
}
6680
]
6781
})

src/rules/direct-slot-children.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ const slotChildToParentMap = Object.entries(slotParentToChildMap).reduce((acc, [
2020
module.exports = {
2121
meta: {
2222
type: 'problem',
23-
schema: [],
23+
schema: [
24+
{
25+
properties: {
26+
skipImportCheck: {
27+
type: 'boolean'
28+
}
29+
}
30+
}
31+
],
2432
messages: {
2533
directSlotChildren: '{{childName}} must be a direct child of {{parentName}}.'
2634
}
@@ -30,9 +38,16 @@ module.exports = {
3038
JSXOpeningElement(jsxNode) {
3139
const name = getJSXOpeningElementName(jsxNode)
3240

41+
// If `skipImportCheck` is true, this rule will check for direct slot children
42+
// in any components (not just ones that are imported from `@primer/react`).
43+
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
44+
3345
// If component is a Primer component and a slot child,
3446
// check if it's a direct child of the slot parent
35-
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && slotChildToParentMap[name]) {
47+
if (
48+
(skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) &&
49+
slotChildToParentMap[name]
50+
) {
3651
const JSXElement = jsxNode.parent
3752
const parent = JSXElement.parent
3853

src/rules/no-system-props.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {
5252
},
5353
create(context) {
5454
// If `skipImportCheck` is true, this rule will check for deprecated styled system props
55-
// used in any components (not just ones that are imported from `@primer/components`).
55+
// used in any components (not just ones that are imported from `@primer/react`).
5656
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
5757

5858
const includeUtilityComponents = context.options[0] ? context.options[0].includeUtilityComponents : false

0 commit comments

Comments
 (0)