Skip to content

Commit

Permalink
Add estree plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Dec 6, 2019
1 parent 77801f5 commit 6016d16
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
@@ -0,0 +1 @@
const a = 1n;
102 changes: 102 additions & 0 deletions packages/babel-parser/test/fixtures/estree/bigInt/basic/output.json
@@ -0,0 +1,102 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 12
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "a"
},
"name": "a"
},
"init": {
"type": "Literal",
"start": 10,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 12
}
},
"value": null,
"raw": "1n",
"bigint": "1"
}
}
],
"kind": "const"
}
]
}
}
@@ -0,0 +1,6 @@
{
"plugins": [
"estree",
"bigInt"
]
}
29 changes: 23 additions & 6 deletions packages/babel-parser/test/helpers/runFixtureTests.js
Expand Up @@ -41,6 +41,11 @@ class FixtureError extends Error {
}
}

// https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
function bigIntReplacer(key, value) {
return typeof value === "bigint" ? null : value;
}

export function runFixtureTests(fixturesPath, parseFunction) {
const fixtures = getFixtures(fixturesPath);

Expand All @@ -61,7 +66,10 @@ export function runFixtureTests(fixturesPath, parseFunction) {
/^.*Got error message: /,
"",
);
fs.writeFileSync(fn, JSON.stringify(task.options, null, " "));
fs.writeFileSync(
fn,
JSON.stringify(task.options, bigIntReplacer, " "),
);
}
}

Expand Down Expand Up @@ -110,7 +118,10 @@ function save(test, ast) {
// Ensure that RegExp and Errors are serialized as strings
forceToString(RegExp, () =>
forceToString(Error, () =>
fs.writeFileSync(test.expect.loc, JSON.stringify(ast, null, " ")),
fs.writeFileSync(
test.expect.loc,
JSON.stringify(ast, bigIntReplacer, " "),
),
),
);
}
Expand Down Expand Up @@ -143,7 +154,10 @@ function runTest(test, parseFunction) {
const fn = path.dirname(test.expect.loc) + "/options.json";
test.options = test.options || {};
test.options.throws = err.message;
fs.writeFileSync(fn, JSON.stringify(test.options, null, " "));
fs.writeFileSync(
fn,
JSON.stringify(test.options, bigIntReplacer, " "),
);
return;
}

Expand Down Expand Up @@ -172,11 +186,14 @@ function runTest(test, parseFunction) {
const fn = path.dirname(test.expect.loc) + "/options.json";
test.options = test.options || {};
delete test.options.throws;
const contents = JSON.stringify(test.options, null, " ");
const contents = JSON.stringify(test.options, bigIntReplacer, " ");
if (contents === "{}") {
fs.unlinkSync(fn);
} else {
fs.writeFileSync(fn, JSON.stringify(test.options, null, " "));
fs.writeFileSync(
fn,
JSON.stringify(test.options, bigIntReplacer, " "),
);
}
test.expect.loc += "on";
return save(test, ast);
Expand All @@ -199,7 +216,7 @@ function runTest(test, parseFunction) {

function ppJSON(v) {
v = v instanceof RegExp || v instanceof Error ? v.toString() : v;
return JSON.stringify(v, null, 2);
return JSON.stringify(v, bigIntReplacer, 2);
}

function addPath(str, pt) {
Expand Down

0 comments on commit 6016d16

Please sign in to comment.