Skip to content

Commit 0dd4685

Browse files
authored
Merge pull request #1122 from dreid/allow-wrapped-pure-components
Fix ignorePureComponents when using class expressions.
2 parents 7ca9841 + 0fac8d9 commit 0dd4685

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

lib/rules/prefer-stateless-function.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,19 @@ module.exports = {
296296
});
297297
}
298298

299-
return {
300-
ClassDeclaration: function (node) {
301-
if (ignorePureComponents && utils.isPureComponent(node)) {
302-
markSCUAsDeclared(node);
303-
}
299+
function visitClass(node) {
300+
if (ignorePureComponents && utils.isPureComponent(node)) {
301+
markSCUAsDeclared(node);
302+
}
304303

305-
if (node.decorators && node.decorators.length) {
306-
markDecoratorsAsUsed(node);
307-
}
308-
},
304+
if (node.decorators && node.decorators.length) {
305+
markDecoratorsAsUsed(node);
306+
}
307+
}
308+
309+
return {
310+
ClassDeclaration: visitClass,
311+
ClassExpression: visitClass,
309312

310313
// Mark `this` destructuring as a usage of `this`
311314
VariableDeclarator: function(node) {
@@ -401,7 +404,6 @@ module.exports = {
401404
if (list[component].hasSCU && list[component].usePropsOrContext) {
402405
continue;
403406
}
404-
405407
context.report({
406408
node: list[component].node,
407409
message: 'Component should be written as a pure function'

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

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ ruleTester.run('prefer-stateless-function', rule, {
6262
options: [{
6363
ignorePureComponents: true
6464
}]
65+
}, {
66+
// Extends from PureComponent in an expression context.
67+
code: [
68+
'const Foo = class extends React.PureComponent {',
69+
' render() {',
70+
' return <div>{this.props.foo}</div>;',
71+
' }',
72+
'};'
73+
].join('\n'),
74+
parserOptions: parserOptions,
75+
options: [{
76+
ignorePureComponents: true
77+
}]
6578
}, {
6679
// Has a lifecyle method
6780
code: [

0 commit comments

Comments
 (0)