Skip to content

Commit

Permalink
Allow spread loops in JSXExpressionContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Oct 7, 2017
1 parent 26abf6e commit 95c3353
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/plugins/jsx/index.js
Expand Up @@ -302,6 +302,8 @@ pp.jsxParseExpressionContainer = function() {
this.next();
if (this.match(tt.braceR)) {
node.expression = this.jsxParseEmptyExpression();
} else if (this.hasPlugin("spreadLoop") && this.match(tt.spreadLoop)) {
node.expression = this.parseSpreadLoop();
} else {
node.expression = this.parseExpression();
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/spreadLoop.js
Expand Up @@ -45,10 +45,11 @@ export default function(parser) {
};

pp.parseSpreadLoop = function(spreadElementType) {
const spreadElement = this.startNode();
const spreadElement = spreadElementType == null ? null : this.startNode();
const spreadLoop = this.startNode();
const loop = this.startNode();
spreadLoop.loop = this.parseForStatement(loop);
if (spreadElementType == null) return this.finishNode(spreadLoop, "SpreadLoop");
spreadElement.argument = this.finishNode(spreadLoop, "SpreadLoop");
return this.finishNode(spreadElement, spreadElementType);
};
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/spread-loop/jsx/basic/actual.js
@@ -0,0 +1,3 @@
<div>
{...for elem e in arr: <Entry elem={e} />}
</div>
372 changes: 372 additions & 0 deletions test/fixtures/spread-loop/jsx/basic/expected.json
@@ -0,0 +1,372 @@
{
"type": "File",
"start": 0,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"program": {
"type": "Program",
"start": 0,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"expression": {
"type": "JSXElement",
"start": 0,
"end": 57,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"openingElement": {
"type": "JSXOpeningElement",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"attributes": [],
"name": {
"type": "JSXIdentifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
}
},
"name": "div"
},
"selfClosing": false
},
"closingElement": {
"type": "JSXClosingElement",
"start": 51,
"end": 57,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 6
}
},
"name": {
"type": "JSXIdentifier",
"start": 53,
"end": 56,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 5
}
},
"name": "div"
}
},
"children": [
{
"type": "JSXText",
"start": 5,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 2,
"column": 2
}
},
"extra": null,
"value": "\n "
},
{
"type": "JSXExpressionContainer",
"start": 8,
"end": 50,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 44
}
},
"expression": {
"type": "SpreadLoop",
"start": 9,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 43
}
},
"loop": {
"type": "ForInArrayStatement",
"start": 9,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 43
}
},
"elem": {
"type": "Identifier",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 16
},
"identifierName": "e"
},
"name": "e"
},
"body": {
"type": "ExpressionStatement",
"start": 31,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 43
}
},
"expression": {
"type": "JSXElement",
"start": 31,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 43
}
},
"openingElement": {
"type": "JSXOpeningElement",
"start": 31,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 25
},
"end": {
"line": 2,
"column": 43
}
},
"attributes": [
{
"type": "JSXAttribute",
"start": 38,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 32
},
"end": {
"line": 2,
"column": 40
}
},
"name": {
"type": "JSXIdentifier",
"start": 38,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 32
},
"end": {
"line": 2,
"column": 36
}
},
"name": "elem"
},
"value": {
"type": "JSXExpressionContainer",
"start": 43,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 37
},
"end": {
"line": 2,
"column": 40
}
},
"expression": {
"type": "Identifier",
"start": 44,
"end": 45,
"loc": {
"start": {
"line": 2,
"column": 38
},
"end": {
"line": 2,
"column": 39
},
"identifierName": "e"
},
"name": "e"
}
}
}
],
"name": {
"type": "JSXIdentifier",
"start": 32,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 26
},
"end": {
"line": 2,
"column": 31
}
},
"name": "Entry"
},
"selfClosing": true
},
"closingElement": null,
"children": []
}
},
"array": {
"type": "Identifier",
"start": 26,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 23
},
"identifierName": "arr"
},
"name": "arr"
}
}
}
},
{
"type": "JSXText",
"start": 50,
"end": 51,
"loc": {
"start": {
"line": 2,
"column": 44
},
"end": {
"line": 3,
"column": 0
}
},
"extra": null,
"value": "\n"
}
]
}
}
],
"directives": []
}
}
File renamed without changes.

0 comments on commit 95c3353

Please sign in to comment.