1 | const Components = require('eslint-plugin-react/lib/util/Components');
|
2 |
|
3 | const {docsUrl} = require('../utilities');
|
4 |
|
5 | const message = [
|
6 | 'Don’t use multiple render methods in a single component;',
|
7 | 'they generally make your component harder to read.',
|
8 | 'Instead break {{name}} out into its own component',
|
9 | 'and render it inside this one.',
|
10 | ].join(' ');
|
11 |
|
12 | module.exports = {
|
13 | meta: {
|
14 | docs: {
|
15 | description:
|
16 | 'Disallow multiple render methods in React component classes',
|
17 | category: 'Best Practices',
|
18 | recommended: true,
|
19 | uri: docsUrl('react-no-multiple-render-methods'),
|
20 | },
|
21 | },
|
22 |
|
23 | create: Components.detect((context, components, utils) => {
|
24 | let isES6Component = false;
|
25 |
|
26 | function report(node) {
|
27 | const {
|
28 | key: {name},
|
29 | } = node;
|
30 |
|
31 | context.report({
|
32 | node,
|
33 | message,
|
34 | data: {name},
|
35 | });
|
36 | }
|
37 |
|
38 | return {
|
39 | ClassDeclaration(node) {
|
40 | isES6Component = utils.isES6Component(node);
|
41 | },
|
42 |
|
43 | MethodDefinition(node) {
|
44 | if (!isES6Component || isRenderMethod(node)) {
|
45 | return;
|
46 | }
|
47 |
|
48 | report(node);
|
49 | },
|
50 | };
|
51 | }),
|
52 | };
|
53 |
|
54 | function isRenderMethod({key: {name}}) {
|
55 | return !name.match(/^render[a-zA-Z0-9]+/i);
|
56 | }
|