1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const Components = require('../util/Components');
|
9 | const docsUrl = require('../util/docsUrl');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = {
|
16 | meta: {
|
17 | docs: {
|
18 | description: 'Prevent multiple component definition per file',
|
19 | category: 'Stylistic Issues',
|
20 | recommended: false,
|
21 | url: docsUrl('no-multi-comp')
|
22 | },
|
23 |
|
24 | schema: [{
|
25 | type: 'object',
|
26 | properties: {
|
27 | ignoreStateless: {
|
28 | default: false,
|
29 | type: 'boolean'
|
30 | }
|
31 | },
|
32 | additionalProperties: false
|
33 | }]
|
34 | },
|
35 |
|
36 | create: Components.detect((context, components, utils) => {
|
37 | const configuration = context.options[0] || {};
|
38 | const ignoreStateless = configuration.ignoreStateless || false;
|
39 |
|
40 | const MULTI_COMP_MESSAGE = 'Declare only one React component per file';
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | function isIgnored(component) {
|
48 | return (
|
49 | ignoreStateless && (
|
50 | /Function/.test(component.node.type)
|
51 | || utils.isPragmaComponentWrapper(component.node)
|
52 | )
|
53 | );
|
54 | }
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | return {
|
61 | 'Program:exit'() {
|
62 | if (components.length() <= 1) {
|
63 | return;
|
64 | }
|
65 |
|
66 | const list = components.list();
|
67 |
|
68 | Object.keys(list).filter((component) => !isIgnored(list[component])).forEach((component, i) => {
|
69 | if (i >= 1) {
|
70 | context.report({
|
71 | node: list[component].node,
|
72 | message: MULTI_COMP_MESSAGE
|
73 | });
|
74 | }
|
75 | });
|
76 | }
|
77 | };
|
78 | })
|
79 | };
|