Skip to main content

stylex_transform/shared/utils/macros/
evaluation.rs

1//! Evaluation macros for consistent error handling across the workspace.
2//!
3//! These macros provide standardized patterns for handling common error cases
4//! when working with expressions, conversions, and evaluations.
5
6/// Panic with a `[StyleX]`-prefixed code frame error.
7///
8/// Wraps the expression in a `ParenExpr` and delegates to
9/// `build_code_frame_error_and_panic` which produces a source-located
10/// `[StyleX] <message>` diagnostic on stderr before panicking.
11///
12/// # Usage
13/// ```ignore
14/// stylex_panic_with_context!(path, traversal_state, "Unary expression not implemented");
15/// ```
16///
17/// # Arguments
18/// - `$expr`: The expression to wrap and report
19/// - `$state`: State manager for error context
20/// - `$msg`: Error message string
21#[macro_export]
22macro_rules! stylex_panic_with_context {
23  ($expr:expr, $state:expr, $msg:expr) => {{
24    let paren_expr = stylex_ast::ast::factories::wrap_in_paren_ref($expr);
25    $crate::shared::utils::log::build_code_frame_error::build_code_frame_error_and_panic(
26      &paren_expr,
27      $expr,
28      $msg,
29      $state,
30    )
31  }};
32}
33
34/// Macro to safely convert an expression to a string with proper error handling.
35/// Returns the string on success, or calls deopt and returns None on failure.
36///
37/// This macro is designed for use in evaluation contexts where we need to:
38/// - Convert an expression to a string
39/// - Call deopt() if conversion fails
40/// - Return None to indicate failure
41///
42/// # Usage
43/// ```ignore
44/// let str_value = expr_to_str_or_deopt!(expr, state, traversal_state, fns, "Expression is not a string");
45/// ```
46///
47/// # Arguments
48/// - `$expr`: The expression to convert
49/// - `$state`: Mutable reference to EvaluationState
50/// - `$traversal_state`: Mutable reference to StateManager
51/// - `$fns`: Reference to FunctionMap
52/// - `$error_msg`: Error message string literal
53#[macro_export]
54macro_rules! expr_to_str_or_deopt {
55  ($expr:expr, $state:expr, $traversal_state:expr, $fns:expr, $error_msg:expr) => {
56    match $crate::shared::utils::ast::convertors::convert_expr_to_str($expr, $traversal_state, $fns)
57    {
58      Some(s) => s,
59      None => {
60        $crate::shared::utils::js::evaluate::deopt($expr, $state, $error_msg);
61        return None;
62      },
63    }
64  };
65}