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
114 changes: 113 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,115 @@ function generateKind(): string {
return w.toString();
}

function generateSetParent(): 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 ast");
w.write("");
w.write("func setChildParent(child *Node, parent *Node) {");
w.push();
w.write("if child != nil {");
w.push();
w.write("child.Parent = parent");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.write("");
w.write("func setNodesParent(nodes []*Node, parent *Node) {");
w.push();
w.write("for _, child := range nodes {");
w.push();
w.write("child.Parent = parent");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.write("");
w.write("func setNodeListParent(list *NodeList, parent *Node) {");
w.push();
w.write("if list != nil {");
w.push();
w.write("for _, child := range list.Nodes {");
w.push();
w.write("child.Parent = parent");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.write("");
w.write("func setModifiersParent(mods *ModifierList, parent *Node) {");
w.push();
w.write("if mods != nil {");
w.push();
w.write("for _, child := range mods.Nodes {");
w.push();
w.write("child.Parent = parent");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.pop();
w.write("}");
w.write("");
w.write("// SetParentInImmediateChildren sets child.Parent = node for every immediate child");
w.write("// of node, matching (*Node).ForEachChild without closure dispatch.");
w.write("func SetParentInImmediateChildren(node *Node) {");
w.push();
w.write("if node.Kind <= 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 => 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("setNodeListParent(n.Statements, node)");
w.write("setChildParent(n.EndOfFileToken, node)");
}
else if (node.handWrittenVisitor) {
// JSDocParameterOrPropertyTag: parent assignment is order-independent.
w.write("setChildParent(n.TagName, node)");
w.write("setChildParent(n.TypeExpression, node)");
w.write("setChildParent(n.Name(), node)");
w.write("setNodeListParent(n.Comment, node)");
}
else {
const childMembers = schemaMembers(node).filter(m => m.isChild());
for (const m of childMembers) {
const access = `n.${m.name}`;
if (m.listKind === "raw") {
w.write(`setNodesParent(${access}, node)`);
}
else if (m.listKind === "ModifierList") {
w.write(`setModifiersParent(${access}, node)`);
}
else if (m.listKind === "NodeList") {
w.write(`setNodeListParent(${access}, node)`);
}
else {
w.write(`setChildParent(${access}, node)`);
}
}
}
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 +1607,7 @@ 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/ast/setparent_generated.go"] as const;

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

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

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

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