From bedba5d3593bed5fad33e2dcd4702e9b9e4a3d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Nowak?= Date: Fri, 12 Apr 2019 21:32:54 +0200 Subject: [PATCH] Ignoring pure components without props and context usage --- docs/rules/prefer-stateless-function.md | 10 +++------- lib/rules/prefer-stateless-function.js | 2 +- tests/lib/rules/prefer-stateless-function.js | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/docs/rules/prefer-stateless-function.md b/docs/rules/prefer-stateless-function.md index 8d844ed69d..4d8be4cb19 100644 --- a/docs/rules/prefer-stateless-function.md +++ b/docs/rules/prefer-stateless-function.md @@ -66,7 +66,7 @@ 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 { @@ -74,14 +74,10 @@ class Foo extends React.PureComponent { return
{this.props.foo}
; } } -``` - -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
Bar
; + return
Baz
; } } ``` diff --git a/lib/rules/prefer-stateless-function.js b/lib/rules/prefer-stateless-function.js index 4550cb54d1..1dd6e63204 100644 --- a/lib/rules/prefer-stateless-function.js +++ b/lib/rules/prefer-stateless-function.js @@ -369,7 +369,7 @@ module.exports = { return; } - if (list[component].hasSCU && list[component].usePropsOrContext) { + if (list[component].hasSCU) { return; } context.report({ diff --git a/tests/lib/rules/prefer-stateless-function.js b/tests/lib/rules/prefer-stateless-function.js index 24ad7c3496..22f6b8fe98 100644 --- a/tests/lib/rules/prefer-stateless-function.js +++ b/tests/lib/rules/prefer-stateless-function.js @@ -304,6 +304,18 @@ ruleTester.run('prefer-stateless-function', rule, { } `, parser: 'babel-eslint' + }, + { + code: ` + class Child extends PureComponent { + render() { + return

I don't

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