Keep type definition for flow-estree up to date (#19969)
This commit is contained in:
1 parent
d4126e8c97
commit
f78e8a64fa
4 files changed
+936
-901
No files matched your search
@@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import url from "node:url";
|
||||
import esbuild from "esbuild";
|
||||
import {
|
||||
@@ -21,4 +23,4 @@ async function buildBabelCodeFrameForTest() {
|
||||
});
|
||||
}
|
||||
|
||||
export { buildBabelCodeFrameForTest };
|
||||
await buildBabelCodeFrameForTest();
|
||||
@@ -1,9 +1,12 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import fs from "node:fs/promises";
|
||||
import url from "node:url";
|
||||
import { codeFrameColumns } from "@babel/code-frame";
|
||||
import * as ts from "typescript";
|
||||
import * as prettier from "../node_modules/prettier/index.mjs";
|
||||
|
||||
const buildScriptVersion = "1";
|
||||
const PROJECT_ROOT = new URL("../", import.meta.url);
|
||||
const FLOW_TYPES = new URL(
|
||||
"./node_modules/flow-estree/dist/types.js.flow",
|
||||
@@ -13,68 +16,89 @@ const FLOW_TYPES_DTS = new URL(
|
||||
"./src/language-js/types/flow-estree.d.ts",
|
||||
PROJECT_ROOT,
|
||||
);
|
||||
const IS_CI = Boolean(process.env.CI);
|
||||
|
||||
let text = await fs.readFile(FLOW_TYPES, "utf8");
|
||||
text = toDts(text);
|
||||
async function generateFlowEstreeTypeDefinition() {
|
||||
const {
|
||||
default: { version },
|
||||
} = await import("flow-estree/package.json", {
|
||||
with: { type: "json" },
|
||||
});
|
||||
const versionComment = `// flow-estree:v${version}, build-script:v${buildScriptVersion}`;
|
||||
|
||||
const getRelativePath = (url) =>
|
||||
new URL(url).href.slice(PROJECT_ROOT.href.length);
|
||||
text = `
|
||||
if (!IS_CI) {
|
||||
try {
|
||||
const dtsText = await fs.readFile(FLOW_TYPES_DTS, "utf8");
|
||||
if (dtsText.includes(versionComment + "\n")) {
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// No op
|
||||
}
|
||||
}
|
||||
|
||||
let text = await fs.readFile(FLOW_TYPES, "utf8");
|
||||
|
||||
text = toDts(text);
|
||||
|
||||
const getRelativePath = (url) =>
|
||||
new URL(url).href.slice(PROJECT_ROOT.href.length);
|
||||
text = `
|
||||
// ! Do NOT edit !
|
||||
// Generated from '${getRelativePath(FLOW_TYPES)}'
|
||||
// Run \`node ${getRelativePath(import.meta.url)}\` to update
|
||||
${versionComment}
|
||||
// spell-checker: disable
|
||||
|
||||
${text}
|
||||
`;
|
||||
|
||||
text = await prettier.format(text, {
|
||||
// Bug? should accept URL
|
||||
filepath: "flow-estree.d.ts",
|
||||
parser: "typescript",
|
||||
});
|
||||
text = await prettier.format(text, {
|
||||
// Bug? should accept URL
|
||||
filepath: "flow-estree.d.ts",
|
||||
parser: "typescript",
|
||||
});
|
||||
|
||||
await fs.writeFile(FLOW_TYPES_DTS, text);
|
||||
await fs.writeFile(FLOW_TYPES_DTS, text);
|
||||
|
||||
const program = ts.createProgram([url.fileURLToPath(FLOW_TYPES_DTS)], {
|
||||
strict: true,
|
||||
});
|
||||
const diagnostics = ts.getPreEmitDiagnostics(program);
|
||||
const program = ts.createProgram([url.fileURLToPath(FLOW_TYPES_DTS)], {
|
||||
strict: true,
|
||||
});
|
||||
const diagnostics = ts.getPreEmitDiagnostics(program);
|
||||
|
||||
if (diagnostics.length > 0) {
|
||||
await fs.rm(FLOW_TYPES_DTS);
|
||||
const [diagnostic] = diagnostics;
|
||||
const { line, character: column } = ts.getLineAndCharacterOfPosition(
|
||||
diagnostic.file,
|
||||
diagnostic.start,
|
||||
);
|
||||
throw new Error(
|
||||
diagnostic.messageText +
|
||||
":\n" +
|
||||
codeFrameColumns(
|
||||
text,
|
||||
{ start: { line: line + 1, column } },
|
||||
{
|
||||
message: `TS${diagnostic.code}: ${diagnostic.messageText}`,
|
||||
},
|
||||
),
|
||||
);
|
||||
if (diagnostics.length > 0) {
|
||||
await fs.rm(FLOW_TYPES_DTS);
|
||||
const [diagnostic] = diagnostics;
|
||||
const { line, character: column } = ts.getLineAndCharacterOfPosition(
|
||||
diagnostic.file,
|
||||
diagnostic.start,
|
||||
);
|
||||
throw new Error(
|
||||
diagnostic.messageText +
|
||||
":\n" +
|
||||
codeFrameColumns(
|
||||
text,
|
||||
{ start: { line: line + 1, column } },
|
||||
{
|
||||
message: `TS${diagnostic.code}: ${diagnostic.messageText}`,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function toDts(text) {
|
||||
// Useless directive
|
||||
text = text.replaceAll("'use strict';", "");
|
||||
|
||||
// `{+foo: string}` -> `{foo: string}`
|
||||
text = text.replaceAll(/(?<=\n)(?<indention>[ {2}]+)\+/g, "$<indention>");
|
||||
|
||||
// `{foo: interface {}}` -> `{foo: {}}`
|
||||
text = text.replaceAll(": interface {", ": {");
|
||||
|
||||
// `{foo: interface extends T {}}` -> `{foo: T & {}}`
|
||||
text = text.replaceAll(
|
||||
/(?<=: )interface extends (?<type>\w+)(?= \{)/g,
|
||||
"$<type> & ",
|
||||
/(?<=: )interface(?: extends (?<type>\w+))?(?= \{)/g,
|
||||
(...args) => {
|
||||
const { type } = args.at(-1);
|
||||
return type ? `${type} & ` : "";
|
||||
},
|
||||
);
|
||||
|
||||
// `ReadonlyArray<?T>` -> `ReadonlyArray<T | null>`
|
||||
@@ -85,9 +109,11 @@ function toDts(text) {
|
||||
|
||||
// `{[string]: T}` -> `{[key: string]: T}`
|
||||
text = text.replaceAll(
|
||||
/(?<=\n)(?<indention>[ {2}]+)\[(?<type>string)\](?=: )/g,
|
||||
"$<indention>[key: $<type>]",
|
||||
/(?<=\n[ {2}]+\[)(?<type>string)(?=\]: )/g,
|
||||
"key: $<type>",
|
||||
);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
await generateFlowEstreeTypeDefinition();
|
||||
Reference in new issue
Block a user