oxc icon indicating copy to clipboard operation
oxc copied to clipboard

transformer: inline const enum

Open Boshen opened this issue 1 year ago • 4 comments

Babel Option: https://babel.dev/docs/babel-preset-typescript#optimizeconstenums

Example:

Input:

const enum X {
    FOO
}

X.FOO

Output:

0 /* X.FOO */;

Boshen avatar Sep 26 '24 11:09 Boshen

Babel revisits the AST https://github.com/babel/babel/blob/94e166782a37074d8c0031fc869f37e1b456194f/packages/babel-plugin-transform-typescript/src/const-enum.ts#L69

For us it should be adding enum data to semantic.

struct EnumTable {
  enums: FxHashMap<AstNodeId, EnumData>
}

struct EnumData {
  symbol_id: SymbolId,
  scope_id: ScopeId,
  ...
}

In the transformer, if the symbol is a const enum, we can lookup the enum value from semantic.

Boshen avatar Oct 03 '24 03:10 Boshen

I'm thinking about implementing inline literal enum values across modules in rolldown.

It would be good if oxc also collects non-const enum data. Rolldown could use this information to inline literal enum values across modules.

hyf0 avatar Oct 07 '24 14:10 hyf0

I'm thinking about implementing inline literal enum values across modules in rolldown.

It would be good if oxc also collects non-const enum data. Rolldown could use this information to inline literal enum values across modules.

I'm keen to work on exposing enum data from Semantic, but there are some prerequisites need to be fulfilled before this. Do you have an estimate time of arrival for this feature?

Boshen avatar Oct 07 '24 14:10 Boshen

Do you have an estimate time of arrival for this feature?

Nope.

Let's have a discussion in the weekly-meeting.

hyf0 avatar Oct 07 '24 14:10 hyf0

Any updates? This will cause my downstream to throw undefined XXX because my upstream uses const enum.

ianzone avatar Jun 05 '25 17:06 ianzone

This would be really nice for Vue Router: I use const enums as a trick to minimize the bundle a bit

posva avatar Jul 22 '25 14:07 posva

Any updates on the progress? @Boshen

weifeiyue avatar Oct 23 '25 13:10 weifeiyue