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 4 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
26 changes: 25 additions & 1 deletion 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
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
#[serde(default)]
pub verbatim_module_syntax: bool,
Expand All @@ -18,6 +18,26 @@ pub struct Config {

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

/// An optimization that inlines TS enum member values within
/// the same module. This assumes the enum member values are
/// never modified.
///
/// Defaults to true.
#[serde(default = "true_by_default")]
pub ts_enum_is_readonly: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@magic-akari oh, I've never noticed

#[serde(default)]
#[deprecated(note = "This value will be always true")]
pub ts_enum_is_readonly: bool,

Should it use it instead? I'm not sure what the API would look like here though.

Copy link
Member

Choose a reason for hiding this comment

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

It's a JavaScript API for end users.
Since we only change the rust side API, this option is irrelevant.

}

impl Default for Config {
fn default() -> Self {
Self {
verbatim_module_syntax: false,
import_not_used_as_values: Default::default(),
no_empty_export: false,
import_export_assign_config: Default::default(),
ts_enum_is_readonly: true,
}
}
}

#[derive(Debug, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -70,3 +90,7 @@ pub enum ImportsNotUsedAsValues {

#[deprecated = "ImportNotUsedAsValues is renamed to ImportsNotUsedAsValues"]
pub type ImportNotUsedAsValues = ImportsNotUsedAsValues;

const fn true_by_default() -> bool {
dsherret marked this conversation as resolved.
Show resolved Hide resolved
true
}
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_readonly: 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_readonly: 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_readonly,
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_readonly && !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_readonly,
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_readonly: false,
..Default::default()
}),
None,
true,
),
ts_enum_is_readonly_false,
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);
"#
);
Loading