use super::common::*;
#[test]
fn test_native_proto_try_catch_basic() {
let js = r##"
function safeDivide(a, b) {
try {
if (b === 0) throw "div by zero";
return a / b;
} catch (e) {
return -1;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_basic");
println!("=== Try-catch basic ===\n{}", zig);
assert!(
zig.contains("_js_try_blk_"),
"Expected labeled block:\n{}",
zig
);
assert!(
zig.contains("} else |_| {"),
"Expected '}} else |_| {{' for catch handler when e is unused:\n{}",
zig
);
assert!(
!zig.contains("const e ="),
"Should NOT have 'const e =' when e is unused:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_basic");
}
#[test]
fn test_native_proto_try_catch_e_binding_used() {
let js = r##"
function catchAndLog(val) {
try {
if (val < 0) throw "bad";
return val;
} catch (e) {
return e;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_e_binding_used");
println!("=== Try-catch e binding (used) ===\n{}", zig);
assert!(
zig.contains("js_error.JsError.fromError(__catch_err,"),
"Expected 'js_error.JsError.fromError(__catch_err, ...)' when e is used in catch body:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_e_binding_used");
}
#[test]
fn test_native_proto_try_catch_e_binding_unused() {
let js = r##"
function catchAndIgnore(val) {
try {
if (val < 0) throw "bad";
return val;
} catch (e) {
return -1;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_e_binding_unused");
println!("=== Try-catch e binding (unused) ===\n{}", zig);
assert!(
!zig.contains("js_error.JsError.fromError(__catch_err,"),
"Should NOT have 'JsError.fromError' when e is unused:\n{}",
zig
);
assert!(
zig.contains("} else |_| {"),
"Expected '}} else |_| {{' when e is unused:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_e_binding_unused");
}
#[test]
fn test_native_proto_try_catch_throw_break() {
let js = r##"
function check(val) {
try {
if (val < 0) throw "negative";
return val;
} catch (e) {
return 0;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_throw_break");
println!("=== Try-catch throw break ===\n{}", zig);
assert!(
zig.contains("break :"),
"Expected break :label for throw inside try:\n{}",
zig
);
assert!(
zig.contains("} else |_| {"),
"Expected catch handler with |_| (e unused):\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_throw_break");
}
#[test]
fn test_native_proto_try_finally() {
let js = r#"
function withCleanup() {
let x = 0;
try {
x = 42;
} finally {
x = 0;
}
return x;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_try_finally");
println!("=== Try-finally ===\n{}", zig);
assert!(
zig.contains("x = 42;") && zig.contains("x = 0;"),
"Expected finally body inlined after try body:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_finally");
}
#[test]
fn test_native_proto_try_catch_finally() {
let js = r##"
function process(val) {
let result = 0;
try {
if (val < 0) throw "bad";
result = val * 2;
} catch (e) {
result = -1;
} finally {
val = 0;
}
return result;
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_finally");
println!("=== Try-catch-finally ===\n{}", zig);
assert!(
zig.contains("defer {") && zig.contains("val = 0;"),
"Expected finally as defer:\n{}",
zig
);
assert!(
zig.contains("} else |_| {"),
"Expected catch handler with |_| (e unused):\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_finally");
}
#[test]
fn test_native_proto_try_catch_no_throw() {
let js = r#"
function safeOp(x) {
try {
return x + 1;
} catch (e) {
return 0;
}
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_no_throw");
println!("=== Try-catch no throw ===\n{}", zig);
assert!(zig.contains("return (x + 1)"), "Expected body:\n{}", zig);
assert!(
!zig.contains("else |__catch_err|") && !zig.contains("} else |_| {"),
"Should not have catch handler when body always exits:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_no_throw");
}
#[test]
fn test_native_proto_throw_bare() {
let js = r##"
function reject(val) {
if (val < 0) throw "bad";
return val;
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_throw_bare");
println!("=== Throw bare ===\n{}", zig);
assert!(
zig.contains("return error.JsThrow"),
"Expected return error.JsThrow for bare throw:\n{}",
zig
);
assert!(
!zig.contains("break :"),
"Should not have break for bare throw:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_throw_bare");
}
#[test]
fn test_native_proto_try_catch_nested_inner_catch() {
let js = r##"
function nestedCatch(a, b) {
try {
try {
if (b === 0) throw "div by zero";
return a / b;
} catch (e) {
return -1;
}
return -2;
} catch (e) {
return -3;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_nested_inner_catch");
println!("=== Nested try-catch (inner catch) ===\n{}", zig);
let result_count = zig.matches("= _js_try_").count(); assert_eq!(
result_count, 4,
"Expected 4 '= _js_try_' assignments for 2 nested try-catch, got {}:\n{}",
result_count, zig
);
assert!(
zig.contains("} else |_| {"),
"Expected '}} else |_| {{' in inner catch when e unused:\n{}",
zig
);
assert!(
zig.contains("break :_js_try_body_blk_0"),
"Expected error propagation break from inner to outer body block:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_nested_inner_catch");
}
#[test]
fn test_native_proto_try_catch_nested_rethrow() {
let js = r##"
function rethrowExample(a, b) {
try {
try {
if (b === 0) throw "div by zero";
return a / b;
} catch (inner) {
throw inner;
}
} catch (outer) {
return -1;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_nested_rethrow");
println!("=== Nested try-catch (rethrow) ===\n{}", zig);
let result_count = zig.matches("= _js_try_").count();
assert_eq!(
result_count, 4,
"Expected 4 '= _js_try_' assignments for nested try-catch rethrow, got {}:\n{}",
result_count, zig
);
assert!(
zig.contains("break :"),
"Expected break :label for rethrow:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_nested_rethrow");
}
#[test]
fn test_native_proto_try_catch_nested_no_throw() {
let js = r#"
function nestedNoThrow(x) {
try {
try {
return x + 1;
} catch (e) {
return 0;
}
} catch (e) {
return -1;
}
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_nested_no_throw");
println!("=== Nested try-catch (no throw) ===\n{}", zig);
assert!(
zig.contains("return (x + 1)"),
"Expected body for no-throw inner try:\n{}",
zig
);
assert!(
zig.contains("} else |_| {"),
"Expected '}} else |_| {{' for outer catch (e unused):\n{}",
zig
);
}
#[test]
fn test_native_proto_try_catch_json_parse() {
let js = r##"
function safeParse(str) {
try {
const result = JSON.parse(str);
return result;
} catch (e) {
return null;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_json_parse");
println!("=== JSON.parse in try-catch ===\n{}", zig);
assert!(
zig.contains("_js_try_body_blk_"),
"Expected labeled body block:\n{}",
zig
);
assert!(
zig.contains("break :"),
"Expected break :label for JSON.parse inside try:\n{}",
zig
);
assert!(
!zig.contains("catch return error.JsThrow"),
"Should NOT have 'catch return error.JsThrow' inside try block:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_json_parse");
}
#[test]
fn test_native_proto_try_catch_json_parse_call() {
let js = r##"
function parseAndIgnore(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}
"##;
let zig = transpile_and_assert(js, "test_native_proto_try_catch_json_parse_call");
println!("=== JSON.parse call in try-catch ===\n{}", zig);
assert!(
zig.contains("break :"),
"Expected break :label for JSON.parse call inside try:\n{}",
zig
);
assert!(
!zig.contains("catch return error.JsThrow"),
"Should NOT have 'catch return error.JsThrow' inside try block:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_try_catch_json_parse_call");
}
#[test]
fn test_native_proto_exponential_operator() {
let js = r#"
/**
* @param {number} base
* @param {number} exp
* @returns {number}
*/
export function intPow(base, exp) {
return base ** exp;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_exponential_operator");
println!("=== Exponential (int) ===\n{}", zig);
assert!(
zig.contains("std.math.pow(f64,"),
"Expected std.math.pow for exponentiation:\n{}",
zig
);
assert!(
zig.contains("pub fn intPow(base: f64, exp: f64) f64 {"),
"Expected f64 params and return type for ** operator:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_exponential_operator");
}
#[test]
fn test_native_proto_exponential_float() {
let js_float = r#"
export function powFloat() {
return 2.0 ** 3.0;
}
"#;
let zig = transpile_and_assert(js_float, "test_native_proto_exponential_float");
println!("=== Exponential (float) ===\n{}", zig);
assert!(
zig.contains("std.math.pow(f64,"),
"Expected std.math.pow for float exponentiation:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_exponential_float");
}
#[test]
fn test_native_proto_exponential_mixed() {
let js = r#"
export function powMixed() {
const base = 2;
const exp = 3.0;
return base ** exp;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_exponential_mixed");
println!("=== Exponential (mixed) ===\n{}", zig);
assert!(
zig.contains("std.math.pow(f64,"),
"Expected std.math.pow for mixed exponentiation:\n{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_exponential_mixed");
}
#[test]
fn test_native_proto_arrow_function() {
let js = r#"export function testArrow() {
const add = (x, y) => x + y;
return add(3, 4);
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_arrow_function");
println!(
"=== Arrow function (basic) ===
{}",
zig
);
assert!(
zig.contains("const _arrow_fn_"),
"Expected arrow function to generate a struct:
{}",
zig
);
assert!(
zig.contains("const add = _arrow_fn_"),
"Expected arrow function to be assigned to variable:
{}",
zig
);
}
#[test]
fn test_native_proto_template_literal_complex() {
let js = r#"export function buildMessage(user, scores) {
const name = user.name;
const avg = scores[0] + scores[1];
return `Hello ${name}, your average score is ${avg}!`;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_template_literal_complex");
println!(
"=== Template literal (complex) ===
{}",
zig
);
assert!(
zig.contains("std.fmt.allocPrint"),
"Expected allocPrint for template with expressions:
{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_template_literal_complex");
}
#[test]
fn test_native_proto_exponential_edge() {
let js = r#"export function powEdge() {
const x = 2.0;
const a = x ** 0; // x^0 = 1
const b = x ** -1; // x^(-1) = 0.5
return a + b;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_exponential_edge");
println!(
"=== Exponential (edge) ===
{}",
zig
);
assert!(
zig.contains("std.math.pow(f64,"),
"Expected std.math.pow for edge case exponentiation:
{}",
zig
);
assert_zig_ast_check(&zig, "test_native_proto_exponential_edge");
}
#[test]
fn test_native_proto_arrow_single_param() {
let js = r#"export function testSingleParam() {
const double = x => x * 2;
return double(5);
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_arrow_single_param");
println!(
"=== Arrow function (single param) ===
{}",
zig
);
assert!(
zig.contains("const _arrow_fn_"),
"Expected arrow function struct"
);
assert!(
zig.contains("const double = _arrow_fn_"),
"Expected assignment"
);
}
#[test]
fn test_native_proto_arrow_block_body() {
let js = r#"export function testBlockArrow() {
const f = x => { return x + 1; };
return f(5);
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_arrow_block_body");
println!(
"=== Arrow function (block) ===
{}",
zig
);
assert!(
zig.contains("const _arrow_fn_"),
"Expected arrow function struct"
);
assert!(
zig.contains("return (x + 1);"),
"Expected return in block body"
);
}
#[test]
fn test_native_proto_closure_basic() {
let js = r#"export function testClosure() {
const x = 10;
const adder = (y) => x + y;
return adder(5);
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_closure_basic");
println!(
"=== Closure (basic) ===
{}",
zig
);
assert!(zig.contains("const Closure_"), "Expected closure struct");
assert!(zig.contains("fn call(self:"), "Expected call method");
assert!(
zig.contains("self.x"),
"Expected captured variable access via self.x"
);
}
#[test]
fn test_native_proto_closure_mutable() {
let js = r#"export function testClosureMutable() {
let count = 0;
const increment = () => { count = count + 1; return count; };
increment();
return count;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_closure_mutable");
println!(
"=== Closure (mutable) ===
{}",
zig
);
assert!(zig.contains("const Closure_"), "Expected closure struct");
assert!(zig.contains("*i64"), "Expected pointer for mutable capture");
assert!(
zig.contains("self.count.*"),
"Expected dereference for mutable capture"
);
}
#[test]
fn test_native_proto_getter() {
let js = r#"export function useGetter() {
const obj = { get x() { return 42; } };
return obj.x;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_getter");
println!(
"=== Getter ===
{}",
zig
);
assert!(
zig.contains(".x = 42"),
"Expected getter value as field: {}",
zig
);
assert!(
!zig.contains("get "),
"Should not have 'get' keyword: {}",
zig
);
}
#[test]
fn test_native_proto_setter_compile_error() {
let js = r#"export function useSetter() {
const obj = { a: 1, set x(v) { this._x = v; } };
return obj.a;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_setter_compile_error");
println!("=== Setter compile error ===\n{}", zig);
assert!(
zig.contains("@compileError"),
"Setter should generate @compileError: {}",
zig
);
assert!(!zig.contains("set "), "No 'set' keyword in output: {}", zig);
assert!(
zig.contains(".a = 1"),
"Regular field should be preserved: {}",
zig
);
}
#[test]
fn test_native_proto_getter_setter_combined() {
let js = r#"export function combineGS() {
const obj = { name: "test", get age() { return 25; }, set age(v) { /* noop */ } };
// age getter provides the field value, setter is skipped
return obj.name;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_getter_setter_combined");
println!(
"=== Combined getter/setter ===
{}",
zig
);
assert!(
zig.contains(".name = \"test\""),
"Regular property should remain: {}",
zig
);
assert!(
zig.contains(".age = 25"),
"Getter should provide field value: {}",
zig
);
assert!(
zig.contains("@compileError"),
"Setter should generate @compileError: {}",
zig
);
assert!(!zig.contains("set "), "No 'set' keyword in output: {}", zig);
}
#[test]
fn test_native_proto_optional_chain_known_struct() {
let js = r#"
export function getProp(obj) {
const val = obj?.name;
return val;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_optional_chain_known_struct");
assert!(
zig.contains("obj.name"),
"Should use direct access obj.name: {}",
zig
);
assert!(
!zig.contains("_oc"),
"Should NOT generate null-check temp var for known struct: {}",
zig
);
}
#[test]
fn test_native_proto_optional_chain_unknown() {
let js = r#"
function getUnknown(obj) {
return obj?.name;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_optional_chain_unknown");
assert!(
zig.contains("(if ("),
"Should generate null check pattern: {}",
zig
);
assert!(
zig.contains(") |_oc"),
"Should generate temp var capture: {}",
zig
);
assert!(
zig.contains(" else null)"),
"Should have else null: {}",
zig
);
}
#[test]
fn test_native_proto_optional_chain_call() {
let js = r#"
function callMaybe(obj) {
return obj?.greet("World");
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_optional_chain_call");
assert!(
zig.contains("(if ("),
"Should generate null check + call pattern: {}",
zig
);
assert!(
zig.contains(" else null)"),
"Should have else null: {}",
zig
);
assert!(zig.contains("greet("), "Should call method greet: {}", zig);
}
#[test]
fn test_optional_chain_call_jsany() {
let js = r#"
function maybeCall() {
let x = null;
return x?.get("key");
}
"#;
let zig = transpile_and_assert(js, "test_optional_chain_call_jsany");
assert!(
!zig.contains("|_oc"),
"JsAny receiver should not use OptionalChain unwrap: {}",
zig
);
assert!(
zig.contains(".isNullish()"),
"JsAny receiver should use isNullish(): {}",
zig
);
assert!(
zig.contains("JsAny.fromUndefined()"),
"JsAny null branch should be JsAny.fromUndefined(): {}",
zig
);
assert!(
zig.contains(".get(\"key\")"),
"Should call .get(\"key\") in non-null branch: {}",
zig
);
}
#[test]
fn test_native_proto_optional_chain_nested() {
let js = r#"
function deep(obj) {
return obj?.a?.b;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_optional_chain_nested");
let oc_count = zig.matches("_oc").count();
assert!(
oc_count >= 2,
"Expected at least 2 temp vars for nested chain, got {}: {}",
oc_count,
zig
);
assert!(
zig.contains(" else null)") || zig.contains(" else null"),
"Should have else null branches: {}",
zig
);
}
#[test]
fn test_native_proto_optional_chain_null_literal() {
let js = r#"
function nullChain() {
return null?.prop;
}
"#;
let zig = transpile_and_assert(js, "test_native_proto_optional_chain_null_literal");
assert!(
zig.contains("(if (") || zig.contains("null"),
"Should handle null literal in chain: {}",
zig
);
}
#[test]
fn test_try_sequential_catchable_error_reset() {
let js = r#"
/** @param {string} jsonStr */
export function testSequentialTry(jsonStr) {
let a = null;
try {
a = JSON.parse(jsonStr);
} catch (e) {
a = null;
}
let b = null;
try {
b = JSON.parse(jsonStr);
} finally {
b = null;
}
return a;
}
"#;
let zig = transpile_and_assert(js, "test_try_sequential_catchable_error_reset");
let try_blk_count = zig.matches("_js_try_blk_").count();
assert!(
try_blk_count >= 4,
"Expected both try blocks to use labeled-block pattern (>= 4 _js_try_blk_ occurrences), got {}: {}",
try_blk_count,
zig
);
}
#[test]
fn test_break_in_finally_compile_error() {
use crate::tests::common::parse_and_transpile;
let js = r#"
export function testBreakFinally(arr) {
let sum = 0;
try {
sum = 1;
} finally {
if (sum > 0) break;
}
return sum;
}
"#;
let result = parse_and_transpile(js, None).unwrap();
assert!(
result
.errors
.iter()
.any(|e| e.contains("break/continue in finally")),
"Expected 'break/continue in finally' in errors: {:?}",
result.errors
);
assert!(
result.zig_code.contains("@compileError"),
"Expected @compileError in zig code:\n{}",
result.zig_code
);
}
#[test]
fn test_continue_in_finally_compile_error() {
use crate::tests::common::parse_and_transpile;
let js = r#"
export function testContinueFinally(arr) {
let sum = 0;
try {
sum = 1;
} finally {
if (sum > 0) continue;
}
return sum;
}
"#;
let result = parse_and_transpile(js, None).unwrap();
assert!(
result
.errors
.iter()
.any(|e| e.contains("break/continue in finally")),
"Expected 'break/continue in finally' in errors: {:?}",
result.errors
);
}
#[test]
fn test_break_in_loop_inside_finally_ok() {
let js = r#"
export function testBreakInLoopFinally() {
let sum = 0;
try {
sum = 1;
} finally {
for (let i = 0; i < 3; i++) {
if (i === 1) break;
}
}
return sum;
}
"#;
let result = parse_and_transpile(js, None).unwrap();
assert!(
!result
.errors
.iter()
.any(|e| e.contains("break/continue in finally")),
"Expected NO 'break/continue in finally' error (break in for loop is valid), errors: {:?}",
result.errors
);
}
#[test]
fn test_p1_11_return_inside_try() {
let js = r#"
export function safeRead(jsonStr) {
try {
return JSON.parse(jsonStr);
} catch (e) {
return null;
}
}
"#;
let zig = transpile_and_assert(js, "test_p1_11_return_inside_try");
assert!(
!zig.contains(") void {"),
"Expected non-void return type for function returning inside try block (was dropped before P1-11):\n{}",
zig
);
}
#[test]
fn test_p2_3_nested_try_throw_detection() {
let js = r#"
export function nestedTryThrow() {
try {
try {
throw new Error("inner");
} catch (inner_e) {
throw new Error("rethrow: " + inner_e.message);
}
} catch (e) {
return e.message;
}
}
"#;
let zig = transpile_and_assert(js, "test_p2_3_nested_try_throw_detection");
assert!(
zig.contains("_js_try_blk_"),
"Expected Case A try emit (labeled blocks) for outer try with nested throw:\n{}",
zig
);
}
#[test]
fn test_c5_arrow_this_in_class_method() {
let js = r#"
class Counter {
constructor() {
this.count = 0;
}
increment() {
const add = () => {
this.count = this.count + 1;
};
add();
}
}
/** @returns {i64} */
export function testC5Counter() {
const c = new Counter();
c.increment();
return c.count;
}
"#;
let zig = transpile_and_assert(js, "test_c5_arrow_this_in_class_method");
println!("=== C5 arrow this in class method ===\n{}", zig);
assert!(
zig.contains("__self: *Counter"),
"Expected __self: *Counter field in closure struct: {}",
zig
);
assert!(
zig.contains(".__self = &self"),
"Expected .__self = &self init in closure instance: {}",
zig
);
assert!(
zig.contains("self.__self.count"),
"Expected self.__self.count access in closure body: {}",
zig
);
}
#[test]
fn test_c5_arrow_this_read_in_class_method() {
let js = r#"
class Box {
constructor(val) {
this.value = val;
}
getValue() {
const getter = () => this.value;
return getter();
}
}
export function testC5Box(v) {
const b = new Box(v);
return b.getValue();
}
"#;
let zig = transpile_and_assert(js, "test_c5_arrow_this_read_in_class_method");
println!("=== C5 arrow this read ===\n{}", zig);
assert!(
zig.contains("__self: *Box"),
"Expected __self: *Box field: {}",
zig
);
assert!(
zig.contains("self.__self.value"),
"Expected self.__self.value: {}",
zig
);
}
#[test]
fn test_c5_fn_expr_this_in_class_method() {
let js = r#"
class Logger {
constructor() {
this.prefix = "LOG";
}
log(msg) {
const formatMsg = function() {
return this.prefix + ": " + msg;
};
return formatMsg();
}
}
export function testC5Logger(m) {
const l = new Logger();
return l.log(m);
}
"#;
let zig = transpile_and_assert(js, "test_c5_fn_expr_this_in_class_method");
println!("=== C5 fn expr this in class method ===\n{}", zig);
assert!(
zig.contains("__self: *Logger"),
"Expected __self: *Logger field: {}",
zig
);
assert!(
zig.contains("self.__self.prefix"),
"Expected self.__self.prefix: {}",
zig
);
}
#[test]
fn test_c5_arrow_this_with_other_captures() {
let js = r#"
class Accum {
constructor() {
this.total = 0;
}
add(delta) {
const step = 1;
const inc = () => {
this.total = this.total + step + delta;
};
inc();
}
}
export function testC5Accum(d) {
const a = new Accum();
a.add(d);
return a.total;
}
"#;
let zig = transpile_and_assert(js, "test_c5_arrow_this_with_other_captures");
println!("=== C5 arrow this with other captures ===\n{}", zig);
assert!(
zig.contains("__self: *Accum"),
"Expected __self: *Accum: {}",
zig
);
assert!(
zig.contains("self.__self.total"),
"Expected self.__self.total: {}",
zig
);
assert!(
zig.contains("self.step") || zig.contains("self.delta"),
"Expected captured step or delta via self: {}",
zig
);
}
#[test]
fn test_c5_arrow_no_this_no_capture() {
let js = r#"
class Processor {
constructor() {
this.data = 0;
}
run(x) {
const double = (n) => n * 2;
return double(x);
}
}
export function testC5Processor(v) {
const p = new Processor();
return p.run(v);
}
"#;
let zig = transpile_and_assert(js, "test_c5_arrow_no_this_no_capture");
println!("=== C5 arrow no this no capture ===\n{}", zig);
assert!(
!zig.contains("__self"),
"Expected no __self when this is not used in arrow: {}",
zig
);
}
#[test]
fn test_c5_arrow_this_in_constructor_callback() {
let js = r#"
class Store {
constructor() {
this.items = 0;
const init = () => {
this.items = 10;
};
init();
}
}
/** @returns {i64} */
export function testC5Store() {
const s = new Store();
return s.items;
}
"#;
let zig = transpile_and_assert(js, "test_c5_arrow_this_in_constructor_callback");
println!("=== C5 arrow this in constructor callback ===\n{}", zig);
assert!(
!zig.contains("__self"),
"Expected no __self in constructor closure (this is rewritten to local var): {}",
zig
);
assert!(
zig.contains("items") || zig.contains("items = 10"),
"Expected local var 'items' access in constructor closure: {}",
zig
);
}