diff --git a/internal/transformers/declarations/transform.go b/internal/transformers/declarations/transform.go index ad80436fb1..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,9 +467,7 @@ 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()) - if startLine == endLine { + if transformers.IsOriginalNodeSingleLine(tx.EmitContext(), input) { tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine) } } 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 +} 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; + } +} + +