Skip to content

Commit

Permalink
feat(es/typescript): Add ts_enum_is_mutable to disable enum inlining (
Browse files Browse the repository at this point in the history
#8115)

**Related issue:**

 - Closes #8114.
  • Loading branch information
dsherret authored Oct 15, 2023
1 parent ad3006e commit 57b171d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/config.rs
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);
"#
);

1 comment on commit 57b171d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 57b171d Previous: 7da3f52 Ratio
es/full/bugs-1 285962 ns/iter (± 3358) 290281 ns/iter (± 3424) 0.99
es/full/minify/libraries/antd 1405561721 ns/iter (± 9634710) 1416638674 ns/iter (± 21486186) 0.99
es/full/minify/libraries/d3 293578866 ns/iter (± 4252848) 297774286 ns/iter (± 3440942) 0.99
es/full/minify/libraries/echarts 1133709593 ns/iter (± 12589371) 1134630164 ns/iter (± 8866371) 1.00
es/full/minify/libraries/jquery 88688386 ns/iter (± 331459) 89532801 ns/iter (± 309873) 0.99
es/full/minify/libraries/lodash 103735700 ns/iter (± 689895) 104753468 ns/iter (± 301989) 0.99
es/full/minify/libraries/moment 52250690 ns/iter (± 88177) 52794327 ns/iter (± 160922) 0.99
es/full/minify/libraries/react 18778922 ns/iter (± 50073) 19047481 ns/iter (± 158342) 0.99
es/full/minify/libraries/terser 231506227 ns/iter (± 1078772) 232872722 ns/iter (± 1824743) 0.99
es/full/minify/libraries/three 410787978 ns/iter (± 5128915) 413114866 ns/iter (± 1337935) 0.99
es/full/minify/libraries/typescript 2807234021 ns/iter (± 7277322) 2846530066 ns/iter (± 9904541) 0.99
es/full/minify/libraries/victory 605929363 ns/iter (± 9900039) 609073673 ns/iter (± 3471456) 0.99
es/full/minify/libraries/vue 126414774 ns/iter (± 808011) 127489727 ns/iter (± 313271) 0.99
es/full/codegen/es3 34193 ns/iter (± 157) 33947 ns/iter (± 97) 1.01
es/full/codegen/es5 34278 ns/iter (± 113) 34033 ns/iter (± 90) 1.01
es/full/codegen/es2015 34207 ns/iter (± 107) 34134 ns/iter (± 86) 1.00
es/full/codegen/es2016 34293 ns/iter (± 89) 34034 ns/iter (± 64) 1.01
es/full/codegen/es2017 34310 ns/iter (± 52) 34212 ns/iter (± 108) 1.00
es/full/codegen/es2018 34277 ns/iter (± 119) 34008 ns/iter (± 79) 1.01
es/full/codegen/es2019 34270 ns/iter (± 83) 34039 ns/iter (± 155) 1.01
es/full/codegen/es2020 34131 ns/iter (± 60) 34075 ns/iter (± 108) 1.00
es/full/all/es3 176321917 ns/iter (± 1239792) 174893038 ns/iter (± 919968) 1.01
es/full/all/es5 169121224 ns/iter (± 756804) 168172736 ns/iter (± 778216) 1.01
es/full/all/es2015 126824512 ns/iter (± 646171) 127766975 ns/iter (± 614183) 0.99
es/full/all/es2016 126821647 ns/iter (± 773319) 126209740 ns/iter (± 1007644) 1.00
es/full/all/es2017 126388716 ns/iter (± 544512) 125854278 ns/iter (± 752301) 1.00
es/full/all/es2018 124121322 ns/iter (± 880802) 123818878 ns/iter (± 568628) 1.00
es/full/all/es2019 123247631 ns/iter (± 850284) 123363559 ns/iter (± 564297) 1.00
es/full/all/es2020 118898476 ns/iter (± 767759) 119385734 ns/iter (± 757408) 1.00
es/full/parser 561380 ns/iter (± 5707) 561227 ns/iter (± 4914) 1.00
es/full/base/fixer 19058 ns/iter (± 112) 18440 ns/iter (± 194) 1.03
es/full/base/resolver_and_hygiene 83243 ns/iter (± 113) 84587 ns/iter (± 346) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.