fix: Fix imports being inserted before doc comments in inline modules

This commit is contained in:
Lukas Wirth 2022-07-14 22:56:56 +02:00
parent 5af3ef527c
commit 6b823b0234
2 changed files with 37 additions and 24 deletions

View File

@ -396,10 +396,19 @@ fn insert_use_(
} }
} }
let l_curly = match scope {
ImportScope::File(_) => None,
// don't insert the imports before the item list/block expr's opening curly brace
ImportScope::Module(item_list) => item_list.l_curly_token(),
// don't insert the imports before the item list's opening curly brace
ImportScope::Block(block) => block.l_curly_token(),
};
// there are no imports in this file at all // there are no imports in this file at all
// so put the import after all inner module attributes and possible license header comments // so put the import after all inner module attributes and possible license header comments
if let Some(last_inner_element) = scope_syntax if let Some(last_inner_element) = scope_syntax
.children_with_tokens() .children_with_tokens()
// skip the curly brace
.skip(l_curly.is_some() as usize)
.take_while(|child| match child { .take_while(|child| match child {
NodeOrToken::Node(node) => is_inner_attribute(node.clone()), NodeOrToken::Node(node) => is_inner_attribute(node.clone()),
NodeOrToken::Token(token) => { NodeOrToken::Token(token) => {
@ -413,20 +422,7 @@ fn insert_use_(
cov_mark::hit!(insert_empty_inner_attr); cov_mark::hit!(insert_empty_inner_attr);
ted::insert(ted::Position::after(&last_inner_element), use_item.syntax()); ted::insert(ted::Position::after(&last_inner_element), use_item.syntax());
ted::insert(ted::Position::after(last_inner_element), make::tokens::single_newline()); ted::insert(ted::Position::after(last_inner_element), make::tokens::single_newline());
return; } else {
}
let l_curly = match scope {
ImportScope::File(_) => {
cov_mark::hit!(insert_empty_file);
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line());
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax());
return;
}
// don't insert the imports before the item list/block expr's opening curly brace
ImportScope::Module(item_list) => item_list.l_curly_token(),
// don't insert the imports before the item list's opening curly brace
ImportScope::Block(block) => block.l_curly_token(),
};
match l_curly { match l_curly {
Some(b) => { Some(b) => {
cov_mark::hit!(insert_empty_module); cov_mark::hit!(insert_empty_module);
@ -434,12 +430,16 @@ fn insert_use_(
ted::insert(ted::Position::after(&b), use_item.syntax()); ted::insert(ted::Position::after(&b), use_item.syntax());
} }
None => { None => {
// This should never happens, broken module syntax node cov_mark::hit!(insert_empty_file);
ted::insert(ted::Position::first_child_of(scope_syntax), make::tokens::blank_line()); ted::insert(
ted::Position::first_child_of(scope_syntax),
make::tokens::blank_line(),
);
ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax()); ted::insert(ted::Position::first_child_of(scope_syntax), use_item.syntax());
} }
} }
} }
}
fn is_inner_attribute(node: SyntaxNode) -> bool { fn is_inner_attribute(node: SyntaxNode) -> bool {
ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner) ast::Attr::cast(node).map(|attr| attr.kind()) == Some(ast::AttrKind::Inner)

View File

@ -441,6 +441,19 @@ fn inserts_after_single_line_inner_comments() {
use foo::bar::Baz;"#, use foo::bar::Baz;"#,
); );
check_none(
"foo::bar::Baz",
r"mod foo {
//! Single line inner comments do not allow any code before them.
$0
}",
r"mod foo {
//! Single line inner comments do not allow any code before them.
use foo::bar::Baz;
}",
);
} }
#[test] #[test]