From 5dfd4eb50d84077a57950f119e5de8976070e49a Mon Sep 17 00:00:00 2001 From: Anix Date: Sun, 5 Apr 2020 02:01:46 +0530 Subject: [PATCH] Docs: examples with arrow functions in no-return-assign (fixes #13135) (#13138) * Chore: docs example for arrow function and tests (fixes #13135) * Chore: removed wrong test case * Chore: refactore snipper and added tests --- docs/rules/no-return-assign.md | 16 ++++++ tests/lib/rules/no-return-assign.js | 82 ++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/docs/rules/no-return-assign.md b/docs/rules/no-return-assign.md index 781d5973a78..ed40c275437 100644 --- a/docs/rules/no-return-assign.md +++ b/docs/rules/no-return-assign.md @@ -40,6 +40,14 @@ function doSomething() { function doSomething() { return foo += 2; } + +const foo = (a, b) => a = b + +const bar = (a, b, c) => (a = b, c == b) + +function doSomething() { + return foo = bar && foo > 0; +} ``` Examples of **correct** code for the default `"except-parens"` option: @@ -58,6 +66,14 @@ function doSomething() { function doSomething() { return (foo = bar + 2); } + +const foo = (a, b) => (a = b) + +const bar = (a, b, c) => ((a = b), c == b) + +function doSomething() { + return (foo = bar) && foo > 0; +} ``` ### always diff --git a/tests/lib/rules/no-return-assign.js b/tests/lib/rules/no-return-assign.js index e9d0c585edc..600503c2de0 100644 --- a/tests/lib/rules/no-return-assign.js +++ b/tests/lib/rules/no-return-assign.js @@ -52,6 +52,19 @@ ruleTester.run("no-return-assign", rule, { { code: "() => (result = a * b)", options: ["except-parens"] + }, + "const foo = (a,b,c) => ((a = b), c)", + `function foo(){ + return (a = b) + }`, + `function bar(){ + return function foo(){ + return (a = b) && c + } + }`, + { + code: "const foo = (a) => (b) => (a = b)", + parserOptions: { ecmaVersion: 6 } } ], invalid: [ @@ -79,7 +92,12 @@ ruleTester.run("no-return-assign", rule, { }, { code: "() => result = a * b", - errors: [{ messageId: "arrowAssignment", type: "ArrowFunctionExpression" }] + errors: [ + { + messageId: "arrowAssignment", + type: "ArrowFunctionExpression" + } + ] }, { code: "function x() { return result = a * b; };", @@ -95,6 +113,68 @@ ruleTester.run("no-return-assign", rule, { code: "function x() { return result || (result = a * b); };", options: ["always"], errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] + }, + { + code: `function foo(){ + return a = b + }`, + errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] + }, + { + code: `function doSomething() { + return foo = bar && foo > 0; + }`, + errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] + }, + { + code: `function doSomething() { + return foo = function(){ + return (bar = bar1) + } + }`, + errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] + }, + { + code: `function doSomething() { + return foo = () => a + }`, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "returnAssignment", + type: "ReturnStatement" + } + ] + }, + { + code: `function doSomething() { + return () => a = () => b + }`, + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "arrowAssignment", + type: "ArrowFunctionExpression" + } + ] + }, + { + code: `function foo(a){ + return function bar(b){ + return a = b + } + }`, + errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }] + }, + { + code: "const foo = (a) => (b) => a = b", + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "arrowAssignment", + type: "ArrowFunctionExpression" + } + ] } ] });