Skip to content

Commit

Permalink
fix: handle __proto__
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Oct 6, 2023
1 parent b6ac8ca commit 9839544
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
33 changes: 26 additions & 7 deletions crates/oxc_transformer/src/es2015/shorthand_properties.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
use oxc_ast::ast::*;
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;
pub struct ShorthandProperties<'a> {
ast: Rc<AstBuilder<'a>>,
}

impl<'a> ShorthandProperties {
pub fn new() -> Self {
Self
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) {
obj_prop.shorthand = false;
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);
}
}
}
}
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Transformer<'a> {
// es2016
es2016_exponentiation_operator: Option<ExponentiationOperator<'a>>,
// es2015
es2015_shorthand_properties: Option<ShorthandProperties>,
es2015_shorthand_properties: Option<ShorthandProperties<'a>>,
}

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

0 comments on commit 9839544

Please sign in to comment.