Skip to content

Commit

Permalink
[Fix] static-property-placement: warn on nonstatic expected-statics
Browse files Browse the repository at this point in the history
Fixes #2581
  • Loading branch information
ljharb committed Oct 8, 2022
1 parent 78ad0f0 commit 5783f5d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/rules/static-property-placement.js
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/static-property-placement.js
Expand Up @@ -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' },
},
],
},
]),
});

0 comments on commit 5783f5d

Please sign in to comment.