Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: examples with arrow functions in no-return-assign (fixes #13135) #13138

Merged
merged 3 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/rules/no-return-assign.md
Original file line number Diff line number Diff line change
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)

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
Original file line number Diff line number Diff line change
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"
}
]
}
]
});