Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ecmascript): move tests to oxc_minifier due to cyclic dependency with oxc_parser #7542

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

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

5 changes: 0 additions & 5 deletions crates/oxc_ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ecmascript/README.md
Original file line number Diff line number Diff line change
@@ -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`.
49 changes: 0 additions & 49 deletions crates/oxc_ecmascript/src/array_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,3 @@ impl<'a> ArrayJoin<'a> for ArrayExpression<'a> {
.map(|v| v.iter().map(AsRef::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(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())
);
}
}
44 changes: 0 additions & 44 deletions crates/oxc_ecmascript/src/prop_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
41 changes: 41 additions & 0 deletions crates/oxc_minifier/tests/ecmascript/array_join.rs
Original file line number Diff line number Diff line change
@@ -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()));
}
2 changes: 2 additions & 0 deletions crates/oxc_minifier/tests/ecmascript/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod array_join;
mod prop_name;
39 changes: 39 additions & 0 deletions crates/oxc_minifier/tests/ecmascript/prop_name.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod ast_passes;
mod ecmascript;
mod mangler;

use oxc_allocator::Allocator;
Expand Down
Loading