Skip to content

Commit 75572ab

Browse files
Fix panic runtime error on ast.KindTupleTypes (#1525)
1 parent 4be54c5 commit 75572ab

File tree

6 files changed

+210
-4
lines changed

6 files changed

+210
-4
lines changed

internal/transformers/declarations/transform.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/microsoft/typescript-go/internal/modulespecifiers"
1111
"github.com/microsoft/typescript-go/internal/nodebuilder"
1212
"github.com/microsoft/typescript-go/internal/printer"
13-
"github.com/microsoft/typescript-go/internal/scanner"
1413
"github.com/microsoft/typescript-go/internal/transformers"
1514
"github.com/microsoft/typescript-go/internal/tspath"
1615
)
@@ -468,9 +467,7 @@ func (tx *DeclarationTransformer) visitDeclarationSubtree(input *ast.Node) *ast.
468467
case ast.KindTupleType:
469468
result = tx.Visitor().VisitEachChild(input)
470469
if result != nil {
471-
startLine, _ := scanner.GetLineAndCharacterOfPosition(tx.state.currentSourceFile, input.Loc.Pos())
472-
endLine, _ := scanner.GetLineAndCharacterOfPosition(tx.state.currentSourceFile, input.Loc.End())
473-
if startLine == endLine {
470+
if transformers.IsOriginalNodeSingleLine(tx.EmitContext(), input) {
474471
tx.EmitContext().AddEmitFlags(result, printer.EFSingleLine)
475472
}
476473
}

internal/transformers/utilities.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/microsoft/typescript-go/internal/ast"
77
"github.com/microsoft/typescript-go/internal/printer"
8+
"github.com/microsoft/typescript-go/internal/scanner"
89
)
910

1011
func IsGeneratedIdentifier(emitContext *printer.EmitContext, name *ast.IdentifierNode) bool {
@@ -245,3 +246,20 @@ func IsSimpleCopiableExpression(expression *ast.Expression) bool {
245246
ast.IsKeywordKind(expression.Kind) ||
246247
ast.IsIdentifier(expression)
247248
}
249+
250+
func IsOriginalNodeSingleLine(emitContext *printer.EmitContext, node *ast.Node) bool {
251+
if node == nil {
252+
return false
253+
}
254+
original := emitContext.MostOriginal(node)
255+
if original == nil {
256+
return false
257+
}
258+
source := ast.GetSourceFileOfNode(original)
259+
if source == nil {
260+
return false
261+
}
262+
startLine, _ := scanner.GetLineAndCharacterOfPosition(source, original.Loc.Pos())
263+
endLine, _ := scanner.GetLineAndCharacterOfPosition(source, original.Loc.End())
264+
return startLine == endLine
265+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] ////
2+
3+
//// [index.d.ts]
4+
// A bunch of random text to move the positions forward
5+
// A bunch of random text to move the positions forward
6+
// A bunch of random text to move the positions forward
7+
// A bunch of random text to move the positions forward
8+
// A bunch of random text to move the positions forward
9+
// A bunch of random text to move the positions forward
10+
// A bunch of random text to move the positions forward
11+
// A bunch of random text to move the positions forward
12+
// A bunch of random text to move the positions forward
13+
14+
type ShouldJustBeAny = [any][0];
15+
16+
declare namespace knex {
17+
export { Knex };
18+
}
19+
20+
declare namespace Knex {
21+
interface Interface {
22+
method(): ShouldJustBeAny;
23+
}
24+
}
25+
26+
export = knex;
27+
28+
//// [index.ts]
29+
import "knex";
30+
declare module "knex" {
31+
namespace Knex {
32+
function newFunc(): Knex.Interface;
33+
}
34+
}
35+
36+
37+
38+
39+
//// [index.js]
40+
import "knex";
41+
42+
43+
//// [index.d.ts]
44+
import "knex";
45+
declare module "knex" {
46+
namespace Knex {
47+
function newFunc(): Knex.Interface;
48+
}
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] ////
2+
3+
=== node_modules/knex/index.d.ts ===
4+
// A bunch of random text to move the positions forward
5+
// A bunch of random text to move the positions forward
6+
// A bunch of random text to move the positions forward
7+
// A bunch of random text to move the positions forward
8+
// A bunch of random text to move the positions forward
9+
// A bunch of random text to move the positions forward
10+
// A bunch of random text to move the positions forward
11+
// A bunch of random text to move the positions forward
12+
// A bunch of random text to move the positions forward
13+
14+
type ShouldJustBeAny = [any][0];
15+
>ShouldJustBeAny : Symbol(ShouldJustBeAny, Decl(index.d.ts, 0, 0))
16+
17+
declare namespace knex {
18+
>knex : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14))
19+
20+
export { Knex };
21+
>Knex : Symbol(Knex, Decl(index.d.ts, 13, 10))
22+
}
23+
24+
declare namespace Knex {
25+
>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23))
26+
27+
interface Interface {
28+
>Interface : Symbol(Interface, Decl(index.d.ts, 16, 24))
29+
30+
method(): ShouldJustBeAny;
31+
>method : Symbol(method, Decl(index.d.ts, 17, 23))
32+
>ShouldJustBeAny : Symbol(ShouldJustBeAny, Decl(index.d.ts, 0, 0))
33+
}
34+
}
35+
36+
export = knex;
37+
>knex : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14))
38+
39+
=== index.ts ===
40+
import "knex";
41+
declare module "knex" {
42+
>"knex" : Symbol(knex, Decl(index.d.ts, 10, 32), Decl(index.ts, 0, 14))
43+
44+
namespace Knex {
45+
>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23))
46+
47+
function newFunc(): Knex.Interface;
48+
>newFunc : Symbol(newFunc, Decl(index.ts, 2, 18))
49+
>Knex : Symbol(Knex, Decl(index.d.ts, 14, 1), Decl(index.ts, 1, 23))
50+
>Interface : Symbol(Interface, Decl(index.d.ts, 16, 24))
51+
}
52+
}
53+
54+
55+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/compiler/declarationEmitAugmentationUsesCorrectSourceFile.ts] ////
2+
3+
=== node_modules/knex/index.d.ts ===
4+
// A bunch of random text to move the positions forward
5+
// A bunch of random text to move the positions forward
6+
// A bunch of random text to move the positions forward
7+
// A bunch of random text to move the positions forward
8+
// A bunch of random text to move the positions forward
9+
// A bunch of random text to move the positions forward
10+
// A bunch of random text to move the positions forward
11+
// A bunch of random text to move the positions forward
12+
// A bunch of random text to move the positions forward
13+
14+
type ShouldJustBeAny = [any][0];
15+
>ShouldJustBeAny : any
16+
17+
declare namespace knex {
18+
export { Knex };
19+
>Knex : any
20+
}
21+
22+
declare namespace Knex {
23+
interface Interface {
24+
method(): ShouldJustBeAny;
25+
>method : () => any
26+
}
27+
}
28+
29+
export = knex;
30+
>knex : typeof import("knex")
31+
32+
=== index.ts ===
33+
import "knex";
34+
declare module "knex" {
35+
>"knex" : typeof import("knex")
36+
37+
namespace Knex {
38+
>Knex : typeof Knex
39+
40+
function newFunc(): Knex.Interface;
41+
>newFunc : () => Interface
42+
>Knex : any
43+
}
44+
}
45+
46+
47+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// @strict: true
2+
// @module: preserve
3+
// @declaration: true
4+
5+
// @filename: node_modules/knex/index.d.ts
6+
7+
// A bunch of random text to move the positions forward
8+
// A bunch of random text to move the positions forward
9+
// A bunch of random text to move the positions forward
10+
// A bunch of random text to move the positions forward
11+
// A bunch of random text to move the positions forward
12+
// A bunch of random text to move the positions forward
13+
// A bunch of random text to move the positions forward
14+
// A bunch of random text to move the positions forward
15+
// A bunch of random text to move the positions forward
16+
17+
type ShouldJustBeAny = [any][0];
18+
19+
declare namespace knex {
20+
export { Knex };
21+
}
22+
23+
declare namespace Knex {
24+
interface Interface {
25+
method(): ShouldJustBeAny;
26+
}
27+
}
28+
29+
export = knex;
30+
31+
// @filename: index.ts
32+
33+
import "knex";
34+
declare module "knex" {
35+
namespace Knex {
36+
function newFunc(): Knex.Interface;
37+
}
38+
}
39+
40+

0 commit comments

Comments
 (0)