silcrow 0.2.1

Deterministic CLI for a citable, checkable graph of laws in a git tree.
use super::*;

#[test]
fn arg_plus_one_moves() {
    let a = "function f(x) { save(x); }\n";
    let b = "function f(x) { save(x + 1); }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn callee_rename_moves() {
    let a = "function f(x) { save(x); }\n";
    let b = "function f(x) { log(x); }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn local_rename_stays() {
    let a = "function f(x) { save(x); }\n";
    let b = "function f(y) { save(y); }\n";
    assert_eq!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn call_order_moves() {
    let a = "function f() { debit(); credit(); }\n";
    let b = "function f() { credit(); debit(); }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn member_name_moves() {
    let a = "function f(obj) { return obj.a; }\n";
    let b = "function f(obj) { return obj.b; }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn swapped_arguments_move() {
    let a = "function f(payer, payee) { moveFunds(payer, payee); }\n";
    let b = "function f(payer, payee) { moveFunds(payee, payer); }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn comparison_moves() {
    let a = "function f(amount) { if (amount < LIMIT) pay(); }\n";
    let b = "function f(amount) { if (amount > LIMIT) pay(); }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn typeof_moves() {
    let a = "function f(x) { return typeof x; }\n";
    let b = "function f(x) { return void x; }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn binding_rename_stays() {
    let a = "function f(x) { const a = load(x); const g = (k) => k + a; const h = j => j; try { g(a); } catch (e) { h(e); } for (const i of a) { g(i); } }\n";
    let b = "function f(y) { const b = load(y); const m = (n) => n + b; const p = q => q; try { m(b); } catch (r) { p(r); } for (const s of b) { m(s); } }\n";
    assert_eq!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn catch_binding_is_not_the_outer() {
    let a = "function f() { const e = 1; try {} catch (e) { save(e); } }\n";
    let b = "function f() { const q = 1; try {} catch (e) { save(q); } }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn shorthand_property_keeps_its_name() {
    let a = "function f() { const x = 1; return { x }; }\n";
    let b = "function f() { const y = 1; return { y }; }\n";
    assert_ne!(pair(&JAVASCRIPT, a, 1).shape, pair(&JAVASCRIPT, b, 1).shape);
}

#[test]
fn parse_error_has_no_digest() {
    assert!(mint_span_digests(&JAVASCRIPT, "function f() { save(; }\n", 1, &[]).is_none());
}

#[test]
fn body_marker_reads_the_arrow_parameter_default() {
    let a = "const f = (days = 14) => {\n  renew(days);\n};\n";
    let b = "const f = (days = 30) => {\n  renew(days);\n};\n";
    let at = |s: &str| pair(&JAVASCRIPT, s, 2).shape;
    assert_ne!(at(a), at(b));
}

#[test]
fn whitespace_in_a_regex_or_string_moves() {
    moves(
        &JAVASCRIPT,
        "function f(s) {\n  return / a/.test(s);\n}\n",
        "function f(s) {\n  return /a/.test(s);\n}\n",
        1,
    );
    moves(
        &JAVASCRIPT,
        "function f(s) {\n  return `a  ${s}`;\n}\n",
        "function f(s) {\n  return `a ${s}`;\n}\n",
        1,
    );
}

#[test]
fn a_var_binds_for_its_whole_function() {
    let f = |inner: &str| {
        format!("function f(x) {{\n  var a = 1; if (x) {{ var {inner} = 2 }} return a;\n}}\n")
    };
    moves(&JAVASCRIPT, &f("a"), &f("b"), 1);
}

#[test]
fn an_array_hole_is_not_a_trailing_separator() {
    moves(
        &JAVASCRIPT,
        "function f() {\n  return [,].length;\n}\n",
        "function f() {\n  return [].length;\n}\n",
        1,
    );
    moves(
        &JAVASCRIPT,
        "function f(a) {\n  return [a,,];\n}\n",
        "function f(a) {\n  return [a,];\n}\n",
        1,
    );
    stays(
        &JAVASCRIPT,
        "function f(a) {\n  return [a,];\n}\n",
        "function f(a) {\n  return [a];\n}\n",
        1,
    );
}

#[test]
fn formatting_a_program_reads_the_same_way_stays() {
    let plain = "function f(s) {\n  g('a', a => a, 0xff);\n}\n";
    stays(
        &JAVASCRIPT,
        plain,
        "function f(s) {\n  g(\"a\", (a) => a, 0xFF)\n}\n",
        1,
    );
    stays(
        &JAVASCRIPT,
        "class A {\n  x = 1; y = 2\n}\n",
        "class A {\n  x = 1\n  y = 2\n}\n",
        1,
    );
    moves(
        &JAVASCRIPT,
        "function f() {\n  for (a;;) {}\n}\n",
        "function f() {\n  for (;a;) {}\n}\n",
        1,
    );
}

#[test]
fn a_parameter_rename_stays_for_a_span_in_the_body() {
    let f = |p: &str| format!("function f({p}) {{\n  // pin\n  return {p} + 1;\n}}\n");
    stays(&JAVASCRIPT, &f("x"), &f("y"), 2);
}

#[test]
fn the_class_heritage_reaches_a_method_body() {
    let class = |base: &str| {
        format!(
            "class A extends {base} {{\n  f() {{\n    // pin\n    return super.f();\n  }}\n}}\n"
        )
    };
    moves(&JAVASCRIPT, &class("B"), &class("C"), 3);
}

#[test]
fn jsx_text_is_read_as_jsx_reads_it() {
    let f = |text: &str| format!("function f() {{\n  return <p>{text}</p>;\n}}\n");
    stays(
        &JAVASCRIPT,
        &f("\n    a b\n    c\n  "),
        &f("\n        a b\n        c\n      "),
        1,
    );
    moves(&JAVASCRIPT, &f("a b"), &f("a  b"), 1);
}

#[test]
fn the_control_headers_around_a_span_reach_it() {
    let head = |h: &str| format!("function f(u) {{\n  {h} {{\n    // pin\n    act();\n  }}\n}}\n");
    moves(&JAVASCRIPT, &head("if (u)"), &head("if (!u)"), 3);
    moves(&JAVASCRIPT, &head("while (u)"), &head("while (!u)"), 3);
    moves(
        &JAVASCRIPT,
        &head("for (const x of u)"),
        &head("for (const x in u)"),
        3,
    );
    let caught = |p: &str| {
        format!("function f() {{\n  try {{\n    one();\n  }} catch ({p}) {{\n    // pin\n    act(e);\n  }}\n}}\n")
    };
    moves(&JAVASCRIPT, &caught("e"), &caught("{ e }"), 5);
    let case = |v: &str| {
        format!("function f(u) {{\n  switch (u) {{\n    case {v}: {{\n      // pin\n      act();\n    }}\n  }}\n}}\n")
    };
    moves(&JAVASCRIPT, &case("1"), &case("2"), 4);
}

#[test]
fn the_call_that_receives_a_closure_reaches_its_span() {
    let call = |head: &str| {
        format!("function f(xs) {{\n  return {head}((x) => {{\n    // pin\n    return x > 3;\n  }});\n}}\n")
    };
    moves(&JAVASCRIPT, &call("xs.filter"), &call("xs.some"), 3);
    moves(&JAVASCRIPT, &call("xs.filter"), &call("ys.filter"), 3);
    let effect = |deps: &str| {
        format!("function f(a, b) {{\n  useEffect(() => {{\n    // pin\n    log(a);\n  }}, {deps});\n}}\n")
    };
    moves(&JAVASCRIPT, &effect("[a, b]"), &effect("[a]"), 3);
    let fun = |n: &str| {
        format!("function f() {{\n  setTimeout(function () {{\n    // pin\n    go();\n  }}, {n});\n}}\n")
    };
    moves(&JAVASCRIPT, &fun("100"), &fun("200"), 3);
    let made = |c: &str| {
        format!(
            "function f() {{\n  return new {c}((done) => {{\n    // pin\n    done();\n  }});\n}}\n"
        )
    };
    moves(&JAVASCRIPT, &made("Promise"), &made("Later"), 3);
    let other = |then: &str| {
        format!("function f(p) {{\n  p.then((v) => {{\n    // pin\n    go(v);\n  }}, (e) => {{\n    {then}\n  }});\n}}\n")
    };
    moves(&JAVASCRIPT, &other("one(e);"), &other("two(e);"), 3);
    let jsx = |attr: &str| {
        format!("function f() {{\n  return <b {attr}={{() => {{\n    // pin\n    go();\n  }}}} />;\n}}\n")
    };
    moves(&JAVASCRIPT, &jsx("onClick"), &jsx("onDoubleClick"), 3);
}

#[test]
fn formatter_output_stays() {
    let body = |stmt: &str| format!("function f(a, b, c) {{\n  {stmt}\n}}\n");
    stays(
        &JAVASCRIPT,
        &body("return (a && b) || c;"),
        &body("return a && b || c;"),
        1,
    );
    stays(
        &JAVASCRIPT,
        &body("return (a - b) - c;"),
        &body("return a - b - c;"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return a - (b - c);"),
        &body("return a - b - c;"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return (a || b) && c;"),
        &body("return a || b && c;"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return (a?.b).c;"),
        &body("return a?.b.c;"),
        1,
    );
    stays(
        &JAVASCRIPT,
        &body("return x(.5, 1.50, 5.);"),
        &body("return x(0.5, 1.5, 5.0);"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return x(0.5);"),
        &body("return x(0.6);"),
        1,
    );
    stays(
        &JAVASCRIPT,
        &body("return 'it\\'s';"),
        &body("return \"it's\";"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return 'a\\\\';"),
        &body("return 'a\\\\\\\\';"),
        1,
    );
    moves(
        &JAVASCRIPT,
        &body("return 'its';"),
        &body("return \"it's\";"),
        1,
    );
}

#[test]
fn jsx_parentheses_stay() {
    let body = |stmt: &str| format!("function F() {{\n  {stmt}\n}}\n");
    stays(
        &JAVASCRIPT,
        &body("return (\n    <div>a</div>\n  );"),
        &body("return <div>a</div>;"),
        1,
    );
}

/// What deno fmt, Prettier and an `eslint --fix` of `no-extra-parens` or `curly` rewrite reads
/// the same in JavaScript and TypeScript, and the near edits that change the program still
/// move.
#[test]
fn redundant_parentheses_and_braces_stay() {
    let body = |b: &str| format!("function f(a, b, c, x) {{\n  {b}\n}}\n");
    let same = [
        ("return !x;", "return !(x);"),
        ("return x >= 1;", "return (x >= 1);"),
        ("const y = a + b;\n  go(y);", "const y = (a + b);\n  go(y);"),
        ("go((k) => k + a);", "go((k) => (k + a));"),
        ("go(a, b);", "go((a), b);"),
        ("if (a < b) g();", "if (a < b) { g(); }"),
        (
            "if (a) g();\n  else h();",
            "if (a) { g(); }\n  else { h(); }",
        ),
        (
            "for (const i of a) g(i);",
            "for (const i of a) {\n    g(i);\n  }",
        ),
        ("while (a) a = g();", "while (a) { a = g(); }"),
        ("if (a) if (b) g();", "if (a) { if (b) g(); }"),
        ("if (a) return;", "if (a) {\n    return;\n  }"),
        ("return a ? b : c;", "return (a ? b : c);"),
        ("const y = a ? b : c;", "const y = (a ? b : c);"),
        ("go(a ? b : c, [x]);", "go((a ? b : c), ([x]));"),
        ("return (k) => k ? a : b;", "return (k) => (k ? a : b);"),
    ];
    // Parentheses whose removal changes how the inside parses: an object read as a block with a
    // label, a sequence split into arguments or statements, an operand that a looser operator
    // or a unary takes apart, a conditional or an assignment that the operator after it
    // splits.
    let other = [
        ("go((x) => ({ id: x }));", "go((x) => { id: x });"),
        ("return () => ({});", "return () => {};"),
        ("return f(() => (a, b));", "return f(() => a, b);"),
        ("x = (a, b);", "x = a, b;"),
        ("return (a ? b : 1) + 2;", "return a ? b : 1 + 2;"),
        ("return (a = b) && c;", "return a = b && c;"),
        ("return typeof (a + b);", "return typeof a + b;"),
        ("return (a ? b : c) ? x : 1;", "return a ? b : c ? x : 1;"),
        ("return (2 ** a) ** b;", "return 2 ** a ** b;"),
        ("return -(a ** b);", "return (-a) ** b;"),
        ("return a - (b - c);", "return a - b - c;"),
        ("return (() => a) || b;", "return () => a || b;"),
        ("return (a + b) * c;", "return a + b * c;"),
        ("return !(a + b);", "return !a + b;"),
        ("go((a, b));", "go(a, b);"),
        ("return (a?.b).c;", "return a?.b.c;"),
        ("return (a?.b)();", "return a?.b();"),
        ("return new (g())();", "return new g()();"),
        ("(\"use strict\");\n  go();", "\"use strict\";\n  go();"),
        (
            "if (a) { if (b) g(); } else h();",
            "if (a) if (b) g(); else h();",
        ),
        ("if (a) { var v = g(); }", "if (a) var v = g();"),
        ("if (a) { g(); h(); }", "if (a) g(); h();"),
    ];
    for lang in [&JAVASCRIPT, &TYPESCRIPT] {
        for (bare, wrapped) in same {
            stays(lang, &body(bare), &body(wrapped), 1);
        }
        for (one, two) in other {
            moves(lang, &body(one), &body(two), 1);
        }
    }
}

/// What an `eslint --fix` of `prefer-const` or `curly` rewrites still moves a pin: the language
/// reads `const` and a declaring block otherwise.
#[test]
fn a_lint_fix_still_moves_a_pin() {
    let body = |b: &str| format!("function f(a) {{\n  {b}\n}}\n");
    for (one, two) in [
        ("let v = g(a);\n  go(v);", "const v = g(a);\n  go(v);"),
        ("if (a) { var v = g(); }", "if (a) var v = g();"),
    ] {
        moves(&TYPESCRIPT, &body(one), &body(two), 1);
    }
}