From 6016d166f3b7b906f0657c23a36aecb2dab83102 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Fri, 6 Dec 2019 13:05:34 -0500 Subject: [PATCH] Add estree plugin tests --- .../fixtures/estree/bigInt/basic/input.js | 1 + .../fixtures/estree/bigInt/basic/output.json | 102 ++++++++++++++++++ .../test/fixtures/estree/bigInt/options.json | 6 ++ .../test/helpers/runFixtureTests.js | 29 +++-- 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/estree/bigInt/basic/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/bigInt/basic/output.json create mode 100644 packages/babel-parser/test/fixtures/estree/bigInt/options.json diff --git a/packages/babel-parser/test/fixtures/estree/bigInt/basic/input.js b/packages/babel-parser/test/fixtures/estree/bigInt/basic/input.js new file mode 100644 index 000000000000..b01de9749f99 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/bigInt/basic/input.js @@ -0,0 +1 @@ +const a = 1n; diff --git a/packages/babel-parser/test/fixtures/estree/bigInt/basic/output.json b/packages/babel-parser/test/fixtures/estree/bigInt/basic/output.json new file mode 100644 index 000000000000..5f2108ad0862 --- /dev/null +++ b/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" + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/estree/bigInt/options.json b/packages/babel-parser/test/fixtures/estree/bigInt/options.json new file mode 100644 index 000000000000..da7e9e174f39 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/bigInt/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "estree", + "bigInt" + ] +} diff --git a/packages/babel-parser/test/helpers/runFixtureTests.js b/packages/babel-parser/test/helpers/runFixtureTests.js index f08ed2312101..e22584368be5 100644 --- a/packages/babel-parser/test/helpers/runFixtureTests.js +++ b/packages/babel-parser/test/helpers/runFixtureTests.js @@ -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); @@ -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, " "), + ); } } @@ -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, " "), + ), ), ); } @@ -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; } @@ -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); @@ -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) {