Skip to content

Commit

Permalink
feat(transformer): Shorthand Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Oct 6, 2023
1 parent 5045853 commit ce86150
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod shorthand_properties;

pub use shorthand_properties::ShorthandProperties;
37 changes: 37 additions & 0 deletions crates/oxc_transformer/src/es2015/shorthand_properties.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use oxc_ast::{ast::*, AstBuilder};
use oxc_span::GetSpan;

use std::rc::Rc;

/// ES2015: Shorthand Properties
///
/// References:
/// * <https://babel.dev/docs/babel-plugin-transform-shorthand-properties>
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-shorthand-properties>
pub struct ShorthandProperties<'a> {
ast: Rc<AstBuilder<'a>>,
}

impl<'a> ShorthandProperties<'a> {
pub fn new(ast: Rc<AstBuilder<'a>>) -> Self {
Self { ast }
}

pub fn transform_object_property<'b>(&mut self, obj_prop: &'b mut ObjectProperty<'a>) {
if obj_prop.shorthand {
obj_prop.shorthand = false;

if obj_prop.key.is_specific_id("__proto__") {
debug_assert!(
!obj_prop.computed,
"shorthand and computed cannot be true at the same time"
);
obj_prop.computed = true;

let proto = StringLiteral { span: obj_prop.key.span(), value: "__proto__".into() };
let expr = self.ast.literal_string_expression(proto);
obj_prop.key = PropertyKey::Expression(expr);
}
}
}
}
11 changes: 11 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! * <https://babel.dev/docs/presets>
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>
mod es2015;
mod es2016;
mod es2019;
mod es2021;
Expand All @@ -19,6 +20,7 @@ use oxc_ast::{ast::*, AstBuilder, VisitMut};
use oxc_span::SourceType;
use std::rc::Rc;

use es2015::ShorthandProperties;
use es2016::ExponentiationOperator;
use es2019::OptionalCatchBinding;
use es2021::LogicalAssignmentOperators;
Expand All @@ -39,6 +41,8 @@ pub struct Transformer<'a> {
es2019_optional_catch_binding: Option<OptionalCatchBinding<'a>>,
// es2016
es2016_exponentiation_operator: Option<ExponentiationOperator<'a>>,
// es2015
es2015_shorthand_properties: Option<ShorthandProperties<'a>>,
}

impl<'a> Transformer<'a> {
Expand Down Expand Up @@ -66,6 +70,9 @@ impl<'a> Transformer<'a> {
if options.target < TransformTarget::ES2016 {
t.es2016_exponentiation_operator.replace(ExponentiationOperator::new(Rc::clone(&ast)));
}
if options.target < TransformTarget::ES2015 {
t.es2015_shorthand_properties.replace(ShorthandProperties::new(Rc::clone(&ast)));
}
t
}

Expand All @@ -92,4 +99,8 @@ impl<'a, 'b> VisitMut<'a, 'b> for Transformer<'a> {
}
self.visit_statements(&mut clause.body.body);
}

fn visit_object_property(&mut self, prop: &'b mut ObjectProperty<'a>) {
self.es2015_shorthand_properties.as_mut().map(|t| t.transform_object_property(prop));
}
}

0 comments on commit ce86150

Please sign in to comment.