prevent .._ when rewrites spread bindings

This commit is contained in:
Hong Jiarong 2025-11-16 01:29:59 +08:00
parent 2c7aedb9e9
commit f527c3509f
2 changed files with 24 additions and 19 deletions

View file

@ -91,7 +91,7 @@ impl<'a> CodeActionWorker<'a> {
self.autofix_remove_unused_import(root, &diag_range);
} else {
self.autofix_unused_symbol(&diag_range);
self.autofix_replace_with_placeholder(&diag_range);
self.autofix_replace_with_placeholder(root, &diag_range);
self.autofix_remove_declaration(root, &diag_range);
}
}
@ -310,8 +310,12 @@ impl<'a> CodeActionWorker<'a> {
Some(())
}
fn autofix_replace_with_placeholder(&mut self, range: &Range<usize>) -> Option<()> {
if range.is_empty() {
fn autofix_replace_with_placeholder(
&mut self,
root: &LinkedNode<'_>,
range: &Range<usize>,
) -> Option<()> {
if range.is_empty() || self.is_spread_binding(root, range) {
return None;
}
@ -336,6 +340,23 @@ impl<'a> CodeActionWorker<'a> {
Some(())
}
fn is_spread_binding(&self, root: &LinkedNode<'_>, range: &Range<usize>) -> bool {
if range.is_empty() {
return false;
}
let cursor = (range.start + range.end) / 2;
let Some(node) = root.leaf_at_compat(cursor) else {
return false;
};
if node.kind() == SyntaxKind::Spread {
return true;
}
node_ancestors(&node).any(|ancestor| ancestor.kind() == SyntaxKind::Spread)
}
/// Remove the declaration corresponding to an unused binding.
fn autofix_remove_declaration(
&mut self,

View file

@ -1,6 +1,5 @@
---
source: crates/tinymist-query/src/code_action.rs
assertion_line: 180
description: Dead code code actions in /dummy-root/s0.typ
expression: "JsonRepr::new_pure(ordered_entries)"
input_file: crates/tinymist-query/src/fixtures/dead_code/destructuring_spread.typ
@ -22,21 +21,6 @@ input_file: crates/tinymist-query/src/fixtures/dead_code/destructuring_spread.ty
},
"kind": "quickfix",
"title": "Prefix `_` to `unused_rest`"
},
{
"edit": {
"changes": {
"s0.typ": [
{
"insertTextFormat": 1,
"newText": "_",
"range": "4:11:4:22"
}
]
}
},
"kind": "quickfix",
"title": "Replace with `_`"
}
],
"message": "unused variable: `unused_rest`\nHint: consider removing this variable or prefixing with underscore: `_unused_rest`",