-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): class properties transform skeleton (#7038)
Skeleton of class properties transform. #7011 contains WIP implementation.
- Loading branch information
1 parent
934cb5e
commit 1d906c6
Showing
5 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! ES2022: Class Properties | ||
//! | ||
//! This plugin transforms class properties to initializers inside class constructor. | ||
//! | ||
//! > This plugin is included in `preset-env`, in ES2022 | ||
//! | ||
//! ## Example | ||
//! | ||
//! Input: | ||
//! ```js | ||
//! class C { | ||
//! foo = 123; | ||
//! #bar = 456; | ||
//! } | ||
//! | ||
//! let x = 123; | ||
//! class D extends S { | ||
//! foo = x; | ||
//! constructor(x) { | ||
//! if (x) { | ||
//! let s = super(x); | ||
//! } else { | ||
//! super(x); | ||
//! } | ||
//! } | ||
//! } | ||
//! ``` | ||
//! | ||
//! Output: | ||
//! ```js | ||
//! var _bar = /*#__PURE__*/ new WeakMap(); | ||
//! class C { | ||
//! constructor() { | ||
//! babelHelpers.defineProperty(this, "foo", 123); | ||
//! babelHelpers.classPrivateFieldInitSpec(this, _bar, 456); | ||
//! } | ||
//! } | ||
//! | ||
//! let x = 123; | ||
//! class D extends S { | ||
//! constructor(_x) { | ||
//! if (_x) { | ||
//! let s = (super(_x), babelHelpers.defineProperty(this, "foo", x)); | ||
//! } else { | ||
//! super(_x); | ||
//! babelHelpers.defineProperty(this, "foo", x); | ||
//! } | ||
//! } | ||
//! } | ||
//! ``` | ||
//! | ||
//! ## Implementation | ||
//! | ||
//! WORK IN PROGRESS. INCOMPLETE. | ||
//! | ||
//! Implementation based on [@babel/plugin-transform-class-properties](https://babel.dev/docs/babel-plugin-transform-class-properties). | ||
//! | ||
//! ## References: | ||
//! * Babel plugin implementation: | ||
//! * <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-class-properties> | ||
//! * <https://github.com/babel/babel/blob/main/packages/babel-helper-create-class-features-plugin/src/index.ts> | ||
//! * <https://github.com/babel/babel/blob/main/packages/babel-helper-create-class-features-plugin/src/fields.ts> | ||
//! * Class properties TC39 proposal: <https://github.com/tc39/proposal-class-fields> | ||
use serde::Deserialize; | ||
|
||
use oxc_ast::ast::*; | ||
use oxc_traverse::{Traverse, TraverseCtx}; | ||
|
||
use crate::TransformCtx; | ||
|
||
#[derive(Debug, Default, Clone, Copy, Deserialize)] | ||
#[serde(default, rename_all = "camelCase")] | ||
pub struct ClassPropertiesOptions { | ||
#[serde(alias = "loose")] | ||
pub(crate) set_public_class_fields: bool, | ||
} | ||
|
||
pub struct ClassProperties<'a, 'ctx> { | ||
#[expect(dead_code)] | ||
options: ClassPropertiesOptions, | ||
#[expect(dead_code)] | ||
ctx: &'ctx TransformCtx<'a>, | ||
} | ||
|
||
impl<'a, 'ctx> ClassProperties<'a, 'ctx> { | ||
pub fn new(options: ClassPropertiesOptions, ctx: &'ctx TransformCtx<'a>) -> Self { | ||
Self { options, ctx } | ||
} | ||
} | ||
|
||
impl<'a, 'ctx> Traverse<'a> for ClassProperties<'a, 'ctx> { | ||
fn enter_class_body(&mut self, _body: &mut ClassBody<'a>, _ctx: &mut TraverseCtx<'a>) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,44 @@ | ||
use oxc_ast::ast::*; | ||
use oxc_traverse::{Traverse, TraverseCtx}; | ||
|
||
use crate::TransformCtx; | ||
|
||
mod class_properties; | ||
mod class_static_block; | ||
mod options; | ||
|
||
use class_properties::ClassProperties; | ||
pub use class_properties::ClassPropertiesOptions; | ||
use class_static_block::ClassStaticBlock; | ||
|
||
pub use options::ES2022Options; | ||
|
||
pub struct ES2022 { | ||
pub struct ES2022<'a, 'ctx> { | ||
options: ES2022Options, | ||
// Plugins | ||
class_static_block: ClassStaticBlock, | ||
class_properties: Option<ClassProperties<'a, 'ctx>>, | ||
} | ||
|
||
impl ES2022 { | ||
pub fn new(options: ES2022Options) -> Self { | ||
Self { options, class_static_block: ClassStaticBlock::new() } | ||
impl<'a, 'ctx> ES2022<'a, 'ctx> { | ||
pub fn new(options: ES2022Options, ctx: &'ctx TransformCtx<'a>) -> Self { | ||
Self { | ||
options, | ||
class_static_block: ClassStaticBlock::new(), | ||
class_properties: options | ||
.class_properties | ||
.map(|options| ClassProperties::new(options, ctx)), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Traverse<'a> for ES2022 { | ||
impl<'a, 'ctx> Traverse<'a> for ES2022<'a, 'ctx> { | ||
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) { | ||
if self.options.class_static_block { | ||
self.class_static_block.enter_class_body(body, ctx); | ||
} | ||
if let Some(class_properties) = &mut self.class_properties { | ||
class_properties.enter_class_body(body, ctx); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Default, Clone, Deserialize)] | ||
use super::ClassPropertiesOptions; | ||
|
||
#[derive(Debug, Default, Clone, Copy, Deserialize)] | ||
#[serde(default, rename_all = "camelCase", deny_unknown_fields)] | ||
pub struct ES2022Options { | ||
#[serde(skip)] | ||
pub class_static_block: bool, | ||
pub class_properties: Option<ClassPropertiesOptions>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters