fix extra spacing (#1149)

This commit is contained in:
Roman Krasiuk 2022-04-16 23:09:00 +03:00 committed by GitHub
parent a99dd1328b
commit 8dd553a5eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 1 deletions

View File

@ -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)
}

View File

@ -32,6 +32,9 @@ pub static RE_SOL_PRAGMA_VERSION: Lazy<Regex> =
pub static RE_SOL_SDPX_LICENSE_IDENTIFIER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"///?\s*SPDX-License-Identifier:\s*(?P<license>.+)").unwrap());
/// A regex used to remove extra lines in flatenned files
pub static RE_THREE_OR_MORE_NEWLINES: Lazy<Regex> = 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"`.
///

View File

@ -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 { }"#
);
}