-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40fae8b
commit 44a2c14
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Disallow unnecessary rest/spread operators (no-useless-rest-spread) | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-rest-spread: "error"*/ | ||
|
||
let list = [a, ...[b, c], d] | ||
foo(...[a, b, c]) | ||
|
||
let [a, b, ...[c, d]] = list; | ||
function foo(a, b, ...[c, d]) { | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/*eslint no-useless-rest-spread: "error"*/ | ||
|
||
let list = [a, b, c, d] | ||
foo(a, b, c) | ||
|
||
let [a, b, c, d] = list; | ||
function foo(a, b, c, d) { | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @fileoverview Rule to disallow unnecessary spread operators. | ||
* @author Toru Nagashima | ||
*/ | ||
|
||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "Disallow unnecessary spread operators.", | ||
category: "Stylistic Issues", | ||
recommended: false, | ||
}, | ||
schema: [], | ||
}, | ||
|
||
create: function(context) { | ||
return { | ||
SpreadElement: function(node) { | ||
if (node.argument.type === "ArrayExpression") { | ||
context.report({ | ||
node: node, | ||
message: "Unexpected a spread operator.", | ||
}) | ||
} | ||
}, | ||
|
||
RestElement: function(node) { | ||
if (node.argument.type === "ArrayPattern") { | ||
context.report({ | ||
node: node, | ||
message: "Unexpected a rest operator.", | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @fileoverview Tests for no-useless-rest-spread rule. | ||
* @author Toru Nagashima | ||
*/ | ||
"use strict" | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
var rule = require("../../../lib/rules/no-useless-rest-spread") | ||
var RuleTester = require("eslint").RuleTester | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
|
||
ruleTester.run("no-useless-rest-spread", rule, { | ||
valid: [ | ||
{code: "let list = [...a, ...b]", env: {es6: true}}, | ||
{code: "foo(...a, ...b)", env: {es6: true}}, | ||
{code: "let [a, b, c, ...d] = obj", env: {es6: true}}, | ||
{code: "function foo(a, b, c, ...d) {}", env: {es6: true}}, | ||
], | ||
invalid: [ | ||
{code: "let list = [...[x, y, x], ...b]", env: {es6: true}, errors: ["Unexpected a spread operator."]}, | ||
{code: "foo(...a, ...[x, y, x])", env: {es6: true}, errors: ["Unexpected a spread operator."]}, | ||
{code: "let [a, ...[b, c, ...d]] = obj", env: {es6: true}, errors: ["Unexpected a rest operator."]}, | ||
|
||
// Acorn cannot parse this | ||
// {code: "function foo(a, ...[b, c, ...d]) {}", env: {es6: true}, errors: ["Unexpected a rest operator."]}, | ||
], | ||
}) |