Skip to content

Commit e2ca770

Browse files
authored
fix(codegen): add support for printing type arguments in new expressions (#15963)
Code that's parsed as follows: ``` const a = new Map<string, number>(); ``` Is printed by codegen as follows: ``` const a = new Map(); ``` While for `CallExpression` we do render them: ``` const a = test<string, number>(); ``` Is rendered the same. This PR adds support for rendering type parameters also for new expressions.
1 parent 9827d92 commit e2ca770

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

crates/oxc_codegen/src/gen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,9 @@ impl GenExpr for NewExpression<'_> {
22022202
p.print_str("new");
22032203
p.print_soft_space();
22042204
self.callee.print_expr(p, Precedence::New, Context::FORBID_CALL);
2205+
if let Some(type_parameters) = &self.type_arguments {
2206+
type_parameters.print(p, ctx);
2207+
}
22052208

22062209
// Omit the "()" when minifying, but only when safe to do so
22072210
if !p.options.minify || !self.arguments.is_empty() || precedence >= Precedence::Postfix

crates/oxc_codegen/tests/integration/snapshots/minify.snap

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,26 @@ c = foo<string>;
132132
----------
133133
c=foo<string> ;
134134
########## 31
135+
new Map<string, number>();
136+
----------
137+
new Map<string,number>;
138+
########## 32
135139
d = x satisfies y;
136140
----------
137141
d=((x) satisfies y);
138-
########## 32
142+
########## 33
139143
export @x declare abstract class C {}
140144
----------
141145
export @x declare abstract class C{}
142-
########## 33
146+
########## 34
143147
div<T>``
144148
----------
145149
div<T>``;
146-
########## 34
150+
########## 35
147151
export type Component<Props = any> = Foo;
148152
----------
149153
export type Component<Props = any>=Foo;
150-
########## 35
154+
########## 36
151155

152156
export type Component<
153157
Props = any,
@@ -163,19 +167,19 @@ export type Component<
163167

164168
----------
165169
export type Component<Props = any,RawBindings = any,D = any,C extends ComputedOptions = ComputedOptions,M extends MethodOptions = MethodOptions,E extends EmitsOptions|Record<string,any[]> = {},S extends Record<string,any> = any>=ConcreteComponent<Props,RawBindings,D,C,M,E,S>|ComponentPublicInstanceConstructor<Props>;
166-
########## 36
170+
########## 37
167171
(a || b) as any
168172
----------
169173
(a||b) as any;
170-
########## 37
174+
########## 38
171175
(a ** b) as any
172176
----------
173177
(a**b) as any;
174-
########## 38
178+
########## 39
175179
(function g() {}) as any
176180
----------
177181
(function g(){}) as any;
178-
########## 39
182+
########## 40
179183

180184
import defaultExport from "module-name";
181185
import * as name from "module-name";
@@ -212,14 +216,14 @@ export { default as name16 } from "module-name";
212216

213217
----------
214218
import defaultExport from"module-name";import*as name from"module-name";import{export1}from"module-name";import{export1 as alias1}from"module-name";import{default as alias}from"module-name";import{export1,export2}from"module-name";import{export1,export2 as alias2}from"module-name";import{"string name" as alias}from"module-name";import defaultExport,{export1}from"module-name";import defaultExport,*as name from"module-name";import"module-name";import{}from"mod";export let name1,name2;export const name3=1,name4=2;export function functionName(){}export class ClassName{}export function*generatorFunctionName(){}export const{name5,name2:bar}=o;export const[name6,name7]=array;export{name8,name81};export{variable1 as name9,variable2 as name10,name82};export{variable1 as "string name"};export{name1 as default1};export*from"module-name";export*as name11 from"module-name";export{name12,nameN}from"module-name";export{import1 as name13,import2 as name14,name15}from"module-name";export{default}from"module-name";export{default as name16}from"module-name";
215-
########## 40
219+
########## 41
216220

217221
import a = require("a");
218222
export import b = require("b");
219223

220224
----------
221225
import a = require("a");export import b = require("b");
222-
########## 41
226+
########## 42
223227
class C {
224228
static
225229
static

crates/oxc_codegen/tests/integration/snapshots/ts.snap

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,31 @@ c = foo<string>;
198198
c = foo<string>;
199199

200200
########## 31
201+
new Map<string, number>();
202+
----------
203+
new Map<string, number>();
204+
205+
########## 32
201206
d = x satisfies y;
202207
----------
203208
d = ((x) satisfies y);
204209

205-
########## 32
210+
########## 33
206211
export @x declare abstract class C {}
207212
----------
208213
export @x declare abstract class C {}
209214

210-
########## 33
215+
########## 34
211216
div<T>``
212217
----------
213218
div<T>``;
214219

215-
########## 34
220+
########## 35
216221
export type Component<Props = any> = Foo;
217222
----------
218223
export type Component<Props = any> = Foo;
219224

220-
########## 35
225+
########## 36
221226

222227
export type Component<
223228
Props = any,
@@ -242,22 +247,22 @@ export type Component<
242247
S extends Record<string, any> = any
243248
> = ConcreteComponent<Props, RawBindings, D, C, M, E, S> | ComponentPublicInstanceConstructor<Props>;
244249

245-
########## 36
250+
########## 37
246251
(a || b) as any
247252
----------
248253
(a || b) as any;
249254

250-
########## 37
255+
########## 38
251256
(a ** b) as any
252257
----------
253258
(a ** b) as any;
254259

255-
########## 38
260+
########## 39
256261
(function g() {}) as any
257262
----------
258263
(function g() {}) as any;
259264

260-
########## 39
265+
########## 40
261266

262267
import defaultExport from "module-name";
263268
import * as name from "module-name";
@@ -323,7 +328,7 @@ export { import1 as name13, import2 as name14, name15 } from 'module-name';
323328
export { default } from 'module-name';
324329
export { default as name16 } from 'module-name';
325330

326-
########## 40
331+
########## 41
327332

328333
import a = require("a");
329334
export import b = require("b");
@@ -332,7 +337,7 @@ export import b = require("b");
332337
import a = require('a');
333338
export import b = require('b');
334339

335-
########## 41
340+
########## 42
336341
class C {
337342
static
338343
static

crates/oxc_codegen/tests/integration/ts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fn ts() {
6868
"a = x!;",
6969
"b = (x as y);",
7070
"c = foo<string>;",
71+
"new Map<string, number>();",
7172
"d = x satisfies y;",
7273
"export @x declare abstract class C {}",
7374
"div<T>``",

0 commit comments

Comments
 (0)