Format modules defined in cfg_attr (#3604)

This commit is contained in:
Seiichi Uchida 2019-06-09 09:20:39 +09:00 committed by GitHub
parent e71bffb008
commit 47a11cd516
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 415 additions and 66 deletions

View file

@ -5,6 +5,8 @@ use syntax::symbol::kw;
use syntax::visit::Visitor;
use syntax_pos::Symbol;
use crate::attr::MetaVisitor;
pub(crate) struct ModItem {
pub(crate) item: ast::Item,
}
@ -103,3 +105,40 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
Ok(())
}
}
/// Extracts `path = "foo.rs"` from attributes.
#[derive(Default)]
pub(crate) struct PathVisitor {
/// A list of path defined in attributes.
paths: Vec<String>,
}
impl PathVisitor {
pub(crate) fn paths(self) -> Vec<String> {
self.paths
}
}
impl<'ast> MetaVisitor<'ast> for PathVisitor {
fn visit_meta_name_value(&mut self, meta_item: &'ast ast::MetaItem, lit: &'ast ast::Lit) {
if meta_item.check_name(Symbol::intern("path")) && lit.node.is_str() {
self.paths.push(lit_to_str(lit));
}
}
}
#[cfg(not(windows))]
fn lit_to_str(lit: &ast::Lit) -> String {
match lit.node {
ast::LitKind::Str(symbol, ..) => symbol.to_string(),
_ => unreachable!(),
}
}
#[cfg(windows)]
fn lit_to_str(lit: &ast::Lit) -> String {
match lit.node {
ast::LitKind::Str(symbol, ..) => symbol.as_str().replace("/", "\\"),
_ => unreachable!(),
}
}