From 6835d8bc6b9f29e4dcd953bbc67b4fe5fbdbefc9 Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Tue, 5 Mar 2019 16:31:23 +0100 Subject: [PATCH] [Fix] `no-this-in-sfc`: Fix false positive on SFC defined as object property Fixes #2147 --- lib/rules/no-this-in-sfc.js | 2 +- tests/lib/rules/no-this-in-sfc.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-this-in-sfc.js b/lib/rules/no-this-in-sfc.js index 8d715e7bae..5d3749d71f 100644 --- a/lib/rules/no-this-in-sfc.js +++ b/lib/rules/no-this-in-sfc.js @@ -31,7 +31,7 @@ module.exports = { MemberExpression(node) { if (node.object.type === 'ThisExpression') { const component = components.get(utils.getParentStatelessComponent()); - if (!component) { + if (!component || component.node && component.node.parent && component.node.parent.type === 'Property') { return; } context.report({ diff --git a/tests/lib/rules/no-this-in-sfc.js b/tests/lib/rules/no-this-in-sfc.js index b5d87f0267..d161fa1336 100644 --- a/tests/lib/rules/no-this-in-sfc.js +++ b/tests/lib/rules/no-this-in-sfc.js @@ -137,6 +137,17 @@ ruleTester.run('no-this-in-sfc', rule, { }; }`, parser: 'babel-eslint' + }, { + code: ` + export const Example = ({ prop }) => { + return { + handleClick: () => {}, + renderNode() { + return
; + }, + }; + };`, + parser: 'babel-eslint' }], invalid: [{ code: `