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 native_class_properties to skip reordering of class properties inits #9421

Merged
merged 9 commits into from
Aug 14, 2024
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
7 changes: 7 additions & 0 deletions .changeset/neat-humans-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_core: patch
swc_fast_ts_strip: patch
swc_ecma_transforms_typescript: minor
---

feat(es/typescript): Add `native_class_properties ` to skip reordering of class properties inits
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms/tests/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn run_test(input: PathBuf) {
typescript(
typescript::Config {
verbatim_module_syntax: false,
native_class_properties: false,
import_not_used_as_values: typescript::ImportsNotUsedAsValues::Remove,
no_empty_export: true,
import_export_assign_config:
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_transforms_typescript/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
/// https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax
#[serde(default)]
pub verbatim_module_syntax: bool,

/// Native class properties support
#[serde(default)]
pub native_class_properties: bool,

/// https://www.typescriptlang.org/tsconfig/#importsNotUsedAsValues
#[serde(default)]
pub import_not_used_as_values: ImportsNotUsedAsValues,

Expand Down
49 changes: 47 additions & 2 deletions crates/swc_ecma_transforms_typescript/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) struct Transform {
import_export_assign_config: TsImportExportAssignConfig,
ts_enum_is_mutable: bool,
verbatim_module_syntax: bool,
native_class_properties: bool,

namespace_id: Option<Id>,
record: TsEnumRecord,
Expand All @@ -74,6 +75,7 @@ pub fn transform(
import_export_assign_config: TsImportExportAssignConfig,
ts_enum_is_mutable: bool,
verbatim_module_syntax: bool,
native_class_properties: bool,
) -> impl Fold + VisitMut {
as_folder(Transform {
unresolved_mark,
Expand All @@ -82,6 +84,7 @@ pub fn transform(
import_export_assign_config,
ts_enum_is_mutable,
verbatim_module_syntax,
native_class_properties,
..Default::default()
})
}
Expand Down Expand Up @@ -139,7 +142,11 @@ impl VisitMut for Transform {
let init_list = mem::replace(&mut self.in_class_prop_init, init_list);

if !prop_list.is_empty() {
self.reorder_class_prop_decls(n, prop_list, init_list);
if self.native_class_properties {
self.reorder_class_prop_decls(n, prop_list, init_list);
} else {
self.reorder_class_prop_decls_and_inits(n, prop_list, init_list);
}
}
}

Expand Down Expand Up @@ -728,7 +735,7 @@ impl Transform {
}

impl Transform {
fn reorder_class_prop_decls(
fn reorder_class_prop_decls_and_inits(
&mut self,
class_member_list: &mut Vec<ClassMember>,
prop_list: Vec<Id>,
Expand Down Expand Up @@ -829,6 +836,44 @@ impl Transform {
.map(ClassMember::ClassProp),
);
}

fn reorder_class_prop_decls(
&mut self,
class_member_list: &mut Vec<ClassMember>,
prop_list: Vec<Id>,
init_list: Vec<Box<Expr>>,
) {
if let Some(constructor) = class_member_list
.iter_mut()
.find_map(|m| m.as_mut_constructor())
{
inject_after_super(constructor, init_list);
}

class_member_list.splice(
0..0,
prop_list
.into_iter()
.map(Ident::from)
.map(PropName::from)
.map(|key| ClassProp {
span: DUMMY_SP,
key,
value: None,
type_ann: None,
is_static: false,
decorators: Vec::new(),
accessibility: None,
is_abstract: false,
is_optional: false,
is_override: false,
readonly: false,
declare: false,
definite: false,
})
.map(ClassMember::ClassProp),
);
}
}

impl Transform {
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 @@ -57,6 +57,7 @@ impl VisitMut for TypeScript {
self.config.import_export_assign_config,
self.config.ts_enum_is_mutable,
self.config.verbatim_module_syntax,
self.config.native_class_properties,
));

if let Some(span) = was_module {
Expand Down
21 changes: 21 additions & 0 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,31 @@ interface Options {
}

interface TransformConfig {
/**
* @see https://www.typescriptlang.org/tsconfig#verbatimModuleSyntax
*/
verbatimModuleSyntax?: boolean;
/**
* Native class properties support
*/
nativeClassProperties?: boolean;
importNotUsedAsValues?: "remove" | "preserve";
/**
* Don't create `export {}`.
* By default, strip creates `export {}` for modules to preserve module
* context.
*
* @see https://github.com/swc-project/swc/issues/1698
*/
noEmptyExport?: boolean;
importExportAssignConfig?: "Classic" | "Preserve" | "NodeNext" | "EsNext";
/**
* 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.
*/
tsEnumIsMutable?: boolean;
}
"#;
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ fn opts(mode: Mode) -> Options {
..Default::default()
},
mode,
transform: Some(swc_ecma_transforms_typescript::Config {
native_class_properties: true,
..Default::default()
}),
..Default::default()
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/class-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo {
x = console.log(1)
constructor(public y = console.log(2)) {
console.log(3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x TypeScript parameter property is not supported in strip-only mode
,-[3:1]
2 | x = console.log(1)
3 | constructor(public y = console.log(2)) {
: ^^^^^^^^^^^^^^^^^^
4 | console.log(3)
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Foo {
y;
x = console.log(1);
constructor(y = console.log(2)){
this.y = y;
console.log(3);
}
}
6 changes: 6 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/class-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo {
x = console.log(1)
constructor(public y = console.log(2)) {
console.log(3)
}
}
Loading