use crate::config::E2eConfig;
use crate::escape::{escape_js, sanitize_filename, sanitize_ident};
use crate::field_access::FieldResolver;
use crate::fixture::{Assertion, CallbackAction, Fixture, FixtureGroup};
use alef_core::backend::GeneratedFile;
use alef_core::config::AlefConfig;
use alef_core::hash::{self, CommentStyle};
use anyhow::Result;
use heck::{ToLowerCamelCase, ToUpperCamelCase};
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;
use std::path::PathBuf;
use super::E2eCodegen;
pub struct WasmCodegen;
impl E2eCodegen for WasmCodegen {
fn generate(
&self,
groups: &[FixtureGroup],
e2e_config: &E2eConfig,
alef_config: &AlefConfig,
) -> Result<Vec<GeneratedFile>> {
let lang = self.language_name();
let output_base = PathBuf::from(e2e_config.effective_output()).join(lang);
let tests_base = output_base.join("tests");
let mut files = Vec::new();
let call = &e2e_config.call;
let overrides = call.overrides.get(lang);
let module_path = overrides
.and_then(|o| o.module.as_ref())
.cloned()
.unwrap_or_else(|| call.module.clone());
let function_name = overrides
.and_then(|o| o.function.as_ref())
.cloned()
.unwrap_or_else(|| call.function.clone());
let options_type = overrides.and_then(|o| o.options_type.clone());
let handle_config_type = overrides.and_then(|o| o.handle_config_type.clone());
let client_factory = overrides.and_then(|o| o.client_factory.as_deref());
let empty_enum_fields = HashMap::new();
let enum_fields = overrides.map(|o| &o.enum_fields).unwrap_or(&empty_enum_fields);
let empty_bigint_fields: Vec<String> = Vec::new();
let bigint_fields = overrides.map(|o| &o.bigint_fields).unwrap_or(&empty_bigint_fields);
let result_var = &call.result_var;
let is_async = call.r#async;
let wasm_pkg = e2e_config.resolve_package("wasm");
let pkg_path = wasm_pkg
.as_ref()
.and_then(|p| p.path.as_ref())
.cloned()
.unwrap_or_else(|| format!("../../crates/{}-wasm/pkg", alef_config.crate_config.name));
let pkg_name = wasm_pkg
.as_ref()
.and_then(|p| p.name.as_ref())
.cloned()
.unwrap_or_else(|| module_path.clone());
let pkg_version = wasm_pkg
.as_ref()
.and_then(|p| p.version.as_ref())
.cloned()
.unwrap_or_else(|| "0.1.0".to_string());
files.push(GeneratedFile {
path: output_base.join("package.json"),
content: render_package_json(&pkg_name, &pkg_path, &pkg_version, e2e_config.dep_mode),
generated_header: false,
});
files.push(GeneratedFile {
path: output_base.join("vitest.config.ts"),
content: render_vitest_config(),
generated_header: true,
});
files.push(GeneratedFile {
path: output_base.join("globalSetup.ts"),
content: render_global_setup(),
generated_header: true,
});
files.push(GeneratedFile {
path: output_base.join("tsconfig.json"),
content: render_tsconfig(),
generated_header: false,
});
for group in groups {
let active: Vec<&Fixture> = group
.fixtures
.iter()
.filter(|f| f.skip.as_ref().is_none_or(|s| !s.should_skip(lang)))
.collect();
if active.is_empty() {
continue;
}
let filename = format!("{}.test.ts", sanitize_filename(&group.category));
let field_resolver = FieldResolver::new(
&e2e_config.fields,
&e2e_config.fields_optional,
&e2e_config.result_fields,
&e2e_config.fields_array,
);
let content = render_test_file(
&group.category,
&active,
&pkg_name,
&function_name,
result_var,
is_async,
&e2e_config.call.args,
&field_resolver,
options_type.as_deref(),
enum_fields,
handle_config_type.as_deref(),
client_factory,
bigint_fields,
e2e_config,
);
files.push(GeneratedFile {
path: tests_base.join(filename),
content,
generated_header: true,
});
}
Ok(files)
}
fn language_name(&self) -> &'static str {
"wasm"
}
}
fn render_package_json(
pkg_name: &str,
pkg_path: &str,
pkg_version: &str,
dep_mode: crate::config::DependencyMode,
) -> String {
let dep_value = match dep_mode {
crate::config::DependencyMode::Registry => pkg_version.to_string(),
crate::config::DependencyMode::Local => format!("file:{pkg_path}"),
};
format!(
r#"{{
"name": "{pkg_name}-e2e-wasm",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {{
"test": "vitest run"
}},
"devDependencies": {{
"{pkg_name}": "{dep_value}",
"vite-plugin-top-level-await": "^1.4.0",
"vite-plugin-wasm": "^3.4.0",
"vitest": "^3.0.0"
}}
}}
"#
)
}
fn render_vitest_config() -> String {
let header = hash::header(CommentStyle::DoubleSlash);
format!(
r#"{header}import {{ defineConfig }} from 'vitest/config';
import wasm from 'vite-plugin-wasm';
import topLevelAwait from 'vite-plugin-top-level-await';
export default defineConfig({{
plugins: [wasm(), topLevelAwait()],
test: {{
include: ['tests/**/*.test.ts'],
globalSetup: './globalSetup.ts',
}},
}});
"#
)
}
fn render_global_setup() -> String {
let header = hash::header(CommentStyle::DoubleSlash);
format!(
r#"{header}import {{ spawn }} from 'child_process';
import {{ resolve }} from 'path';
let serverProcess;
export async function setup() {{
// Mock server binary must be pre-built (e.g. by CI or `cargo build --manifest-path e2e/rust/Cargo.toml --bin mock-server --release`)
serverProcess = spawn(
resolve(__dirname, '../rust/target/release/mock-server'),
[resolve(__dirname, '../../fixtures')],
{{ stdio: ['pipe', 'pipe', 'inherit'] }}
);
const url = await new Promise((resolve, reject) => {{
serverProcess.stdout.on('data', (data) => {{
const match = data.toString().match(/MOCK_SERVER_URL=(.*)/);
if (match) resolve(match[1].trim());
}});
setTimeout(() => reject(new Error('Mock server startup timeout')), 30000);
}});
process.env.MOCK_SERVER_URL = url;
}}
export async function teardown() {{
if (serverProcess) {{
serverProcess.stdin.end();
serverProcess.kill();
}}
}}
"#
)
}
fn render_tsconfig() -> String {
r#"{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"strictNullChecks": false,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["tests/**/*.ts", "vitest.config.ts"]
}
"#
.to_string()
}
#[allow(clippy::too_many_arguments)]
fn render_test_file(
category: &str,
fixtures: &[&Fixture],
pkg_name: &str,
function_name: &str,
_result_var: &str,
_is_async: bool,
args: &[crate::config::ArgMapping],
field_resolver: &FieldResolver,
options_type: Option<&str>,
enum_fields: &HashMap<String, String>,
handle_config_type: Option<&str>,
client_factory: Option<&str>,
bigint_fields: &[String],
e2e_config: &E2eConfig,
) -> String {
let mut out = String::new();
out.push_str(&hash::header(CommentStyle::DoubleSlash));
let _ = writeln!(out, "import {{ describe, it, expect }} from 'vitest';");
let needs_options_import = options_type.is_some()
&& fixtures.iter().any(|f| {
args.iter().any(|arg| {
if arg.arg_type != "json_object" {
return false;
}
let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
let val = if field == "input" {
Some(&f.input)
} else {
f.input.get(field)
};
val.is_some_and(|v| !v.is_null())
})
});
let mut enum_imports: std::collections::BTreeSet<&String> = std::collections::BTreeSet::new();
if needs_options_import {
for fixture in fixtures {
for arg in args {
if arg.arg_type == "json_object" {
let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
let val = if field == "input" {
Some(&fixture.input)
} else {
fixture.input.get(field)
};
if let Some(val) = val {
if let Some(obj) = val.as_object() {
for k in obj.keys() {
if let Some(enum_type) = enum_fields.get(k) {
enum_imports.insert(enum_type);
}
}
}
}
}
}
}
}
let handle_constructors: Vec<String> = args
.iter()
.filter(|arg| arg.arg_type == "handle")
.map(|arg| format!("create{}", arg.name.to_upper_camel_case()))
.collect();
{
let mut imports: Vec<String> = if client_factory.is_some() {
vec![]
} else {
vec![function_name.to_string()]
};
for fixture in fixtures {
if fixture.call.is_some() {
let call_config = e2e_config.resolve_call(fixture.call.as_deref());
let fixture_fn = resolve_wasm_function_name(call_config);
if client_factory.is_none() && !imports.contains(&fixture_fn) {
imports.push(fixture_fn);
}
}
}
for fixture in fixtures {
for assertion in &fixture.assertions {
if assertion.assertion_type == "method_result" {
if let Some(method_name) = &assertion.method {
if let Some(helper_fn) = wasm_method_helper_import(method_name) {
if !imports.contains(&helper_fn) {
imports.push(helper_fn);
}
}
}
}
}
}
if let Some(factory) = client_factory {
let camel = factory.to_lower_camel_case();
if !imports.contains(&camel) {
imports.push(camel);
}
}
imports.extend(handle_constructors);
if let (true, Some(opts_type)) = (needs_options_import, options_type) {
imports.push(opts_type.to_string());
imports.extend(enum_imports.iter().map(|s| s.to_string()));
}
if let Some(hct) = handle_config_type {
if !imports.contains(&hct.to_string()) {
imports.push(hct.to_string());
}
}
let _ = writeln!(out, "import {{ {} }} from '{pkg_name}';", imports.join(", "));
}
let _ = writeln!(out);
let _ = writeln!(out, "describe('{category}', () => {{");
for (i, fixture) in fixtures.iter().enumerate() {
render_test_case(
&mut out,
fixture,
field_resolver,
options_type,
enum_fields,
handle_config_type,
client_factory,
bigint_fields,
e2e_config,
);
if i + 1 < fixtures.len() {
let _ = writeln!(out);
}
}
let _ = writeln!(out, "}});");
out
}
fn resolve_wasm_function_name(call_config: &crate::config::CallConfig) -> String {
call_config
.overrides
.get("wasm")
.and_then(|o| o.function.clone())
.unwrap_or_else(|| call_config.function.clone())
}
fn wasm_method_helper_import(method_name: &str) -> Option<String> {
match method_name {
"has_error_nodes" => Some("treeHasErrorNodes".to_string()),
"error_count" | "tree_error_count" => Some("treeErrorCount".to_string()),
"tree_to_sexp" => Some("treeToSexp".to_string()),
"contains_node_type" => Some("treeContainsNodeType".to_string()),
"find_nodes_by_type" => Some("findNodesByType".to_string()),
"run_query" => Some("runQuery".to_string()),
_ => None,
}
}
#[allow(clippy::too_many_arguments)]
fn render_test_case(
out: &mut String,
fixture: &Fixture,
field_resolver: &FieldResolver,
options_type: Option<&str>,
enum_fields: &HashMap<String, String>,
handle_config_type: Option<&str>,
client_factory: Option<&str>,
bigint_fields: &[String],
e2e_config: &E2eConfig,
) {
let call_config = e2e_config.resolve_call(fixture.call.as_deref());
let resolved_function_name = resolve_wasm_function_name(call_config);
let function_name = resolved_function_name.as_str();
let resolved_result_var = call_config.result_var.clone();
let result_var = resolved_result_var.as_str();
let is_async = call_config.r#async;
let args = &call_config.args;
let test_name = sanitize_ident(&fixture.id);
let description = fixture.description.replace('\'', "\\'");
let async_kw = if is_async { "async " } else { "" };
let await_kw = if is_async { "await " } else { "" };
let expects_error = fixture.assertions.iter().any(|a| a.assertion_type == "error");
let (mut setup_lines, arg_parts) = build_args_and_setup(
&fixture.input,
args,
options_type,
enum_fields,
&fixture.id,
handle_config_type,
bigint_fields,
);
let args_str = arg_parts.join(", ");
let mut visitor_arg = String::new();
if let Some(visitor_spec) = &fixture.visitor {
visitor_arg = build_wasm_visitor(&mut setup_lines, visitor_spec);
}
let final_args = if visitor_arg.is_empty() {
args_str
} else if args_str.is_empty() {
format!("{{ visitor: {visitor_arg} }}")
} else {
format!("{args_str}, {{ visitor: {visitor_arg} }}")
};
let call_expr = if client_factory.is_some() {
format!("client.{function_name}({final_args})")
} else {
format!("{function_name}({final_args})")
};
let has_base_url_arg = args.iter().any(|arg| arg.arg_type == "base_url");
let base_url_expr = if has_base_url_arg {
format!("`${{process.env.MOCK_SERVER_URL}}/fixtures/{}`", fixture.id)
} else {
"process.env.MOCK_SERVER_URL".to_string()
};
if expects_error {
let _ = writeln!(out, " it('{test_name}: {description}', {async_kw}() => {{");
if let Some(factory) = client_factory {
let factory_camel = factory.to_lower_camel_case();
let _ = writeln!(
out,
" const client = {await_kw}{factory_camel}('test-key', {base_url_expr});"
);
}
for line in &setup_lines {
let _ = writeln!(out, " {line}");
}
if is_async {
let _ = writeln!(
out,
" await expect({async_kw}() => {await_kw}{call_expr}).rejects.toThrow();"
);
} else {
let _ = writeln!(out, " expect(() => {call_expr}).toThrow();");
}
let _ = writeln!(out, " }});");
return;
}
let _ = writeln!(out, " it('{test_name}: {description}', {async_kw}() => {{");
if let Some(factory) = client_factory {
let factory_camel = factory.to_lower_camel_case();
let _ = writeln!(
out,
" const client = {await_kw}{factory_camel}('test-key', {base_url_expr});"
);
}
for line in &setup_lines {
let _ = writeln!(out, " {line}");
}
let has_usable_assertion = fixture.assertions.iter().any(|a| {
if a.assertion_type == "not_error" || a.assertion_type == "error" {
return false;
}
match &a.field {
Some(f) if !f.is_empty() => field_resolver.is_valid_for_result(f),
_ => true,
}
});
if has_usable_assertion {
let _ = writeln!(out, " const {result_var} = {await_kw}{call_expr};");
} else {
let _ = writeln!(out, " {await_kw}{call_expr};");
}
for assertion in &fixture.assertions {
render_assertion(out, assertion, result_var, field_resolver);
}
let _ = writeln!(out, " }});");
}
fn build_args_and_setup(
input: &serde_json::Value,
args: &[crate::config::ArgMapping],
options_type: Option<&str>,
enum_fields: &HashMap<String, String>,
fixture_id: &str,
handle_config_type: Option<&str>,
bigint_fields: &[String],
) -> (Vec<String>, Vec<String>) {
let mut setup_lines = Vec::new();
let mut parts = Vec::new();
if args.is_empty() {
parts.push(json_to_js(input));
return (setup_lines, parts);
}
for arg in args {
if arg.arg_type == "mock_url" {
setup_lines.push(format!(
"const {} = `${{process.env.MOCK_SERVER_URL}}/fixtures/{fixture_id}`;",
arg.name,
));
parts.push(arg.name.clone());
continue;
}
if arg.arg_type == "base_url" {
setup_lines.push(format!(
"const {} = `${{process.env.MOCK_SERVER_URL}}/fixtures/{fixture_id}`;",
arg.name,
));
parts.push(arg.name.clone());
continue;
}
if arg.arg_type == "handle" {
let constructor_name = format!("create{}", arg.name.to_upper_camel_case());
let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
let config_value = input.get(field).unwrap_or(&serde_json::Value::Null);
if config_value.is_null()
|| config_value.is_object() && config_value.as_object().is_some_and(|o| o.is_empty())
{
setup_lines.push(format!("const {} = {constructor_name}(null);", arg.name));
} else if let (Some(hct), Some(obj)) = (handle_config_type, config_value.as_object()) {
let config_var = format!("{}Config", arg.name);
setup_lines.push(format!("const {config_var} = new {hct}();"));
for (k, field_val) in obj {
let camel_key = k.to_lower_camel_case();
let js_val = json_to_js(field_val);
setup_lines.push(format!("{config_var}.{camel_key} = {js_val};"));
}
setup_lines.push(format!("const {} = {constructor_name}({config_var});", arg.name));
} else {
let js_val = json_to_js(config_value);
setup_lines.push(format!("const {} = {constructor_name}({js_val});", arg.name));
}
parts.push(arg.name.clone());
continue;
}
let field = arg.field.strip_prefix("input.").unwrap_or(&arg.field);
let val = if field == "input" {
Some(input)
} else {
input.get(field)
};
match val {
None | Some(serde_json::Value::Null) if arg.optional => continue,
None | Some(serde_json::Value::Null) => {
let default_val = match arg.arg_type.as_str() {
"string" => "''".to_string(),
"int" | "integer" => "0".to_string(),
"float" | "number" => "0.0".to_string(),
"bool" | "boolean" => "false".to_string(),
_ => "null".to_string(),
};
parts.push(default_val);
}
Some(v) => {
if arg.arg_type == "json_object" && !v.is_null() {
if let Some(opts_type) = options_type {
if let Some(obj) = v.as_object() {
setup_lines.push(format!("const options = new {opts_type}();"));
for (k, field_val) in obj {
let camel_key = k.to_lower_camel_case();
let js_val = if let Some(enum_type) = enum_fields.get(k) {
if let Some(s) = field_val.as_str() {
let pascal_val = s.to_upper_camel_case();
format!("{enum_type}.{pascal_val}")
} else {
json_to_js(field_val)
}
} else if bigint_fields.iter().any(|f| f == &camel_key) && field_val.is_number() {
format!("BigInt({})", json_to_js(field_val))
} else {
json_to_js(field_val)
};
setup_lines.push(format!("options.{camel_key} = {js_val};"));
}
parts.push("options".to_string());
continue;
}
}
}
parts.push(json_to_js(v));
}
}
}
(setup_lines, parts)
}
fn render_assertion(out: &mut String, assertion: &Assertion, result_var: &str, field_resolver: &FieldResolver) {
if let Some(f) = &assertion.field {
if !f.is_empty() && !field_resolver.is_valid_for_result(f) {
let _ = writeln!(out, " // skipped: field '{f}' not available on result type");
return;
}
}
let field_expr = match &assertion.field {
Some(f) if !f.is_empty() => field_resolver.accessor(f, "wasm", result_var),
_ => result_var.to_string(),
};
match assertion.assertion_type.as_str() {
"equals" => {
if let Some(expected) = &assertion.value {
let js_val = json_to_js(expected);
if expected.is_string() {
let _ = writeln!(out, " expect({field_expr}.trim()).toBe({js_val});");
} else {
let _ = writeln!(out, " expect({field_expr}).toBe({js_val});");
}
}
}
"contains" => {
if let Some(expected) = &assertion.value {
let js_val = json_to_js(expected);
let _ = writeln!(out, " expect({field_expr}).toContain({js_val});");
}
}
"contains_all" => {
if let Some(values) = &assertion.values {
for val in values {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({field_expr}).toContain({js_val});");
}
}
}
"not_contains" => {
if let Some(expected) = &assertion.value {
let js_val = json_to_js(expected);
let _ = writeln!(out, " expect({field_expr}).not.toContain({js_val});");
}
}
"not_empty" => {
let _ = writeln!(out, " expect({field_expr}.length).toBeGreaterThan(0);");
}
"is_empty" => {
let _ = writeln!(out, " expect({field_expr}.trim()).toHaveLength(0);");
}
"contains_any" => {
if let Some(values) = &assertion.values {
let items: Vec<String> = values.iter().map(json_to_js).collect();
let arr_str = items.join(", ");
let _ = writeln!(
out,
" expect([{arr_str}].some((v) => {field_expr}.includes(v))).toBe(true);"
);
}
}
"greater_than" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({field_expr}).toBeGreaterThan({js_val});");
}
}
"less_than" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({field_expr}).toBeLessThan({js_val});");
}
}
"greater_than_or_equal" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({field_expr}).toBeGreaterThanOrEqual({js_val});");
}
}
"less_than_or_equal" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({field_expr}).toBeLessThanOrEqual({js_val});");
}
}
"starts_with" => {
if let Some(expected) = &assertion.value {
let js_val = json_to_js(expected);
let _ = writeln!(out, " expect({field_expr}.startsWith({js_val})).toBe(true);");
}
}
"count_min" => {
if let Some(val) = &assertion.value {
if let Some(n) = val.as_u64() {
let _ = writeln!(out, " expect({field_expr}.length).toBeGreaterThanOrEqual({n});");
}
}
}
"count_equals" => {
if let Some(val) = &assertion.value {
if let Some(n) = val.as_u64() {
let _ = writeln!(out, " expect({field_expr}.length).toBe({n});");
}
}
}
"is_true" => {
let _ = writeln!(out, " expect({field_expr}).toBe(true);");
}
"is_false" => {
let _ = writeln!(out, " expect({field_expr}).toBe(false);");
}
"method_result" => {
if let Some(method_name) = &assertion.method {
let call_expr = build_wasm_method_call(result_var, method_name, assertion.args.as_ref());
let check = assertion.check.as_deref().unwrap_or("is_true");
match check {
"equals" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({call_expr}).toBe({js_val});");
}
}
"is_true" => {
let _ = writeln!(out, " expect({call_expr}).toBe(true);");
}
"is_false" => {
let _ = writeln!(out, " expect({call_expr}).toBe(false);");
}
"greater_than_or_equal" => {
if let Some(val) = &assertion.value {
let n = val.as_u64().unwrap_or(0);
let _ = writeln!(out, " expect({call_expr}).toBeGreaterThanOrEqual({n});");
}
}
"count_min" => {
if let Some(val) = &assertion.value {
let n = val.as_u64().unwrap_or(0);
let _ = writeln!(out, " expect({call_expr}.length).toBeGreaterThanOrEqual({n});");
}
}
"contains" => {
if let Some(val) = &assertion.value {
let js_val = json_to_js(val);
let _ = writeln!(out, " expect({call_expr}).toContain({js_val});");
}
}
"is_error" => {
let _ = writeln!(out, " expect(() => {{ {call_expr}; }}).toThrow();");
}
other_check => {
panic!("WASM e2e generator: unsupported method_result check type: {other_check}");
}
}
} else {
panic!("WASM e2e generator: method_result assertion missing 'method' field");
}
}
"not_error" => {
}
"error" => {
}
other => {
panic!("WASM e2e generator: unsupported assertion type: {other}");
}
}
}
fn build_wasm_method_call(result_var: &str, method_name: &str, args: Option<&serde_json::Value>) -> String {
match method_name {
"root_child_count" => format!("{result_var}.rootNode.childCount"),
"root_node_type" => format!("{result_var}.rootNode.type"),
"named_children_count" => format!("{result_var}.rootNode.namedChildCount"),
"has_error_nodes" => format!("treeHasErrorNodes({result_var})"),
"error_count" | "tree_error_count" => format!("treeErrorCount({result_var})"),
"tree_to_sexp" => format!("treeToSexp({result_var})"),
"contains_node_type" => {
let node_type = args
.and_then(|a| a.get("node_type"))
.and_then(|v| v.as_str())
.unwrap_or("");
format!("treeContainsNodeType({result_var}, \"{node_type}\")")
}
"find_nodes_by_type" => {
let node_type = args
.and_then(|a| a.get("node_type"))
.and_then(|v| v.as_str())
.unwrap_or("");
format!("findNodesByType({result_var}, \"{node_type}\")")
}
"run_query" => {
let query_source = args
.and_then(|a| a.get("query_source"))
.and_then(|v| v.as_str())
.unwrap_or("");
let language = args
.and_then(|a| a.get("language"))
.and_then(|v| v.as_str())
.unwrap_or("");
format!("runQuery({result_var}, \"{language}\", \"{query_source}\", source)")
}
other => {
let camel_method = other.to_lower_camel_case();
if let Some(args_val) = args {
let arg_str = args_val
.as_object()
.map(|obj| {
obj.iter()
.map(|(k, v)| format!("{}: {}", k, json_to_js(v)))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
format!("{result_var}.{camel_method}({arg_str})")
} else {
format!("{result_var}.{camel_method}()")
}
}
}
}
fn json_to_js(value: &serde_json::Value) -> String {
match value {
serde_json::Value::String(s) => format!("\"{}\"", escape_js(s)),
serde_json::Value::Bool(b) => b.to_string(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::Null => "null".to_string(),
serde_json::Value::Array(arr) => {
let items: Vec<String> = arr.iter().map(json_to_js).collect();
format!("[{}]", items.join(", "))
}
serde_json::Value::Object(map) => {
let entries: Vec<String> = map
.iter()
.map(|(k, v)| {
let key = if k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
&& !k.starts_with(|c: char| c.is_ascii_digit())
{
k.clone()
} else {
format!("\"{}\"", escape_js(k))
};
format!("{key}: {}", json_to_js(v))
})
.collect();
format!("{{ {} }}", entries.join(", "))
}
}
}
fn build_wasm_visitor(setup_lines: &mut Vec<String>, visitor_spec: &crate::fixture::VisitorSpec) -> String {
use std::fmt::Write as FmtWrite;
let mut visitor_obj = String::new();
let _ = writeln!(visitor_obj, "{{");
for (method_name, action) in &visitor_spec.callbacks {
emit_wasm_visitor_method(&mut visitor_obj, method_name, action);
}
let _ = writeln!(visitor_obj, " }}");
setup_lines.push(format!("const _testVisitor = {visitor_obj}"));
"_testVisitor".to_string()
}
fn emit_wasm_visitor_method(out: &mut String, method_name: &str, action: &CallbackAction) {
use std::fmt::Write as FmtWrite;
let camel_method = to_camel_case_wasm(method_name);
let params = match method_name {
"visit_link" => "ctx, href, text, title",
"visit_image" => "ctx, src, alt, title",
"visit_heading" => "ctx, level, text, id",
"visit_code_block" => "ctx, lang, code",
"visit_code_inline"
| "visit_strong"
| "visit_emphasis"
| "visit_strikethrough"
| "visit_underline"
| "visit_subscript"
| "visit_superscript"
| "visit_mark"
| "visit_button"
| "visit_summary"
| "visit_figcaption"
| "visit_definition_term"
| "visit_definition_description" => "ctx, text",
"visit_text" => "ctx, text",
"visit_list_item" => "ctx, ordered, marker, text",
"visit_blockquote" => "ctx, content, depth",
"visit_table_row" => "ctx, cells, isHeader",
"visit_custom_element" => "ctx, tagName, html",
"visit_form" => "ctx, actionUrl, method",
"visit_input" => "ctx, inputType, name, value",
"visit_audio" | "visit_video" | "visit_iframe" => "ctx, src",
"visit_details" => "ctx, isOpen",
"visit_element_end" | "visit_table_end" | "visit_definition_list_end" | "visit_figure_end" => "ctx, output",
"visit_list_start" => "ctx, ordered",
"visit_list_end" => "ctx, ordered, output",
_ => "ctx",
};
let _ = writeln!(out, " {camel_method}({params}): string | {{ custom: string }} {{");
match action {
CallbackAction::Skip => {
let _ = writeln!(out, " return \"skip\";");
}
CallbackAction::Continue => {
let _ = writeln!(out, " return \"continue\";");
}
CallbackAction::PreserveHtml => {
let _ = writeln!(out, " return \"preserve_html\";");
}
CallbackAction::Custom { output } => {
let escaped = escape_js(output);
let _ = writeln!(out, " return {{ custom: {escaped} }};");
}
CallbackAction::CustomTemplate { template } => {
let _ = writeln!(out, " return {{ custom: `{template}` }};");
}
}
let _ = writeln!(out, " }},");
}
fn to_camel_case_wasm(snake: &str) -> String {
use heck::ToLowerCamelCase;
snake.to_lower_camel_case()
}