Skip to content

Commit f5deb55

Browse files
Dunqingclaude
andcommitted
feat(napi/transform): expose optimizeConstEnums and optimizeEnums options (#21388)
Surface the TypeScript transformer's `optimize_const_enums` and `optimize_enums` options through the napi bindings so JavaScript consumers can opt into const enum inlining and regular (non-const) enum inlining. Previously both were hardcoded to `false` in the `From<TypeScriptOptions>` conversion, making them unreachable from JS. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b3ed467 commit f5deb55

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

napi/transform/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,24 @@ export interface TypeScriptOptions {
616616
* Defaults to `false`.
617617
*/
618618
removeClassFieldsWithoutInitializer?: boolean
619+
/**
620+
* When true, optimize const enums by inlining their values at usage sites
621+
* and removing the enum declaration.
622+
*
623+
* @default false
624+
*/
625+
optimizeConstEnums?: boolean
626+
/**
627+
* When true, optimize regular (non-const) enums by inlining their member
628+
* accesses at usage sites when the member value is statically known.
629+
*
630+
* Non-exported enum declarations are also removed when all members are
631+
* evaluable and no references to the enum as a runtime value exist
632+
* (e.g., `console.log(Foo)`, `typeof Foo`, or passing the enum as an argument).
633+
*
634+
* @default false
635+
*/
636+
optimizeEnums?: boolean
619637
/**
620638
* Also generate a `.d.ts` declaration file for TypeScript files.
621639
*

napi/transform/src/transformer.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,20 @@ pub struct TypeScriptOptions {
314314
///
315315
/// Defaults to `false`.
316316
pub remove_class_fields_without_initializer: Option<bool>,
317+
/// When true, optimize const enums by inlining their values at usage sites
318+
/// and removing the enum declaration.
319+
///
320+
/// @default false
321+
pub optimize_const_enums: Option<bool>,
322+
/// When true, optimize regular (non-const) enums by inlining their member
323+
/// accesses at usage sites when the member value is statically known.
324+
///
325+
/// Non-exported enum declarations are also removed when all members are
326+
/// evaluable and no references to the enum as a runtime value exist
327+
/// (e.g., `console.log(Foo)`, `typeof Foo`, or passing the enum as an argument).
328+
///
329+
/// @default false
330+
pub optimize_enums: Option<bool>,
317331
/// Also generate a `.d.ts` declaration file for TypeScript files.
318332
///
319333
/// The source file must be compliant with all
@@ -345,8 +359,8 @@ impl From<TypeScriptOptions> for oxc::transformer::TypeScriptOptions {
345359
.unwrap_or(ops.only_remove_type_imports),
346360
allow_namespaces: options.allow_namespaces.unwrap_or(ops.allow_namespaces),
347361
allow_declare_fields: options.allow_declare_fields.unwrap_or(ops.allow_declare_fields),
348-
optimize_const_enums: false,
349-
optimize_enums: false,
362+
optimize_const_enums: options.optimize_const_enums.unwrap_or(ops.optimize_const_enums),
363+
optimize_enums: options.optimize_enums.unwrap_or(ops.optimize_enums),
350364
remove_class_fields_without_initializer: options
351365
.remove_class_fields_without_initializer
352366
.unwrap_or(ops.remove_class_fields_without_initializer),

napi/transform/test/transform.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,32 @@ describe("typescript", () => {
409409
`);
410410
});
411411

412+
test("optimizeConstEnums", () => {
413+
const code = `
414+
const enum Color { Red = 1, Green, Blue }
415+
console.log(Color.Red, Color.Green, Color.Blue);
416+
`;
417+
const ret = transformSync("test.ts", code, {
418+
typescript: {
419+
optimizeConstEnums: true,
420+
},
421+
});
422+
expect(ret.code).toEqual("console.log(1, 2, 3);\n");
423+
});
424+
425+
test("optimizeEnums", () => {
426+
const code = `
427+
enum Status { Active = 1, Inactive = 2 }
428+
console.log(Status.Active, Status.Inactive);
429+
`;
430+
const ret = transformSync("test.ts", code, {
431+
typescript: {
432+
optimizeEnums: true,
433+
},
434+
});
435+
expect(ret.code).toEqual("console.log(1, 2);\n");
436+
});
437+
412438
test("align `useDefineForClassFields: false`", () => {
413439
const code = `
414440
class Foo {

0 commit comments

Comments
 (0)