Skip to content

Commit

Permalink
feat(ecma): implement array join method
Browse files Browse the repository at this point in the history
  • Loading branch information
7086cmd committed Oct 27, 2024
1 parent 1e2f012 commit ea46486
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/oxc_ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ oxc_syntax = { workspace = true, features = ["to_js_string"] }
num-bigint = { workspace = true }
num-traits = { workspace = true }

[dev-dependencies]
oxc_allocator = { workspace = true }

[features]
default = []
side_effects = []
Expand Down
61 changes: 61 additions & 0 deletions crates/oxc_ecmascript/src/array_join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::ToJsString;
use oxc_ast::ast::*;

pub trait ArrayJoin<'a> {
fn array_join(&self, separator: Option<&str>) -> Option<String>;
}

impl<'a> ArrayJoin<'a> for ArrayExpression<'a> {
fn array_join(&self, separator: Option<&str>) -> Option<String> {
let strings = self.elements.iter().map(|x| x.to_js_string()).collect::<Option<Vec<_>>>();
strings
.map(|v| v.iter().map(|s| s.as_ref()).collect::<Vec<_>>().join(separator.unwrap_or("")))
}
}

#[cfg(test)]
mod tests {
use super::*;
use oxc_allocator::{Allocator, CloneIn};
use oxc_ast::AstBuilder;
use oxc_span::SPAN;

#[test]
fn test() {
let allocator = Allocator::default();
let ast = AstBuilder::new(&allocator);
let mut elements = ast.vec();
elements.push(ast.array_expression_element_elision(SPAN));
elements.push(ast.array_expression_element_expression(ast.expression_null_literal(SPAN)));
elements.push(ast.array_expression_element_expression(ast.expression_numeric_literal(
SPAN,
42f64,
"42",
NumberBase::Decimal,
)));
elements.push(
ast.array_expression_element_expression(ast.expression_string_literal(SPAN, "foo")),
);
elements.push(
ast.array_expression_element_expression(ast.expression_boolean_literal(SPAN, true)),
);
elements.push(ast.array_expression_element_expression(ast.expression_big_int_literal(
SPAN,
"42n",
BigintBase::Decimal,
)));
let array = ast.array_expression(SPAN, elements.clone_in(&allocator), None);
let mut array2 = array.clone_in(&allocator);
array2
.elements
.push(ast.array_expression_element_expression(ast.expression_from_array(array)));
array2.elements.push(ast.array_expression_element_expression(ast.expression_object(
SPAN,
ast.vec(),
None,
)));
let joined = array2.array_join(Some(","));
println!("{:?}", joined);
assert_eq!(joined, Some(",,42,foo,true,42n,,,42,foo,true,42n,[object Object]".to_string()));
}
}
4 changes: 2 additions & 2 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod is_litral_value;
mod is_literal_value;
mod value;
mod value_type;

Expand All @@ -11,7 +11,7 @@ use oxc_ast::ast::*;

use crate::{side_effects::MayHaveSideEffects, ToBigInt, ToBoolean, ToInt32, ToJsString, ToNumber};

pub use self::{is_litral_value::IsLiteralValue, value::ConstantValue, value_type::ValueType};
pub use self::{is_literal_value::IsLiteralValue, value::ConstantValue, value_type::ValueType};

pub trait ConstantEvaluation<'a> {
fn is_global_reference(&self, ident: &IdentifierReference<'a>) -> bool {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod constant_evaluation;

#[cfg(feature = "side_effects")]
pub mod side_effects;
mod array_join;

pub use self::{
bound_names::BoundNames, is_simple_parameter_list::IsSimpleParameterList,
Expand Down
22 changes: 19 additions & 3 deletions crates/oxc_ecmascript/src/to_string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::borrow::Cow;

use crate::array_join::ArrayJoin;
use crate::ToBoolean;
use oxc_ast::ast::*;
use oxc_syntax::operator::UnaryOperator;

use crate::ToBoolean;

/// `ToString`
///
/// <https://tc39.es/ecma262/#sec-tostring>
Expand All @@ -30,6 +30,22 @@ impl<'a> ToJsString<'a> for Expression<'a> {
}
}

impl<'a> ToJsString<'a> for ArrayExpressionElement<'a> {
fn to_js_string(&self) -> Option<Cow<'a, str>> {
match self {
ArrayExpressionElement::Elision(_) => Some(Cow::Borrowed("")),
ArrayExpressionElement::SpreadElement(_) => None,
ArrayExpressionElement::NullLiteral(_) => Some(Cow::Borrowed("")),
ArrayExpressionElement::Identifier(id) if id.name.as_str() == "undefined" => {
Some(Cow::Borrowed(""))
}
expr @ match_expression!(ArrayExpressionElement) => {
expr.as_expression().map(|e| e.to_js_string()).flatten()
}
}
}
}

impl<'a> ToJsString<'a> for StringLiteral<'a> {
fn to_js_string(&self) -> Option<Cow<'a, str>> {
Some(Cow::Borrowed(self.value.as_str()))
Expand Down Expand Up @@ -101,7 +117,7 @@ impl<'a> ToJsString<'a> for UnaryExpression<'a> {
impl<'a> ToJsString<'a> for ArrayExpression<'a> {
fn to_js_string(&self) -> Option<Cow<'a, str>> {
// TODO: https://github.com/google/closure-compiler/blob/e13f5cd0a5d3d35f2db1e6c03fdf67ef02946009/src/com/google/javascript/jscomp/NodeUtil.java#L302-L303
None
self.array_join(Some(",")).map(Cow::Owned)
}
}

Expand Down

0 comments on commit ea46486

Please sign in to comment.