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(transformer/jsx): move tests setup into a macro #7618

Merged
Merged
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
54 changes: 32 additions & 22 deletions crates/oxc_transformer/src/jsx/jsx_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,26 +1073,38 @@ mod test {
use std::path::Path;

use oxc_allocator::Allocator;
use oxc_ast::{ast::Expression, AstBuilder};
use oxc_ast::ast::Expression;
use oxc_semantic::{ScopeTree, SymbolTable};
use oxc_traverse::ReusableTraverseCtx;

use crate::{context::TransformCtx, TransformOptions};
use crate::{TransformCtx, TransformOptions};

use super::Pragma;

macro_rules! setup {
($traverse_ctx:ident, $transform_ctx:ident) => {
let allocator = Allocator::default();
let traverse_ctx =
ReusableTraverseCtx::new(ScopeTree::default(), SymbolTable::default(), &allocator);
// SAFETY: Macro user only gets a `&mut TraverseCtx`, which cannot be abused
let mut traverse_ctx = unsafe { traverse_ctx.unwrap() };
let $traverse_ctx = &mut traverse_ctx;
let $transform_ctx =
TransformCtx::new(Path::new("test.jsx"), &TransformOptions::default());
};
}

#[test]
fn this_expr_pragma() {
let alloc = Allocator::default();
let ast = AstBuilder::new(&alloc);
let ctx = TransformCtx::new(Path::new("test.jsx"), &TransformOptions::default());
// SAFETY: constructed to avoid unsoundness
let mut traverse_ctx = unsafe {
ReusableTraverseCtx::new(ScopeTree::default(), SymbolTable::default(), &alloc).unwrap()
};
setup!(traverse_ctx, transform_ctx);

let pragma = Pragma::parse(Some(&"this.a.b".to_string()), "createElement", ast, &ctx);
let expr = pragma.create_expression(&mut traverse_ctx);
let pragma = Pragma::parse(
Some(&"this.a.b".to_string()),
"createElement",
traverse_ctx.ast,
&transform_ctx,
);
let expr = pragma.create_expression(traverse_ctx);

let Expression::StaticMemberExpression(outer_member) = &expr else { panic!() };
let Expression::StaticMemberExpression(inner_member) = &outer_member.object else {
Expand All @@ -1105,17 +1117,15 @@ mod test {

#[test]
fn import_meta_pragma() {
let alloc = Allocator::default();
let ast = AstBuilder::new(&alloc);
let ctx = TransformCtx::new(Path::new("test.jsx"), &TransformOptions::default());
// SAFETY: constructed to avoid unsoundness
let mut traverse_ctx = unsafe {
ReusableTraverseCtx::new(ScopeTree::default(), SymbolTable::default(), &alloc).unwrap()
};

let pragma =
Pragma::parse(Some(&"import.meta.prop".to_string()), "createElement", ast, &ctx);
let expr = pragma.create_expression(&mut traverse_ctx);
setup!(traverse_ctx, transform_ctx);

let pragma = Pragma::parse(
Some(&"import.meta.prop".to_string()),
"createElement",
traverse_ctx.ast,
&transform_ctx,
);
let expr = pragma.create_expression(traverse_ctx);

let Expression::StaticMemberExpression(member) = &expr else { panic!() };
let Expression::MetaProperty(meta_prop) = &member.object else { panic!() };
Expand Down
Loading