From bc12c84d2fea026bc35fb895787444a86ed8ab84 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Thu, 15 Feb 2018 22:48:06 +0900 Subject: [PATCH] Do not try to parse external modules when skipping children --- rustfmt-core/src/lib.rs | 15 +++++++++++---- .../tests/source/configs/skip_children/true.rs | 3 +++ .../tests/target/configs/skip_children/true.rs | 3 +++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 rustfmt-core/tests/source/configs/skip_children/true.rs create mode 100644 rustfmt-core/tests/target/configs/skip_children/true.rs diff --git a/rustfmt-core/src/lib.rs b/rustfmt-core/src/lib.rs index 092aeb7d088c..3e82df9088c1 100644 --- a/rustfmt-core/src/lib.rs +++ b/rustfmt-core/src/lib.rs @@ -507,14 +507,18 @@ fn format_lines( report.file_error_map.insert(name.clone(), errors); } -fn parse_input( +fn parse_input<'sess>( input: Input, - parse_session: &ParseSess, -) -> Result> { + parse_session: &'sess ParseSess, + config: &Config, +) -> Result>> { let result = match input { Input::File(file) => { let mut parser = parse::new_parser_from_file(parse_session, &file); parser.cfg_mods = false; + if config.skip_children() { + parser.recurse_into_file_modules = false; + } parser.parse_crate_mod() } Input::Text(text) => { @@ -524,6 +528,9 @@ fn parse_input( text, ); parser.cfg_mods = false; + if config.skip_children() { + parser.recurse_into_file_modules = false; + } parser.parse_crate_mod() } }; @@ -647,7 +654,7 @@ pub fn format_input( Input::Text(..) => FileName::Custom("stdin".to_owned()), }; - let krate = match parse_input(input, &parse_session) { + let krate = match parse_input(input, &parse_session, config) { Ok(krate) => krate, Err(diagnostic) => { if let Some(mut diagnostic) = diagnostic { diff --git a/rustfmt-core/tests/source/configs/skip_children/true.rs b/rustfmt-core/tests/source/configs/skip_children/true.rs new file mode 100644 index 000000000000..cb2b6fce2d2b --- /dev/null +++ b/rustfmt-core/tests/source/configs/skip_children/true.rs @@ -0,0 +1,3 @@ +// rustfmt-skip_children: true + +mod foo ; diff --git a/rustfmt-core/tests/target/configs/skip_children/true.rs b/rustfmt-core/tests/target/configs/skip_children/true.rs new file mode 100644 index 000000000000..3ea9f46fdf24 --- /dev/null +++ b/rustfmt-core/tests/target/configs/skip_children/true.rs @@ -0,0 +1,3 @@ +// rustfmt-skip_children: true + +mod foo;