Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ES2018 object spread transformation.
//!
//! This plugin transforms object spread properties (`{ ...x }`) to a series of `_objectSpread` calls.
//! This plugin transforms rest properties for object destructuring assignment and spread properties for object literals.
//!
//! > This plugin is included in `preset-env`, in ES2018
//!
Expand All @@ -26,27 +26,47 @@
//! * Babel plugin implementation: <https://github.com/babel/babel/tree/main/packages/babel-plugin-transform-object-rest-spread>
//! * Object rest/spread TC39 proposal: <https://github.com/tc39/proposal-object-rest-spread>

use serde::Deserialize;

use oxc_ast::{ast::*, NONE};
use oxc_semantic::{ReferenceFlags, SymbolId};
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};

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

use super::ObjectRestSpreadOptions;
#[derive(Debug, Default, Clone, Copy, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct ObjectRestSpreadOptions {
#[serde(alias = "loose")]
pub(crate) set_spread_properties: bool,

pub struct ObjectSpread<'a, 'ctx> {
options: ObjectRestSpreadOptions,
pub(crate) use_built_ins: bool,
}

pub struct ObjectRestSpread<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
options: ObjectRestSpreadOptions,
}

impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
pub fn new(options: ObjectRestSpreadOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { options, ctx }
Self { ctx, options }
}
}
impl<'a, 'ctx> Traverse<'a> for ObjectSpread<'a, 'ctx> {

impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.transform_object_expression(expr, ctx);
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
fn transform_object_expression(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::ObjectExpression(obj_expr) = expr else {
return;
};
Expand Down Expand Up @@ -97,9 +117,7 @@ impl<'a, 'ctx> Traverse<'a> for ObjectSpread<'a, 'ctx> {
*expr = ctx.ast.expression_call(SPAN, callee, NONE, arguments, false);
}
}
}

impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
#[expect(clippy::option_option)]
fn get_object_symbol_id(&self, ctx: &mut TraverseCtx<'a>) -> Option<Option<SymbolId>> {
if self.options.set_spread_properties {
Expand All @@ -118,7 +136,7 @@ impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
if let Some(object_id) = object_id {
Self::object_assign(object_id, ctx)
} else {
self.babel_external_helper(ctx)
self.ctx.helper_load(Helper::ObjectSpread2, ctx)
}
}

Expand All @@ -127,11 +145,6 @@ impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
ctx.create_reference_id(SPAN, Atom::from("Object"), symbol_id, ReferenceFlags::Read);
let object = ctx.ast.expression_from_identifier_reference(ident);
let property = ctx.ast.identifier_name(SPAN, Atom::from("assign"));

Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

fn babel_external_helper(&self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
self.ctx.helper_load(Helper::ObjectSpread2, ctx)
}
}
73 changes: 0 additions & 73 deletions crates/oxc_transformer/src/es2018/object_rest_spread/mod.rs

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/options/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ impl TransformOptions {
arrow_function: None,
},
es2016: ES2016Options { exponentiation_operator: true },
es2018: ES2018Options { object_rest_spread: Some(ObjectRestSpreadOptions::default()) },
es2017: ES2017Options {
// Turned off because it is not ready.
async_to_generator: false,
},
es2018: ES2018Options { object_rest_spread: Some(ObjectRestSpreadOptions::default()) },
es2019: ES2019Options { optional_catch_binding: true },
es2020: ES2020Options { nullish_coalescing_operator: true },
es2021: ES2021Options { logical_assignment_operators: true },
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_traverse/src/context/bound_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::TraverseCtx;
/// * `BoundIdentifier` is `Clone` (unlike `BindingIdentifier`).
/// * `BoundIdentifier` re-uses the same `Atom` for all `BindingIdentifier` / `IdentifierReference`s
/// created from it.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct BoundIdentifier<'a> {
pub name: Atom<'a>,
pub symbol_id: SymbolId,
Expand Down