Expand description
Compile-time validated JavaScript macro for Viewpoint.
This crate provides a js! macro that validates JavaScript syntax at compile time,
similar to how serde_json::json! validates JSON. This catches JavaScript syntax
errors early, before they reach the browser.
§Features
- Compile-time validation: JavaScript syntax errors are caught during compilation
- Value interpolation: Embed Rust expressions using
#{expr}syntax (quoted/escaped) - Raw interpolation: Inject pre-built JavaScript using
@{expr}syntax (unquoted) - Zero runtime overhead: Static strings when no interpolation is used
- Clear error messages: Points to the exact location of syntax errors
§Usage
use viewpoint_js::js;
use viewpoint_js_core::ToJsValue; // Needed for value interpolation
// Simple expression - produces &'static str
let code = js!{ 1 + 2 };
// Arrow function
let code = js!{ () => window.innerWidth };
// With value interpolation (requires ToJsValue in scope)
let selector = ".my-class";
let code = js!{ document.querySelector(#{selector}) };
// With raw interpolation (inject JS expression as-is)
let selector_expr = "document.querySelectorAll('.item')";
let code = js!{ Array.from(@{selector_expr}) };
// Multi-line function
let code = js!{
(() => {
const items = document.querySelectorAll("li");
return items.length;
})()
};§Value Interpolation (#{expr})
Use #{expr} to embed Rust values into JavaScript. Values are automatically
converted to JavaScript representations via the ToJsValue trait:
- Strings are quoted and escaped
- Numbers are inserted as-is
- Booleans become
trueorfalse Option::Nonebecomesnull
§Raw Interpolation (@{expr})
Use @{expr} to inject pre-built JavaScript expressions directly without
quoting or escaping. The expression must return something that implements
AsRef<str>. This is useful for:
- Injecting dynamically-built selector expressions
- Composing JavaScript from multiple parts
- Including pre-validated JavaScript fragments
Macros§
- js
- A macro that validates JavaScript syntax at compile time.