diff --git a/Cargo.lock b/Cargo.lock index 0a8690882dea7..72ccf81b36cf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1627,9 +1627,7 @@ version = "0.38.0" dependencies = [ "num-bigint", "num-traits", - "oxc_allocator", "oxc_ast", - "oxc_parser", "oxc_span", "oxc_syntax", ] diff --git a/crates/oxc_ecmascript/Cargo.toml b/crates/oxc_ecmascript/Cargo.toml index ca2b7087c7c56..65e59de4bcfc4 100644 --- a/crates/oxc_ecmascript/Cargo.toml +++ b/crates/oxc_ecmascript/Cargo.toml @@ -28,11 +28,6 @@ oxc_syntax = { workspace = true, features = ["to_js_string"] } num-bigint = { workspace = true } num-traits = { workspace = true } -[dev-dependencies] -# Parser and allocator are only used in tests to make testing easier -oxc_allocator = { workspace = true } -oxc_parser = { workspace = true } - [features] default = [] side_effects = [] diff --git a/crates/oxc_ecmascript/README.md b/crates/oxc_ecmascript/README.md new file mode 100644 index 0000000000000..77648375a5017 --- /dev/null +++ b/crates/oxc_ecmascript/README.md @@ -0,0 +1,5 @@ +# `oxc_ecmascript` + +ECMAScript Operations defined in the spec https://tc39.es/ecma262/ + +Tests reside in `crates/oxc_minifier/tests/ecmascript` due to cyclic dependency with `oxc_parser`. diff --git a/crates/oxc_ecmascript/src/array_join.rs b/crates/oxc_ecmascript/src/array_join.rs index 90035877e07ff..d2b42476f09ac 100644 --- a/crates/oxc_ecmascript/src/array_join.rs +++ b/crates/oxc_ecmascript/src/array_join.rs @@ -15,52 +15,3 @@ impl<'a> ArrayJoin<'a> for ArrayExpression<'a> { .map(|v| v.iter().map(AsRef::as_ref).collect::>().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(ArrayExpressionElement::NullLiteral(ast.alloc(ast.null_literal(SPAN)))); - elements.push(ArrayExpressionElement::NumericLiteral(ast.alloc(ast.numeric_literal( - SPAN, - 42f64, - "42", - NumberBase::Decimal, - )))); - elements.push(ArrayExpressionElement::StringLiteral( - ast.alloc(ast.string_literal(SPAN, "foo", None)), - )); - elements.push(ArrayExpressionElement::BooleanLiteral( - ast.alloc(ast.boolean_literal(SPAN, true)), - )); - elements.push(ArrayExpressionElement::BigIntLiteral(ast.alloc(ast.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(ArrayExpressionElement::ArrayExpression(ast.alloc(array))); - array2.elements.push(ArrayExpressionElement::ObjectExpression( - ast.alloc(ast.object_expression(SPAN, ast.vec(), None)), - )); - let joined = array2.array_join(Some("_")); - assert_eq!(joined, Some("__42_foo_true_42n_,,42,foo,true,42n_[object Object]".to_string())); - - let joined2 = array2.array_join(None); - // By default, in `Array.prototype.toString`, the separator is a comma. However, in `Array.prototype.join`, the separator is none if not given. - assert_eq!( - joined2, - Some(",,42,foo,true,42n,,,42,foo,true,42n,[object Object]".to_string()) - ); - } -} diff --git a/crates/oxc_ecmascript/src/prop_name.rs b/crates/oxc_ecmascript/src/prop_name.rs index 6d84a39c2efce..9dd8a620331ad 100644 --- a/crates/oxc_ecmascript/src/prop_name.rs +++ b/crates/oxc_ecmascript/src/prop_name.rs @@ -65,47 +65,3 @@ impl PropName for PropertyDefinition<'_> { self.key.prop_name() } } - -#[cfg(test)] -mod test { - use oxc_allocator::Allocator; - use oxc_ast::{ast::ObjectExpression, Visit}; - use oxc_parser::Parser; - use oxc_span::SourceType; - - use crate::PropName; - - #[test] - fn test_prop_name() { - #[derive(Debug, Default)] - struct TestVisitor; - - impl<'a> Visit<'a> for TestVisitor { - fn visit_object_expression(&mut self, obj_expr: &ObjectExpression<'a>) { - assert_eq!("a", obj_expr.properties[0].prop_name().unwrap().0); - assert_eq!("b", obj_expr.properties[1].prop_name().unwrap().0); - assert_eq!("c", obj_expr.properties[2].prop_name().unwrap().0); - assert_eq!("d", obj_expr.properties[3].prop_name().unwrap().0); - assert_eq!(None, obj_expr.properties[4].prop_name()); - } - } - - let allocator = Allocator::default(); - let source_type = SourceType::default(); - let source = r" - const obj = { - a() {}, - get b() {}, - set c(_) {}, - d: 1, - [e]() {}, - } - "; - let ret = Parser::new(&allocator, source, source_type).parse(); - assert!(!ret.program.is_empty()); - assert!(ret.errors.is_empty()); - - let mut visitor = TestVisitor; - visitor.visit_program(&ret.program); - } -} diff --git a/crates/oxc_minifier/tests/ecmascript/array_join.rs b/crates/oxc_minifier/tests/ecmascript/array_join.rs new file mode 100644 index 0000000000000..4f42f351f6d3e --- /dev/null +++ b/crates/oxc_minifier/tests/ecmascript/array_join.rs @@ -0,0 +1,41 @@ +use oxc_allocator::{Allocator, CloneIn}; +use oxc_ast::{ast::*, AstBuilder}; +use oxc_ecmascript::ArrayJoin; +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(ArrayExpressionElement::NullLiteral(ast.alloc(ast.null_literal(SPAN)))); + elements.push(ArrayExpressionElement::NumericLiteral(ast.alloc(ast.numeric_literal( + SPAN, + 42f64, + "42", + NumberBase::Decimal, + )))); + elements.push(ArrayExpressionElement::StringLiteral( + ast.alloc(ast.string_literal(SPAN, "foo", None)), + )); + elements + .push(ArrayExpressionElement::BooleanLiteral(ast.alloc(ast.boolean_literal(SPAN, true)))); + elements.push(ArrayExpressionElement::BigIntLiteral(ast.alloc(ast.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(ArrayExpressionElement::ArrayExpression(ast.alloc(array))); + array2.elements.push(ArrayExpressionElement::ObjectExpression( + ast.alloc(ast.object_expression(SPAN, ast.vec(), None)), + )); + let joined = array2.array_join(Some("_")); + assert_eq!(joined, Some("__42_foo_true_42n_,,42,foo,true,42n_[object Object]".to_string())); + + let joined2 = array2.array_join(None); + // By default, in `Array.prototype.toString`, the separator is a comma. However, in `Array.prototype.join`, the separator is none if not given. + assert_eq!(joined2, Some(",,42,foo,true,42n,,,42,foo,true,42n,[object Object]".to_string())); +} diff --git a/crates/oxc_minifier/tests/ecmascript/mod.rs b/crates/oxc_minifier/tests/ecmascript/mod.rs new file mode 100644 index 0000000000000..fd7746917462e --- /dev/null +++ b/crates/oxc_minifier/tests/ecmascript/mod.rs @@ -0,0 +1,2 @@ +mod array_join; +mod prop_name; diff --git a/crates/oxc_minifier/tests/ecmascript/prop_name.rs b/crates/oxc_minifier/tests/ecmascript/prop_name.rs new file mode 100644 index 0000000000000..f08caa1399a98 --- /dev/null +++ b/crates/oxc_minifier/tests/ecmascript/prop_name.rs @@ -0,0 +1,39 @@ +use oxc_allocator::Allocator; +use oxc_ast::{ast::ObjectExpression, Visit}; +use oxc_ecmascript::PropName; +use oxc_parser::Parser; +use oxc_span::SourceType; + +#[test] +fn test_prop_name() { + #[derive(Debug, Default)] + struct TestVisitor; + + impl<'a> Visit<'a> for TestVisitor { + fn visit_object_expression(&mut self, obj_expr: &ObjectExpression<'a>) { + assert_eq!("a", obj_expr.properties[0].prop_name().unwrap().0); + assert_eq!("b", obj_expr.properties[1].prop_name().unwrap().0); + assert_eq!("c", obj_expr.properties[2].prop_name().unwrap().0); + assert_eq!("d", obj_expr.properties[3].prop_name().unwrap().0); + assert_eq!(None, obj_expr.properties[4].prop_name()); + } + } + + let allocator = Allocator::default(); + let source_type = SourceType::default(); + let source = r" + const obj = { + a() {}, + get b() {}, + set c(_) {}, + d: 1, + [e]() {}, + } + "; + let ret = Parser::new(&allocator, source, source_type).parse(); + assert!(!ret.program.is_empty()); + assert!(ret.errors.is_empty()); + + let mut visitor = TestVisitor; + visitor.visit_program(&ret.program); +} diff --git a/crates/oxc_minifier/tests/mod.rs b/crates/oxc_minifier/tests/mod.rs index 3fe9dc3ae5519..accb6a370f575 100644 --- a/crates/oxc_minifier/tests/mod.rs +++ b/crates/oxc_minifier/tests/mod.rs @@ -1,4 +1,5 @@ mod ast_passes; +mod ecmascript; mod mangler; use oxc_allocator::Allocator;