Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] static-property-placement: do not report non-components #2893

Merged
merged 1 commit into from Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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