Skip to content

Commit 02c1c9a

Browse files
authored
fix with statement crash (#64574)
1 parent edf7da4 commit 02c1c9a

6 files changed

Lines changed: 123 additions & 1 deletion

File tree

‎tsc/internal/transformers/moduletransforms/commonjsmodule.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,10 +1260,14 @@ func (tx *CommonJSModuleTransformer) visitTopLevelNestedLabeledStatement(node *a
12601260
// Visits a top-level nested `with` statement as it may contain `var` declarations that are hoisted and may still be
12611261
// exported with `export {}`.
12621262
func (tx *CommonJSModuleTransformer) visitTopLevelNestedWithStatement(node *ast.WithStatement) *ast.Node {
1263+
statement := tx.topLevelNestedVisitor.VisitEmbeddedStatement(node.Statement)
1264+
if statement == nil {
1265+
statement = tx.Factory().NewEmptyStatement()
1266+
}
12631267
return tx.Factory().UpdateWithStatement(
12641268
node,
12651269
tx.Visitor().VisitNode(node.Expression),
1266-
tx.topLevelNestedVisitor.VisitEmbeddedStatement(node.Statement),
1270+
statement,
12671271
)
12681272
}
12691273

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
withStatementCommonJsEmit.cts(3,1): error TS1101: 'with' statements are not allowed in strict mode.
2+
withStatementCommonJsEmit.cts(3,1): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
3+
withStatementCommonJsEmit.cts(4,1): error TS1101: 'with' statements are not allowed in strict mode.
4+
withStatementCommonJsEmit.cts(4,1): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
5+
withStatementCommonJsEmit.cts(5,1): error TS1101: 'with' statements are not allowed in strict mode.
6+
withStatementCommonJsEmit.cts(5,1): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
7+
withStatementCommonJsEmit.cts(6,1): error TS1101: 'with' statements are not allowed in strict mode.
8+
withStatementCommonJsEmit.cts(6,1): error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
9+
10+
11+
==== withStatementCommonJsEmit.cts (8 errors) ====
12+
declare const obj: object;
13+
14+
with (obj) export let a;
15+
~~~~
16+
!!! error TS1101: 'with' statements are not allowed in strict mode.
17+
~~~~~~~~~~
18+
!!! error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
19+
with (obj) export let d = 1;
20+
~~~~
21+
!!! error TS1101: 'with' statements are not allowed in strict mode.
22+
~~~~~~~~~~
23+
!!! error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
24+
with (obj) var local = 2;
25+
~~~~
26+
!!! error TS1101: 'with' statements are not allowed in strict mode.
27+
~~~~~~~~~~
28+
!!! error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
29+
with (obj) {
30+
~~~~
31+
!!! error TS1101: 'with' statements are not allowed in strict mode.
32+
~~~~~~~~~~
33+
!!! error TS2410: The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'.
34+
export let e;
35+
}
36+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/withStatementCommonJsEmit.ts] ////
2+
3+
//// [withStatementCommonJsEmit.cts]
4+
declare const obj: object;
5+
6+
with (obj) export let a;
7+
with (obj) export let d = 1;
8+
with (obj) var local = 2;
9+
with (obj) {
10+
export let e;
11+
}
12+
13+
14+
//// [withStatementCommonJsEmit.cjs]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
with (obj)
18+
;
19+
with (obj)
20+
exports.d = 1;
21+
with (obj)
22+
var local = 2;
23+
with (obj) {
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/withStatementCommonJsEmit.ts] ////
2+
3+
=== withStatementCommonJsEmit.cts ===
4+
declare const obj: object;
5+
>obj : Symbol(obj, Decl(withStatementCommonJsEmit.cts, 0, 13))
6+
7+
with (obj) export let a;
8+
>obj : Symbol(obj, Decl(withStatementCommonJsEmit.cts, 0, 13))
9+
10+
with (obj) export let d = 1;
11+
>obj : Symbol(obj, Decl(withStatementCommonJsEmit.cts, 0, 13))
12+
13+
with (obj) var local = 2;
14+
>obj : Symbol(obj, Decl(withStatementCommonJsEmit.cts, 0, 13))
15+
16+
with (obj) {
17+
>obj : Symbol(obj, Decl(withStatementCommonJsEmit.cts, 0, 13))
18+
19+
export let e;
20+
}
21+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/withStatementCommonJsEmit.ts] ////
2+
3+
=== withStatementCommonJsEmit.cts ===
4+
declare const obj: object;
5+
>obj : object
6+
7+
with (obj) export let a;
8+
>obj : object
9+
>a : any
10+
11+
with (obj) export let d = 1;
12+
>obj : object
13+
>d : any
14+
>1 : any
15+
16+
with (obj) var local = 2;
17+
>obj : object
18+
>local : any
19+
>2 : any
20+
21+
with (obj) {
22+
>obj : object
23+
24+
export let e;
25+
>e : any
26+
}
27+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @module: commonjs
2+
// @filename: withStatementCommonJsEmit.cts
3+
declare const obj: object;
4+
5+
with (obj) export let a;
6+
with (obj) export let d = 1;
7+
with (obj) var local = 2;
8+
with (obj) {
9+
export let e;
10+
}

0 commit comments

Comments
 (0)