1 | const {polarisComponentFromJSX, docsUrl} = require('../utilities');
|
2 |
|
3 | const COMPONENTS_WITH_SECTIONED_PROP = ['Popover', 'Card', 'Layout'];
|
4 |
|
5 | module.exports = {
|
6 | meta: {
|
7 | docs: {
|
8 | description:
|
9 | 'Prefer the use of the `sectioned` props in Polaris components instead of wrapping all contents in a `Section` component.',
|
10 | category: 'Best Practices',
|
11 | recommended: true,
|
12 | uri: docsUrl('polaris-prefer-sectioned-prop'),
|
13 | },
|
14 | schema: [],
|
15 | },
|
16 |
|
17 | create(context) {
|
18 | return {
|
19 | JSXElement(node) {
|
20 | const {children} = node;
|
21 | const component = polarisComponentFromJSX(node, context);
|
22 | if (
|
23 | !component ||
|
24 | !COMPONENTS_WITH_SECTIONED_PROP.includes(component) ||
|
25 | children.length === 0 ||
|
26 | children.length > 1 ||
|
27 | children[0].type !== 'JSXElement' ||
|
28 | children[0].openingElement.attributes.length > 0
|
29 | ) {
|
30 | return;
|
31 | }
|
32 |
|
33 | const child = polarisComponentFromJSX(node.children[0], context);
|
34 |
|
35 | if (child === `${component}.Section`) {
|
36 | context.report(
|
37 | node,
|
38 | `Use the \`sectioned\` prop on ${component} instead of wrapping all its contents in a ${child}`,
|
39 | );
|
40 | }
|
41 | },
|
42 | };
|
43 | },
|
44 | };
|