UNPKG

1.35 kBJavaScriptView Raw
1/**
2 * @fileoverview Report "this" being used in stateless functional components.
3 */
4
5'use strict';
6
7const Components = require('../util/Components');
8const docsUrl = require('../util/docsUrl');
9
10// ------------------------------------------------------------------------------
11// Constants
12// ------------------------------------------------------------------------------
13
14const ERROR_MESSAGE = 'Stateless functional components should not use `this`';
15
16// ------------------------------------------------------------------------------
17// Rule Definition
18// ------------------------------------------------------------------------------
19
20module.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};