Skip to content

Commit 2bcc3c2

Browse files
authored
User sourceType: 'commonjs' where possible (#18333)
1 parent ce68b2b commit 2bcc3c2

File tree

7 files changed

+47
-37
lines changed

7 files changed

+47
-37
lines changed

src/language-js/parse/acorn.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import createParser from "./utils/create-parser.js";
77
import {
88
getSourceType,
99
SOURCE_TYPE_COMBINATIONS,
10+
SOURCE_TYPE_COMMONJS,
1011
SOURCE_TYPE_MODULE,
12+
SOURCE_TYPE_SCRIPT,
1113
} from "./utils/source-types.js";
1214

13-
/** @import {Options} from "acorn" */
15+
/**
16+
@import {Options} from "acorn";
17+
*/
1418

1519
/** @type {Options} */
1620
const parseOptions = {
@@ -55,14 +59,20 @@ const getParser = () => {
5559
return parser;
5660
};
5761

62+
/**
63+
@param {string} text
64+
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS | undefined} sourceType
65+
*/
5866
function parseWithOptions(text, sourceType) {
5967
const parser = getParser();
6068

6169
const comments = [];
6270

6371
const ast = parser.parse(text, {
6472
...parseOptions,
65-
sourceType,
73+
// https://github.com/acornjs/acorn/pull/1377 not released yet
74+
sourceType:
75+
sourceType === SOURCE_TYPE_COMMONJS ? SOURCE_TYPE_SCRIPT : sourceType,
6676
allowImportExportEverywhere: sourceType === SOURCE_TYPE_MODULE,
6777
onComment: comments,
6878
});

src/language-js/parse/babel.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import createBabelParseError from "./utils/create-babel-parse-error.js";
77
import createParser from "./utils/create-parser.js";
88
import {
99
getSourceType,
10+
SOURCE_TYPE_COMMONJS,
1011
SOURCE_TYPE_MODULE,
11-
SOURCE_TYPE_SCRIPT,
1212
} from "./utils/source-types.js";
1313
import wrapBabelExpression from "./utils/wrap-babel-expression.js";
1414

@@ -121,10 +121,17 @@ function createParse({ isExpression = false, optionsCombinations }) {
121121

122122
let combinations = optionsCombinations;
123123
const sourceType = options.__babelSourceType ?? getSourceType(filepath);
124-
if (sourceType === SOURCE_TYPE_SCRIPT) {
124+
if (sourceType && sourceType !== SOURCE_TYPE_MODULE) {
125125
combinations = combinations.map((options) => ({
126126
...options,
127127
sourceType,
128+
// `sourceType: "commonjs"` does not allow these two properties
129+
...(sourceType === SOURCE_TYPE_COMMONJS
130+
? {
131+
allowReturnOutsideFunction: undefined,
132+
allowNewTargetOutsideFunction: undefined,
133+
}
134+
: undefined),
128135
}));
129136
}
130137

src/language-js/parse/espree.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const parseOptions = {
1919
tokens: false,
2020
ecmaFeatures: {
2121
jsx: true,
22-
globalReturn: true,
2322
impliedStrict: false,
2423
},
2524
};

src/language-js/parse/meriyah.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import createParser from "./utils/create-parser.js";
66
import {
77
getSourceType,
88
SOURCE_TYPE_COMBINATIONS,
9-
SOURCE_TYPE_MODULE,
109
} from "./utils/source-types.js";
1110

1211
/**
1312
@import {ESTree as MeriyahESTree} from "meriyah";
14-
@import {SOURCE_TYPE_SCRIPT} from "./utils/source-types.js";
13+
@import {SOURCE_TYPE_MODULE, SOURCE_TYPE_COMMONJS} from "./utils/source-types.js";
1514
*/
1615

1716
// https://github.com/meriyah/meriyah/blob/4676f60b6c149d7082bde2c9147f9ae2359c8075/src/parser.ts#L185
@@ -46,15 +45,15 @@ const parseOptions = {
4645

4746
/**
4847
@param {string} text
49-
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_SCRIPT} sourceType
48+
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS} sourceType
5049
*/
5150
function parseWithOptions(text, sourceType) {
5251
/** @type {MeriyahESTree.Comment[]} */
5352
const comments = [];
5453

5554
const ast = meriyahParse(text, {
5655
...parseOptions,
57-
sourceType: sourceType === SOURCE_TYPE_MODULE ? sourceType : "commonjs",
56+
sourceType,
5857
onComment: comments,
5958
});
6059

src/language-js/parse/oxc.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ import { tryCombinations } from "../../utils/try-combinations.js";
55
import postprocess from "./postprocess/index.js";
66
import createParser from "./utils/create-parser.js";
77
import jsxRegexp from "./utils/jsx-regexp.evaluate.js";
8-
import { getSourceType } from "./utils/source-types.js";
8+
import {
9+
getSourceType,
10+
SOURCE_TYPE_COMMONJS,
11+
SOURCE_TYPE_SCRIPT,
12+
} from "./utils/source-types.js";
913

1014
/** @import {ParseResult, ParserOptions as ParserOptionsWithoutExperimentalRawTransfer} from "oxc-parser" */
11-
/** @typedef {ParserOptionsWithoutExperimentalRawTransfer & {experimentalRawTransfer?: boolean}} ParserOptions */
15+
/** @typedef {Omit<ParserOptionsWithoutExperimentalRawTransfer, "sourceType"> & {
16+
sourceType?: ParserOptionsWithoutExperimentalRawTransfer["sourceType"] | "commonjs",
17+
experimentalRawTransfer?: boolean,
18+
}} ParserOptions */
1219

1320
function createParseError(error, { text }) {
1421
/* c8 ignore next 3 */
@@ -41,10 +48,17 @@ function createParseError(error, { text }) {
4148
@returns {Promise<ParseResult>}
4249
*/
4350
async function parseWithOptions(filepath, text, options) {
51+
let { sourceType } = options;
52+
// https://github.com/oxc-project/oxc/issues/16200
53+
if (sourceType === SOURCE_TYPE_COMMONJS) {
54+
sourceType = SOURCE_TYPE_SCRIPT;
55+
}
56+
4457
const result = await oxcParse(filepath, text, {
4558
preserveParens: true,
4659
showSemanticErrors: false,
4760
...options,
61+
sourceType,
4862
});
4963

5064
const { errors } = result;
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
export const SOURCE_TYPE_MODULE = "module";
2-
export const SOURCE_TYPE_SCRIPT = "script";
1+
export const SOURCE_TYPE_MODULE = /** @type {const} */ ("module");
2+
export const SOURCE_TYPE_SCRIPT = /** @type {const} */ ("script");
3+
export const SOURCE_TYPE_COMMONJS = /** @type {const} */ ("commonjs");
34

4-
/** @type {["module", "script"]} */
5+
/** @type {readonly [SOURCE_TYPE_MODULE, SOURCE_TYPE_COMMONJS]} */
56
export const SOURCE_TYPE_COMBINATIONS = [
67
SOURCE_TYPE_MODULE,
7-
SOURCE_TYPE_SCRIPT,
8+
SOURCE_TYPE_COMMONJS,
89
];
910

10-
/** @returns {"module" | "script" | undefined} */
11+
/** @returns {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS | undefined} */
1112
export function getSourceType(filepath) {
1213
if (typeof filepath !== "string") {
1314
return;
@@ -20,6 +21,6 @@ export function getSourceType(filepath) {
2021
}
2122

2223
if (/\.(?:cjs|cts)$/iu.test(filepath)) {
23-
return SOURCE_TYPE_SCRIPT;
24+
return SOURCE_TYPE_COMMONJS;
2425
}
2526
}

types/espree/index.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)