Skip to content

Commit f0c0b4d

Browse files
authored
Merge pull request jsx-eslint#2238 from pawelnvk/pure-function-validation
[Fix] `prefer-stateless-function`: Ignoring pure components without props and context usage
2 parents 157332f + bedba5d commit f0c0b4d

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

docs/rules/prefer-stateless-function.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,18 @@ class Foo extends React.Component {
6666

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

69-
The following pattern is considered okay and does **not** cause warnings:
69+
The following patterns are considered okay and does **not** cause warnings:
7070

7171
```jsx
7272
class Foo extends React.PureComponent {
7373
render() {
7474
return <div>{this.props.foo}</div>;
7575
}
7676
}
77-
```
78-
79-
The following pattern is considered a warning because it's not using props or context:
8077

81-
```jsx
82-
class Foo extends React.PureComponent {
78+
class Bar extends React.PureComponent {
8379
render() {
84-
return <div>Bar</div>;
80+
return <div>Baz</div>;
8581
}
8682
}
8783
```

lib/rules/prefer-stateless-function.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ module.exports = {
369369
return;
370370
}
371371

372-
if (list[component].hasSCU && list[component].usePropsOrContext) {
372+
if (list[component].hasSCU) {
373373
return;
374374
}
375375
context.report({

tests/lib/rules/prefer-stateless-function.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ ruleTester.run('prefer-stateless-function', rule, {
304304
}
305305
`,
306306
parser: 'babel-eslint'
307+
},
308+
{
309+
code: `
310+
class Child extends PureComponent {
311+
render() {
312+
return <h1>I don't</h1>;
313+
}
314+
}
315+
`,
316+
options: [{
317+
ignorePureComponents: true
318+
}]
307319
}
308320
],
309321

@@ -339,9 +351,6 @@ ruleTester.run('prefer-stateless-function', rule, {
339351
}
340352
}
341353
`,
342-
options: [{
343-
ignorePureComponents: true
344-
}],
345354
errors: [{
346355
message: 'Component should be written as a pure function'
347356
}]

0 commit comments

Comments
 (0)