From 19a0444ce472a7c97557c5a8413fdeddbbe7cbcd Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 2 Aug 2026 16:02:35 +0800 Subject: [PATCH] Add test for commonjs features (#19766) --- .../__snapshots__/format.test.js.snap | 244 ++++++++++++++++++ tests/format/js/commonjs/format.test.js | 44 ++++ 2 files changed, 288 insertions(+) create mode 100644 tests/format/js/commonjs/__snapshots__/format.test.js.snap create mode 100644 tests/format/js/commonjs/format.test.js diff --git a/tests/format/js/commonjs/__snapshots__/format.test.js.snap b/tests/format/js/commonjs/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..011372f1b --- /dev/null +++ b/tests/format/js/commonjs/__snapshots__/format.test.js.snap @@ -0,0 +1,244 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`snippet: new-target.cjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +new.target +=====================================output===================================== +new.target; + +================================================================================ +`; + +exports[`snippet: new-target.cjs format[flow] 1`] = ` +"Unexpected token \`.\` (1:4) +> 1 | new.target + | ^ +Cause: Unexpected token \`.\` (1:3)" +`; + +exports[`snippet: new-target.cjs format[hermes] 1`] = ` +"'new.target' not in a function (1:1) +> 1 | new.target + | ^ +Cause: 'new.target' not in a function (1:0) +new.target +^~~~~~~~~~" +`; + +exports[`snippet: new-target.mjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +new.target +=====================================output===================================== +new.target; + +================================================================================ +`; + +exports[`snippet: new-target.mjs format[acorn] 1`] = ` +"'new.target' can only be used in functions and class static block (1:1) +> 1 | new.target + | ^ +Cause: 'new.target' can only be used in functions and class static block (1:0)" +`; + +exports[`snippet: new-target.mjs format[espree] 1`] = ` +"'new.target' can only be used in functions and class static block (1:1) +> 1 | new.target + | ^ +Cause: 'new.target' can only be used in functions and class static block" +`; + +exports[`snippet: new-target.mjs format[flow] 1`] = ` +"Unexpected token \`.\` (1:4) +> 1 | new.target + | ^ +Cause: Unexpected token \`.\` (1:3)" +`; + +exports[`snippet: new-target.mjs format[hermes] 1`] = ` +"'new.target' not in a function (1:1) +> 1 | new.target + | ^ +Cause: 'new.target' not in a function (1:0) +new.target +^~~~~~~~~~" +`; + +exports[`snippet: new-target.mjs format[meriyah] 1`] = ` +"new.target only allowed within functions or static blocks (1:5) +> 1 | new.target + | ^^^^^^ +Cause: [1:4-1:10]: new.target only allowed within functions or static blocks" +`; + +exports[`snippet: new-target.mjs format[oxc] 1`] = ` +"Unexpected new.target expression (1:1) +> 1 | new.target + | ^^^^^^^^^^" +`; + +exports[`snippet: new-target.mjs format[oxc-ts] 1`] = ` +"Unexpected new.target expression (1:1) +> 1 | new.target + | ^^^^^^^^^^" +`; + +exports[`snippet: new-target.unknown format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +new.target +=====================================output===================================== +new.target; + +================================================================================ +`; + +exports[`snippet: new-target.unknown format[flow] 1`] = ` +"Unexpected token \`.\` (1:4) +> 1 | new.target + | ^ +Cause: Unexpected token \`.\` (1:3)" +`; + +exports[`snippet: new-target.unknown format[hermes] 1`] = ` +"'new.target' not in a function (1:1) +> 1 | new.target + | ^ +Cause: 'new.target' not in a function (1:0) +new.target +^~~~~~~~~~" +`; + +exports[`snippet: return.cjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +return +=====================================output===================================== +return; + +================================================================================ +`; + +exports[`snippet: return.mjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +return +=====================================output===================================== +return; + +================================================================================ +`; + +exports[`snippet: return.mjs format[espree] 1`] = ` +"'return' outside of function (1:1) +> 1 | return + | ^ +Cause: 'return' outside of function" +`; + +exports[`snippet: return.mjs format[meriyah] 1`] = ` +"Illegal return statement (1:1) +> 1 | return + | ^^^^^^ +Cause: [1:0-1:6]: Illegal return statement" +`; + +exports[`snippet: return.mjs format[yuku] 1`] = ` +"'return' statement is only valid inside a function (1:1) +> 1 | return + | ^^^^^^" +`; + +exports[`snippet: return.mjs format[yuku-ts] 1`] = ` +"'return' statement is only valid inside a function (1:1) +> 1 | return + | ^^^^^^" +`; + +exports[`snippet: return.unknown format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +return +=====================================output===================================== +return; + +================================================================================ +`; + +exports[`snippet: using.cjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +using foo = bar +=====================================output===================================== +using foo = bar; + +================================================================================ +`; + +exports[`snippet: using.cjs format[flow] 1`] = ` +"Unexpected identifier, expected the end of an expression statement (\`;\`) (1:7) +> 1 | using foo = bar + | ^ +Cause: Unexpected identifier, expected the end of an expression statement (\`;\`) (1:6)" +`; + +exports[`snippet: using.cjs format[meriyah] 1`] = ` +"Unexpected token: 'identifier' (1:7) +> 1 | using foo = bar + | ^^^ +Cause: [1:6-1:9]: Unexpected token: 'identifier'" +`; + +exports[`snippet: using.mjs format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +using foo = bar +=====================================output===================================== +using foo = bar; + +================================================================================ +`; + +exports[`snippet: using.mjs format[flow] 1`] = ` +"Unexpected identifier, expected the end of an expression statement (\`;\`) (1:7) +> 1 | using foo = bar + | ^ +Cause: Unexpected identifier, expected the end of an expression statement (\`;\`) (1:6)" +`; + +exports[`snippet: using.unknown format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +using foo = bar +=====================================output===================================== +using foo = bar; + +================================================================================ +`; + +exports[`snippet: using.unknown format[flow] 1`] = ` +"Unexpected identifier, expected the end of an expression statement (\`;\`) (1:7) +> 1 | using foo = bar + | ^ +Cause: Unexpected identifier, expected the end of an expression statement (\`;\`) (1:6)" +`; diff --git a/tests/format/js/commonjs/format.test.js b/tests/format/js/commonjs/format.test.js new file mode 100644 index 000000000..5635fb99b --- /dev/null +++ b/tests/format/js/commonjs/format.test.js @@ -0,0 +1,44 @@ +const tests = [ + { + code: "return", + baseFilename: "return", + }, + { + code: "new.target", + baseFilename: "new-target", + }, + { + code: "using foo = bar", + baseFilename: "using", + }, +].flatMap(({ code, baseFilename }) => + [".cjs", ".mjs", ".unknown"].map((extension) => { + const filename = `${baseFilename}${extension}`; + return { name: filename, code, filename }; + }), +); + +runFormatTest( + { importMeta: import.meta, snippets: tests }, + ["babel", "flow", "typescript"], + { + errors: { + acorn: ["new-target.mjs"], + espree: ["return.mjs", "new-target.mjs"], + meriyah: ["using.cjs", "return.mjs", "new-target.mjs"], + flow: [ + "new-target.cjs", + "new-target.mjs", + "new-target.unknown", + "using.cjs", + "using.mjs", + "using.unknown", + ], + hermes: ["new-target.cjs", "new-target.mjs", "new-target.unknown"], + oxc: ["new-target.mjs"], + "oxc-ts": ["new-target.mjs"], + yuku: ["return.mjs"], + "yuku-ts": ["return.mjs"], + }, + }, +);