Get ride of require() in acorn and espree parser (#15335)
This commit is contained in:
1 parent
094b30c06f
commit
6184a1ed42
4 files changed
+47
-26
No files matched your search
+34
-12
@@ -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",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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<AcornParser.extend> | 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;
|
||||
|
||||
@@ -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 = (
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
/**
|
||||
* @template {function} T
|
||||
* @param {T[]} combinations
|
||||
* @returns {ReturnType<T>}
|
||||
*/
|
||||
function tryCombinations(combinations) {
|
||||
const errors = [];
|
||||
for (const fn of combinations) {
|
||||
|
||||
Reference in new issue
Block a user