From bcfdbd54f7f40ed826d035a0788e00c23f1174aa Mon Sep 17 00:00:00 2001 From: Johnny Zabala Date: Fri, 10 Jul 2020 20:49:50 -0400 Subject: [PATCH] [Fix] `no-this-in-sfc`/component detection: add arrow function to list of allowed position for component --- lib/util/Components.js | 3 +- tests/lib/rules/no-this-in-sfc.js | 66 ++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 888f3ee59f..c68aa06e2e 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -670,7 +670,8 @@ function componentRule(rule, context) { case 'AssignmentExpression': case 'Property': case 'ReturnStatement': - case 'ExportDefaultDeclaration': { + case 'ExportDefaultDeclaration': + case 'ArrowFunctionExpression': { return true; } case 'SequenceExpression': { diff --git a/tests/lib/rules/no-this-in-sfc.js b/tests/lib/rules/no-this-in-sfc.js index f672d40109..4f39502928 100644 --- a/tests/lib/rules/no-this-in-sfc.js +++ b/tests/lib/rules/no-this-in-sfc.js @@ -112,16 +112,6 @@ ruleTester.run('no-this-in-sfc', rule, { }; } }` - }, { - code: ` - class Foo { - bar() { - () => () => { - this.something(); - return null; - }; - } - }` }, { code: ` class Foo { @@ -131,15 +121,6 @@ ruleTester.run('no-this-in-sfc', rule, { }; }`, parser: parsers.BABEL_ESLINT - }, { - code: ` - class Foo { - bar = () => () => { - this.something(); - return null; - }; - }`, - parser: parsers.BABEL_ESLINT }, { code: ` export const Example = ({ prop }) => { @@ -151,6 +132,21 @@ ruleTester.run('no-this-in-sfc', rule, { }; };`, parser: parsers.BABEL_ESLINT + }, { + code: ` + export const prepareLogin = new ValidatedMethod({ + name: "user.prepare", + validate: new SimpleSchema({ + }).validator(), + run({ remember }) { + if (Meteor.isServer) { + const connectionId = this.connection.id; // react/no-this-in-sfc + return Methods.prepareLogin(connectionId, remember); + } + return null; + }, + }); + ` }], invalid: [{ code: ` @@ -217,6 +213,27 @@ ruleTester.run('no-this-in-sfc', rule, { return
{this.props.foo}
; }`, errors: [{message: ERROR_MESSAGE}, {message: ERROR_MESSAGE}] + }, { + code: ` + class Foo { + bar() { + return () => { + this.something(); + return null; + } + } + }`, + errors: [{message: ERROR_MESSAGE}] + }, { + code: ` + class Foo { + bar = () => () => { + this.something(); + return null; + }; + }`, + parser: parsers.BABEL_ESLINT, + errors: [{message: ERROR_MESSAGE}] }, { code: ` class Foo { @@ -230,5 +247,16 @@ ruleTester.run('no-this-in-sfc', rule, { } }`, errors: [{message: ERROR_MESSAGE}] + }, { + code: ` + class Foo { + bar() { + () => () => { + this.something(); + return null; + }; + } + }`, + errors: [{message: ERROR_MESSAGE}] }] });