Skip to content

Commit

Permalink
Merge pull request #1122 from dreid/allow-wrapped-pure-components
Browse files Browse the repository at this point in the history
Fix ignorePureComponents when using class expressions.
  • Loading branch information
yannickcr committed May 15, 2017
2 parents 7ca9841 + 0fac8d9 commit 0dd4685
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/rules/prefer-stateless-function.js
Expand Up @@ -296,16 +296,19 @@ module.exports = {
});
}

return {
ClassDeclaration: function (node) {
if (ignorePureComponents && utils.isPureComponent(node)) {
markSCUAsDeclared(node);
}
function visitClass(node) {
if (ignorePureComponents && utils.isPureComponent(node)) {
markSCUAsDeclared(node);
}

if (node.decorators && node.decorators.length) {
markDecoratorsAsUsed(node);
}
},
if (node.decorators && node.decorators.length) {
markDecoratorsAsUsed(node);
}
}

return {
ClassDeclaration: visitClass,
ClassExpression: visitClass,

// Mark `this` destructuring as a usage of `this`
VariableDeclarator: function(node) {
Expand Down Expand Up @@ -401,7 +404,6 @@ module.exports = {
if (list[component].hasSCU && list[component].usePropsOrContext) {
continue;
}

context.report({
node: list[component].node,
message: 'Component should be written as a pure function'
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/prefer-stateless-function.js
Expand Up @@ -62,6 +62,19 @@ ruleTester.run('prefer-stateless-function', rule, {
options: [{
ignorePureComponents: true
}]
}, {
// Extends from PureComponent in an expression context.
code: [
'const Foo = class extends React.PureComponent {',
' render() {',
' return <div>{this.props.foo}</div>;',
' }',
'};'
].join('\n'),
parserOptions: parserOptions,
options: [{
ignorePureComponents: true
}]
}, {
// Has a lifecyle method
code: [
Expand Down

0 comments on commit 0dd4685

Please sign in to comment.