Skip to content

Commit

Permalink
[Fix] static-property-placement: do not report non-components
Browse files Browse the repository at this point in the history
Fixes #2884.
  • Loading branch information
golopot authored and ljharb committed Dec 30, 2020
1 parent 4a6827d commit 2201f9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,9 +8,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)

[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894
[#2893]: https://github.com/yannickcr/eslint-plugin-react/pull/2893

## [7.22.0] - 2020.12.29

Expand Down
10 changes: 8 additions & 2 deletions lib/rules/static-property-placement.js
Expand Up @@ -131,7 +131,13 @@ module.exports = {
// Public
// ----------------------------------------------------------------------
return {
ClassProperty: (node) => reportNodeIncorrectlyPositioned(node, STATIC_PUBLIC_FIELD),
ClassProperty: (node) => {
if (!utils.getParentES6Component()) {
return;
}

reportNodeIncorrectlyPositioned(node, STATIC_PUBLIC_FIELD);
},

MemberExpression: (node) => {
// If definition type is undefined then it must not be a defining expression or if the definition is inside a
Expand All @@ -155,7 +161,7 @@ module.exports = {

MethodDefinition: (node) => {
// If the function is inside a class and is static getter then check if correctly positioned
if (isContextInClass() && node.static && node.kind === 'get') {
if (utils.getParentES6Component() && node.static && node.kind === 'get') {
// Report error if needed
reportNodeIncorrectlyPositioned(node, STATIC_GETTER);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/static-property-placement.js
Expand Up @@ -181,6 +181,26 @@ ruleTester.run('static-property-placement', rule, {
};
`].join('\n')
},

{
// Do not error on non-component classes #2884
code: `
class Foo {
static get propTypes() {}
}
`
},

{
// Do not error on non-component classes #2884
code: `
class Foo {
static propTypes = {}
}
`,
options: [PROPERTY_ASSIGNMENT]
},

// ------------------------------------------------------------------------------
// no properties
// ------------------------------------------------------------------------------
Expand Down

0 comments on commit 2201f9d

Please sign in to comment.