Skip to content

Commit 7892130

Browse files
Copilotjakebailey
andauthored
Fix panic in printer when type-only export is body of an if statement (#3928)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent f9db4c3 commit 7892130

6 files changed

Lines changed: 37 additions & 3 deletions

File tree

internal/printer/emitcontext.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,11 @@ func (c *EmitContext) VisitIterationBody(body *ast.Statement, visitor *ast.NodeV
943943
}
944944

945945
func (c *EmitContext) VisitEmbeddedStatement(node *ast.Statement, visitor *ast.NodeVisitor) *ast.Statement {
946-
embeddedStatement := visitor.VisitEmbeddedStatement(node)
947-
if embeddedStatement == nil {
946+
if node == nil {
948947
return nil
949948
}
950-
if ast.IsNotEmittedStatement(embeddedStatement) {
949+
embeddedStatement := visitor.VisitEmbeddedStatement(node)
950+
if embeddedStatement == nil || ast.IsNotEmittedStatement(embeddedStatement) {
951951
emptyStatement := visitor.Factory.NewEmptyStatement()
952952
emptyStatement.Loc = node.Loc
953953
c.SetOriginal(emptyStatement, node)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
typeOnlyExportAsIfBody.ts(1,11): error TS1233: An export declaration can only be used at the top level of a namespace or module.
2+
3+
4+
==== typeOnlyExportAsIfBody.ts (1 errors) ====
5+
if (true) export type {};
6+
~~~~~~
7+
!!! error TS1233: An export declaration can only be used at the top level of a namespace or module.
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/typeOnlyExportAsIfBody.ts] ////
2+
3+
//// [typeOnlyExportAsIfBody.ts]
4+
if (true) export type {};
5+
6+
7+
//// [typeOnlyExportAsIfBody.js]
8+
"use strict";
9+
if (true)
10+
;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/typeOnlyExportAsIfBody.ts] ////
2+
3+
=== typeOnlyExportAsIfBody.ts ===
4+
5+
if (true) export type {};
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/typeOnlyExportAsIfBody.ts] ////
2+
3+
=== typeOnlyExportAsIfBody.ts ===
4+
if (true) export type {};
5+
>true : true
6+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @module: esnext
2+
// @moduleResolution: bundler
3+
4+
if (true) export type {};

0 commit comments

Comments
 (0)