Skip to content

Commit

Permalink
Merge pull request #2238 from pawelnvk/pure-function-validation
Browse files Browse the repository at this point in the history
[Fix] `prefer-stateless-function`: Ignoring pure components without props and context usage
  • Loading branch information
ljharb committed Apr 12, 2019
2 parents 157332f + bedba5d commit f0c0b4d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
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

0 comments on commit f0c0b4d

Please sign in to comment.