-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): Shorthand Properties
- Loading branch information
1 parent
5045853
commit ce86150
Showing
3 changed files
with
51 additions
and
0 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,3 @@ | ||
mod shorthand_properties; | ||
|
||
pub use shorthand_properties::ShorthandProperties; |
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,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); | ||
} | ||
} | ||
} | ||
} |
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