Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/typescript): Add ts_enum_is_mutable to disable enum inlining #8115

Merged
merged 6 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/config.rs
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ pub struct Config {

#[serde(default)]
pub import_export_assign_config: TsImportExportAssignConfig,

/// Disables an optimization that inlines TS enum member values
/// within the same module that assumes the enum member values
/// are never modified.
///
/// Defaults to false.
#[serde(default)]
pub ts_enum_is_mutable: bool,
}

#[derive(Debug, Default, Serialize, Deserialize)]
Expand Down
12 changes: 6 additions & 6 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(crate) struct Transform {
top_level_ctxt: SyntaxContext,

import_export_assign_config: TsImportExportAssignConfig,
ts_enum_is_mutable: bool,
verbatim_module_syntax: bool,

namespace_id: Option<Id>,
Expand All @@ -64,12 +65,14 @@ pub(crate) struct Transform {
pub fn transform(
top_level_mark: Mark,
import_export_assign_config: TsImportExportAssignConfig,
ts_enum_is_mutable: bool,
verbatim_module_syntax: bool,
) -> impl Fold + VisitMut {
as_folder(Transform {
top_level_mark,
top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark),
import_export_assign_config,
ts_enum_is_mutable,
verbatim_module_syntax,
..Default::default()
})
Expand All @@ -81,13 +84,10 @@ impl VisitMut for Transform {
fn visit_mut_program(&mut self, n: &mut Program) {
n.visit_mut_children_with(self);

if self.record.is_empty() {
return;
if !self.ts_enum_is_mutable && !self.record.is_empty() {
let record = mem::take(&mut self.record);
n.visit_mut_children_with(&mut InlineEnum::new(record));
}

let record = mem::take(&mut self.record);

n.visit_mut_children_with(&mut InlineEnum::new(record));
}

fn visit_mut_module_items(&mut self, n: &mut Vec<ModuleItem>) {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_typescript/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl VisitMut for TypeScript {
n.visit_mut_with(&mut transform(
self.top_level_mark,
self.config.import_export_assign_config,
self.config.ts_enum_is_mutable,
self.config.verbatim_module_syntax,
));

Expand Down
31 changes: 31 additions & 0 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4552,3 +4552,34 @@ test!(
}
"
);

test!(
Syntax::Typescript(TsConfig::default()),
|_| tr_config(
Some(typescript::Config {
ts_enum_is_mutable: true,
..Default::default()
}),
None,
true,
),
ts_enum_is_mutable_true,
r#"
enum D {
A,
B,
}

(D as any).A = 5;
console.log(D.A);
"#,
r#"
var D;
(function(D) {
D[D["A"] = 0] = "A";
D[D["B"] = 1] = "B";
})(D || (D = {}));
D.A = 5;
console.log(D.A);
"#
);