Skip to content

Fix ignorePureComponents when using class expressions. #1122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions lib/rules/prefer-stateless-function.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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