Use sourceType: commonjs in acorn parser (#18906)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-03-05 01:26:48 +08:00
1 parent af93db5a61
commit 1038b1231b
5 files changed
+6 -25

No files matched your search

+3 -1
View File
@@ -4,7 +4,7 @@ import spawn from "nano-spawn";
async function getChangedFiles() {
const { stdout } = await spawn("git", ["diff", "--name-only"]);
return stdout.split("\n");
return stdout ? stdout.split("\n") : [];
}
async function ensureNoFilesChanged() {
@@ -26,6 +26,8 @@ async function ensureNoFilesChanged() {
} catch (error) {
process.exitCode = error.exitCode;
}
process.exitCode ||= 1;
}
await ensureNoFilesChanged();
+3 -6
View File
@@ -7,13 +7,12 @@ import createParser from "./utilities/create-parser.js";
import {
getSourceType,
SOURCE_TYPE_COMBINATIONS,
SOURCE_TYPE_COMMONJS,
SOURCE_TYPE_MODULE,
SOURCE_TYPE_SCRIPT,
} from "./utilities/source-types.js";
/**
@import {Options} from "acorn";
@import {Options} from "acorn"
@import {SOURCE_TYPE_COMMONJS} from "./utilities/source-types.js"
*/
/** @type {Options} */
@@ -70,9 +69,7 @@ function parseWithOptions(text, sourceType) {
const ast = parser.parse(text, {
...parseOptions,
// https://github.com/acornjs/acorn/pull/1377 not released yet
sourceType:
sourceType === SOURCE_TYPE_COMMONJS ? SOURCE_TYPE_SCRIPT : sourceType,
sourceType,
allowImportExportEverywhere: sourceType === SOURCE_TYPE_MODULE,
onComment: comments,
});
@@ -1,5 +1,4 @@
export const SOURCE_TYPE_MODULE = /** @type {const} */ ("module");
export const SOURCE_TYPE_SCRIPT = /** @type {const} */ ("script");
export const SOURCE_TYPE_COMMONJS = /** @type {const} */ ("commonjs");
/** @type {readonly [SOURCE_TYPE_MODULE, SOURCE_TYPE_COMMONJS]} */
@@ -13,14 +13,6 @@ const x = new.target;
================================================================================
`;
exports[`outside-functions.js format[acorn] 1`] = `
"'new.target' can only be used in functions and class static block (1:11)
> 1 | const x = new.target;
| ^
2 |
Cause: 'new.target' can only be used in functions and class static block (1:10)"
`;
exports[`outside-functions.js format[espree] 1`] = `
"'new.target' can only be used in functions and class static block (1:11)
> 1 | const x = new.target;
@@ -72,14 +64,6 @@ exports[`range.js format 1`] = `
: ================================================================================
`;
exports[`range.js format[acorn] 1`] = `
"'new.target' can only be used in functions and class static block (2:1)
1 | const x =
> 2 | new.target;
| ^
Cause: 'new.target' can only be used in functions and class static block (2:0)"
`;
exports[`range.js format[espree] 1`] = `
"'new.target' can only be used in functions and class static block (2:1)
1 | const x =
@@ -1,6 +1,5 @@
runFormatTest(import.meta, ["babel", "flow", "typescript"], {
errors: {
acorn: true,
espree: true,
flow: true,
hermes: true,