Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
use std::collections::HashMap; | ||
|
||
use swc_atoms::JsWord; | ||
use swc_common::DUMMY_SP; | ||
use swc_ecma_visit::swc_ecma_ast::{ | ||
Expr, KeyValueProp, Lit, ObjectLit, Prop, PropName, PropOrSpread, Str, | ||
}; | ||
|
||
enum KeyStrategy { | ||
Merge, | ||
Override, | ||
} | ||
|
||
/** | ||
* Inserts all the keys / values from b into a. | ||
* On clash: | ||
* - if both values are objects, merge | ||
* - otherwise, overwrite with b | ||
* | ||
* todo(arlyon): this could be slow for many keys | ||
*/ | ||
pub fn merge_literals(mut a: ObjectLit, b: ObjectLit) -> ObjectLit { | ||
let mut strategies: HashMap<JsWord, (usize, KeyStrategy)> = Default::default(); | ||
|
||
for (idx, prop) in a.props.iter().enumerate() { | ||
if let PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
key: PropName::Str(Str { value: name, .. }), | ||
value: box value, | ||
})) = prop | ||
{ | ||
strategies.insert( | ||
name.to_owned(), | ||
( | ||
idx, | ||
match value { | ||
Expr::Object(_) => KeyStrategy::Merge, | ||
_ => KeyStrategy::Override, | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
for prop in b.props { | ||
if let PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
key: PropName::Str(Str { value: name, .. }), | ||
.. | ||
})) = &prop | ||
{ | ||
match strategies.get(name) { | ||
Some((idx, KeyStrategy::Merge)) => { | ||
if let ( | ||
Some(PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
value: box Expr::Object(lit1), | ||
.. | ||
}))), | ||
PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
value: box Expr::Object(lit2), | ||
.. | ||
})), | ||
) = (a.props.get_mut(*idx), prop) | ||
{ | ||
let temp = std::mem::replace(lit1, to_lit(&[])); | ||
std::mem::replace(lit1, merge_literals(temp, lit2)); | ||
} | ||
} | ||
Some((idx, KeyStrategy::Override)) => a.props.insert(*idx, prop), | ||
_ => a.props.push(prop), | ||
} | ||
} | ||
} | ||
|
||
a | ||
} | ||
|
||
pub fn to_lit(items: &[(&str, &str)]) -> ObjectLit { | ||
ObjectLit { | ||
span: DUMMY_SP, | ||
props: items | ||
.iter() | ||
.map(|(key, value)| { | ||
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { | ||
key: PropName::Str(Str { | ||
span: DUMMY_SP, | ||
raw: None, | ||
value: (*key).into(), | ||
}), | ||
value: Box::new(Expr::Lit(Lit::Str(Str { | ||
span: DUMMY_SP, | ||
raw: None, | ||
value: (*value).into(), | ||
}))), | ||
}))) | ||
}) | ||
.collect(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use swc_common::DUMMY_SP; | ||
use swc_ecma_visit::swc_ecma_ast::{ | ||
Expr, KeyValueProp, Lit, ObjectLit, Prop, PropName, PropOrSpread, Str, | ||
}; | ||
|
||
use crate::util::{merge_literals, to_lit}; | ||
|
||
#[test] | ||
fn a_and_b_merges() { | ||
let a = to_lit(&[("a", "value")]); | ||
let b = to_lit(&[("b", "value")]); | ||
let c = merge_literals(a, b); | ||
assert!(c.props.len() == 2); | ||
} | ||
|
||
#[test] | ||
fn empty_b_does_nothing() { | ||
let a = ObjectLit { | ||
span: DUMMY_SP, | ||
props: vec![], | ||
}; | ||
|
||
let b = ObjectLit { | ||
span: DUMMY_SP, | ||
props: vec![], | ||
}; | ||
|
||
let c = merge_literals(a.clone(), b); | ||
|
||
assert!(a.eq(&c)); | ||
} | ||
|
||
#[test] | ||
fn override_b_replaces() { | ||
let a = to_lit(&[("replace", "a")]); | ||
let b = to_lit(&[("replace", "b")]); | ||
|
||
let c = merge_literals(a.clone(), b); | ||
|
||
if let PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
value: box Expr::Lit(Lit::Str(Str { value, .. })), | ||
.. | ||
})) = &c.props[0] | ||
{ | ||
assert!("b".eq(&*value)) | ||
} else { | ||
panic!("fail") | ||
} | ||
} | ||
|
||
#[test] | ||
fn mergeable_b_merges() { | ||
let a = ObjectLit { | ||
span: DUMMY_SP, | ||
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { | ||
value: Box::new(Expr::Object(to_lit(&[("a", "value")]))), | ||
key: PropName::Str(Str { | ||
raw: None, | ||
span: DUMMY_SP, | ||
value: "merge".into(), | ||
}), | ||
})))], | ||
}; | ||
let b = ObjectLit { | ||
span: DUMMY_SP, | ||
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { | ||
value: Box::new(Expr::Object(to_lit(&[("b", "value")]))), | ||
key: PropName::Str(Str { | ||
raw: None, | ||
span: DUMMY_SP, | ||
value: "merge".into(), | ||
}), | ||
})))], | ||
}; | ||
|
||
let c = merge_literals(a.clone(), b); | ||
|
||
if let PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp { | ||
value: box Expr::Object(ObjectLit { props, .. }), | ||
.. | ||
})) = &c.props[0] | ||
{ | ||
assert!(props.len() == 2) | ||
} else { | ||
panic!("fail") | ||
} | ||
} | ||
} |