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:
parent
5c867240f7
commit
7353a38846
@ -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
|
||||
|
||||
2
test/fixtures/es2015/arrow-functions/in-logic-expression/actual.js
vendored
Normal file
2
test/fixtures/es2015/arrow-functions/in-logic-expression/actual.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
(() => {}) || true;
|
||||
(() => {}) ? a : b;
|
||||
227
test/fixtures/es2015/arrow-functions/in-logic-expression/expected.json
vendored
Normal file
227
test/fixtures/es2015/arrow-functions/in-logic-expression/expected.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
1
test/fixtures/es2015/arrow-functions/no-binary-left/actual.js
vendored
Normal file
1
test/fixtures/es2015/arrow-functions/no-binary-left/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
() => {} || true
|
||||
3
test/fixtures/es2015/arrow-functions/no-binary-left/options.json
vendored
Normal file
3
test/fixtures/es2015/arrow-functions/no-binary-left/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token, expected ; (1:9)"
|
||||
}
|
||||
1
test/fixtures/es2015/arrow-functions/no-ternary-test/actual.js
vendored
Normal file
1
test/fixtures/es2015/arrow-functions/no-ternary-test/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
() => {} ? 1 : 2;
|
||||
3
test/fixtures/es2015/arrow-functions/no-ternary-test/options.json
vendored
Normal file
3
test/fixtures/es2015/arrow-functions/no-ternary-test/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "Unexpected token, expected ; (1:9)"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user