Skip to content

Commit 5dfd4eb

Browse files
authored
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
1 parent adc8fa8 commit 5dfd4eb

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

docs/rules/no-return-assign.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ function doSomething() {
4040
function doSomething() {
4141
return foo += 2;
4242
}
43+
44+
const foo = (a, b) => a = b
45+
46+
const bar = (a, b, c) => (a = b, c == b)
47+
48+
function doSomething() {
49+
return foo = bar && foo > 0;
50+
}
4351
```
4452

4553
Examples of **correct** code for the default `"except-parens"` option:
@@ -58,6 +66,14 @@ function doSomething() {
5866
function doSomething() {
5967
return (foo = bar + 2);
6068
}
69+
70+
const foo = (a, b) => (a = b)
71+
72+
const bar = (a, b, c) => ((a = b), c == b)
73+
74+
function doSomething() {
75+
return (foo = bar) && foo > 0;
76+
}
6177
```
6278

6379
### always

tests/lib/rules/no-return-assign.js

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ ruleTester.run("no-return-assign", rule, {
5252
{
5353
code: "() => (result = a * b)",
5454
options: ["except-parens"]
55+
},
56+
"const foo = (a,b,c) => ((a = b), c)",
57+
`function foo(){
58+
return (a = b)
59+
}`,
60+
`function bar(){
61+
return function foo(){
62+
return (a = b) && c
63+
}
64+
}`,
65+
{
66+
code: "const foo = (a) => (b) => (a = b)",
67+
parserOptions: { ecmaVersion: 6 }
5568
}
5669
],
5770
invalid: [
@@ -79,7 +92,12 @@ ruleTester.run("no-return-assign", rule, {
7992
},
8093
{
8194
code: "() => result = a * b",
82-
errors: [{ messageId: "arrowAssignment", type: "ArrowFunctionExpression" }]
95+
errors: [
96+
{
97+
messageId: "arrowAssignment",
98+
type: "ArrowFunctionExpression"
99+
}
100+
]
83101
},
84102
{
85103
code: "function x() { return result = a * b; };",
@@ -95,6 +113,68 @@ ruleTester.run("no-return-assign", rule, {
95113
code: "function x() { return result || (result = a * b); };",
96114
options: ["always"],
97115
errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }]
116+
},
117+
{
118+
code: `function foo(){
119+
return a = b
120+
}`,
121+
errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }]
122+
},
123+
{
124+
code: `function doSomething() {
125+
return foo = bar && foo > 0;
126+
}`,
127+
errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }]
128+
},
129+
{
130+
code: `function doSomething() {
131+
return foo = function(){
132+
return (bar = bar1)
133+
}
134+
}`,
135+
errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }]
136+
},
137+
{
138+
code: `function doSomething() {
139+
return foo = () => a
140+
}`,
141+
parserOptions: { ecmaVersion: 6 },
142+
errors: [
143+
{
144+
messageId: "returnAssignment",
145+
type: "ReturnStatement"
146+
}
147+
]
148+
},
149+
{
150+
code: `function doSomething() {
151+
return () => a = () => b
152+
}`,
153+
parserOptions: { ecmaVersion: 6 },
154+
errors: [
155+
{
156+
messageId: "arrowAssignment",
157+
type: "ArrowFunctionExpression"
158+
}
159+
]
160+
},
161+
{
162+
code: `function foo(a){
163+
return function bar(b){
164+
return a = b
165+
}
166+
}`,
167+
errors: [{ messageId: "returnAssignment", type: "ReturnStatement" }]
168+
},
169+
{
170+
code: "const foo = (a) => (b) => a = b",
171+
parserOptions: { ecmaVersion: 6 },
172+
errors: [
173+
{
174+
messageId: "arrowAssignment",
175+
type: "ArrowFunctionExpression"
176+
}
177+
]
98178
}
99179
]
100180
});

0 commit comments

Comments
 (0)