Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Fix: array-bracket-spacing for destructuring typed parameter (fixes #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaygraveyard committed Dec 20, 2015
1 parent 8e2f6f9 commit e7b623d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ original ones as well!).
"rules": {
"babel/generator-star-spacing": 1,
"babel/new-cap": 1,
"babel/array-bracket-spacing": 1,
"babel/object-curly-spacing": 1,
"babel/object-shorthand": 1,
"babel/arrow-parens": 1,
Expand All @@ -39,6 +40,7 @@ Each rule corresponds to a core `eslint` rule, and has the same options.

- `babel/generator-star-spacing`: Handles async/await functions correctly
- `babel/new-cap`: Ignores capitalized decorators (`@Decorator`)
- `babel/array-bracket-spacing`: Handles destructuring arrays with flow type in function parametres
- `babel/object-curly-spacing`: doesn't complain about `export x from "mod";` or `export * as x from "mod";`
- `babel/object-shorthand`: doesn't fail when using object spread (`...obj`)
- `babel/arrow-parens`: Handles async functions correctly
Expand Down
7 changes: 6 additions & 1 deletion rules/array-bracket-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,16 @@ module.exports = function(context) {

var first = sourceCode.getFirstToken(node),
second = sourceCode.getFirstToken(node, 1),
penultimate = sourceCode.getLastToken(node, 1),
last = sourceCode.getLastToken(node),
firstElement = node.elements[0],
lastElement = node.elements[node.elements.length - 1];

while (last.type !== "Punctuation" && last.value !== "]") {
last = sourceCode.getTokenBefore(last);
}

var penultimate = sourceCode.getTokenBefore(last);

var openingBracketMustBeSpaced =
options.objectsInArraysException && isObjectType(firstElement) ||
options.arraysInArraysException && isArrayType(firstElement) ||
Expand Down
61 changes: 60 additions & 1 deletion tests/array-bracket-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ ruleTester.run("array-bracket-spacing", rule, {
{ code: "var foo = [1, {'bar': 'baz'}, 5];", options: ["never"] },
{ code: "var foo = [{'bar': 'baz'}, 1, 5];", options: ["never"] },
{ code: "var foo = [1, 5, {'bar': 'baz'}];", options: ["never"] },
{ code: "var obj = {'foo': [1, 2]}", options: ["never"] }
{ code: "var obj = {'foo': [1, 2]}", options: ["never"] },

// Babel test cases.
// always - destructuring typed array param
{ code: "function fn([ a,b ]: Array<any>){}", options: ["always"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } },

// never - destructuring typed array param
{ code: "function fn([a,b]: Array<any >){}", options: ["never"], parser: "babel-eslint", ecmaFeatures: { destructuring: true } },

],

Expand Down Expand Up @@ -669,6 +676,58 @@ ruleTester.run("array-bracket-spacing", rule, {
column: 26
}
]
},

// Babel test cases.

// always - destructuring typed array param
{
code: "function fn([a,b]: Array<any>){}",
output: "function fn([ a,b ]: Array<any>){}",
options: ["always"],
parser: "babel-eslint",
ecmaFeatures: {
destructuring: true
},
errors: [
{
message: "A space is required after '['",
type: "ArrayPattern",
line: 1,
column: 13
},
{
message: "A space is required before ']'",
type: "ArrayPattern",
line: 1,
column: 17
}
]
},

// never - destructuring typed array param
{
code: "function fn([ a,b ]: Array<any>){}",
output: "function fn([a,b]: Array<any>){}",
options: ["never"],
parser: "babel-eslint",
ecmaFeatures: {
destructuring: true
},
errors: [
{
message: "There should be no space after '['",
type: "ArrayPattern",
line: 1,
column: 13
},
{
message: "There should be no space before ']'",
type: "ArrayPattern",
line: 1,
column: 19
}
]
}
]
});

0 comments on commit e7b623d

Please sign in to comment.