Skip to content

Commit

Permalink
Ignoring pure components without props and context usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelnvk committed Apr 12, 2019
1 parent 35de81b commit bedba5d
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 bedba5d

Please sign in to comment.