Skip to content

Commit 2201f9d

Browse files
chiawendtljharb
authored andcommitted
[Fix] static-property-placement: do not report non-components
Fixes #2884.
1 parent 4a6827d commit 2201f9d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Fixed
99
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
1010
* [`jsx-no-constructed-context-values`]: avoid a crash with boolean shorthand ([#2895][] @ljharb)
11+
* [`static-property-placement`]: do not report non-components ([#2893][] @golopot)
1112

1213
[#2895]: https://github.com/yannickcr/eslint-plugin-react/issues/2895
1314
[#2894]: https://github.com/yannickcr/eslint-plugin-react/issues/2894
15+
[#2893]: https://github.com/yannickcr/eslint-plugin-react/pull/2893
1416

1517
## [7.22.0] - 2020.12.29
1618

lib/rules/static-property-placement.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,13 @@ module.exports = {
131131
// Public
132132
// ----------------------------------------------------------------------
133133
return {
134-
ClassProperty: (node) => reportNodeIncorrectlyPositioned(node, STATIC_PUBLIC_FIELD),
134+
ClassProperty: (node) => {
135+
if (!utils.getParentES6Component()) {
136+
return;
137+
}
138+
139+
reportNodeIncorrectlyPositioned(node, STATIC_PUBLIC_FIELD);
140+
},
135141

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

156162
MethodDefinition: (node) => {
157163
// If the function is inside a class and is static getter then check if correctly positioned
158-
if (isContextInClass() && node.static && node.kind === 'get') {
164+
if (utils.getParentES6Component() && node.static && node.kind === 'get') {
159165
// Report error if needed
160166
reportNodeIncorrectlyPositioned(node, STATIC_GETTER);
161167
}

tests/lib/rules/static-property-placement.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,26 @@ ruleTester.run('static-property-placement', rule, {
181181
};
182182
`].join('\n')
183183
},
184+
185+
{
186+
// Do not error on non-component classes #2884
187+
code: `
188+
class Foo {
189+
static get propTypes() {}
190+
}
191+
`
192+
},
193+
194+
{
195+
// Do not error on non-component classes #2884
196+
code: `
197+
class Foo {
198+
static propTypes = {}
199+
}
200+
`,
201+
options: [PROPERTY_ASSIGNMENT]
202+
},
203+
184204
// ------------------------------------------------------------------------------
185205
// no properties
186206
// ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)