From 24ea51b13bf55f34cee66aed99c2fac6f368bca4 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 13 Nov 2026 19:52:43 -0800 Subject: [PATCH] Build entity names for synthesized type queries Qualified annotation names and instantiated symbol chains could place property access expressions in TypeQuery.ExprName. Identical printed spellings concealed those malformed children. Convert annotation expression chains and normalize checker query names, keeping type arguments after the complete name. Require entity names in the printer once both producers supply them. Keep the qualified object-spread codefix text unchanged. --- tsc/internal/checker/nodebuilderimpl.go | 10 +++++-- ...nnotationOnExports34-object-spread_test.go | 27 +++++++++++++++++++ .../codeactions_fixmissingtypeannotation.go | 2 +- tsc/internal/printer/factory.go | 11 ++++++++ tsc/internal/printer/printer.go | 4 --- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 2d2d01e83affa..f93844470efe6 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -757,19 +757,25 @@ func (b *NodeBuilderImpl) symbolToTypeNode(symbol *ast.Symbol, mask ast.SymbolFl } + restoreFlags := b.saveRestoreFlags() + if isTypeOf { + // Type queries permit arguments only after the complete entity name. + b.ctx.flags &^= nodebuilder.FlagsWriteTypeParametersInQualifiedName + } entityName := b.createAccessFromSymbolChain(chain, len(chain)-1, 0, typeArguments) + restoreFlags() if ast.IsIndexedAccessTypeNode(entityName) { return entityName // Indexed accesses can never be `typeof` } if ast.IsEntityName(entityName) { if isTypeOf { - return b.f.NewTypeQueryNode(entityName, nil) + return b.f.NewTypeQueryNode(entityName, typeArguments) } return b.f.NewTypeReferenceNode(entityName, typeArguments) } if isTypeOf && ast.IsExpressionWithTypeArguments(entityName) { expr := entityName.AsExpressionWithTypeArguments() - return b.f.NewTypeQueryNode(b.f.DeepCloneNode(expr.Expression), expr.TypeArguments) + return b.f.NewTypeQueryNode(printer.CreateEntityNameFromExpression(b.f, expr.Expression), expr.TypeArguments) } return entityName } diff --git a/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go b/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go index c264dbb96c43c..d68bc39e305a3 100644 --- a/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go +++ b/tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go @@ -74,3 +74,30 @@ export const All: typeof All_Part1_1 & typeof Start & typeof All_Part3 & typeof Index: 1, }) } + +func TestCodeFixMissingTypeAnnotationOnQualifiedObjectSpread(t *testing.T) { + t.Parallel() + defer testutil.RecoverAndFail(t, "Panic on fourslash test") + const content = `// @isolatedDeclarations: true +// @declaration: true +// @Filename: /code.ts +declare namespace N { + namespace Nested { + const values: { x: number }; + } +} +export const all = { ...N.Nested.values };` + f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) + defer done() + f.VerifyCodeFix(t, fourslash.VerifyCodeFixOptions{ + Description: "Add annotation of type 'typeof N.Nested.values'", + NewFileContent: `declare namespace N { + namespace Nested { + const values: { x: number }; + } +} +export const all: typeof N.Nested.values = { + ...N.Nested.values +};`, + }) +} diff --git a/tsc/internal/ls/codeactions_fixmissingtypeannotation.go b/tsc/internal/ls/codeactions_fixmissingtypeannotation.go index 783f2207f78aa..7318ce1830bf9 100644 --- a/tsc/internal/ls/codeactions_fixmissingtypeannotation.go +++ b/tsc/internal/ls/codeactions_fixmissingtypeannotation.go @@ -935,7 +935,7 @@ func (f *isolatedDeclarationsFixer) getExtraFlags(node *ast.Node, t *checker.Typ // createTypeOfFromEntityNameExpression creates a `typeof X` type query node. func (f *isolatedDeclarationsFixer) createTypeOfFromEntityNameExpression(node *ast.Node) *ast.TypeNode { return f.changeTracker.NodeFactory.NewTypeQueryNode( - f.changeTracker.NodeFactory.DeepCloneNode(node), nil, + printer.CreateEntityNameFromExpression(f.changeTracker.NodeFactory, node), nil, ) } diff --git a/tsc/internal/printer/factory.go b/tsc/internal/printer/factory.go index dbc9242abe400..596cad98c612f 100644 --- a/tsc/internal/printer/factory.go +++ b/tsc/internal/printer/factory.go @@ -281,6 +281,17 @@ func (f *NodeFactory) InlineExpressions(expressions []*ast.Expression) *ast.Expr // Utilities // +func CreateEntityNameFromExpression(f *ast.NodeFactory, node *ast.Expression) *ast.EntityName { + if ast.IsIdentifier(node) { + return f.DeepCloneNode(node) + } + debug.Assert(ast.IsPropertyAccessExpression(node) && ast.IsIdentifier(node.Name())) + return f.NewQualifiedName( + CreateEntityNameFromExpression(f, node.Expression()), + f.DeepCloneNode(node.Name()), + ) +} + func (f *NodeFactory) CreateExpressionFromEntityName(node *ast.Node) *ast.Expression { if ast.IsQualifiedName(node) { left := f.CreateExpressionFromEntityName(node.AsQualifiedName().Left) diff --git a/tsc/internal/printer/printer.go b/tsc/internal/printer/printer.go index a4cb6e3591cba..e1a70968e4403 100644 --- a/tsc/internal/printer/printer.go +++ b/tsc/internal/printer/printer.go @@ -1229,10 +1229,6 @@ func (p *Printer) emitEntityName(node *ast.EntityName) { p.emitIdentifierReference(node.AsIdentifier()) case ast.KindQualifiedName: p.emitQualifiedName(node.AsQualifiedName()) - case ast.KindPropertyAccessExpression: - // TypeQuery nodes may have PropertyAccessExpression as exprName (e.g. typeof foo.x). - // TS's emitter handles this via generic emit(); we dispatch to expression emitter here. - p.emitExpression(node, ast.OperatorPrecedenceDisallowComma) default: panic(fmt.Sprintf("unexpected EntityName: %v", node.Kind)) }