fix symbol rename for unlabeled arg (#7244)

Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
Jess Frazelle
2025-05-28 12:50:18 -07:00
committed by GitHub
parent 355a450c09
commit aaff027830

View File

@ -1950,6 +1950,10 @@ impl CallExpressionKw {
}
pub fn replace_value(&mut self, source_range: SourceRange, new_value: Expr) {
if let Some(unlabeled) = &mut self.unlabeled {
unlabeled.replace_value(source_range, new_value.clone());
}
for arg in &mut self.arguments {
arg.arg.replace_value(source_range, new_value.clone());
}
@ -1959,6 +1963,10 @@ impl CallExpressionKw {
fn rename_identifiers(&mut self, old_name: &str, new_name: &str) {
self.callee.rename(old_name, new_name);
if let Some(unlabeled) = &mut self.unlabeled {
unlabeled.rename_identifiers(old_name, new_name);
}
for arg in &mut self.arguments {
arg.arg.rename_identifiers(old_name, new_name);
}
@ -4343,7 +4351,7 @@ startSketchOn(XY)
}
#[test]
fn module_name() {
fn test_module_name() {
#[track_caller]
fn assert_mod_name(stmt: &str, name: &str) {
let tokens = crate::parsing::token::lex(stmt, ModuleId::default()).unwrap();
@ -4357,4 +4365,36 @@ startSketchOn(XY)
assert_mod_name("import 'foo/main.kcl'", "foo");
assert_mod_name("import 'foo\\bar\\main.kcl'", "bar");
}
#[test]
fn test_rename_in_math_in_std_function() {
let code = r#"rise = 4.5
run = 8
angle = atan(rise / run)"#;
let mut program = crate::parsing::top_level_parse(code).unwrap();
// We want to rename `run` to `run2`.
let run = program.body.get(1).unwrap().clone();
let BodyItem::VariableDeclaration(var_decl) = &run else {
panic!("expected a variable declaration")
};
let Expr::Literal(lit) = &var_decl.declaration.init else {
panic!("expected a literal");
};
assert_eq!(lit.raw, "8");
// Rename it.
program.rename_symbol("yoyo", var_decl.as_source_range().start() + 1);
// Recast the program to a string.
let formatted = program.recast(&Default::default(), 0);
assert_eq!(
formatted,
r#"rise = 4.5
yoyo = 8
angle = atan(rise / yoyo)
"#
);
}
}