дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
fix(editor): keep Rearrange Code from moving a generator under a field without a semicolon (#8039)
Rearrange Code could move a method starting with *, in or instanceof to the top of a class right below a field with no semicolon, where it continued the field's initializer (x = 1 then *gen() {} reads as 1 * gen()). The same happened when the only semicolon ending the field sat on a comment line that moved with the method. Such classes are now left alone; a semicolon or static block above, or a first method that stays first, still lets the class rearrange.
Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
8fefbc1c82
коміт
40fb4923e6
1 file changed
+34
@@ -428,6 +428,17 @@ fn script_class_change(body: Node, text: &str) -> Option<Change> {
|
||||
return None;
|
||||
}
|
||||
let ranges: Vec<Range<usize>> = blocks.into_iter().map(|b| b.3).collect();
|
||||
// A method moved to the top would continue a field left open above it when it starts with an
|
||||
// operator: `x = 1` then `*gen() {}` reads as `1 * gen()`.
|
||||
let children: Vec<Node> = body.children(&mut body.walk()).filter(|n| n.kind() != "comment").collect();
|
||||
let top = children.iter().find(|n| n.start_byte() >= ranges[order[0]].start);
|
||||
let above = children.iter().rev().find(|n| n.end_byte() <= ranges[0].start);
|
||||
if order[0] != 0
|
||||
&& top.is_some_and(|n| n.child(0).is_some_and(|c| matches!(&text[c.byte_range()], "*" | "in" | "instanceof")))
|
||||
&& !above.is_some_and(|n| n.kind() == "class_static_block" || text[n.byte_range()].ends_with(['{', ';']))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
// Whole lines move: what shares the last method's last line (the closing brace, a comment
|
||||
// running on below it) stays at the end, so the last method must too.
|
||||
let end = ranges.last()?.end;
|
||||
@@ -664,10 +675,18 @@ mod tests {
|
||||
"const A = class {\n b() {}\n constructor() {} };",
|
||||
// So would the first line of a comment running on below it.
|
||||
"class A {\n b() {}\n constructor() {}; /* note\n */\n}\n",
|
||||
// A generator moved below a field without a semicolon would continue its initializer,
|
||||
// and so would one moved above the semicolon ending it.
|
||||
"class A {\n x = 1\n #p() {}\n *gen() {}\n}\n",
|
||||
"class A {\n x = {}\n #p() {}\n *gen() {}\n}\n",
|
||||
"class A {\n x = 1\n #p() {}\n in(a) {}\n}\n",
|
||||
"class A {\n x = 1\n #p() {}\n instanceof(a) {}\n}\n",
|
||||
"class A {\n x = 1\n /* c */ ;\n #p() {}\n *gen() {}\n}\n",
|
||||
// A syntax error anywhere in the file.
|
||||
"class A {\n b() {}\n constructor() {}\n}\nlet = ;\n",
|
||||
] {
|
||||
assert_eq!(script("typescript", text), None, "{text}");
|
||||
assert_eq!(script("javascript", text), None, "{text}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -696,6 +715,21 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_generator_moves_to_the_top_only_below_a_closed_member() {
|
||||
let text = "class A {\n x = 1;\n #p() {}\n *gen() {}\n}\n";
|
||||
let expected = "class A {\n x = 1;\n *gen() {}\n #p() {}\n}\n";
|
||||
assert_eq!(script("javascript", text).as_deref(), Some(expected));
|
||||
let text = "class A {\n static {}\n #p() {}\n *gen() {}\n}\n";
|
||||
let expected = "class A {\n static {}\n *gen() {}\n #p() {}\n}\n";
|
||||
assert_eq!(script("javascript", text).as_deref(), Some(expected));
|
||||
// The first method stays first, so the field's line ending is untouched.
|
||||
let text = "class A {\n x = 1\n /* c */ ;\n constructor() {}\n #p() {}\n *gen() {}\n}\n";
|
||||
let expected = "class A {\n x = 1\n /* c */ ;\n constructor() {}\n *gen() {}\n #p() {}\n}\n";
|
||||
assert_eq!(script("typescript", text).as_deref(), Some(expected));
|
||||
assert_eq!(script("javascript", text).as_deref(), Some(expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn static_and_instance_methods_with_one_name_stay_separate() {
|
||||
let text = "class A {\n of() {}\n static of() {}\n}\n";
|
||||
|
||||
Reference in new issue
Заблокувати користувача