Skip to content
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
9 changes: 7 additions & 2 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ pp.parseNumericLiteralMember = function () {

pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
const node = this.startNode(), indentLevel = this.state.indentLevel;
let allowEmptyBody = false;

// must start with colon or arrow
if (isIfExpression) {
Expand All @@ -178,6 +179,8 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
} else if (this.eat(tt.colon)) {
if (!this.isLineBreak()) return this.parseStatement(false);
} else if (this.eat(tt.arrow)) {
allowEmptyBody = true;

if (!this.isLineBreak()) {
if (this.match(tt.braceL)) {
// restart node at brace start instead of arrow start
Expand All @@ -186,7 +189,7 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
this.parseBlockBody(node, allowDirectives, false, tt.braceR);
this.addExtra(node, "curly", true);
return this.finishNode(node, "BlockStatement");
} else {
} else if (this.state.type.startsExpr) {
return this.parseMaybeAssign();
}
}
Expand All @@ -197,7 +200,9 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
// never parse directives if curly braces aren't used (TODO: document)
this.parseBlockBody(node, false, false, indentLevel);
this.addExtra(node, "curly", false);
if (!node.body.length) this.unexpected(node.start, "Expected an Indent or Statement");
if (!allowEmptyBody && !node.body.length) {
this.unexpected(node.start, "Expected an Indent or Statement");
}

return this.finishNode(node, "BlockStatement");
};
Expand Down
2 changes: 1 addition & 1 deletion src/tokenizer/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const types = {
doubleColon: new TokenType("::", { beforeExpr }),
dot: new TokenType("."),
question: new TokenType("?", { beforeExpr }),
arrow: new TokenType("=>", { beforeExpr }),
arrow: new TokenType("=>", { beforeExpr, startsExpr }),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this would only happen when this.hasPlugin("lightscript"), but I don't think there's an easy way to do that yet. So this is fine for now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unfortunately this is all declarative code, running outside the context of a particular this or parser configuration. Would require major changes to the way babylon initializes itself.

template: new TokenType("template"),
ellipsis: new TokenType("...", { beforeExpr }),
backQuote: new TokenType("`", { startsExpr }),
Expand Down
266 changes: 1 addition & 265 deletions test/fixtures/flow/type-annotations/99/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,269 +161,5 @@
],
"directives": []
},
"comments": [],
"tokens": [
{
"type": {
"label": "(",
"beforeExpr": true,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "foo",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
}
}
},
{
"type": {
"label": ",",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 4,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 5
}
}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "bar",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
}
}
},
{
"type": {
"label": ")",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"start": 9,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 10
}
}
},
{
"type": {
"label": ":",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 11
}
}
},
{
"type": {
"label": "name",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null
},
"value": "z",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
}
}
},
{
"type": {
"label": "=>",
"beforeExpr": true,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 14,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 14


},
"end": {
"line": 1,
"column": 16
}
}
},
{
"type": {
"label": "null",
"keyword": "null",
"beforeExpr": false,
"startsExpr": true,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": "null",
"start": 17,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 21
}
}
},
{
"type": {
"label": "eof",
"beforeExpr": false,
"startsExpr": false,
"rightAssociative": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"start": 21,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 21
}
}
}
]
"comments": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-> ->
Loading