From 0f9a1aefee242fe2185c74a1e7a33ded05b97176 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 27 Sep 2024 20:46:37 +0800 Subject: [PATCH] Rewrite tests for importing JSON (#16517) --- .../__snapshots__/format.test.js.snap | 54 +++++++++++++++++++ .../js/import-assertions/format.test.js | 3 ++ .../js/import-assertions/keyword-detect.js | 6 +++ .../__snapshots__/format.test.js.snap | 34 +++++------- .../js/import-attributes/keyword-detect.js | 8 +-- 5 files changed, 78 insertions(+), 27 deletions(-) create mode 100644 tests/format/js/import-assertions/keyword-detect.js diff --git a/tests/format/js/import-assertions/__snapshots__/format.test.js.snap b/tests/format/js/import-assertions/__snapshots__/format.test.js.snap index d136619cf..4fd518858 100644 --- a/tests/format/js/import-assertions/__snapshots__/format.test.js.snap +++ b/tests/format/js/import-assertions/__snapshots__/format.test.js.snap @@ -88,6 +88,60 @@ import * as baz from "baz.json" /* comment */ assert {}; ================================================================================ `; +exports[`keyword-detect.js [acorn] format 1`] = ` +"Unexpected token (1:33) +> 1 | import "./test.json" /* with */ assert /* with */ { type: "json" } + | ^ + 2 | import {default as b} from "./test.json" /* with */ assert /* with */ { type: "json" } + 3 | + 4 | export {default as e} from "./test.json" /* with */ assert /* with */ { type: "json" } +Cause: Unexpected token (1:32)" +`; + +exports[`keyword-detect.js [espree] format 1`] = ` +"Unexpected token assert (1:33) +> 1 | import "./test.json" /* with */ assert /* with */ { type: "json" } + | ^ + 2 | import {default as b} from "./test.json" /* with */ assert /* with */ { type: "json" } + 3 | + 4 | export {default as e} from "./test.json" /* with */ assert /* with */ { type: "json" } +Cause: Unexpected token assert" +`; + +exports[`keyword-detect.js [meriyah] format 1`] = ` +"Unexpected token: 'identifier' (1:33) +> 1 | import "./test.json" /* with */ assert /* with */ { type: "json" } + | ^^^^^^ + 2 | import {default as b} from "./test.json" /* with */ assert /* with */ { type: "json" } + 3 | + 4 | export {default as e} from "./test.json" /* with */ assert /* with */ { type: "json" } +Cause: [1:32-1:38]: Unexpected token: 'identifier'" +`; + +exports[`keyword-detect.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +import "./test.json" /* with */ assert /* with */ { type: "json" } +import {default as b} from "./test.json" /* with */ assert /* with */ { type: "json" } + +export {default as e} from "./test.json" /* with */ assert /* with */ { type: "json" } + +export * from "./test.json" /* with */ assert /* with */ { type: "json" } + +=====================================output===================================== +import "./test.json" /* with */ /* with */ assert { type: "json" }; +import { default as b } from "./test.json" /* with */ /* with */ assert { type: "json" }; + +export { default as e } from "./test.json" /* with */ /* with */ assert { type: "json" }; + +export * from "./test.json" /* with */ /* with */ assert { type: "json" }; + +================================================================================ +`; + exports[`multi-types.js [acorn] format 1`] = ` "Unexpected token (1:31) > 1 | import json from "./foo.json" assert { type: "json", type: "bar" }; diff --git a/tests/format/js/import-assertions/format.test.js b/tests/format/js/import-assertions/format.test.js index 9235cec47..057403037 100644 --- a/tests/format/js/import-assertions/format.test.js +++ b/tests/format/js/import-assertions/format.test.js @@ -8,6 +8,7 @@ runFormatTest(import.meta, ["babel", "typescript"], { "re-export.js", "without-from.js", "non-type.js", + "keyword-detect.js", ], espree: [ "dynamic-import.js", @@ -17,6 +18,7 @@ runFormatTest(import.meta, ["babel", "typescript"], { "re-export.js", "without-from.js", "non-type.js", + "keyword-detect.js", ], meriyah: [ "empty.js", @@ -25,6 +27,7 @@ runFormatTest(import.meta, ["babel", "typescript"], { "re-export.js", "without-from.js", "non-type.js", + "keyword-detect.js", ], }, }); diff --git a/tests/format/js/import-assertions/keyword-detect.js b/tests/format/js/import-assertions/keyword-detect.js new file mode 100644 index 000000000..1f12de365 --- /dev/null +++ b/tests/format/js/import-assertions/keyword-detect.js @@ -0,0 +1,6 @@ +import "./test.json" /* with */ assert /* with */ { type: "json" } +import {default as b} from "./test.json" /* with */ assert /* with */ { type: "json" } + +export {default as e} from "./test.json" /* with */ assert /* with */ { type: "json" } + +export * from "./test.json" /* with */ assert /* with */ { type: "json" } diff --git a/tests/format/js/import-attributes/__snapshots__/format.test.js.snap b/tests/format/js/import-attributes/__snapshots__/format.test.js.snap index f06352769..b0c7a07d7 100644 --- a/tests/format/js/import-attributes/__snapshots__/format.test.js.snap +++ b/tests/format/js/import-attributes/__snapshots__/format.test.js.snap @@ -82,9 +82,9 @@ exports[`keyword-detect.js [acorn] format 1`] = ` "Unexpected token (1:35) > 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" } | ^ - 2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" } - 3 | import "./test.json" /* with */ assert /* with */ { type: "json" } - 4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" } + 2 | import a from "./test.json" /* assert */ with /* assert */ { type: "json" } + 3 | + 4 | export {default as c} from "./test.json" /* assert */ with /* assert */ { type: "json" } Cause: Unexpected token (1:34)" `; @@ -92,9 +92,9 @@ exports[`keyword-detect.js [espree] format 1`] = ` "Unexpected token with (1:35) > 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" } | ^ - 2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" } - 3 | import "./test.json" /* with */ assert /* with */ { type: "json" } - 4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" } + 2 | import a from "./test.json" /* assert */ with /* assert */ { type: "json" } + 3 | + 4 | export {default as c} from "./test.json" /* assert */ with /* assert */ { type: "json" } Cause: Unexpected token with" `; @@ -102,9 +102,9 @@ exports[`keyword-detect.js [meriyah] format 1`] = ` "Invalid binding in JSON import (1:69) > 1 | import "./test.json" /* assert */ with /* assert */ { type: "json" } | ^ - 2 | import {} from "./test.json" /* assert */ with /* assert */ { type: "json" } - 3 | import "./test.json" /* with */ assert /* with */ { type: "json" } - 4 | import {} from "./test.json" /* with */ assert /* with */ { type: "json" } + 2 | import a from "./test.json" /* assert */ with /* assert */ { type: "json" } + 3 | + 4 | export {default as c} from "./test.json" /* assert */ with /* assert */ { type: "json" } Cause: [1:68-1:69]: Invalid binding in JSON import" `; @@ -115,27 +115,19 @@ printWidth: 80 | printWidth =====================================input====================================== import "./test.json" /* assert */ with /* assert */ { type: "json" } -import {} from "./test.json" /* assert */ with /* assert */ { type: "json" } -import "./test.json" /* with */ assert /* with */ { type: "json" } -import {} from "./test.json" /* with */ assert /* with */ { type: "json" } +import a from "./test.json" /* assert */ with /* assert */ { type: "json" } -export {} from "./test.json" /* assert */ with /* assert */ { type: "json" } -export {} from "./test.json" /* with */ assert /* with */ { type: "json" } +export {default as c} from "./test.json" /* assert */ with /* assert */ { type: "json" } export * from "./test.json" /* assert */ with /* assert */ { type: "json" } -export * from "./test.json" /* with */ assert /* with */ { type: "json" } =====================================output===================================== import "./test.json" /* assert */ /* assert */ with { type: "json" }; -import {} from "./test.json" /* assert */ /* assert */ with { type: "json" }; -import "./test.json" /* with */ /* with */ assert { type: "json" }; -import {} from "./test.json" /* with */ /* with */ assert { type: "json" }; +import a from "./test.json" /* assert */ /* assert */ with { type: "json" }; -export {} from "./test.json" /* assert */ /* assert */ with { type: "json" }; -export {} from "./test.json" /* with */ /* with */ assert { type: "json" }; +export { default as c } from "./test.json" /* assert */ /* assert */ with { type: "json" }; export * from "./test.json" /* assert */ /* assert */ with { type: "json" }; -export * from "./test.json" /* with */ /* with */ assert { type: "json" }; ================================================================================ `; diff --git a/tests/format/js/import-attributes/keyword-detect.js b/tests/format/js/import-attributes/keyword-detect.js index 4d0ed0657..2db5299c8 100644 --- a/tests/format/js/import-attributes/keyword-detect.js +++ b/tests/format/js/import-attributes/keyword-detect.js @@ -1,10 +1,6 @@ import "./test.json" /* assert */ with /* assert */ { type: "json" } -import {} from "./test.json" /* assert */ with /* assert */ { type: "json" } -import "./test.json" /* with */ assert /* with */ { type: "json" } -import {} from "./test.json" /* with */ assert /* with */ { type: "json" } +import a from "./test.json" /* assert */ with /* assert */ { type: "json" } -export {} from "./test.json" /* assert */ with /* assert */ { type: "json" } -export {} from "./test.json" /* with */ assert /* with */ { type: "json" } +export {default as c} from "./test.json" /* assert */ with /* assert */ { type: "json" } export * from "./test.json" /* assert */ with /* assert */ { type: "json" } -export * from "./test.json" /* with */ assert /* with */ { type: "json" }