From d7c7957536239351c882b7cf5ccc8d9f853a89d4 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:01:44 -0800 Subject: [PATCH] binder: generate direct bindEachChild traversal to avoid ForEachChild closures MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Binder.bindEachChild previously called node.ForEachChild(b.bindFunc), paying an indirect closure call on every child across the AST. Generate (*Binder).bindEachChild from tools/scripts/tsc/ast.json in bindchildren_generated.go matching (*ast.Node).ForEachChild order, remove b.bindFunc, and replace doWithConditionalBranches indirect calls with direct bindWithConditionalBranches. benchstat (-pgo=off, parent vs commit, n=6): goos: linux goarch: amd64 │ old │ new │ │ sec/op │ sec/op vs base │ Tsc/vscode 6.537 ± 2% 6.535 ± 1% ~ (p=0.699 n=6) Tsc/mui-docs 3.889 ± 2% 3.916 ± 1% ~ (p=0.240 n=6) geomean 5.042 5.059 +0.32% │ old │ new │ │ B-rss │ B-rss vs base │ Tsc/vscode 4.735Gi ± 2% 4.752Gi ± 2% ~ (p=0.818 n=6) Tsc/mui-docs 2.151Gi ± 7% 2.166Gi ± 5% ~ (p=1.000 n=6) geomean 3.191Gi 3.209Gi +0.54% --- tools/scripts/tsc/generate-go-ast.ts | 77 +- tsc/internal/binder/bindchildren_generated.go | 770 ++++++++++++++++++ tsc/internal/binder/binder.go | 53 +- 3 files changed, 880 insertions(+), 20 deletions(-) create mode 100644 tsc/internal/binder/bindchildren_generated.go diff --git a/tools/scripts/tsc/generate-go-ast.ts b/tools/scripts/tsc/generate-go-ast.ts index a85caf48d5e7b..3cef5991a19e1 100644 --- a/tools/scripts/tsc/generate-go-ast.ts +++ b/tools/scripts/tsc/generate-go-ast.ts @@ -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; @@ -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(); @@ -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)) { diff --git a/tsc/internal/binder/bindchildren_generated.go b/tsc/internal/binder/bindchildren_generated.go new file mode 100644 index 0000000000000..f6afc98b6ccdf --- /dev/null +++ b/tsc/internal/binder/bindchildren_generated.go @@ -0,0 +1,770 @@ +// Code generated by tools/scripts/tsc/generate-go-ast.ts; DO NOT EDIT. + +package binder + +import "github.com/microsoft/TypeScript/tsc/internal/ast" + +// bindEachChild binds every immediate child of node in the exact same order +// as (*ast.Node).ForEachChild, without indirect closure dispatch. +func (b *Binder) bindEachChild(node *ast.Node) { + if node.Kind <= ast.KindLastToken { + return + } + switch node.Kind { + case ast.KindQualifiedName: + n := node.AsQualifiedName() + b.bindOpt(n.Left) + b.bindOpt(n.Right) + case ast.KindComputedPropertyName: + n := node.AsComputedPropertyName() + b.bindOpt(n.Expression) + case ast.KindDecorator: + n := node.AsDecorator() + b.bindOpt(n.Expression) + case ast.KindIfStatement: + n := node.AsIfStatement() + b.bindOpt(n.Expression) + b.bindOpt(n.ThenStatement) + b.bindOpt(n.ElseStatement) + case ast.KindDoStatement: + n := node.AsDoStatement() + b.bindOpt(n.Statement) + b.bindOpt(n.Expression) + case ast.KindWhileStatement: + n := node.AsWhileStatement() + b.bindOpt(n.Expression) + b.bindOpt(n.Statement) + case ast.KindForStatement: + n := node.AsForStatement() + b.bindOpt(n.Initializer) + b.bindOpt(n.Condition) + b.bindOpt(n.Incrementor) + b.bindOpt(n.Statement) + case ast.KindForInStatement, ast.KindForOfStatement: + n := node.AsForInOrOfStatement() + b.bindOpt(n.AwaitModifier) + b.bindOpt(n.Initializer) + b.bindOpt(n.Expression) + b.bindOpt(n.Statement) + case ast.KindBreakStatement: + n := node.AsBreakStatement() + b.bindOpt(n.Label) + case ast.KindContinueStatement: + n := node.AsContinueStatement() + b.bindOpt(n.Label) + case ast.KindReturnStatement: + n := node.AsReturnStatement() + b.bindOpt(n.Expression) + case ast.KindWithStatement: + n := node.AsWithStatement() + b.bindOpt(n.Expression) + b.bindOpt(n.Statement) + case ast.KindSwitchStatement: + n := node.AsSwitchStatement() + b.bindOpt(n.Expression) + b.bindOpt(n.CaseBlock) + case ast.KindCaseBlock: + n := node.AsCaseBlock() + b.bindNodeList(n.Clauses) + case ast.KindCaseClause: + n := node.AsCaseClause() + b.bindOpt(n.Expression) + b.bindNodeList(n.Statements) + case ast.KindDefaultClause: + n := node.AsDefaultClause() + b.bindNodeList(n.Statements) + case ast.KindThrowStatement: + n := node.AsThrowStatement() + b.bindOpt(n.Expression) + case ast.KindTryStatement: + n := node.AsTryStatement() + b.bindOpt(n.TryBlock) + b.bindOpt(n.CatchClause) + b.bindOpt(n.FinallyBlock) + case ast.KindCatchClause: + n := node.AsCatchClause() + b.bindOpt(n.VariableDeclaration) + b.bindOpt(n.Block) + case ast.KindLabeledStatement: + n := node.AsLabeledStatement() + b.bindOpt(n.Label) + b.bindOpt(n.Statement) + case ast.KindExpressionStatement: + n := node.AsExpressionStatement() + b.bindOpt(n.Expression) + case ast.KindBlock: + n := node.AsBlock() + b.bindNodeList(n.Statements) + case ast.KindVariableStatement: + n := node.AsVariableStatement() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.DeclarationList) + case ast.KindVariableDeclaration: + n := node.AsVariableDeclaration() + b.bindOpt(n.Name()) + b.bindOpt(n.ExclamationToken) + b.bindOpt(n.Type) + b.bindOpt(n.Initializer) + case ast.KindVariableDeclarationList: + n := node.AsVariableDeclarationList() + b.bindNodeList(n.Declarations) + case ast.KindObjectBindingPattern, ast.KindArrayBindingPattern: + n := node.AsBindingPattern() + b.bindNodeList(n.Elements) + case ast.KindParameter: + n := node.AsParameterDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.DotDotDotToken) + b.bindOpt(n.Name()) + b.bindOpt(n.QuestionToken) + b.bindOpt(n.Type) + b.bindOpt(n.Initializer) + case ast.KindBindingElement: + n := node.AsBindingElement() + b.bindOpt(n.DotDotDotToken) + b.bindOpt(n.PropertyName) + b.bindOpt(n.Name()) + b.bindOpt(n.Initializer) + case ast.KindMissingDeclaration: + n := node.AsMissingDeclaration() + b.bindModifiers(n.Modifiers()) + case ast.KindFunctionDeclaration: + n := node.AsFunctionDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.AsteriskToken) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindClassDeclaration: + n := node.AsClassDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.HeritageClauses) + b.bindNodeList(n.Members) + case ast.KindClassExpression: + n := node.AsClassExpression() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.HeritageClauses) + b.bindNodeList(n.Members) + case ast.KindHeritageClause: + n := node.AsHeritageClause() + b.bindNodeList(n.Types) + case ast.KindInterfaceDeclaration: + n := node.AsInterfaceDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.HeritageClauses) + b.bindNodeList(n.Members) + case ast.KindTypeAliasDeclaration, ast.KindJSTypeAliasDeclaration: + n := node.AsTypeAliasDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindOpt(n.Type) + case ast.KindEnumMember: + n := node.AsEnumMember() + b.bindOpt(n.Name()) + b.bindOpt(n.Initializer) + case ast.KindEnumDeclaration: + n := node.AsEnumDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.Members) + case ast.KindModuleBlock: + n := node.AsModuleBlock() + b.bindNodeList(n.Statements) + case ast.KindImportDeclaration, ast.KindJSImportDeclaration: + n := node.AsImportDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.ImportClause) + b.bindOpt(n.ModuleSpecifier) + b.bindOpt(n.Attributes) + case ast.KindExternalModuleReference: + n := node.AsExternalModuleReference() + b.bindOpt(n.Expression) + case ast.KindNamespaceImport: + n := node.AsNamespaceImport() + b.bindOpt(n.Name()) + case ast.KindNamedImports: + n := node.AsNamedImports() + b.bindNodeList(n.Elements) + case ast.KindExportAssignment: + n := node.AsExportAssignment() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Type) + b.bindOpt(n.Expression) + case ast.KindNamespaceExportDeclaration: + n := node.AsNamespaceExportDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + case ast.KindNamespaceExport: + n := node.AsNamespaceExport() + b.bindOpt(n.Name()) + case ast.KindNamedExports: + n := node.AsNamedExports() + b.bindNodeList(n.Elements) + case ast.KindExportSpecifier: + n := node.AsExportSpecifier() + b.bindOpt(n.PropertyName) + b.bindOpt(n.Name()) + case ast.KindCallSignature: + n := node.AsCallSignatureDeclaration() + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindConstructSignature: + n := node.AsConstructSignatureDeclaration() + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindConstructor: + n := node.AsConstructorDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindGetAccessor: + n := node.AsGetAccessorDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindSetAccessor: + n := node.AsSetAccessorDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindIndexSignature: + n := node.AsIndexSignatureDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindMethodSignature: + n := node.AsMethodSignatureDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindMethodDeclaration: + n := node.AsMethodDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.AsteriskToken) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindPropertySignature: + n := node.AsPropertySignatureDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindOpt(n.Type) + b.bindOpt(n.Initializer) + case ast.KindPropertyDeclaration: + n := node.AsPropertyDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindOpt(n.Type) + b.bindOpt(n.Initializer) + case ast.KindClassStaticBlockDeclaration: + n := node.AsClassStaticBlockDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Body) + case ast.KindBinaryExpression: + n := node.AsBinaryExpression() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Left) + b.bindOpt(n.Type) + b.bindOpt(n.OperatorToken) + b.bindOpt(n.Right) + case ast.KindPrefixUnaryExpression: + n := node.AsPrefixUnaryExpression() + b.bindOpt(n.Operand) + case ast.KindPostfixUnaryExpression: + n := node.AsPostfixUnaryExpression() + b.bindOpt(n.Operand) + case ast.KindYieldExpression: + n := node.AsYieldExpression() + b.bindOpt(n.AsteriskToken) + b.bindOpt(n.Expression) + case ast.KindArrowFunction: + n := node.AsArrowFunction() + b.bindModifiers(n.Modifiers()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.EqualsGreaterThanToken) + b.bindOpt(n.Body) + case ast.KindFunctionExpression: + n := node.AsFunctionExpression() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.AsteriskToken) + b.bindOpt(n.Name()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + b.bindOpt(n.FullSignature) + b.bindOpt(n.Body) + case ast.KindAsExpression: + n := node.AsAsExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.Type) + case ast.KindSatisfiesExpression: + n := node.AsSatisfiesExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.Type) + case ast.KindConditionalExpression: + n := node.AsConditionalExpression() + b.bindOpt(n.Condition) + b.bindOpt(n.QuestionToken) + b.bindOpt(n.WhenTrue) + b.bindOpt(n.ColonToken) + b.bindOpt(n.WhenFalse) + case ast.KindPropertyAccessExpression: + n := node.AsPropertyAccessExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.QuestionDotToken) + b.bindOpt(n.Name()) + case ast.KindElementAccessExpression: + n := node.AsElementAccessExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.QuestionDotToken) + b.bindOpt(n.ArgumentExpression) + case ast.KindCallExpression: + n := node.AsCallExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.QuestionDotToken) + b.bindNodeList(n.TypeArguments) + b.bindNodeList(n.Arguments) + case ast.KindNewExpression: + n := node.AsNewExpression() + b.bindOpt(n.Expression) + b.bindNodeList(n.TypeArguments) + b.bindNodeList(n.Arguments) + case ast.KindMetaProperty: + n := node.AsMetaProperty() + b.bindOpt(n.Name()) + case ast.KindNonNullExpression: + n := node.AsNonNullExpression() + b.bindOpt(n.Expression) + case ast.KindSpreadElement: + n := node.AsSpreadElement() + b.bindOpt(n.Expression) + case ast.KindTemplateExpression: + n := node.AsTemplateExpression() + b.bindOpt(n.Head) + b.bindNodeList(n.TemplateSpans) + case ast.KindTemplateSpan: + n := node.AsTemplateSpan() + b.bindOpt(n.Expression) + b.bindOpt(n.Literal) + case ast.KindTaggedTemplateExpression: + n := node.AsTaggedTemplateExpression() + b.bindOpt(n.Tag) + b.bindOpt(n.QuestionDotToken) + b.bindNodeList(n.TypeArguments) + b.bindOpt(n.Template) + case ast.KindParenthesizedExpression: + n := node.AsParenthesizedExpression() + b.bindOpt(n.Expression) + case ast.KindArrayLiteralExpression: + n := node.AsArrayLiteralExpression() + b.bindNodeList(n.Elements) + case ast.KindObjectLiteralExpression: + n := node.AsObjectLiteralExpression() + b.bindNodeList(n.Properties) + case ast.KindSpreadAssignment: + n := node.AsSpreadAssignment() + b.bindOpt(n.Expression) + case ast.KindPropertyAssignment: + n := node.AsPropertyAssignment() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindOpt(n.Type) + b.bindOpt(n.Initializer) + case ast.KindShorthandPropertyAssignment: + n := node.AsShorthandPropertyAssignment() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.PostfixToken) + b.bindOpt(n.Type) + b.bindOpt(n.EqualsToken) + b.bindOpt(n.ObjectAssignmentInitializer) + case ast.KindDeleteExpression: + n := node.AsDeleteExpression() + b.bindOpt(n.Expression) + case ast.KindTypeOfExpression: + n := node.AsTypeOfExpression() + b.bindOpt(n.Expression) + case ast.KindVoidExpression: + n := node.AsVoidExpression() + b.bindOpt(n.Expression) + case ast.KindAwaitExpression: + n := node.AsAwaitExpression() + b.bindOpt(n.Expression) + case ast.KindTypeAssertionExpression: + n := node.AsTypeAssertion() + b.bindOpt(n.Type) + b.bindOpt(n.Expression) + case ast.KindUnionType: + n := node.AsUnionTypeNode() + b.bindNodeList(n.Types) + case ast.KindIntersectionType: + n := node.AsIntersectionTypeNode() + b.bindNodeList(n.Types) + case ast.KindConditionalType: + n := node.AsConditionalTypeNode() + b.bindOpt(n.CheckType) + b.bindOpt(n.ExtendsType) + b.bindOpt(n.TrueType) + b.bindOpt(n.FalseType) + case ast.KindTypeOperator: + n := node.AsTypeOperatorNode() + b.bindOpt(n.Type) + case ast.KindInferType: + n := node.AsInferTypeNode() + b.bindOpt(n.TypeParameter) + case ast.KindArrayType: + n := node.AsArrayTypeNode() + b.bindOpt(n.ElementType) + case ast.KindIndexedAccessType: + n := node.AsIndexedAccessTypeNode() + b.bindOpt(n.ObjectType) + b.bindOpt(n.IndexType) + case ast.KindTypeReference: + n := node.AsTypeReferenceNode() + b.bindOpt(n.TypeName) + b.bindNodeList(n.TypeArguments) + case ast.KindExpressionWithTypeArguments: + n := node.AsExpressionWithTypeArguments() + b.bindOpt(n.Expression) + b.bindNodeList(n.TypeArguments) + case ast.KindLiteralType: + n := node.AsLiteralTypeNode() + b.bindOpt(n.Literal) + case ast.KindTypePredicate: + n := node.AsTypePredicateNode() + b.bindOpt(n.AssertsModifier) + b.bindOpt(n.ParameterName) + b.bindOpt(n.Type) + case ast.KindImportAttribute: + n := node.AsImportAttribute() + b.bindOpt(n.Name()) + b.bindOpt(n.Value) + case ast.KindImportAttributes: + n := node.AsImportAttributes() + b.bindNodeList(n.Attributes) + case ast.KindTypeQuery: + n := node.AsTypeQueryNode() + b.bindOpt(n.ExprName) + b.bindNodeList(n.TypeArguments) + case ast.KindMappedType: + n := node.AsMappedTypeNode() + b.bindOpt(n.ReadonlyToken) + b.bindOpt(n.TypeParameter) + b.bindOpt(n.NameType) + b.bindOpt(n.QuestionToken) + b.bindOpt(n.Type) + b.bindNodeList(n.Members) + case ast.KindTypeLiteral: + n := node.AsTypeLiteralNode() + b.bindNodeList(n.Members) + case ast.KindTupleType: + n := node.AsTupleTypeNode() + b.bindNodeList(n.Elements) + case ast.KindNamedTupleMember: + n := node.AsNamedTupleMember() + b.bindOpt(n.DotDotDotToken) + b.bindOpt(n.Name()) + b.bindOpt(n.QuestionToken) + b.bindOpt(n.Type) + case ast.KindOptionalType: + n := node.AsOptionalTypeNode() + b.bindOpt(n.Type) + case ast.KindRestType: + n := node.AsRestTypeNode() + b.bindOpt(n.Type) + case ast.KindParenthesizedType: + n := node.AsParenthesizedTypeNode() + b.bindOpt(n.Type) + case ast.KindFunctionType: + n := node.AsFunctionTypeNode() + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindConstructorType: + n := node.AsConstructorTypeNode() + b.bindModifiers(n.Modifiers()) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindTemplateLiteralType: + n := node.AsTemplateLiteralTypeNode() + b.bindOpt(n.Head) + b.bindNodeList(n.TemplateSpans) + case ast.KindTemplateLiteralTypeSpan: + n := node.AsTemplateLiteralTypeSpan() + b.bindOpt(n.Type) + b.bindOpt(n.Literal) + case ast.KindSyntheticExpression: + n := node.AsSyntheticExpression() + b.bindOpt(n.TupleNameSource) + case ast.KindPartiallyEmittedExpression: + n := node.AsPartiallyEmittedExpression() + b.bindOpt(n.Expression) + case ast.KindJsxElement: + n := node.AsJsxElement() + b.bindOpt(n.OpeningElement) + b.bindNodeList(n.Children) + b.bindOpt(n.ClosingElement) + case ast.KindJsxAttributes: + n := node.AsJsxAttributes() + b.bindNodeList(n.Properties) + case ast.KindJsxNamespacedName: + n := node.AsJsxNamespacedName() + b.bindOpt(n.Namespace) + b.bindOpt(n.Name()) + case ast.KindJsxOpeningElement: + n := node.AsJsxOpeningElement() + b.bindOpt(n.TagName) + b.bindNodeList(n.TypeArguments) + b.bindOpt(n.Attributes) + case ast.KindJsxSelfClosingElement: + n := node.AsJsxSelfClosingElement() + b.bindOpt(n.TagName) + b.bindNodeList(n.TypeArguments) + b.bindOpt(n.Attributes) + case ast.KindJsxFragment: + n := node.AsJsxFragment() + b.bindOpt(n.OpeningFragment) + b.bindNodeList(n.Children) + b.bindOpt(n.ClosingFragment) + case ast.KindJsxAttribute: + n := node.AsJsxAttribute() + b.bindOpt(n.Name()) + b.bindOpt(n.Initializer) + case ast.KindJsxSpreadAttribute: + n := node.AsJsxSpreadAttribute() + b.bindOpt(n.Expression) + case ast.KindJsxClosingElement: + n := node.AsJsxClosingElement() + b.bindOpt(n.TagName) + case ast.KindJsxExpression: + n := node.AsJsxExpression() + b.bindOpt(n.DotDotDotToken) + b.bindOpt(n.Expression) + case ast.KindSyntaxList: + n := node.AsSyntaxList() + b.bindEach(n.Children) + case ast.KindJSDoc: + n := node.AsJSDoc() + b.bindNodeList(n.Comment) + b.bindNodeList(n.Tags) + case ast.KindJSDocTypeExpression: + n := node.AsJSDocTypeExpression() + b.bindOpt(n.Type) + case ast.KindJSDocNonNullableType: + n := node.AsJSDocNonNullableType() + b.bindOpt(n.Type) + case ast.KindJSDocNullableType: + n := node.AsJSDocNullableType() + b.bindOpt(n.Type) + case ast.KindJSDocVariadicType: + n := node.AsJSDocVariadicType() + b.bindOpt(n.Type) + case ast.KindJSDocOptionalType: + n := node.AsJSDocOptionalType() + b.bindOpt(n.Type) + case ast.KindJSDocTypeTag: + n := node.AsJSDocTypeTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocUnknownTag: + n := node.AsJSDocUnknownTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocTemplateTag: + n := node.AsJSDocTemplateTag() + b.bindOpt(n.TagName) + b.bindOpt(n.Constraint) + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Comment) + case ast.KindJSDocReturnTag: + n := node.AsJSDocReturnTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocPublicTag: + n := node.AsJSDocPublicTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocPrivateTag: + n := node.AsJSDocPrivateTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocProtectedTag: + n := node.AsJSDocProtectedTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocReadonlyTag: + n := node.AsJSDocReadonlyTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocOverrideTag: + n := node.AsJSDocOverrideTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocDeprecatedTag: + n := node.AsJSDocDeprecatedTag() + b.bindOpt(n.TagName) + b.bindNodeList(n.Comment) + case ast.KindJSDocSeeTag: + n := node.AsJSDocSeeTag() + b.bindOpt(n.TagName) + b.bindOpt(n.NameExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocImplementsTag: + n := node.AsJSDocImplementsTag() + b.bindOpt(n.TagName) + b.bindOpt(n.ClassName) + b.bindNodeList(n.Comment) + case ast.KindJSDocAugmentsTag: + n := node.AsJSDocAugmentsTag() + b.bindOpt(n.TagName) + b.bindOpt(n.ClassName) + b.bindNodeList(n.Comment) + case ast.KindJSDocSatisfiesTag: + n := node.AsJSDocSatisfiesTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocThrowsTag: + n := node.AsJSDocThrowsTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocThisTag: + n := node.AsJSDocThisTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocImportTag: + n := node.AsJSDocImportTag() + b.bindOpt(n.TagName) + b.bindOpt(n.ImportClause) + b.bindOpt(n.ModuleSpecifier) + b.bindOpt(n.Attributes) + b.bindNodeList(n.Comment) + case ast.KindJSDocCallbackTag: + n := node.AsJSDocCallbackTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindOpt(n.Name()) + b.bindNodeList(n.Comment) + case ast.KindJSDocOverloadTag: + n := node.AsJSDocOverloadTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindNodeList(n.Comment) + case ast.KindJSDocTypedefTag: + n := node.AsJSDocTypedefTag() + b.bindOpt(n.TagName) + b.bindOpt(n.TypeExpression) + b.bindOpt(n.Name()) + b.bindNodeList(n.Comment) + case ast.KindJSDocSignature: + n := node.AsJSDocSignature() + b.bindNodeList(n.TypeParameters) + b.bindNodeList(n.Parameters) + b.bindOpt(n.Type) + case ast.KindJSDocNameReference: + n := node.AsJSDocNameReference() + b.bindOpt(n.Name()) + case ast.KindSourceFile: + n := node.AsSourceFile() + b.bindNodeList(n.Statements) + b.bindOpt(n.EndOfFileToken) + case ast.KindModuleDeclaration: + n := node.AsModuleDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.Attributes) + b.bindOpt(n.Body) + case ast.KindImportEqualsDeclaration: + n := node.AsImportEqualsDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.ModuleReference) + case ast.KindExportDeclaration: + n := node.AsExportDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.ExportClause) + b.bindOpt(n.ModuleSpecifier) + b.bindOpt(n.Attributes) + case ast.KindImportType: + n := node.AsImportTypeNode() + b.bindOpt(n.Argument) + b.bindOpt(n.Attributes) + b.bindOpt(n.Qualifier) + b.bindNodeList(n.TypeArguments) + case ast.KindImportClause: + n := node.AsImportClause() + b.bindOpt(n.Name()) + b.bindOpt(n.NamedBindings) + case ast.KindImportSpecifier: + n := node.AsImportSpecifier() + b.bindOpt(n.PropertyName) + b.bindOpt(n.Name()) + case ast.KindJSDocLink: + n := node.AsJSDocLink() + b.bindOpt(n.Name()) + case ast.KindJSDocLinkPlain: + n := node.AsJSDocLinkPlain() + b.bindOpt(n.Name()) + case ast.KindJSDocLinkCode: + n := node.AsJSDocLinkCode() + b.bindOpt(n.Name()) + case ast.KindTypeParameter: + n := node.AsTypeParameterDeclaration() + b.bindModifiers(n.Modifiers()) + b.bindOpt(n.Name()) + b.bindOpt(n.Constraint) + b.bindOpt(n.Expression) + b.bindOpt(n.DefaultType) + case ast.KindSyntheticReferenceExpression: + n := node.AsSyntheticReferenceExpression() + b.bindOpt(n.Expression) + b.bindOpt(n.ThisArg) + case ast.KindJSDocTypeLiteral: + n := node.AsJSDocTypeLiteral() + b.bindEach(n.JSDocPropertyTags) + case ast.KindJSDocParameterTag, ast.KindJSDocPropertyTag: + n := node.AsJSDocParameterOrPropertyTag() + b.bindJSDocParameterOrPropertyTag(n) + } +} diff --git a/tsc/internal/binder/binder.go b/tsc/internal/binder/binder.go index 1f887c1c1354a..15c35e6b0adad 100644 --- a/tsc/internal/binder/binder.go +++ b/tsc/internal/binder/binder.go @@ -49,7 +49,6 @@ type ExpandoAssignmentInfo struct { type Binder struct { file *ast.SourceFile - bindFunc func(*ast.Node) bool unreachableFlow *ast.FlowNode container *ast.Node @@ -106,9 +105,7 @@ func BindSourceFile(file *ast.SourceFile) { var binderPool = sync.Pool{ New: func() any { - b := &Binder{} - b.bindFunc = b.bind // Allocate closure once - return b + return &Binder{} }, } @@ -117,7 +114,7 @@ func getBinder() *Binder { } func putBinder(b *Binder) { - *b = Binder{bindFunc: b.bindFunc} + *b = Binder{} binderPool.Put(b) } @@ -584,9 +581,9 @@ func (b *Binder) finishFlowLabel(label *ast.FlowLabel) *ast.FlowNode { return label } -func (b *Binder) bind(node *ast.Node) bool { +func (b *Binder) bind(node *ast.Node) { if node == nil { - return false + return } // Even though in the AST the jsdoc @typedef node belongs to the current node, // its symbol might be in the same scope with the current node's symbol. Consider: @@ -756,7 +753,6 @@ func (b *Binder) bind(node *ast.Node) bool { node.Flags |= ast.NodeFlagsThisNodeOrAnySubNodesHasError b.seenParseError = true } - return false } func (b *Binder) bindPropertyWorker(node *ast.Node) { @@ -1770,8 +1766,10 @@ func (b *Binder) bindChildren(node *ast.Node) { b.inAssignmentPattern = saveInAssignmentPattern } -func (b *Binder) bindEachChild(node *ast.Node) { - node.ForEachChild(b.bindFunc) +func (b *Binder) bindOpt(node *ast.Node) { + if node != nil { + b.bind(node) + } } func (b *Binder) bindEach(nodes []*ast.Node) { @@ -1781,11 +1779,23 @@ func (b *Binder) bindEach(nodes []*ast.Node) { } func (b *Binder) bindNodeList(nodeList *ast.NodeList) { - if nodeList != nil { + if nodeList != nil && len(nodeList.Nodes) != 0 { b.bindEach(nodeList.Nodes) } } +func (b *Binder) bindJSDocParameterOrPropertyTag(node *ast.JSDocParameterOrPropertyTag) { + b.bindOpt(node.TagName) + if node.IsNameFirst { + b.bindOpt(node.Name()) + b.bindOpt(node.TypeExpression) + } else { + b.bindOpt(node.TypeExpression) + b.bindOpt(node.Name()) + } + b.bindNodeList(node.Comment) +} + func (b *Binder) bindModifiers(modifiers *ast.ModifierList) { if modifiers != nil { b.bindEach(modifiers.Nodes) @@ -1815,18 +1825,18 @@ func (b *Binder) setContinueTarget(node *ast.Node, target *ast.FlowLabel) *ast.F return target } -func (b *Binder) doWithConditionalBranches(action func(b *Binder, value *ast.Node) bool, value *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel) { +func (b *Binder) bindWithConditionalBranches(value *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel) { savedTrueTarget := b.currentTrueTarget savedFalseTarget := b.currentFalseTarget b.currentTrueTarget = trueTarget b.currentFalseTarget = falseTarget - action(b, value) + b.bind(value) b.currentTrueTarget = savedTrueTarget b.currentFalseTarget = savedFalseTarget } func (b *Binder) bindCondition(node *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel) { - b.doWithConditionalBranches((*Binder).bind, node, trueTarget, falseTarget) + b.bindWithConditionalBranches(node, trueTarget, falseTarget) if node == nil || !isLogicalAssignmentExpression(node) && !ast.IsLogicalExpression(node) && !(ast.IsOptionalChain(node) && ast.IsOutermostOptionalChain(node)) { b.addAntecedent(trueTarget, b.createFlowCondition(ast.FlowFlagsTrueCondition, b.currentFlow, node)) b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition, b.currentFlow, node)) @@ -2321,7 +2331,7 @@ func (b *Binder) bindLogicalLikeExpression(node *ast.Node, trueTarget *ast.FlowL b.currentFlow = b.finishFlowLabel(preRightLabel) b.bind(expr.OperatorToken) if ast.IsLogicalOrCoalescingAssignmentOperator(expr.OperatorToken.Kind) { - b.doWithConditionalBranches((*Binder).bind, expr.Right, trueTarget, falseTarget) + b.bindWithConditionalBranches(expr.Right, trueTarget, falseTarget) b.bindAssignmentTargetFlow(expr.Left) b.addAntecedent(trueTarget, b.createFlowCondition(ast.FlowFlagsTrueCondition, b.currentFlow, node)) b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition, b.currentFlow, node)) @@ -2460,7 +2470,13 @@ func (b *Binder) bindOptionalChain(node *ast.Node, trueTarget *ast.FlowLabel, fa if preChainLabel != nil { b.currentFlow = b.finishFlowLabel(preChainLabel) } - b.doWithConditionalBranches((*Binder).bindOptionalChainRest, node, trueTarget, falseTarget) + savedTrueTarget := b.currentTrueTarget + savedFalseTarget := b.currentFalseTarget + b.currentTrueTarget = trueTarget + b.currentFalseTarget = falseTarget + b.bindOptionalChainRest(node) + b.currentTrueTarget = savedTrueTarget + b.currentFalseTarget = savedFalseTarget if ast.IsOutermostOptionalChain(node) { b.addAntecedent(trueTarget, b.createFlowCondition(ast.FlowFlagsTrueCondition, b.currentFlow, node)) b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition, b.currentFlow, node)) @@ -2468,14 +2484,14 @@ func (b *Binder) bindOptionalChain(node *ast.Node, trueTarget *ast.FlowLabel, fa } func (b *Binder) bindOptionalExpression(node *ast.Node, trueTarget *ast.FlowLabel, falseTarget *ast.FlowLabel) { - b.doWithConditionalBranches((*Binder).bind, node, trueTarget, falseTarget) + b.bindWithConditionalBranches(node, trueTarget, falseTarget) if !ast.IsOptionalChain(node) || ast.IsOutermostOptionalChain(node) { b.addAntecedent(trueTarget, b.createFlowCondition(ast.FlowFlagsTrueCondition, b.currentFlow, node)) b.addAntecedent(falseTarget, b.createFlowCondition(ast.FlowFlagsFalseCondition, b.currentFlow, node)) } } -func (b *Binder) bindOptionalChainRest(node *ast.Node) bool { +func (b *Binder) bindOptionalChainRest(node *ast.Node) { switch node.Kind { case ast.KindPropertyAccessExpression: b.bind(node.QuestionDotToken()) @@ -2488,7 +2504,6 @@ func (b *Binder) bindOptionalChainRest(node *ast.Node) bool { b.bindNodeList(node.TypeArgumentList()) b.bindEach(node.Arguments()) } - return false } func (b *Binder) bindCallExpressionFlow(node *ast.Node) {