diff --git a/scripts/build/config.js b/scripts/build/config.js index f5f31626b..85946cf8f 100644 --- a/scripts/build/config.js +++ b/scripts/build/config.js @@ -256,24 +256,46 @@ const pluginFiles = [ input: "src/plugins/acorn.js", replaceModule: [ { - module: require.resolve("espree"), - process: (text) => - text - .replaceAll( - /exports\.(?:Syntax|VisitorKeys|latestEcmaVersion|supportedEcmaVersions|tokenize|version) = .*?;/g, - "", - ) - .replaceAll( - /const (Syntax|VisitorKeys|latestEcmaVersion|supportedEcmaVersions) = /g, - "const $1 = undefined && ", - ) - .replace("require('eslint-visitor-keys')", "{}"), + module: await resolveEsmModulePath("espree"), + process(text) { + const lines = text.split("\n"); + + let lineIndex; + + // Remove `eslint-visitor-keys` + lineIndex = lines.findIndex((line) => + line.endsWith(' from "eslint-visitor-keys";'), + ); + lines.splice(lineIndex, 1); + + // Remove code after `// Public` + lineIndex = lines.indexOf("// Public") - 1; + lines.length = lineIndex; + + // Save code after `// Parser` + lineIndex = lines.indexOf("// Parser") - 1; + const parserCodeLines = lines.slice(lineIndex); + lines.length = lineIndex; + + // Remove code after `// Tokenizer` + lineIndex = lines.indexOf("// Tokenizer") - 1; + lines.length = lineIndex; + + text = [...lines, ...parserCodeLines].join("\n"); + + return text; + }, }, { // We don't use value of JSXText module: getPackageFile("acorn-jsx/xhtml.js"), text: "module.exports = {};", }, + { + module: getPackageFile("acorn-jsx/index.js"), + find: 'require("acorn")', + replacement: "undefined", + }, ], }, { diff --git a/src/language-js/parse/acorn.js b/src/language-js/parse/acorn.js index 28e7d9916..aafc0054a 100644 --- a/src/language-js/parse/acorn.js +++ b/src/language-js/parse/acorn.js @@ -1,12 +1,11 @@ -import { createRequire } from "node:module"; +import { Parser as AcornParser } from "acorn"; +import acornJsx from "acorn-jsx"; import createError from "../../common/parser-create-error.js"; import tryCombinations from "../../utils/try-combinations.js"; import createParser from "./utils/create-parser.js"; import postprocess from "./postprocess/index.js"; import getSourceType from "./utils/get-source-type.js"; -const require = createRequire(import.meta.url); - /** @type {import("acorn").Options} */ const parseOptions = { ecmaVersion: "latest", @@ -36,13 +35,10 @@ function createParseError(error) { }); } +/** @type {ReturnType | undefined} */ let parser; const getParser = () => { - if (!parser) { - const { Parser: AcornParser } = require("acorn"); - const acornJsx = require("acorn-jsx"); - parser = AcornParser.extend(acornJsx()); - } + parser ??= AcornParser.extend(acornJsx()); return parser; }; @@ -52,7 +48,6 @@ function parseWithOptions(text, sourceType) { const comments = []; const tokens = []; - /** @type {any} */ const ast = parser.parse(text, { ...parseOptions, sourceType, @@ -60,7 +55,10 @@ function parseWithOptions(text, sourceType) { onComment: comments, onToken: tokens, }); + + // @ts-expect-error -- expected ast.comments = comments; + // @ts-expect-error -- expected ast.tokens = tokens; return ast; diff --git a/src/language-js/parse/espree.js b/src/language-js/parse/espree.js index 476330462..e9eb33b3b 100644 --- a/src/language-js/parse/espree.js +++ b/src/language-js/parse/espree.js @@ -1,12 +1,10 @@ -import { createRequire } from "node:module"; +import { parse as espreeParse } from "espree"; import createError from "../../common/parser-create-error.js"; import tryCombinations from "../../utils/try-combinations.js"; import createParser from "./utils/create-parser.js"; import postprocess from "./postprocess/index.js"; import getSourceType from "./utils/get-source-type.js"; -const require = createRequire(import.meta.url); - /** @type {import("espree").Options} */ const parseOptions = { ecmaVersion: "latest", @@ -37,8 +35,6 @@ function createParseError(error) { } function parse(text, options = {}) { - const { parse: espreeParse } = require("espree"); - const sourceType = getSourceType(options); // prettier-ignore const combinations = ( diff --git a/src/utils/try-combinations.js b/src/utils/try-combinations.js index 7ef0871b6..51709a1e1 100644 --- a/src/utils/try-combinations.js +++ b/src/utils/try-combinations.js @@ -1,3 +1,8 @@ +/** + * @template {function} T + * @param {T[]} combinations + * @returns {ReturnType} + */ function tryCombinations(combinations) { const errors = []; for (const fn of combinations) {