From 04b356222abcff8147b493c1832afe1e0905dfaf Mon Sep 17 00:00:00 2001 From: Hamidreza Hanafi Date: Fri, 8 Aug 2025 19:54:40 -0400 Subject: [PATCH 1/3] Fix based on comments --- .../transformers/declarations/transform.go | 8 ++- ...onEmitAugmentationUsesCorrectSourceFile.js | 49 +++++++++++++++++ ...tAugmentationUsesCorrectSourceFile.symbols | 55 +++++++++++++++++++ ...mitAugmentationUsesCorrectSourceFile.types | 47 ++++++++++++++++ ...onEmitAugmentationUsesCorrectSourceFile.ts | 40 ++++++++++++++ 5 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.js create mode 100644 testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.symbols create mode 100644 testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.types create mode 100644 testdata/tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index ad80436fb1..0325ac7ba1 100644 --- a/internal/transformers/declarations/transform.go +++ b/internal/transformers/declarations/transform.go @@ -468,8 +468,12 @@ func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast. case ast.KindTupleType: result = tx.Visitor().VisitEachChild(input) if result != nil { - startLine, _ := scanner.GetLineAndCharacterOfPosition(tx.state.currentSourceFile, input.Loc.Pos()) - endLine, _ := scanner.GetLineAndCharacterOfPosition(tx.state.currentSourceFile, input.Loc.End()) + sourceFileForLocation := tx.state.currentSourceFile + if nodeFile := ast.GetSourceFileOfNode(input); nodeFile != nil && nodeFile != sourceFileForLocation { + sourceFileForLocation = nodeFile + } + startLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFileForLocation, input.Loc.Pos()) + endLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFileForLocation, input.Loc.End()) if startLine == endLine { tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) } diff --git a/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.js b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.js new file mode 100644 index 0000000000..5ce8ff6983 --- /dev/null +++ b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.js @@ -0,0 +1,49 @@ +//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] //// + +//// [index.d.ts] +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward + +type ShouldJustBeAny = [any][0]; + +declare namespace knex { + export { Knex }; +} + +declare namespace Knex { + interface Interface { + method(): ShouldJustBeAny; + } +} + +export = knex; + +//// [index.ts] +import "knex"; +declare module "knex" { + namespace Knex { + function newFunc(): Knex.Interface; + } +} + + + + +//// [index.js] +import "knex"; + + +//// [index.d.ts] +import "knex"; +declare module "knex" { + namespace Knex { + function newFunc(): Knex.Interface; + } +} diff --git a/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.symbols b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.symbols new file mode 100644 index 0000000000..ce3dbfbd1a --- /dev/null +++ b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.symbols @@ -0,0 +1,55 @@ +//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] //// + +=== node_modules/knex/index.d.ts === +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward + +type ShouldJustBeAny = [any][0]; +>ShouldJustBeAny : Symbol(ShouldJustBeAny, Decl(index.d.ts, 0, 0)) + +declare namespace knex { +>knex : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14)) + + export { Knex }; +>Knex : Symbol(Knex, Decl(index.d.ts, 13, 10)) +} + +declare namespace Knex { +>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23)) + + interface Interface { +>Interface : Symbol(Interface, Decl(index.d.ts, 16, 24)) + + method(): ShouldJustBeAny; +>method : Symbol(method, Decl(index.d.ts, 17, 23)) +>ShouldJustBeAny : Symbol(ShouldJustBeAny, Decl(index.d.ts, 0, 0)) + } +} + +export = knex; +>knex : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14)) + +=== index.ts === +import "knex"; +declare module "knex" { +>"knex" : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14)) + + namespace Knex { +>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23)) + + function newFunc(): Knex.Interface; +>newFunc : Symbol(newFunc, Decl(index.ts, 2, 18)) +>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23)) +>Interface : Symbol(Interface, Decl(index.d.ts, 16, 24)) + } +} + + + diff --git a/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.types b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.types new file mode 100644 index 0000000000..59932e3351 --- /dev/null +++ b/testdata/baselines/reference/compiler/declarationEmitAugmentationUsesCorrectSourceFile.types @@ -0,0 +1,47 @@ +//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] //// + +=== node_modules/knex/index.d.ts === +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward + +type ShouldJustBeAny = [any][0]; +>ShouldJustBeAny : any + +declare namespace knex { + export { Knex }; +>Knex : any +} + +declare namespace Knex { + interface Interface { + method(): ShouldJustBeAny; +>method : () => any + } +} + +export = knex; +>knex : typeof import("knex") + +=== index.ts === +import "knex"; +declare module "knex" { +>"knex" : typeof import("knex") + + namespace Knex { +>Knex : typeof Knex + + function newFunc(): Knex.Interface; +>newFunc : () => Interface +>Knex : any + } +} + + + diff --git a/testdata/tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts b/testdata/tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts new file mode 100644 index 0000000000..015d63fe48 --- /dev/null +++ b/testdata/tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts @@ -0,0 +1,40 @@ +// @strict: true +// @module: preserve +// @declaration: true + +// @filename: node_modules/knex/index.d.ts + +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward +// A bunch of random text to move the positions forward + +type ShouldJustBeAny = [any][0]; + +declare namespace knex { + export { Knex }; +} + +declare namespace Knex { + interface Interface { + method(): ShouldJustBeAny; + } +} + +export = knex; + +// @filename: index.ts + +import "knex"; +declare module "knex" { + namespace Knex { + function newFunc(): Knex.Interface; + } +} + + From 0016d79a7af92189ad563fc9a30880c066d34b16 Mon Sep 17 00:00:00 2001 From: Hamidreza Hanafi Date: Fri, 8 Aug 2025 20:13:19 -0400 Subject: [PATCH 2/3] address comments --- internal/transformers/declarations/transform.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index 0325ac7ba1..0fa5d45953 100644 --- a/internal/transformers/declarations/transform.go +++ b/internal/transformers/declarations/transform.go @@ -468,14 +468,14 @@ func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast. case ast.KindTupleType: result = tx.Visitor().VisitEachChild(input) if result != nil { - sourceFileForLocation := tx.state.currentSourceFile - if nodeFile := ast.GetSourceFileOfNode(input); nodeFile != nil && nodeFile != sourceFileForLocation { - sourceFileForLocation = nodeFile - } - startLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFileForLocation, input.Loc.Pos()) - endLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFileForLocation, input.Loc.End()) - if startLine == endLine { - tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) + original := tx.EmitContext().MostOriginal(input) + originalSourceFile := ast.GetSourceFileOfNode(original) + if originalSourceFile != nil { + startLine, _ := scanner.GetLineAndCharacterOfPosition(originalSourceFile, original.Loc.Pos()) + endLine, _ := scanner.GetLineAndCharacterOfPosition(originalSourceFile, original.Loc.End()) + if startLine == endLine { + tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) + } } } case ast.KindJSDocTypeExpression: From dc868675de6a7a01473e0a10a7d2821cf5cdd840 Mon Sep 17 00:00:00 2001 From: Hamidreza Hanafi Date: Mon, 11 Aug 2025 17:31:26 -0400 Subject: [PATCH 3/3] add helper function --- .../transformers/declarations/transform.go | 11 ++--------- internal/transformers/utilities.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index 0fa5d45953..2f81b28a72 100644 --- a/internal/transformers/declarations/transform.go +++ b/internal/transformers/declarations/transform.go @@ -10,7 +10,6 @@ import ( "github.com/microsoft/typescript-go/internal/modulespecifiers" "github.com/microsoft/typescript-go/internal/nodebuilder" "github.com/microsoft/typescript-go/internal/printer" - "github.com/microsoft/typescript-go/internal/scanner" "github.com/microsoft/typescript-go/internal/transformers" "github.com/microsoft/typescript-go/internal/tspath" ) @@ -468,14 +467,8 @@ func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast. case ast.KindTupleType: result = tx.Visitor().VisitEachChild(input) if result != nil { - original := tx.EmitContext().MostOriginal(input) - originalSourceFile := ast.GetSourceFileOfNode(original) - if originalSourceFile != nil { - startLine, _ := scanner.GetLineAndCharacterOfPosition(originalSourceFile, original.Loc.Pos()) - endLine, _ := scanner.GetLineAndCharacterOfPosition(originalSourceFile, original.Loc.End()) - if startLine == endLine { - tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) - } + if transformers.IsOriginalNodeSingleLine(tx.EmitContext(), input) { + tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) } } case ast.KindJSDocTypeExpression: diff --git a/internal/transformers/utilities.go b/internal/transformers/utilities.go index 010af33d0a..7d4cde7862 100644 --- a/internal/transformers/utilities.go +++ b/internal/transformers/utilities.go @@ -5,6 +5,7 @@ import ( "github.com/microsoft/typescript-go/internal/ast" "github.com/microsoft/typescript-go/internal/printer" + "github.com/microsoft/typescript-go/internal/scanner" ) func IsGeneratedIdentifier(emitContext *printer.EmitContext, name *ast.IdentifierNode) bool { @@ -245,3 +246,20 @@ func IsSimpleCopiableExpression(expression *ast.Expression) bool { ast.IsKeywordKind(expression.Kind) || ast.IsIdentifier(expression) } + +func IsOriginalNodeSingleLine(emitContext *printer.EmitContext, node *ast.Node) bool { + if node == nil { + return false + } + original := emitContext.MostOriginal(node) + if original == nil { + return false + } + source := ast.GetSourceFileOfNode(original) + if source == nil { + return false + } + startLine, _ := scanner.GetLineAndCharacterOfPosition(source, original.Loc.Pos()) + endLine, _ := scanner.GetLineAndCharacterOfPosition(source, original.Loc.End()) + return startLine == endLine +}