1 | /**
|
---|
2 | * @fileoverview Flag shouldComponentUpdate when extending PureComponent
|
---|
3 | */
|
---|
4 |
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | const astUtil = require('../util/ast');
|
---|
8 | const componentUtil = require('../util/componentUtil');
|
---|
9 | const docsUrl = require('../util/docsUrl');
|
---|
10 | const report = require('../util/report');
|
---|
11 |
|
---|
12 | // ------------------------------------------------------------------------------
|
---|
13 | // Rule Definition
|
---|
14 | // ------------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | const messages = {
|
---|
17 | noShouldCompUpdate: '{{component}} does not need shouldComponentUpdate when extending React.PureComponent.',
|
---|
18 | };
|
---|
19 |
|
---|
20 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
21 | module.exports = {
|
---|
22 | meta: {
|
---|
23 | docs: {
|
---|
24 | description: 'Disallow usage of shouldComponentUpdate when extending React.PureComponent',
|
---|
25 | category: 'Possible Errors',
|
---|
26 | recommended: false,
|
---|
27 | url: docsUrl('no-redundant-should-component-update'),
|
---|
28 | },
|
---|
29 |
|
---|
30 | messages,
|
---|
31 |
|
---|
32 | schema: [],
|
---|
33 | },
|
---|
34 |
|
---|
35 | create(context) {
|
---|
36 | /**
|
---|
37 | * Checks for shouldComponentUpdate property
|
---|
38 | * @param {ASTNode} node The AST node being checked.
|
---|
39 | * @returns {Boolean} Whether or not the property exists.
|
---|
40 | */
|
---|
41 | function hasShouldComponentUpdate(node) {
|
---|
42 | const properties = astUtil.getComponentProperties(node);
|
---|
43 | return properties.some((property) => {
|
---|
44 | const name = astUtil.getPropertyName(property);
|
---|
45 | return name === 'shouldComponentUpdate';
|
---|
46 | });
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Get name of node if available
|
---|
51 | * @param {ASTNode} node The AST node being checked.
|
---|
52 | * @return {String} The name of the node
|
---|
53 | */
|
---|
54 | function getNodeName(node) {
|
---|
55 | if (node.id) {
|
---|
56 | return node.id.name;
|
---|
57 | }
|
---|
58 | if (node.parent && node.parent.id) {
|
---|
59 | return node.parent.id.name;
|
---|
60 | }
|
---|
61 | return '';
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Checks for violation of rule
|
---|
66 | * @param {ASTNode} node The AST node being checked.
|
---|
67 | */
|
---|
68 | function checkForViolation(node) {
|
---|
69 | if (componentUtil.isPureComponent(node, context)) {
|
---|
70 | const hasScu = hasShouldComponentUpdate(node);
|
---|
71 | if (hasScu) {
|
---|
72 | const className = getNodeName(node);
|
---|
73 | report(context, messages.noShouldCompUpdate, 'noShouldCompUpdate', {
|
---|
74 | node,
|
---|
75 | data: {
|
---|
76 | component: className,
|
---|
77 | },
|
---|
78 | });
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | return {
|
---|
84 | ClassDeclaration: checkForViolation,
|
---|
85 | ClassExpression: checkForViolation,
|
---|
86 | };
|
---|
87 | },
|
---|
88 | };
|
---|