Skip to content

Commit 1ee32ff

Browse files
committed
don't try to unwrap functions with comments in
1 parent a85e024 commit 1ee32ff

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

compiler-core/src/language_server/code_action.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,6 +7714,15 @@ impl<'a> UnwrapAnonymousFunction<'a> {
77147714
_ => return,
77157715
}
77167716

7717+
// We can't apply to functions with comments in (yet)
7718+
if self
7719+
.module
7720+
.extra
7721+
.has_comment_between(location.start, location.end)
7722+
{
7723+
return;
7724+
}
7725+
77177726
// We can only apply to anonymous functions containing a single function call
77187727
let [
77197728
TypedStatement::Expression(TypedExpr::Call {

compiler-core/src/language_server/tests/action.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9395,7 +9395,43 @@ fn op(first a, second b) {
93959395
);
93969396
}
93979397

9398+
#[test]
9399+
fn dont_unwrap_anonymous_function_with_comment_after() {
9400+
assert_no_code_actions!(
9401+
UNWRAP_ANONYMOUS_FUNCTION,
9402+
"pub fn main() {
9403+
fn(a) {
9404+
op(a)
9405+
// look out!
9406+
}
9407+
}
9408+
9409+
fn op(a) {
9410+
todo
9411+
}
9412+
",
9413+
find_position_of("fn(a)").to_selection()
9414+
);
9415+
}
93989416

9417+
#[test]
9418+
fn dont_unwrap_anonymous_function_with_comment_before() {
9419+
assert_no_code_actions!(
9420+
UNWRAP_ANONYMOUS_FUNCTION,
9421+
"pub fn main() {
9422+
fn(a) {
9423+
// look out!
9424+
op(a)
9425+
}
9426+
}
9427+
9428+
fn op(a) {
9429+
todo
9430+
}
9431+
",
9432+
find_position_of("fn(a)").to_selection()
9433+
);
9434+
}
93999435

94009436
#[test]
94019437
fn unwrap_anonymous_function_unavailable_when_args_discarded() {

compiler-core/src/parse/extra.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl ModuleExtra {
4040
self.first_comment_between(start, end).is_some()
4141
}
4242

43+
// TODO: this won't necessarily find the first comment, just any comment
4344
pub(crate) fn first_comment_between(&self, start: u32, end: u32) -> Option<SrcSpan> {
4445
self.comments
4546
.binary_search_by(|comment| {

0 commit comments

Comments
 (0)