mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-26 06:34:20 +08:00
Merge main
This commit is contained in:
@@ -282,6 +282,26 @@ const adaptedGetters = new Set([
|
||||
"operand",
|
||||
"questionToken",
|
||||
]);
|
||||
// A getter selects its child's wire slot by kind directly: an integer
|
||||
// switch, without astChildOrder's switch on the child's name. `answer`
|
||||
// maps a slot to the returned expression; `missing` is returned for kinds
|
||||
// without the child.
|
||||
const slotCases = (name, answer, missing) => {
|
||||
const kindsBySlot = new Map();
|
||||
for (const [kind, childNames] of Object.entries(protocol.childProperties)) {
|
||||
const slot = childNames.indexOf(name);
|
||||
if (slot < 0) continue;
|
||||
if (!kindsBySlot.has(slot)) kindsBySlot.set(slot, []);
|
||||
kindsBySlot.get(slot).push(kind);
|
||||
}
|
||||
const lines = [" switch (this.kind) {"];
|
||||
for (const [slot, kinds] of [...kindsBySlot].sort((a, b) => a[0] - b[0])) {
|
||||
for (const kind of kinds) lines.push(` case ${kind}:`);
|
||||
lines.push(` return ${answer(slot)};`);
|
||||
}
|
||||
lines.push(" default:", ` return ${missing};`, " }");
|
||||
return lines.join("\n");
|
||||
};
|
||||
const getters = names
|
||||
.filter((name) => !adaptedGetters.has(name))
|
||||
.map((name) => {
|
||||
@@ -292,12 +312,19 @@ const getters = names
|
||||
.replaceAll("RemoteNode", "AstNode");
|
||||
const method =
|
||||
type.includes("AstNode |") && type.includes("AstNodeList")
|
||||
? "child"
|
||||
? "childAt"
|
||||
: type.includes("AstNodeList")
|
||||
? "childList"
|
||||
: "childNode";
|
||||
return ` get ${name}(): ${type} {\n return this.${method}(${JSON.stringify(name)});\n }`;
|
||||
? "childListAt"
|
||||
: "childNodeAt";
|
||||
return ` get ${name}(): ${type} {\n${slotCases(name, (slot) => `this.${method}(${slot})`, "undefined")}\n }`;
|
||||
});
|
||||
// The adapted getters read their raw child through the same slot switch.
|
||||
const adaptedSlots = [...adaptedGetters]
|
||||
.sort()
|
||||
.map(
|
||||
(name) =>
|
||||
` private ${name}Slot(): number {\n${slotCases(name, (slot) => String(slot), "-1")}\n }`,
|
||||
);
|
||||
const nodeTarget = join(root, "packages/compiler/src/frontend/ts7/ast-node.ts");
|
||||
const before = readFileSync(nodeTarget, "utf8");
|
||||
if (
|
||||
@@ -311,6 +338,7 @@ const after = before.replace(
|
||||
" // BEGIN GENERATED CHILD GETTERS",
|
||||
` // Generated from typescript@${version}; run scripts/generate-ts7-ast-schema.mjs.`,
|
||||
...getters,
|
||||
...adaptedSlots,
|
||||
" // END GENERATED CHILD GETTERS",
|
||||
].join("\n"),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user