Fix inserting imports in front of inner attributes

This commit is contained in:
Lukas Wirth 2020-09-03 14:38:34 +02:00
parent c1925df7fc
commit 98e2f674e9
2 changed files with 54 additions and 3 deletions

View file

@ -348,9 +348,9 @@ use std::fmt::{Debug, nested::{self, Display}};
impl std::fmt::nested<|> for Foo {
}
",
// FIXME(veykril): self is being pulled out for some reason now
// FIXME(veykril): nested is duplicated now
r"
use std::fmt::{Debug, nested::{Display}, nested};
use std::fmt::{Debug, nested::{self, Display}, nested};
impl nested for Foo {
}
@ -518,6 +518,7 @@ fn main() {
",
r"
#![allow(dead_code)]
use std::fmt::Debug;
fn main() {

View file

@ -275,7 +275,27 @@ fn find_insert_position(
(InsertPosition::After(node.into()), AddBlankLine::BeforeTwice)
}
// there are no imports in this file at all
None => (InsertPosition::First, AddBlankLine::AfterTwice),
None => {
// check if the scope has a inner attributes, we dont want to insert in front of it
match scope
.children()
// no flat_map here cause we want to short circuit the iterator
.map(ast::Attr::cast)
.take_while(|attr| {
attr.as_ref()
.map(|attr| attr.kind() == ast::AttrKind::Inner)
.unwrap_or(false)
})
.last()
.flatten()
{
Some(attr) => (
InsertPosition::After(attr.syntax().clone().into()),
AddBlankLine::BeforeTwice,
),
None => (InsertPosition::First, AddBlankLine::AfterTwice),
}
}
},
}
}
@ -459,6 +479,36 @@ fn main() {}",
)
}
#[test]
fn insert_after_inner_attr() {
// empty files will get two trailing newlines
// this is due to the test case insert_no_imports above
check_full(
"foo::bar",
r"#![allow(unused_imports)]",
r"#![allow(unused_imports)]
use foo::bar;",
)
}
#[test]
fn insert_after_inner_attr2() {
// empty files will get two trailing newlines
// this is due to the test case insert_no_imports above
check_full(
"foo::bar",
r"#![allow(unused_imports)]
fn main() {}",
r"#![allow(unused_imports)]
use foo::bar;
fn main() {}",
)
}
#[test]
fn merge_groups() {
check_last("std::io", r"use std::fmt;", r"use std::{fmt, io};")