User sourceType: 'commonjs' where possible (#18333)
This commit is contained in:
1 parent
ce68b2b07d
commit
2bcc3c2a54
7 files changed
+47
-37
No files matched your search
@@ -7,10 +7,14 @@ import createParser from "./utils/create-parser.js";
|
||||
import {
|
||||
getSourceType,
|
||||
SOURCE_TYPE_COMBINATIONS,
|
||||
SOURCE_TYPE_COMMONJS,
|
||||
SOURCE_TYPE_MODULE,
|
||||
SOURCE_TYPE_SCRIPT,
|
||||
} from "./utils/source-types.js";
|
||||
|
||||
/** @import {Options} from "acorn" */
|
||||
/**
|
||||
@import {Options} from "acorn";
|
||||
*/
|
||||
|
||||
/** @type {Options} */
|
||||
const parseOptions = {
|
||||
@@ -55,6 +59,10 @@ const getParser = () => {
|
||||
return parser;
|
||||
};
|
||||
|
||||
/**
|
||||
@param {string} text
|
||||
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS | undefined} sourceType
|
||||
*/
|
||||
function parseWithOptions(text, sourceType) {
|
||||
const parser = getParser();
|
||||
|
||||
@@ -62,7 +70,9 @@ function parseWithOptions(text, sourceType) {
|
||||
|
||||
const ast = parser.parse(text, {
|
||||
...parseOptions,
|
||||
sourceType,
|
||||
// https://github.com/acornjs/acorn/pull/1377 not released yet
|
||||
sourceType:
|
||||
sourceType === SOURCE_TYPE_COMMONJS ? SOURCE_TYPE_SCRIPT : sourceType,
|
||||
allowImportExportEverywhere: sourceType === SOURCE_TYPE_MODULE,
|
||||
onComment: comments,
|
||||
});
|
||||
|
||||
@@ -7,8 +7,8 @@ import createBabelParseError from "./utils/create-babel-parse-error.js";
|
||||
import createParser from "./utils/create-parser.js";
|
||||
import {
|
||||
getSourceType,
|
||||
SOURCE_TYPE_COMMONJS,
|
||||
SOURCE_TYPE_MODULE,
|
||||
SOURCE_TYPE_SCRIPT,
|
||||
} from "./utils/source-types.js";
|
||||
import wrapBabelExpression from "./utils/wrap-babel-expression.js";
|
||||
|
||||
@@ -121,10 +121,17 @@ function createParse({ isExpression = false, optionsCombinations }) {
|
||||
|
||||
let combinations = optionsCombinations;
|
||||
const sourceType = options.__babelSourceType ?? getSourceType(filepath);
|
||||
if (sourceType === SOURCE_TYPE_SCRIPT) {
|
||||
if (sourceType && sourceType !== SOURCE_TYPE_MODULE) {
|
||||
combinations = combinations.map((options) => ({
|
||||
...options,
|
||||
sourceType,
|
||||
// `sourceType: "commonjs"` does not allow these two properties
|
||||
...(sourceType === SOURCE_TYPE_COMMONJS
|
||||
? {
|
||||
allowReturnOutsideFunction: undefined,
|
||||
allowNewTargetOutsideFunction: undefined,
|
||||
}
|
||||
: undefined),
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ const parseOptions = {
|
||||
tokens: false,
|
||||
ecmaFeatures: {
|
||||
jsx: true,
|
||||
globalReturn: true,
|
||||
impliedStrict: false,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -6,12 +6,11 @@ import createParser from "./utils/create-parser.js";
|
||||
import {
|
||||
getSourceType,
|
||||
SOURCE_TYPE_COMBINATIONS,
|
||||
SOURCE_TYPE_MODULE,
|
||||
} from "./utils/source-types.js";
|
||||
|
||||
/**
|
||||
@import {ESTree as MeriyahESTree} from "meriyah";
|
||||
@import {SOURCE_TYPE_SCRIPT} from "./utils/source-types.js";
|
||||
@import {SOURCE_TYPE_MODULE, SOURCE_TYPE_COMMONJS} from "./utils/source-types.js";
|
||||
*/
|
||||
|
||||
// https://github.com/meriyah/meriyah/blob/4676f60b6c149d7082bde2c9147f9ae2359c8075/src/parser.ts#L185
|
||||
@@ -46,7 +45,7 @@ const parseOptions = {
|
||||
|
||||
/**
|
||||
@param {string} text
|
||||
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_SCRIPT} sourceType
|
||||
@param {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS} sourceType
|
||||
*/
|
||||
function parseWithOptions(text, sourceType) {
|
||||
/** @type {MeriyahESTree.Comment[]} */
|
||||
@@ -54,7 +53,7 @@ function parseWithOptions(text, sourceType) {
|
||||
|
||||
const ast = meriyahParse(text, {
|
||||
...parseOptions,
|
||||
sourceType: sourceType === SOURCE_TYPE_MODULE ? sourceType : "commonjs",
|
||||
sourceType,
|
||||
onComment: comments,
|
||||
});
|
||||
|
||||
|
||||
@@ -5,10 +5,17 @@ import { tryCombinations } from "../../utils/try-combinations.js";
|
||||
import postprocess from "./postprocess/index.js";
|
||||
import createParser from "./utils/create-parser.js";
|
||||
import jsxRegexp from "./utils/jsx-regexp.evaluate.js";
|
||||
import { getSourceType } from "./utils/source-types.js";
|
||||
import {
|
||||
getSourceType,
|
||||
SOURCE_TYPE_COMMONJS,
|
||||
SOURCE_TYPE_SCRIPT,
|
||||
} from "./utils/source-types.js";
|
||||
|
||||
/** @import {ParseResult, ParserOptions as ParserOptionsWithoutExperimentalRawTransfer} from "oxc-parser" */
|
||||
/** @typedef {ParserOptionsWithoutExperimentalRawTransfer & {experimentalRawTransfer?: boolean}} ParserOptions */
|
||||
/** @typedef {Omit<ParserOptionsWithoutExperimentalRawTransfer, "sourceType"> & {
|
||||
sourceType?: ParserOptionsWithoutExperimentalRawTransfer["sourceType"] | "commonjs",
|
||||
experimentalRawTransfer?: boolean,
|
||||
}} ParserOptions */
|
||||
|
||||
function createParseError(error, { text }) {
|
||||
/* c8 ignore next 3 */
|
||||
@@ -41,10 +48,17 @@ function createParseError(error, { text }) {
|
||||
@returns {Promise<ParseResult>}
|
||||
*/
|
||||
async function parseWithOptions(filepath, text, options) {
|
||||
let { sourceType } = options;
|
||||
// https://github.com/oxc-project/oxc/issues/16200
|
||||
if (sourceType === SOURCE_TYPE_COMMONJS) {
|
||||
sourceType = SOURCE_TYPE_SCRIPT;
|
||||
}
|
||||
|
||||
const result = await oxcParse(filepath, text, {
|
||||
preserveParens: true,
|
||||
showSemanticErrors: false,
|
||||
...options,
|
||||
sourceType,
|
||||
});
|
||||
|
||||
const { errors } = result;
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
export const SOURCE_TYPE_MODULE = "module";
|
||||
export const SOURCE_TYPE_SCRIPT = "script";
|
||||
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 {["module", "script"]} */
|
||||
/** @type {readonly [SOURCE_TYPE_MODULE, SOURCE_TYPE_COMMONJS]} */
|
||||
export const SOURCE_TYPE_COMBINATIONS = [
|
||||
SOURCE_TYPE_MODULE,
|
||||
SOURCE_TYPE_SCRIPT,
|
||||
SOURCE_TYPE_COMMONJS,
|
||||
];
|
||||
|
||||
/** @returns {"module" | "script" | undefined} */
|
||||
/** @returns {SOURCE_TYPE_MODULE | SOURCE_TYPE_COMMONJS | undefined} */
|
||||
export function getSourceType(filepath) {
|
||||
if (typeof filepath !== "string") {
|
||||
return;
|
||||
@@ -20,6 +21,6 @@ export function getSourceType(filepath) {
|
||||
}
|
||||
|
||||
if (/\.(?:cjs|cts)$/iu.test(filepath)) {
|
||||
return SOURCE_TYPE_SCRIPT;
|
||||
return SOURCE_TYPE_COMMONJS;
|
||||
}
|
||||
}
|
||||
Vendored
-20
@@ -1,20 +0,0 @@
|
||||
declare module "espree" {
|
||||
// https://github.com/eslint/espree#options
|
||||
export interface Options {
|
||||
range?: boolean;
|
||||
loc?: boolean;
|
||||
comment?: boolean;
|
||||
tokens?: boolean;
|
||||
ecmaVersion?: "latest";
|
||||
sourceType?: "script" | "module";
|
||||
ecmaFeatures?: {
|
||||
jsx?: boolean;
|
||||
globalReturn?: boolean;
|
||||
impliedStrict?: boolean;
|
||||
};
|
||||
}
|
||||
// https://github.com/eslint/espree#parse
|
||||
export function parse(code: string, options?: Options): any;
|
||||
// https://github.com/eslint/espree#tokenize
|
||||
export function tokenize(code: string, options?: Options): any;
|
||||
}
|
||||
Reference in new issue
Block a user