From 8dd553a5ebbc19846fcd2d5a037c4f044f51c430 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Sat, 16 Apr 2022 23:09:00 +0300 Subject: [PATCH] fix extra spacing (#1149) --- ethers-solc/src/config.rs | 3 +- ethers-solc/src/utils.rs | 3 ++ ethers-solc/tests/project.rs | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/ethers-solc/src/config.rs b/ethers-solc/src/config.rs index d39db454..3b67de83 100644 --- a/ethers-solc/src/config.rs +++ b/ethers-solc/src/config.rs @@ -311,7 +311,7 @@ impl ProjectPathsConfig { for import in imports.iter() { let import_path = self.resolve_import(target_dir, import.data())?; let s = self.flatten_node(&import_path, graph, imported, true, true, true)?; - let import_content = s.trim().as_bytes(); + let import_content = s.as_bytes(); let import_content_len = import_content.len() as isize; let (start, end) = import.loc_by_offset(offset); content.splice(start..end, import_content.iter().copied()); @@ -321,6 +321,7 @@ impl ProjectPathsConfig { let result = String::from_utf8(content).map_err(|err| { SolcError::msg(format!("failed to convert extended bytes to string: {}", err)) })?; + let result = utils::RE_THREE_OR_MORE_NEWLINES.replace_all(&result, "\n\n").into_owned(); Ok(result) } diff --git a/ethers-solc/src/utils.rs b/ethers-solc/src/utils.rs index cbc42d51..bd354228 100644 --- a/ethers-solc/src/utils.rs +++ b/ethers-solc/src/utils.rs @@ -32,6 +32,9 @@ pub static RE_SOL_PRAGMA_VERSION: Lazy = pub static RE_SOL_SDPX_LICENSE_IDENTIFIER: Lazy = Lazy::new(|| Regex::new(r"///?\s*SPDX-License-Identifier:\s*(?P.+)").unwrap()); +/// A regex used to remove extra lines in flatenned files +pub static RE_THREE_OR_MORE_NEWLINES: Lazy = Lazy::new(|| Regex::new("\n{3,}").unwrap()); + /// Returns all path parts from any solidity import statement in a string, /// `import "./contracts/Contract.sol";` -> `"./contracts/Contract.sol"`. /// diff --git a/ethers-solc/tests/project.rs b/ethers-solc/tests/project.rs index 86939884..2b360c9e 100644 --- a/ethers-solc/tests/project.rs +++ b/ethers-solc/tests/project.rs @@ -492,8 +492,11 @@ contract C { } result, r#" pragma solidity ^0.8.10; + contract C { } + contract B { } + contract A { } "# ); @@ -547,8 +550,11 @@ contract C { } r#" pragma solidity ^0.8.10; pragma experimental ABIEncoderV2; + contract C { } + contract B { } + contract A { } "# ); @@ -572,6 +578,7 @@ fn can_flatten_file_with_duplicates() { pragma solidity >=0.6.0; contract Bar {} + contract Foo {} contract FooBar {} @@ -598,6 +605,7 @@ fn can_flatten_on_solang_failure() { pragma solidity ^0.8.10; library Lib {} + // Intentionally erroneous code contract Contract { failure(); @@ -650,9 +658,64 @@ contract C { } assert_eq!( result.trim(), r#"pragma solidity ^0.8.10; + contract C { } + error IllegalArgument(); error IllegalState(); + +contract A { }"# + ); +} + +#[test] +fn can_flatten_remove_extra_spacing() { + let project = TempProject::dapptools().unwrap(); + + let f = project + .add_source( + "A", + r#"pragma solidity ^0.8.10; +import "./C.sol"; +import "./B.sol"; +contract A { } +"#, + ) + .unwrap(); + + project + .add_source( + "B", + r#"// This is a B Contract +pragma solidity ^0.8.10; + +import "./C.sol"; + +contract B { } +"#, + ) + .unwrap(); + + project + .add_source( + "C", + r#"pragma solidity ^0.8.10; +contract C { } +"#, + ) + .unwrap(); + + let result = project.flatten(&f).unwrap(); + assert_eq!( + result.trim(), + r#"pragma solidity ^0.8.10; + +contract C { } + +// This is a B Contract + +contract B { } + contract A { }"# ); }