Skip to content

Commit 4e8e86a

Browse files
committed
Fix ignorePureComponents when using class expressions.
1 parent 9bb68c1 commit 4e8e86a

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/rules/prefer-stateless-function.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,15 @@ module.exports = {
286286
});
287287
}
288288

289+
function visitClass(node) {
290+
if (ignorePureComponents && utils.isPureComponent(node)) {
291+
markSCUAsDeclared(node);
292+
}
293+
}
294+
289295
return {
290-
ClassDeclaration: function (node) {
291-
if (ignorePureComponents && utils.isPureComponent(node)) {
292-
markSCUAsDeclared(node);
293-
}
294-
},
296+
ClassDeclaration: visitClass,
297+
ClassExpression: visitClass,
295298

296299
// Mark `this` destructuring as a usage of `this`
297300
VariableDeclarator: function(node) {
@@ -386,7 +389,6 @@ module.exports = {
386389
if (list[component].hasSCU && list[component].usePropsOrContext) {
387390
continue;
388391
}
389-
390392
context.report({
391393
node: list[component].node,
392394
message: 'Component should be written as a pure function'

tests/lib/rules/prefer-stateless-function.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ ruleTester.run('prefer-stateless-function', rule, {
6464
options: [{
6565
ignorePureComponents: true
6666
}]
67+
}, {
68+
// Extends from PureComponent in an expression context.
69+
code: [
70+
'const Foo = class extends React.PureComponent {',
71+
' render() {',
72+
' return <div>{this.props.foo}</div>;',
73+
' }',
74+
'};'
75+
].join('\n'),
76+
parserOptions: parserOptions,
77+
options: [{
78+
ignorePureComponents: true
79+
}]
6780
}, {
6881
// Has a lifecyle method
6982
code: [

0 commit comments

Comments
 (0)