From 5783f5db473ecfabb640a29896b3dbf185bee1af Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 7 Oct 2022 20:21:37 -0700 Subject: [PATCH] [Fix] `static-property-placement`: warn on nonstatic expected-statics Fixes #2581 --- CHANGELOG.md | 2 ++ lib/rules/static-property-placement.js | 8 +++++++- tests/lib/rules/static-property-placement.js | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75b94cc39d..ff07225b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange * [`no-arrow-function-lifecycle`]: when converting from an arrow, remove the semi and wrapping parens ([#3337][] @ljharb) * [`jsx-key`]: Ignore elements inside `React.Children.toArray()` ([#1591][] @silvenon) * [`jsx-no-constructed-context-values`]: fix false positive for usage in non-components ([#3448][] @golopot) +* [`static-property-placement`]: warn on nonstatic expected-statics ([#2581][] @ljharb) ### Changed * [Docs] [`no-unknown-property`]: fix typo in link ([#3445][] @denkristoffer) @@ -22,6 +23,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange [#3444]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3444 [#3436]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3436 [#3337]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3337 +[#2581]: https://github.com/jsx-eslint/eslint-plugin-react/issues/2581 [#1591]: https://github.com/jsx-eslint/eslint-plugin-react/pull/1591 ## [7.31.8] - 2022.09.08 diff --git a/lib/rules/static-property-placement.js b/lib/rules/static-property-placement.js index 27805b8ba7..59f559121b 100644 --- a/lib/rules/static-property-placement.js +++ b/lib/rules/static-property-placement.js @@ -128,7 +128,13 @@ module.exports = { }); // If name is set but the configured rule does not match expected then report error - if (name && config[name] !== expectedRule) { + if ( + name + && ( + config[name] !== expectedRule + || (!node.static && (config[name] === STATIC_PUBLIC_FIELD || config[name] === STATIC_GETTER)) + ) + ) { const messageId = ERROR_MESSAGES[config[name]]; report(context, messages[messageId], messageId, { node, diff --git a/tests/lib/rules/static-property-placement.js b/tests/lib/rules/static-property-placement.js index 4649fb6956..084881b318 100644 --- a/tests/lib/rules/static-property-placement.js +++ b/tests/lib/rules/static-property-placement.js @@ -2186,5 +2186,20 @@ ruleTester.run('static-property-placement', rule, { }, ], }, + { + code: ` + class MyComponent extends React.Component { + displayName = 'Foo'; + } + `, + features: ['class fields'], + options: [STATIC_PUBLIC_FIELD], + errors: [ + { + messageId: 'notStaticClassProp', + data: { name: 'displayName' }, + }, + ], + }, ]), });