Skip to content

Commit a7d60a7

Browse files
wcjohnsonrattrayalex
authored andcommitted
Allow empty arrow bodies (#9)
1 parent c23f6f0 commit a7d60a7

File tree

16 files changed

+835
-271
lines changed

16 files changed

+835
-271
lines changed

src/plugins/lightscript.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ pp.parseNumericLiteralMember = function () {
170170

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

174175
// must start with colon or arrow
175176
if (isIfExpression) {
@@ -178,6 +179,8 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
178179
} else if (this.eat(tt.colon)) {
179180
if (!this.isLineBreak()) return this.parseStatement(false);
180181
} else if (this.eat(tt.arrow)) {
182+
allowEmptyBody = true;
183+
181184
if (!this.isLineBreak()) {
182185
if (this.match(tt.braceL)) {
183186
// restart node at brace start instead of arrow start
@@ -186,7 +189,7 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
186189
this.parseBlockBody(node, allowDirectives, false, tt.braceR);
187190
this.addExtra(node, "curly", true);
188191
return this.finishNode(node, "BlockStatement");
189-
} else {
192+
} else if (this.state.type.startsExpr) {
190193
return this.parseMaybeAssign();
191194
}
192195
}
@@ -197,7 +200,9 @@ pp.parseWhiteBlock = function (allowDirectives?, isIfExpression?) {
197200
// never parse directives if curly braces aren't used (TODO: document)
198201
this.parseBlockBody(node, false, false, indentLevel);
199202
this.addExtra(node, "curly", false);
200-
if (!node.body.length) this.unexpected(node.start, "Expected an Indent or Statement");
203+
if (!allowEmptyBody && !node.body.length) {
204+
this.unexpected(node.start, "Expected an Indent or Statement");
205+
}
201206

202207
return this.finishNode(node, "BlockStatement");
203208
};

src/tokenizer/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const types = {
8080
doubleColon: new TokenType("::", { beforeExpr }),
8181
dot: new TokenType("."),
8282
question: new TokenType("?", { beforeExpr }),
83-
arrow: new TokenType("=>", { beforeExpr }),
83+
arrow: new TokenType("=>", { beforeExpr, startsExpr }),
8484
template: new TokenType("template"),
8585
ellipsis: new TokenType("...", { beforeExpr }),
8686
backQuote: new TokenType("`", { startsExpr }),

test/fixtures/flow/type-annotations/99/expected.json

Lines changed: 1 addition & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -161,269 +161,5 @@
161161
],
162162
"directives": []
163163
},
164-
"comments": [],
165-
"tokens": [
166-
{
167-
"type": {
168-
"label": "(",
169-
"beforeExpr": true,
170-
"startsExpr": true,
171-
"rightAssociative": false,
172-
"isLoop": false,
173-
"isAssign": false,
174-
"prefix": false,
175-
"postfix": false,
176-
"binop": null
177-
},
178-
"start": 0,
179-
"end": 1,
180-
"loc": {
181-
"start": {
182-
"line": 1,
183-
"column": 0
184-
},
185-
"end": {
186-
"line": 1,
187-
"column": 1
188-
}
189-
}
190-
},
191-
{
192-
"type": {
193-
"label": "name",
194-
"beforeExpr": false,
195-
"startsExpr": true,
196-
"rightAssociative": false,
197-
"isLoop": false,
198-
"isAssign": false,
199-
"prefix": false,
200-
"postfix": false,
201-
"binop": null
202-
},
203-
"value": "foo",
204-
"start": 1,
205-
"end": 4,
206-
"loc": {
207-
"start": {
208-
"line": 1,
209-
"column": 1
210-
},
211-
"end": {
212-
"line": 1,
213-
"column": 4
214-
}
215-
}
216-
},
217-
{
218-
"type": {
219-
"label": ",",
220-
"beforeExpr": true,
221-
"startsExpr": false,
222-
"rightAssociative": false,
223-
"isLoop": false,
224-
"isAssign": false,
225-
"prefix": false,
226-
"postfix": false,
227-
"binop": null,
228-
"updateContext": null
229-
},
230-
"start": 4,
231-
"end": 5,
232-
"loc": {
233-
"start": {
234-
"line": 1,
235-
"column": 4
236-
},
237-
"end": {
238-
"line": 1,
239-
"column": 5
240-
}
241-
}
242-
},
243-
{
244-
"type": {
245-
"label": "name",
246-
"beforeExpr": false,
247-
"startsExpr": true,
248-
"rightAssociative": false,
249-
"isLoop": false,
250-
"isAssign": false,
251-
"prefix": false,
252-
"postfix": false,
253-
"binop": null
254-
},
255-
"value": "bar",
256-
"start": 6,
257-
"end": 9,
258-
"loc": {
259-
"start": {
260-
"line": 1,
261-
"column": 6
262-
},
263-
"end": {
264-
"line": 1,
265-
"column": 9
266-
}
267-
}
268-
},
269-
{
270-
"type": {
271-
"label": ")",
272-
"beforeExpr": false,
273-
"startsExpr": false,
274-
"rightAssociative": false,
275-
"isLoop": false,
276-
"isAssign": false,
277-
"prefix": false,
278-
"postfix": false,
279-
"binop": null
280-
},
281-
"start": 9,
282-
"end": 10,
283-
"loc": {
284-
"start": {
285-
"line": 1,
286-
"column": 9
287-
},
288-
"end": {
289-
"line": 1,
290-
"column": 10
291-
}
292-
}
293-
},
294-
{
295-
"type": {
296-
"label": ":",
297-
"beforeExpr": true,
298-
"startsExpr": false,
299-
"rightAssociative": false,
300-
"isLoop": false,
301-
"isAssign": false,
302-
"prefix": false,
303-
"postfix": false,
304-
"binop": null,
305-
"updateContext": null
306-
},
307-
"start": 10,
308-
"end": 11,
309-
"loc": {
310-
"start": {
311-
"line": 1,
312-
"column": 10
313-
},
314-
"end": {
315-
"line": 1,
316-
"column": 11
317-
}
318-
}
319-
},
320-
{
321-
"type": {
322-
"label": "name",
323-
"beforeExpr": false,
324-
"startsExpr": true,
325-
"rightAssociative": false,
326-
"isLoop": false,
327-
"isAssign": false,
328-
"prefix": false,
329-
"postfix": false,
330-
"binop": null
331-
},
332-
"value": "z",
333-
"start": 12,
334-
"end": 13,
335-
"loc": {
336-
"start": {
337-
"line": 1,
338-
"column": 12
339-
},
340-
"end": {
341-
"line": 1,
342-
"column": 13
343-
}
344-
}
345-
},
346-
{
347-
"type": {
348-
"label": "=>",
349-
"beforeExpr": true,
350-
"startsExpr": false,
351-
"rightAssociative": false,
352-
"isLoop": false,
353-
"isAssign": false,
354-
"prefix": false,
355-
"postfix": false,
356-
"binop": null,
357-
"updateContext": null
358-
},
359-
"start": 14,
360-
"end": 16,
361-
"loc": {
362-
"start": {
363-
"line": 1,
364-
"column": 14
365-
366-
367-
},
368-
"end": {
369-
"line": 1,
370-
"column": 16
371-
}
372-
}
373-
},
374-
{
375-
"type": {
376-
"label": "null",
377-
"keyword": "null",
378-
"beforeExpr": false,
379-
"startsExpr": true,
380-
"rightAssociative": false,
381-
"isLoop": false,
382-
"isAssign": false,
383-
"prefix": false,
384-
"postfix": false,
385-
"binop": null,
386-
"updateContext": null
387-
},
388-
"value": "null",
389-
"start": 17,
390-
"end": 21,
391-
"loc": {
392-
"start": {
393-
"line": 1,
394-
"column": 17
395-
},
396-
"end": {
397-
"line": 1,
398-
"column": 21
399-
}
400-
}
401-
},
402-
{
403-
"type": {
404-
"label": "eof",
405-
"beforeExpr": false,
406-
"startsExpr": false,
407-
"rightAssociative": false,
408-
"isLoop": false,
409-
"isAssign": false,
410-
"prefix": false,
411-
"postfix": false,
412-
"binop": null,
413-
"updateContext": null
414-
},
415-
"start": 21,
416-
"end": 21,
417-
"loc": {
418-
"start": {
419-
"line": 1,
420-
"column": 21
421-
},
422-
"end": {
423-
"line": 1,
424-
"column": 21
425-
}
426-
}
427-
}
428-
]
164+
"comments": []
429165
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-> ->

0 commit comments

Comments
 (0)