Skip to content

Commit

Permalink
Restore @babel/parser tests (#10854)
Browse files Browse the repository at this point in the history
* Make parser tests work again

* Update failing fixtures

* Fix serialization of BigInt

* Fix
  • Loading branch information
nicolo-ribaudo committed Dec 11, 2019
1 parent 20e43ad commit 5156d3e
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 42 deletions.
Expand Up @@ -13,6 +13,7 @@
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:1)",
"SyntaxError: Invalid left-hand side in assignment expression (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,6 +13,7 @@
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:5)",
"SyntaxError: Invalid left-hand side in for-in statement (1:5)"
],
"program": {
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: You're trying to assign to a parenthesized expression, eg. instead of `({a}) = 0` use `({a} = 0)` (1:1)"
"SyntaxError: Invalid parenthesized assignment pattern (1:0)"
],
"program": {
"type": "Program",
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: You're trying to assign to a parenthesized expression, eg. instead of `([a]) = 0` use `([a] = 0)` (1:1)"
"SyntaxError: Invalid parenthesized assignment pattern (1:0)"
],
"program": {
"type": "Program",
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: You're trying to assign to a parenthesized expression, eg. instead of `([a]) = 0` use `([a] = 0)` (1:1)"
"SyntaxError: Invalid parenthesized assignment pattern (1:1)"
],
"program": {
"type": "Program",
Expand Down
Expand Up @@ -13,7 +13,7 @@
}
},
"errors": [
"SyntaxError: You're trying to assign to a parenthesized expression, eg. instead of `({a}) = 0` use `({a} = 0)` (1:1)"
"SyntaxError: Invalid parenthesized assignment pattern (1:1)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,32 +1,32 @@
{
"type": "File",
"start": 0,
"end": 22,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
"column": 20
}
},
"errors": [
"SyntaxError: Object pattern can't contain getter or setter (1:8)"
"SyntaxError: Object pattern can't contain getter or setter (1:7)"
],
"program": {
"type": "Program",
"start": 0,
"end": 22,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
"column": 20
}
},
"sourceType": "script",
Expand All @@ -35,74 +35,74 @@
{
"type": "ExpressionStatement",
"start": 0,
"end": 22,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
"column": 20
}
},
"expression": {
"type": "AssignmentExpression",
"start": 2,
"end": 20,
"start": 1,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 2
"column": 1
},
"end": {
"line": 1,
"column": 20
"column": 19
}
},
"operator": "=",
"left": {
"type": "ObjectPattern",
"start": 2,
"end": 16,
"start": 1,
"end": 15,
"loc": {
"start": {
"line": 1,
"column": 2
"column": 1
},
"end": {
"line": 1,
"column": 16
"column": 15
}
},
"properties": [
{
"type": "ObjectMethod",
"start": 4,
"end": 14,
"start": 3,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 4
"column": 3
},
"end": {
"line": 1,
"column": 14
"column": 13
}
},
"method": false,
"key": {
"type": "Identifier",
"start": 8,
"end": 9,
"start": 7,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 8
"column": 7
},
"end": {
"line": 1,
"column": 9
"column": 8
},
"identifierName": "x"
},
Expand All @@ -116,16 +116,16 @@
"params": [],
"body": {
"type": "BlockStatement",
"start": 12,
"end": 14,
"start": 11,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
"column": 11
},
"end": {
"line": 1,
"column": 14
"column": 13
}
},
"body": [],
Expand All @@ -136,16 +136,16 @@
},
"right": {
"type": "NumericLiteral",
"start": 19,
"end": 20,
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 19
"column": 18
},
"end": {
"line": 1,
"column": 20
"column": 19
}
},
"extra": {
Expand Down
Expand Up @@ -13,6 +13,7 @@
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:1)",
"SyntaxError: Invalid left-hand side in assignment expression (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,6 +13,7 @@
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:1)",
"SyntaxError: Invalid left-hand side in assignment expression (1:1)"
],
"program": {
Expand Down
Expand Up @@ -13,6 +13,7 @@
}
},
"errors": [
"SyntaxError: Invalid parenthesized assignment pattern (1:5)",
"SyntaxError: Invalid left-hand side in for-in statement (1:5)"
],
"program": {
Expand Down
Expand Up @@ -89,7 +89,10 @@
"column": 12
}
},
"value": "1",
"value": {
"$$ babel internal serialized type": "BigInt",
"value": "1n"
},
"raw": "1n",
"bigint": "1"
}
Expand Down
34 changes: 28 additions & 6 deletions packages/babel-parser/test/helpers/runFixtureTests.js
Expand Up @@ -7,6 +7,8 @@ import path from "path";

const rootPath = path.join(__dirname, "../../../..");

const serialized = "$$ babel internal serialized type";

class FixtureError extends Error {
constructor(previousError, fixturePath, code) {
super(previousError.message);
Expand Down Expand Up @@ -127,15 +129,21 @@ function overrideToJSON(cb) {
const { toJSON } = obj.prototype;
originalToJSONMap.set(obj, toJSON);
obj.prototype.toJSON = function() {
if (typeof this === "bigint") {
return { [serialized]: "BigInt", value: serialize(this) };
}

return this.toString();
};
}

cb();
const result = cb();

for (const obj of notJSONparseableObj) {
obj.prototype.toJSON = originalToJSONMap.get(obj);
}

return result;
}

function runTest(test, parseFunction) {
Expand Down Expand Up @@ -213,8 +221,22 @@ function runTest(test, parseFunction) {
}
}

function serialize(value) {
if (typeof value === "bigint") {
return value.toString() + "n";
}
return JSON.stringify(value, null, 2);
}

function ppJSON(v) {
return JSON.stringify(v, null, 2);
if (v && typeof v === "object" && v[serialized]) {
switch (v[serialized]) {
case "BigInt":
return typeof BigInt === "undefined" ? "null" : v.value;
}
}

return serialize(v);
}

function addPath(str, pt) {
Expand All @@ -226,12 +248,12 @@ function addPath(str, pt) {
}

function misMatch(exp, act) {
overrideToJSON(() => {
return overrideToJSON(() => {
if (
exp instanceof RegExp ||
act instanceof RegExp ||
exp instanceof Error ||
act instanceof Error
act instanceof Error ||
typeof act === "bigint" ||
(exp && typeof exp === "object" && exp[serialized])
) {
const left = ppJSON(exp);
const right = ppJSON(act);
Expand Down

0 comments on commit 5156d3e

Please sign in to comment.