Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anikethsaha committed Apr 4, 2020
1 parent adc8fa8 commit 5dfd4eb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/rules/no-return-assign.md
Expand Up @@ -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)

This comment has been minimized.

Copy link
@EECOLOR

EECOLOR May 18, 2021

Is it intentional that this is an invalid case?

The a = b part of (a = b, c == d) is in a place meant for side effects. Changing it to a == b would have no effect and it seems that having a == b in that location would actually be an error.

From a naive perspective this is not intentionally added as a version that is not accepted by the rule, it seems this was added to clarify how the rule is currently implemented.

I am asking because I might want to add some code that allows this version: (a = b, c == b), it still should prevent this version: (a = b, c = b)


function doSomething() {
return foo = bar && foo > 0;
}
```

Examples of **correct** code for the default `"except-parens"` option:
Expand All @@ -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
Expand Down
82 changes: 81 additions & 1 deletion tests/lib/rules/no-return-assign.js
Expand Up @@ -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: [
Expand Down Expand Up @@ -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; };",
Expand All @@ -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"
}
]
}
]
});

0 comments on commit 5dfd4eb

Please sign in to comment.