Skip to content

Commit 57e2d72

Browse files
jenil94ljharb
authored andcommitted
[fix] require-optimization: fix when using arrow function in class components
1 parent 7bc55cc commit 57e2d72

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/rules/require-optimization.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ module.exports = {
166166

167167
return {
168168
ArrowFunctionExpression(node) {
169+
// Skip if the function is declared in the class
170+
if (isFunctionInClass()) {
171+
return;
172+
}
169173
// Stateless Functional Components cannot be optimized (yet)
170174
markSCUAsDeclared(node);
171175
},

tests/lib/rules/require-optimization.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ ruleTester.run('react-require-optimization', rule, {
118118
code: `
119119
const obj = { prop: [,,,,,] }
120120
`
121+
}, {
122+
code: `
123+
import React from "react";
124+
class YourComponent extends React.Component {
125+
handleClick = () => {}
126+
shouldComponentUpdate(){
127+
return true;
128+
}
129+
render() {
130+
return <div onClick={this.handleClick}>123</div>
131+
}
132+
}
133+
`,
134+
parser: parsers.BABEL_ESLINT,
135+
errors: [{
136+
message: MESSAGE
137+
}]
121138
}],
122139

123140
invalid: [{
@@ -142,6 +159,20 @@ ruleTester.run('react-require-optimization', rule, {
142159
errors: [{
143160
message: MESSAGE
144161
}]
162+
}, {
163+
code: `
164+
import React from "react";
165+
class YourComponent extends React.Component {
166+
handleClick = () => {}
167+
render() {
168+
return <div onClick={this.handleClick}>123</div>
169+
}
170+
}
171+
`,
172+
parser: parsers.BABEL_ESLINT,
173+
errors: [{
174+
message: MESSAGE
175+
}]
145176
}, {
146177
code: `
147178
import React, {Component} from "react";

0 commit comments

Comments
 (0)