mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
test(ecmascript): move tests to oxc_minifier due to cyclic dependency with oxc_parser (#7542)
This commit is contained in:
parent
2c57fd57ce
commit
9d6e14bb67
9 changed files with 88 additions and 100 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -1627,9 +1627,7 @@ version = "0.38.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"oxc_allocator",
|
|
||||||
"oxc_ast",
|
"oxc_ast",
|
||||||
"oxc_parser",
|
|
||||||
"oxc_span",
|
"oxc_span",
|
||||||
"oxc_syntax",
|
"oxc_syntax",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -28,11 +28,6 @@ oxc_syntax = { workspace = true, features = ["to_js_string"] }
|
||||||
num-bigint = { workspace = true }
|
num-bigint = { workspace = true }
|
||||||
num-traits = { 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]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
side_effects = []
|
side_effects = []
|
||||||
|
|
|
||||||
5
crates/oxc_ecmascript/README.md
Normal file
5
crates/oxc_ecmascript/README.md
Normal file
|
|
@ -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`.
|
||||||
|
|
@ -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(",")))
|
.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())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -65,47 +65,3 @@ impl PropName for PropertyDefinition<'_> {
|
||||||
self.key.prop_name()
|
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
crates/oxc_minifier/tests/ecmascript/array_join.rs
Normal file
41
crates/oxc_minifier/tests/ecmascript/array_join.rs
Normal file
|
|
@ -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
crates/oxc_minifier/tests/ecmascript/mod.rs
Normal file
2
crates/oxc_minifier/tests/ecmascript/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
mod array_join;
|
||||||
|
mod prop_name;
|
||||||
39
crates/oxc_minifier/tests/ecmascript/prop_name.rs
Normal file
39
crates/oxc_minifier/tests/ecmascript/prop_name.rs
Normal file
|
|
@ -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,4 +1,5 @@
|
||||||
mod ast_passes;
|
mod ast_passes;
|
||||||
|
mod ecmascript;
|
||||||
mod mangler;
|
mod mangler;
|
||||||
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue