Skip to content

Commit

Permalink
Feat: set_public_class_fields assumption
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 31, 2024
1 parent 7259384 commit c71dda8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/compiler_assumptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub struct CompilerAssumptions {
pub set_computed_properties: bool,

#[serde(default)]
#[deprecated = "Not Implemented"]
pub set_public_class_fields: bool,

#[serde(default)]
Expand Down
17 changes: 12 additions & 5 deletions crates/oxc_transformer/src/es2022/class_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use oxc_span::SPAN;
use oxc_syntax::{node::NodeId, scope::ScopeFlags, symbol::SymbolFlags};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};

use crate::{common::helper_loader::Helper, TransformCtx};
use crate::{common::helper_loader::Helper, CompilerAssumptions, TransformCtx};

#[derive(Debug, Default, Clone, Copy, Deserialize)]
#[serde(default, rename_all = "camelCase")]
Expand All @@ -79,13 +79,20 @@ pub struct ClassPropertiesOptions {
}

pub struct ClassProperties<'a, 'ctx> {
options: ClassPropertiesOptions,
set_public_class_fields: bool,
ctx: &'ctx TransformCtx<'a>,
}

impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
pub fn new(options: ClassPropertiesOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { options, ctx }
pub fn new(
options: ClassPropertiesOptions,
assumptions: &CompilerAssumptions,
ctx: &'ctx TransformCtx<'a>,
) -> Self {
let set_public_class_fields =
options.set_public_class_fields || assumptions.set_public_class_fields;

Self { set_public_class_fields, ctx }
}
}

Expand Down Expand Up @@ -186,7 +193,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
};

// Convert to assignment or `_defineProperty` call, depending on `loose` option
let init_expr = if self.options.set_public_class_fields {
let init_expr = if self.set_public_class_fields {
// `this.foo = value`
let key = match &prop.key {
PropertyKey::StaticIdentifier(ident) => ident.as_ref().clone(),
Expand Down
10 changes: 7 additions & 3 deletions crates/oxc_transformer/src/es2022/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::TransformCtx;
use crate::{CompilerAssumptions, TransformCtx};

mod class_properties;
mod class_static_block;
Expand All @@ -21,13 +21,17 @@ pub struct ES2022<'a, 'ctx> {
}

impl<'a, 'ctx> ES2022<'a, 'ctx> {
pub fn new(options: ES2022Options, ctx: &'ctx TransformCtx<'a>) -> Self {
pub fn new(
options: ES2022Options,
assumptions: &CompilerAssumptions,
ctx: &'ctx TransformCtx<'a>,
) -> Self {
Self {
options,
class_static_block: ClassStaticBlock::new(),
class_properties: options
.class_properties
.map(|options| ClassProperties::new(options, ctx)),
.map(|options| ClassProperties::new(options, assumptions, ctx)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<'a> Transformer<'a> {
.is_typescript()
.then(|| TypeScript::new(&self.options.typescript, &self.ctx)),
x1_jsx: Jsx::new(self.options.jsx, ast_builder, &self.ctx),
x2_es2022: ES2022::new(self.options.env.es2022, &self.ctx),
x2_es2022: ES2022::new(self.options.env.es2022, &self.options.assumptions, &self.ctx),
x2_es2021: ES2021::new(self.options.env.es2021, &self.ctx),
x2_es2020: ES2020::new(self.options.env.es2020, &self.ctx),
x2_es2019: ES2019::new(self.options.env.es2019),
Expand Down
16 changes: 2 additions & 14 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 394/1321
Passed: 398/1321

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -1427,7 +1427,7 @@ x Output mismatch
x Output mismatch


# babel-plugin-transform-class-properties (17/243)
# babel-plugin-transform-class-properties (21/243)
* assumption-constantSuper/complex-super-class/input.js
x Output mismatch

Expand All @@ -1449,21 +1449,12 @@ x Output mismatch
* assumption-setPublicClassFields/constructor-collision/input.js
x Output mismatch

* assumption-setPublicClassFields/derived/input.js
x Output mismatch

* assumption-setPublicClassFields/foobar/input.js
x Output mismatch

* assumption-setPublicClassFields/instance/input.js
x Output mismatch

* assumption-setPublicClassFields/instance-computed/input.js
x Output mismatch

* assumption-setPublicClassFields/instance-undefined/input.js
x Output mismatch

* assumption-setPublicClassFields/length-name-use-define/input.js
x Output mismatch

Expand Down Expand Up @@ -1503,9 +1494,6 @@ x Output mismatch
* assumption-setPublicClassFields/static-undefined/input.js
x Output mismatch

* assumption-setPublicClassFields/super-call/input.js
x Output mismatch

* assumption-setPublicClassFields/super-expression/input.js
x Output mismatch

Expand Down

0 comments on commit c71dda8

Please sign in to comment.