#![allow(clippy::literal_string_with_formatting_args)]
use super::*;
struct Case {
path: &'static str,
source: &'static str,
line: i64,
kind: &'static str,
expected: Outcome,
}
enum Outcome {
Applied(&'static str),
Refused(&'static str),
}
use Outcome::{Applied, Refused};
fn run_case(case: &Case) {
let dir = tempdir().unwrap();
let path = dir.path().join(case.path);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(&path, case.source).unwrap();
let decoy_path = case.path.replace("main", "decoy");
let decoy_source = match case.kind {
"Type" if case.path.ends_with(".rs") => "pub struct Input;\n",
"Type" => "export type Input = string;\n",
_ if case.path.ends_with(".rs") => "pub fn helper() -> i32 { 9 }\n",
_ if case.path.ends_with(".py") => "def helper():\n return 9\n",
_ => "export const helper = () => 9;\n",
};
fs::write(dir.path().join(&decoy_path), decoy_source).unwrap();
let capability = build_index(dir.path());
let name = if case.kind == "Type" {
"Input"
} else {
"helper"
};
let symbol = dict(&[
("name", vm_string(name)),
("path", vm_string(case.path)),
("line", VmValue::Int(case.line)),
("kind", vm_string(case.kind)),
]);
let result = rename(&capability, symbol, "renamed", "workspace");
let after = fs::read_to_string(&path).unwrap();
match case.expected {
Applied(expected) => {
assert_eq!(
s(field(&result, "result")),
"applied",
"{}: {result:?}",
case.source
);
assert_eq!(after, expected, "{}", case.source);
}
Refused(detail) => {
assert_eq!(
s(field(&result, "result")),
"ambiguous_symbol",
"{}: {result:?}",
case.source
);
assert!(
s(field(&result, "details")).contains(detail),
"{}: {result:?}",
case.source
);
assert_eq!(after, case.source);
}
}
assert_eq!(
fs::read_to_string(dir.path().join(&decoy_path)).unwrap(),
decoy_source
);
}
#[test]
fn a_body_local_seed_renames_only_its_own_scope() {
let cases = [
Case {
path: "main.test.ts",
source: "export const helper = (n: number) => n;\ndescribe('a', () => {\n const helper = (input: number) => input + 1;\n const pick = { helper: 1 };\n type Out = ReturnType<typeof helper>;\n expect(helper(1)).toBe(pick.helper);\n});\ndescribe('b', () => {\n const helper = (input: number) => input * 2;\n helper(3);\n});\nexport const used = helper(0);\n",
line: 3,
kind: "Function",
expected: Applied("export const helper = (n: number) => n;\ndescribe('a', () => {\n const renamed = (input: number) => input + 1;\n const pick = { helper: 1 };\n type Out = ReturnType<typeof renamed>;\n expect(renamed(1)).toBe(pick.helper);\n});\ndescribe('b', () => {\n const helper = (input: number) => input * 2;\n helper(3);\n});\nexport const used = helper(0);\n"),
},
Case {
path: "main.ts",
source: "export function helper() { return 0; }\nexport function outer() {\n function helper() { return 1; }\n return helper();\n}\n",
line: 3,
kind: "Function",
expected: Applied("export function helper() { return 0; }\nexport function outer() {\n function renamed() { return 1; }\n return renamed();\n}\n"),
},
Case {
path: "main.ts",
source: "type Input = string;\nexport function a() {\n type Input = number;\n const x: Input = 1;\n return x;\n}\nexport function b() {\n type Input = boolean;\n const y: Input = true;\n return y;\n}\n",
line: 3,
kind: "Type",
expected: Applied("type Input = string;\nexport function a() {\n type renamed = number;\n const x: renamed = 1;\n return x;\n}\nexport function b() {\n type Input = boolean;\n const y: Input = true;\n return y;\n}\n"),
},
Case {
path: "src/main.rs",
source: "pub fn helper() -> i32 { 0 }\npub struct Point { pub helper: i32 }\npub fn outer(p: Point) -> i32 {\n fn helper() -> i32 { 1 }\n helper() + p.helper\n}\npub fn other() -> i32 { helper() }\n",
line: 4,
kind: "Function",
expected: Applied("pub fn helper() -> i32 { 0 }\npub struct Point { pub helper: i32 }\npub fn outer(p: Point) -> i32 {\n fn renamed() -> i32 { 1 }\n renamed() + p.helper\n}\npub fn other() -> i32 { helper() }\n"),
},
Case {
path: "src/main.rs",
source: "pub fn helper() -> i32 { 0 }\n#[cfg(test)]\nmod tests {\n fn helper() -> i32 { 2 }\n #[test]\n fn uses() {\n assert_eq!(helper(), 2);\n }\n}\n",
line: 4,
kind: "Function",
expected: Applied("pub fn helper() -> i32 { 0 }\n#[cfg(test)]\nmod tests {\n fn renamed() -> i32 { 2 }\n #[test]\n fn uses() {\n assert_eq!(renamed(), 2);\n }\n}\n"),
},
Case {
path: "main.py",
source: "def helper():\n return 0\n\n\ndef outer(value, obj, call):\n if value:\n def helper():\n return value\n return helper() + obj.helper + call(helper=1)\n",
line: 7,
kind: "Function",
expected: Applied("def helper():\n return 0\n\n\ndef outer(value, obj, call):\n if value:\n def renamed():\n return value\n return renamed() + obj.helper + call(helper=1)\n"),
},
];
for case in &cases {
run_case(case);
}
}
#[test]
fn a_body_local_seed_refuses_a_shadowing_or_unbound_spelling_in_its_scope() {
let cases = [
Case {
path: "main.ts",
source: "export function outer() {\n const helper = (n: number) => n;\n return [1].map((helper) => helper);\n}\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "main.ts",
source: "export function outer() {\n const helper = (n: number) => n;\n return { helper };\n}\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "main.js",
source: "export function outer(flag) {\n if (flag) {\n function helper() { return 1; }\n }\n return helper();\n}\n",
line: 3,
kind: "Function",
expected: Refused("separate declarations"),
},
Case {
path: "main.ts",
source: "export function outer(helper: number) {\n function helper() { return 1; }\n return helper();\n}\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "main.ts",
source: "namespace Lab {\n const helper = () => 1;\n export const used = helper();\n}\n",
line: 2,
kind: "Function",
expected: Refused("separate declarations"),
},
Case {
path: "main.ts",
source: "export function a() {\n type Input = number;\n function g<Input>(x: Input): Input { return x; }\n const y: Input = 1;\n return g(y);\n}\n",
line: 2,
kind: "Type",
expected: Refused("unresolved binding"),
},
Case {
path: "src/main.rs",
source: "pub fn outer() -> i32 {\n fn helper() -> i32 { 1 }\n let helper = 2;\n helper\n}\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "src/main.rs",
source: "pub fn outer() -> i32 {\n fn helper() -> i32 { 1 }\n {\n my_macros::declare!();\n helper()\n }\n}\n",
line: 2,
kind: "Function",
expected: Refused("is incomplete (dynamic_name_access)"),
},
Case {
path: "src/main.rs",
source: "mod tests {\n fn helper() -> i32 { 2 }\n mod child;\n pub fn uses() -> i32 { helper() }\n}\n",
line: 2,
kind: "Function",
expected: Refused("may reference it"),
},
Case {
path: "src/main.rs",
source: "pub fn helper() -> i32 { 0 }\npub mod tests {\n pub fn helper() -> i32 { 2 }\n pub fn uses() -> i32 { helper() }\n}\n",
line: 3,
kind: "Function",
expected: Refused("separate declarations"),
},
Case {
path: "main.py",
source: "def outer():\n def helper():\n return 1\n def inner():\n nonlocal helper\n helper = None\n return helper()\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "main.py",
source: "def outer(meta):\n def helper():\n return 1\n class Inner(metaclass=meta):\n value = helper()\n return Inner\n",
line: 2,
kind: "Function",
expected: Refused("unresolved binding"),
},
Case {
path: "main.py",
source: "def outer():\n class Inner:\n def helper(self):\n return 1\n return Inner().helper()\n",
line: 3,
kind: "Function",
expected: Refused("separate declarations"),
},
Case {
path: "main.ts",
source: "export function outer() {\n const helper = () => 3;\n enum E { helper = 2, double = helper * 2 }\n return helper() + E.double;\n}\n",
line: 2,
kind: "Function",
expected: Refused("declares a member of that name"),
},
Case {
path: "main.ts",
source: "export function outer() {\n const helper = () => 3;\n enum E { 'helper' = 2, double = helper * 2 }\n return helper() + E.double;\n}\n",
line: 2,
kind: "Function",
expected: Refused("declares a member of that name"),
},
Case {
path: "src/main.rs",
source: "macro_rules! make { () => { Input } }\npub fn outer() {\n struct Input;\n let _v = make!();\n}\n",
line: 3,
kind: "Type",
expected: Refused("may expand to it"),
},
Case {
path: "src/main.rs",
source: "macro_rules! make { () => { Input } }\npub fn outer() {\n struct Input;\n let _v = vec![make!()];\n}\n",
line: 3,
kind: "Type",
expected: Refused("may expand to it"),
},
Case {
path: "src/main.rs",
source: "pub fn outer() -> String {\n fn helper() -> i32 { 1 }\n let _ = helper();\n format!(\"{helper:p}\")\n}\n",
line: 2,
kind: "Function",
expected: Refused("may capture `helper` implicitly"),
},
];
for case in &cases {
run_case(case);
}
}
#[test]
fn a_file_level_rust_seed_refuses_an_implicit_format_capture() {
let dir = tempdir().unwrap();
let path = dir.path().join("src/main.rs");
fs::create_dir_all(path.parent().unwrap()).unwrap();
let source = "pub fn helper() -> i32 { 1 }\npub fn show() -> String {\n let _ = helper();\n format!(\"{helper:p}\")\n}\n";
fs::write(&path, source).unwrap();
let capability = build_index(dir.path());
let symbol = dict(&[
("name", vm_string("helper")),
("path", vm_string("src/main.rs")),
("kind", vm_string("Function")),
]);
let result = rename(&capability, symbol, "renamed", "workspace");
assert_eq!(
s(field(&result, "result")),
"ambiguous_symbol",
"{result:?}"
);
assert!(
s(field(&result, "details")).contains("may capture `helper` implicitly"),
"{result:?}"
);
assert_eq!(fs::read_to_string(&path).unwrap(), source);
let plain = source.replace("format!(\"{helper:p}\")", "String::new()");
fs::write(&path, &plain).unwrap();
let capability = build_index(dir.path());
let symbol = dict(&[
("name", vm_string("helper")),
("path", vm_string("src/main.rs")),
("kind", vm_string("Function")),
]);
let result = rename(&capability, symbol, "renamed", "workspace");
assert_eq!(s(field(&result, "result")), "applied", "{result:?}");
}