From 863e43d78247dd932cafcbb734b46bdcc4eab318 Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Wed, 30 Dec 2020 18:46:39 +0800 Subject: [PATCH] [Fix] `static-property-placement`: do not report non-components Fixes #2884. --- lib/rules/static-property-placement.js | 10 ++++++++-- tests/lib/rules/static-property-placement.js | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/rules/static-property-placement.js b/lib/rules/static-property-placement.js index 69bc6b0d98..b370ab726f 100644 --- a/lib/rules/static-property-placement.js +++ b/lib/rules/static-property-placement.js @@ -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 @@ -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); } diff --git a/tests/lib/rules/static-property-placement.js b/tests/lib/rules/static-property-placement.js index e53f0a0da9..4c700d21ec 100644 --- a/tests/lib/rules/static-property-placement.js +++ b/tests/lib/rules/static-property-placement.js @@ -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 // ------------------------------------------------------------------------------