Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion tools/scripts/tsc/generate-go-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,74 @@ function generateKind(): string {
return w.toString();
}

function generateBinderEachChild(): string {
const w = new CodeWriter();
w.write("// Code generated by tools/scripts/tsc/generate-go-ast.ts; DO NOT EDIT.");
w.write("");
w.write("package binder");
w.write("");
w.write('import "github.com/microsoft/TypeScript/tsc/internal/ast"');
w.write("");
w.write("// bindEachChild binds every immediate child of node in the exact same order");
w.write("// as (*ast.Node).ForEachChild, without indirect closure dispatch.");
w.write("func (b *Binder) bindEachChild(node *ast.Node) {");
w.push();
w.write("if node.Kind <= ast.KindLastToken {");
w.push();
w.write("return");
w.pop();
w.write("}");
w.write("switch node.Kind {");
for (const node of api.nodes()) {
if (!hasForEachChild(node)) continue;
const kinds = node.allKinds().map(k => `ast.${k.formatGoConstant()}`);
if (kinds.length === 0) continue;
w.write(`case ${kinds.join(", ")}:`);
w.push();
w.write(`n := node.As${node.name}()`);
if (node.name === "SourceFile") {
w.write("b.bindNodeList(n.Statements)");
w.write("b.bindOpt(n.EndOfFileToken)");
}
else if (node.handWrittenVisitor) {
w.write(`b.bind${node.name}(n)`);
}
else {
const childMembers = schemaMembers(node).filter(m => m.isChild());
for (const m of childMembers) {
let access: string;
if (m.name === "modifiers") {
access = "n.Modifiers()";
}
else if (m.name === "name") {
access = "n.Name()";
}
else {
access = `n.${m.name}`;
}
if (m.listKind === "raw") {
w.write(`b.bindEach(${access})`);
}
else if (m.listKind === "ModifierList") {
w.write(`b.bindModifiers(${access})`);
}
else if (m.listKind === "NodeList") {
w.write(`b.bindNodeList(${access})`);
}
else {
w.write(`b.bindOpt(${access})`);
}
}
}
w.pop();
}
w.write("}");
w.pop();
w.write("}");
w.write("");
return w.toString();
}

function writeAndFormat(filePath: string, generateContent: () => string, force: boolean) {
const generated = new GeneratedFile(filePath, [import.meta.filename, path.join(ROOT, "tools/scripts/tsc/schema.ts"), path.join(ROOT, "tools/scripts/tsc/ast.json")]);
if (generated.isCurrent(force)) return;
Expand All @@ -1498,7 +1566,11 @@ function writeAndFormat(filePath: string, generateContent: () => string, force:
console.log(`Wrote ${filePath}`);
}

export const outputs = ["tsc/internal/ast/ast_generated.go", "tsc/internal/ast/kind_generated.go"] as const;
export const outputs = [
"tsc/internal/ast/ast_generated.go",
"tsc/internal/ast/kind_generated.go",
"tsc/internal/binder/bindchildren_generated.go",
] as const;

export default function main(force = false) {
verifyNoDuplicateBases();
Expand All @@ -1509,6 +1581,9 @@ export default function main(force = false) {

const kindOutPath = path.join(ROOT, outputs[1]);
writeAndFormat(kindOutPath, () => generateKind() + "\n", force);

const bindChildrenOutPath = path.join(ROOT, outputs[2]);
writeAndFormat(bindChildrenOutPath, () => generateBinderEachChild() + "\n", force);
}

if (process.argv[1] === fileURLToPath(import.meta.url)) {
Expand Down
Loading
Loading