Switch to oxidized flow parser (#19398)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Sam Zhouandfisker authored and GitHub committed 2026-06-23 08:44:23 +08:00
1 parent 3b5e6d059c
commit 9c0e48cd25
53 files changed
+575 -345

No files matched your search

@@ -110,17 +110,6 @@ export default function esbuildPluginThrowWarnings({
continue;
}
if (
[
"node_modules/flow-parser/flow_parser.js",
"dist/_parser-flow.js.umd.js",
"dist/_parser-flow.js.esm.mjs",
].includes(warning.location.file) &&
warning.id === "duplicate-case"
) {
continue;
}
console.log(warning);
throw new Error(warning.text);
}
+11 -6
View File
@@ -1,4 +1,5 @@
import path from "node:path";
import { outdent } from "outdent";
import { DIST_DIR, PACKAGES_DIRECTORY } from "../../utilities/index.js";
import { createJavascriptModuleBuilder } from "../builders/javascript-module.js";
import { getPackageFile } from "../utilities.js";
@@ -27,21 +28,25 @@ const mainModule = {
{
module: getPackageFile("hermes-parser/dist/HermesParser.js"),
process(text) {
text =
'const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;' +
text;
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
${text}
`;
return text;
},
},
{
module: getPackageFile("hermes-parser/dist/HermesParserWASM.js"),
process(text) {
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
${text}
`;
text = text.replaceAll("process.argv", "[]");
text = text.replaceAll('require("fs")', "undefined");
text = text.replaceAll('require("path")', "undefined");
text =
'const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;' +
text;
return text;
},
+20 -19
View File
@@ -258,29 +258,30 @@ const pluginFiles = [
input: "src/plugins/flow.js",
replaceModule: [
{
module: require.resolve("flow-parser"),
module: getPackageFile("flow-parser/oxidized/FlowParser.js"),
process(text) {
const { fsModuleNameVariableName } = text.match(
/,(?<fsModuleNameVariableName>[\p{ID_Start}_$][\p{ID_Continue}$]*)="fs",/u,
).groups;
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
text = text
.replaceAll(`require(${fsModuleNameVariableName})`, "{}")
.replaceAll('require("fs")', "{}")
.replaceAll('require("constants")', "{}");
${text}
`;
return text;
},
},
{
module: getPackageFile("flow-parser/oxidized/FlowParserWASM.js"),
process(text) {
text = outdent`
const Buffer = globalThis.Buffer ?? require("buffer/").Buffer;
const { globalThisVariableName } = text.match(
/\(function\((?<globalThisVariableName>[\p{ID_Start}$][\p{ID_Continue}$]*)\)\{"use strict";/u,
).groups;
// flow-parser adds a global error handler cause the error stack been huge
// https://github.com/facebook/flow/issues/9299
// NOTE: Can't reproduce in flow-parser@0.298.0, but the code still there
// NOTE2: This is not tested
text = text.replaceAll(`${globalThisVariableName}.process`, "({})");
${text}
`;
text = text.replaceAll("process.argv", "[]");
text = text.replaceAll('require("fs")', "undefined");
text = text.replaceAll('require("path")', "undefined");
text = text.replaceAll(
`${globalThisVariableName}.addEventListener`,
"(() =>{})",
'require("crypto")',
`require(${JSON.stringify(path.join(import.meta.dirname, "../shims/crypto-random-fill-sync.cjs"))})`,
);
return text;
@@ -0,0 +1,25 @@
"use strict";
// eslint-disable-next-line no-redeclare
const { crypto } = globalThis;
if (crypto && typeof crypto.getRandomValues === "function") {
module.exports = {
randomFillSync(view) {
crypto.getRandomValues(view);
return view;
},
};
}
let state = 0x9e3779b9;
module.exports = {
randomFillSync(view) {
for (let index = 0; index < view.length; index++) {
state = Math.imul(state ^ (state >>> 15), state | 1);
state ^= state + Math.imul(state ^ (state >>> 7), state | 61);
view[index] = (state ^ (state >>> 14)) & 255;
}
return view;
},
};