Skip to content
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

[Fix] Ignoring pure components without props and context usage #2238

Merged
merged 1 commit into from Apr 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions docs/rules/prefer-stateless-function.md
Expand Up @@ -66,22 +66,18 @@ class Foo extends React.Component {

When `true` the rule will ignore Components extending from `React.PureComponent` that use `this.props` or `this.context`.

The following pattern is considered okay and does **not** cause warnings:
The following patterns are considered okay and does **not** cause warnings:

```jsx
class Foo extends React.PureComponent {
render() {
return <div>{this.props.foo}</div>;
}
}
```

The following pattern is considered a warning because it's not using props or context:

```jsx
class Foo extends React.PureComponent {
class Bar extends React.PureComponent {
render() {
return <div>Bar</div>;
return <div>Baz</div>;
}
}
```
2 changes: 1 addition & 1 deletion lib/rules/prefer-stateless-function.js
Expand Up @@ -369,7 +369,7 @@ module.exports = {
return;
}

if (list[component].hasSCU && list[component].usePropsOrContext) {
if (list[component].hasSCU) {
return;
}
context.report({
Expand Down
15 changes: 12 additions & 3 deletions tests/lib/rules/prefer-stateless-function.js
Expand Up @@ -304,6 +304,18 @@ ruleTester.run('prefer-stateless-function', rule, {
}
`,
parser: 'babel-eslint'
},
{
code: `
class Child extends PureComponent {
render() {
return <h1>I don't</h1>;
}
}
`,
options: [{
ignorePureComponents: true
}]
}
],

Expand Down Expand Up @@ -339,9 +351,6 @@ ruleTester.run('prefer-stateless-function', rule, {
}
}
`,
options: [{
ignorePureComponents: true
}],
errors: [{
message: 'Component should be written as a pure function'
}]
Expand Down