Arrow fns can't be used as the left side of a binary or ternary expression (#559)

* Arrow fns can't be used as the left side of a binary or ternary expression

Fixes #536

* Add test for logical expression with arrow function

* Fix eslint
This commit is contained in:
Nicolò Ribaudo 2017-08-05 00:27:11 +02:00 committed by Daniel Tschinder
parent 5c867240f7
commit 7353a38846
7 changed files with 255 additions and 2 deletions

View File

@ -191,7 +191,15 @@ export default class ExpressionParser extends LValParser {
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
if (
expr.type === "ArrowFunctionExpression" &&
expr.start === potentialArrowAt
) {
return expr;
}
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
return this.parseConditional(
@ -228,12 +236,20 @@ export default class ExpressionParser extends LValParser {
parseExprOps(noIn: ?boolean, refShorthandDefaultPos: Pos): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseMaybeUnary(refShorthandDefaultPos);
if (
expr.type === "ArrowFunctionExpression" &&
expr.start === potentialArrowAt
) {
return expr;
}
if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
return expr;
} else {
return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
}
return this.parseExprOp(expr, startPos, startLoc, -1, noIn);
}
// Parse binary operators with the operator precedence parsing

View File

@ -0,0 +1,2 @@
(() => {}) || true;
(() => {}) ? a : b;

View File

@ -0,0 +1,227 @@
{
"type": "File",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 19
}
},
"program": {
"type": "Program",
"start": 0,
"end": 39,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 19
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 19
}
},
"expression": {
"type": "LogicalExpression",
"start": 0,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 18
}
},
"left": {
"type": "ArrowFunctionExpression",
"start": 1,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 9
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 7,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 9
}
},
"body": [],
"directives": []
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
},
"operator": "||",
"right": {
"type": "BooleanLiteral",
"start": 14,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 18
}
},
"value": true
}
}
},
{
"type": "ExpressionStatement",
"start": 20,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 19
}
},
"expression": {
"type": "ConditionalExpression",
"start": 20,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"test": {
"type": "ArrowFunctionExpression",
"start": 21,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 9
}
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 27,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 9
}
},
"body": [],
"directives": []
},
"extra": {
"parenthesized": true,
"parenStart": 20
}
},
"consequent": {
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 14
},
"identifierName": "a"
},
"name": "a"
},
"alternate": {
"type": "Identifier",
"start": 37,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 18
},
"identifierName": "b"
},
"name": "b"
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1 @@
() => {} || true

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ; (1:9)"
}

View File

@ -0,0 +1 @@
() => {} ? 1 : 2;

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ; (1:9)"
}