1 |
|
2 |
|
3 |
|
4 |
|
5 | 'use strict';
|
6 |
|
7 | const Components = require('../util/Components');
|
8 | const docsUrl = require('../util/docsUrl');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const ERROR_MESSAGE = 'Stateless functional components should not use `this`';
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | module.exports = {
|
21 | meta: {
|
22 | docs: {
|
23 | description: 'Report "this" being used in stateless components',
|
24 | category: 'Possible Errors',
|
25 | recommended: false,
|
26 | url: docsUrl('no-this-in-sfc')
|
27 | },
|
28 | schema: []
|
29 | },
|
30 |
|
31 | create: Components.detect((context, components, utils) => ({
|
32 | MemberExpression(node) {
|
33 | if (node.object.type === 'ThisExpression') {
|
34 | const component = components.get(utils.getParentStatelessComponent());
|
35 | if (!component || component.node && component.node.parent && component.node.parent.type === 'Property') {
|
36 | return;
|
37 | }
|
38 | context.report({
|
39 | node,
|
40 | message: ERROR_MESSAGE
|
41 | });
|
42 | }
|
43 | }
|
44 | }))
|
45 | };
|