biomejs / biome

A toolchain for web projects, aimed to provide functionalities to maintain them. Biome offers formatter and linter, usable via CLI and LSP.

Home Page:https://biomejs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› `rename_node_declaration` have error case for `JsShorthandPropertyObjectMember`

fireairforce opened this issue Β· comments

Environment information

Not related.

What happened?

I have a source code look like:

type A = { color: number; }

const color: number = 1;
const a: A = { color } as A;

So i want to use rename the JSBinding as follow code:

use biome_js_parser::parse;
use biome_js_semantic::{semantic_model, SemanticModelOptions};

pub fn main() {
  let r = parse(source, JsFileSource::tsx(), biome_js_parser::JsParserOptions::default());
 let root = r.tree();
 let model = semantic_model(&root, SemanticModelOptions::default());
 let mut mutation = root.clone().begin();
 let declarations = root.syntax().descendants().filter_map(AnyJsIdentifierBinding::cast);
    for binding in declarations {
        let new_name = format!("{}_new", binding.text());
        mutation.rename_node_declaration(&model, binding, &new_name, source);
    }

    let res = mutation.commit();
    let root = AnyJsRoot::cast(res).unwrap();

    println(root.toString())
}

If i input my source code into the functon, i will get an output look like:

type A_new = { color: number; }

const color_new: number = 1;
const a_new: A_new = { color_new } as A_new;

It looks obviously the output of JsShorthandPropertyObjectMember wrong here, so i just want to ask if there are any way to fix it correctly?

Expected result

Maybe we can get output like:

type A_new = { color: number; }

const color_new: number = 1;
const a_new: A_new = { color: color_new } as A_new;

At least the output is correct.

Code of Conduct

  • I agree to follow Biome's Code of Conduct

It's probably a bug.

The rename functionality was introduced years ago as an experiment of the semantic model, but quickly realised that it would be useless since it doesn't work across files. It's possible something regressed in the last refactors of the semantic model

Feel free to send a PR to fix it