-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer): duplicate keys (#1649)
- Loading branch information
Showing
7 changed files
with
86 additions
and
1 deletion.
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,74 @@ | ||
#![allow(clippy::similar_names)] | ||
|
||
use std::{collections::HashSet, rc::Rc}; | ||
|
||
use oxc_ast::{ast::*, AstBuilder}; | ||
use oxc_span::{Atom, SPAN}; | ||
|
||
use crate::options::{TransformOptions, TransformTarget}; | ||
|
||
/// ES2015: Duplicate Keys | ||
/// | ||
/// References: | ||
/// * <https://babeljs.io/docs/babel-plugin-transform-duplicate-keys> | ||
/// * <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-duplicate-keys> | ||
pub struct DuplicateKeys<'a> { | ||
ast: Rc<AstBuilder<'a>>, | ||
} | ||
|
||
impl<'a> DuplicateKeys<'a> { | ||
pub fn new(ast: Rc<AstBuilder<'a>>, options: &TransformOptions) -> Option<Self> { | ||
(options.target < TransformTarget::ES2015 || options.duplicate_keys).then(|| Self { ast }) | ||
} | ||
|
||
pub fn transform_object_expression<'b>(&mut self, obj_expr: &'b mut ObjectExpression<'a>) { | ||
let mut visited_data: HashSet<Atom> = HashSet::new(); | ||
let mut visited_getters: HashSet<Atom> = HashSet::new(); | ||
let mut visited_setters: HashSet<Atom> = HashSet::new(); | ||
|
||
for property in obj_expr.properties.iter_mut() { | ||
let ObjectPropertyKind::ObjectProperty(obj_property) = property else { | ||
continue; | ||
}; | ||
|
||
if obj_property.computed { | ||
continue; | ||
} | ||
|
||
if let Some(name) = &obj_property.key.static_name() { | ||
let mut is_duplicate = false; | ||
|
||
match obj_property.kind { | ||
PropertyKind::Get => { | ||
if visited_data.contains(name) || visited_getters.contains(name) { | ||
is_duplicate = true; | ||
} | ||
visited_getters.insert(name.clone()); | ||
} | ||
PropertyKind::Set => { | ||
if visited_data.contains(name) || visited_setters.contains(name) { | ||
is_duplicate = true; | ||
} | ||
visited_setters.insert(name.clone()); | ||
} | ||
PropertyKind::Init => { | ||
if visited_data.contains(name) | ||
|| visited_setters.contains(name) | ||
|| visited_getters.contains(name) | ||
{ | ||
is_duplicate = true; | ||
} | ||
visited_data.insert(name.clone()); | ||
} | ||
} | ||
|
||
if is_duplicate { | ||
obj_property.computed = true; | ||
let string_literal = StringLiteral::new(SPAN, name.as_str().into()); | ||
let expr = self.ast.literal_string_expression(string_literal); | ||
obj_property.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod duplicate_keys; | ||
mod function_name; | ||
mod shorthand_properties; | ||
mod template_literals; | ||
|
||
pub use duplicate_keys::DuplicateKeys; | ||
pub use function_name::FunctionName; | ||
pub use shorthand_properties::ShorthandProperties; | ||
pub use template_literals::TemplateLiterals; |
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
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
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
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
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