Fix parenthesis for nullish coalescing (#10269)
* ♻️ added condition to check for left and right of nullish coalescing operator and if any is a logical expression without a paren then throw an error * 🐛 bugs fixed and test cases updated for babel parser * ♻️ code comments added * 🐛 spell error rectified * ♻️ failing test updated * 🐛 push tests after make build * Skip nullish-coalescing flow precedence tests They're now incorrect * ♻️ error message updated, binop priority of other logical operators +1 from ?? and * ♻️ increaed the binOp for in and instanceOf, added logic to print the brackets in an ?? && || expression, test cases added * 🐛 failing test fixed and comments updated * ♻️ new lines added between tests * ♻️ basic tests for checking the binOp of instanceOf, in and relational operators to be equal added * ♻️ new lines added in between tests
This commit is contained in:
committed by
Nicolò Ribaudo
parent
3e8a5c5e28
commit
b02e35c19a
@@ -457,7 +457,7 @@
|
||||
"isAssign": false,
|
||||
"prefix": true,
|
||||
"postfix": false,
|
||||
"binop": 9,
|
||||
"binop": 10,
|
||||
"updateContext": null
|
||||
},
|
||||
"value": "+",
|
||||
|
||||
@@ -1 +1 @@
|
||||
a && b ?? c;
|
||||
(a && b) ?? c;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -9,13 +9,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
@@ -32,7 +32,7 @@
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -40,13 +40,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -54,35 +54,35 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 6,
|
||||
"start": 1,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 1,
|
||||
"start": 1,
|
||||
"end": 2,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
"column": 2
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
@@ -91,35 +91,39 @@
|
||||
"operator": "&&",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 6,
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "b"
|
||||
},
|
||||
"name": "b"
|
||||
},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 0
|
||||
}
|
||||
},
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 11,
|
||||
"start": 12,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
"column": 12
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
},
|
||||
"identifierName": "c"
|
||||
},
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
c && d ?? e;
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"nullishCoalescingOperator",
|
||||
"estree"
|
||||
],
|
||||
"throws": "Nullish coalescing operator(??) requires parens when mixing with logical operators (1:0)"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
a ?? b && c;
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"nullishCoalescingOperator"
|
||||
],
|
||||
"throws": "Nullish coalescing operator(??) requires parens when mixing with logical operators (1:5)"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
e ?? f ?? g || h;
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"plugins": [
|
||||
"nullishCoalescingOperator"
|
||||
],
|
||||
"throws": "Nullish coalescing operator(??) requires parens when mixing with logical operators (1:10)"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
h || i ?? j;
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"plugins": [
|
||||
"nullishCoalescingOperator",
|
||||
"estree"
|
||||
],
|
||||
"throws": "Nullish coalescing operator(??) requires parens when mixing with logical operators (1:0)"
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
a ?? b && c;
|
||||
a ?? (b && c);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -9,13 +9,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
@@ -32,7 +32,7 @@
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -40,13 +40,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -54,7 +54,7 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
@@ -77,30 +77,30 @@
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 5,
|
||||
"end": 11,
|
||||
"start": 6,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 6,
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "b"
|
||||
},
|
||||
@@ -109,20 +109,24 @@
|
||||
"operator": "&&",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 11,
|
||||
"start": 11,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 12
|
||||
},
|
||||
"identifierName": "c"
|
||||
},
|
||||
"name": "c"
|
||||
},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
a ?? b || c;
|
||||
a ?? (b || c);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -9,13 +9,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
@@ -32,7 +32,7 @@
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -40,13 +40,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -54,76 +54,80 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "LogicalExpression",
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 6,
|
||||
"end": 1,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
"name": "a"
|
||||
},
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 6,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 1,
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
"name": "a"
|
||||
},
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 6,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "b"
|
||||
},
|
||||
"name": "b"
|
||||
}
|
||||
},
|
||||
"operator": "||",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 11,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"identifierName": "c"
|
||||
},
|
||||
"name": "c"
|
||||
"operator": "||",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 11,
|
||||
"end": 12,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
},
|
||||
"identifierName": "c"
|
||||
},
|
||||
"name": "c"
|
||||
},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
a || b ?? c;
|
||||
(a || b) ?? c;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -9,13 +9,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
@@ -32,7 +32,7 @@
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 12,
|
||||
"end": 14,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -40,13 +40,13 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 12
|
||||
"column": 14
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 11,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
@@ -54,35 +54,35 @@
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "LogicalExpression",
|
||||
"start": 0,
|
||||
"end": 6,
|
||||
"start": 1,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
"column": 7
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 1,
|
||||
"start": 1,
|
||||
"end": 2,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
"column": 1
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 1
|
||||
"column": 2
|
||||
},
|
||||
"identifierName": "a"
|
||||
},
|
||||
@@ -91,35 +91,39 @@
|
||||
"operator": "||",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 5,
|
||||
"end": 6,
|
||||
"start": 6,
|
||||
"end": 7,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 5
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
"column": 7
|
||||
},
|
||||
"identifierName": "b"
|
||||
},
|
||||
"name": "b"
|
||||
},
|
||||
"extra": {
|
||||
"parenthesized": true,
|
||||
"parenStart": 0
|
||||
}
|
||||
},
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "Identifier",
|
||||
"start": 10,
|
||||
"end": 11,
|
||||
"start": 12,
|
||||
"end": 13,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 10
|
||||
"column": 12
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 11
|
||||
"column": 13
|
||||
},
|
||||
"identifierName": "c"
|
||||
},
|
||||
|
||||
15
packages/babel-parser/test/unit/tokenizer/types.js
Normal file
15
packages/babel-parser/test/unit/tokenizer/types.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import { types } from "../../../src/tokenizer/types";
|
||||
|
||||
describe("token types", () => {
|
||||
it("should check if the binOp for relational === in", () => {
|
||||
expect(types.relational.binop).toEqual(types._in.binop);
|
||||
});
|
||||
|
||||
it("should check if the binOp for relational === instanceOf", () => {
|
||||
expect(types.relational.binop).toEqual(types._instanceof.binop);
|
||||
});
|
||||
|
||||
it("should check if the binOp for in === instanceOf", () => {
|
||||
expect(types._in.binop).toEqual(types._instanceof.binop);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user