Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Fix: Add more tests for destructuring and spread (fixes #306) #308

Merged
merged 1 commit into from
Jun 3, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ module.exports = function convert(config) {
case SyntaxKind.ArrayLiteralExpression: {

const arrayAssignNode = nodeUtils.findAncestorOfKind(node, SyntaxKind.BinaryExpression);
const arrayIsInForOf = node.parent && node.parent.kind === SyntaxKind.ForOfStatement;
const arrayIsInForIn = node.parent && node.parent.kind === SyntaxKind.ForInStatement;
let arrayIsInAssignment;

if (arrayAssignNode) {
Expand All @@ -581,7 +583,7 @@ module.exports = function convert(config) {
}

// TypeScript uses ArrayLiteralExpression in destructuring assignment, too
if (arrayIsInAssignment) {
if (arrayIsInAssignment || arrayIsInForOf || arrayIsInForIn) {
Object.assign(result, {
type: AST_NODE_TYPES.ArrayPattern,
elements: node.elements.map(convertChild)
Expand Down Expand Up @@ -925,6 +927,11 @@ module.exports = function convert(config) {
left: arrayItem,
right: convertChild(node.initializer)
});
} else if (node.dotDotDotToken) {
Object.assign(result, {
type: AST_NODE_TYPES.RestElement,
argument: arrayItem
});
} else {
return arrayItem;
}
Expand Down Expand Up @@ -1058,16 +1065,31 @@ module.exports = function convert(config) {

// Patterns

case SyntaxKind.SpreadElement:
case SyntaxKind.SpreadElement: {
let type = AST_NODE_TYPES.SpreadElement;

if (node.parent &&
node.parent.parent &&
node.parent.parent.kind === SyntaxKind.BinaryExpression
) {
if (node.parent.parent.left === node.parent) {
type = AST_NODE_TYPES.RestElement;
} else if (node.parent.parent.right === node.parent) {
type = AST_NODE_TYPES.SpreadElement;
}
}

Object.assign(result, {
type: AST_NODE_TYPES.SpreadElement,
type,
argument: convertChild(node.expression)
});
break;
}
case SyntaxKind.SpreadAssignment: {
let type = AST_NODE_TYPES.ExperimentalSpreadProperty;

if (node.parent &&
node.parent.parent &&
node.parent.parent.kind === SyntaxKind.BinaryExpression
) {
if (node.parent.parent.right === node.parent) {
Expand Down
5 changes: 5 additions & 0 deletions lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function findFirstMatchingChild(node, sourceFile, predicate) {
if (child && predicate(child)) {
return child;
}

const grandChild = findFirstMatchingChild(child, sourceFile, predicate);
if (grandChild) {
return grandChild;
}
}
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
module.exports = {
"type": "Program",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"range": [
0,
11
],
"body": [
{
"type": "ExpressionStatement",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"range": [
0,
11
],
"expression": {
"type": "ArrowFunctionExpression",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 10
}
},
"range": [
0,
10
],
"id": null,
"generator": false,
"expression": true,
"async": false,
"params": [
{
"type": "ArrayPattern",
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
}
},
"range": [
1,
4
],
"elements": [
{
"type": "Identifier",
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
},
"range": [
2,
3
],
"name": "y"
}
]
}
],
"body": {
"type": "Identifier",
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"range": [
9,
10
],
"name": "x"
}
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "Punctuator",
"value": "(",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
},
"range": [
0,
1
]
},
{
"type": "Punctuator",
"value": "[",
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 2
}
},
"range": [
1,
2
]
},
{
"type": "Identifier",
"value": "y",
"loc": {
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 3
}
},
"range": [
2,
3
]
},
{
"type": "Punctuator",
"value": "]",
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
}
},
"range": [
3,
4
]
},
{
"type": "Punctuator",
"value": ")",
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
},
"range": [
4,
5
]
},
{
"type": "Punctuator",
"value": "=>",
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 8
}
},
"range": [
6,
8
]
},
{
"type": "Identifier",
"value": "x",
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
},
"range": [
9,
10
]
},
{
"type": "Punctuator",
"value": ";",
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
},
"range": [
10,
11
]
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
([y]) => x;
Loading