fix(solc): flatten import loc (#946)

* protograva test-data

* revert import parsing change

* compare flatten results against full strings
This commit is contained in:
Roman Krasiuk 2022-02-22 06:22:24 -08:00 committed by GitHub
parent ffb8582dd5
commit 1f822e47e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 13 deletions

View File

@ -278,8 +278,11 @@ impl ProjectPathsConfig {
for import in imports.iter() {
let import_path = self.resolve_import(target_dir, import.data())?;
let import_content = self.flatten_node(&import_path, graph, imported, true, true)?;
let import_content = import_content.trim().as_bytes().to_owned();
let import_content = self
.flatten_node(&import_path, graph, imported, true, true)?
.trim()
.as_bytes()
.to_owned();
let import_content_len = import_content.len() as isize;
let (start, end) = import.loc_by_offset(offset);
content.splice(start..end, import_content);

View File

@ -807,9 +807,13 @@ fn parse_data(content: &str, file: &Path) -> SolData {
.map(|(cap, name)| {
SolDataUnit::new(name.as_str().to_owned(), cap.to_owned().into())
});
imports = utils::find_import_paths(content)
.map(|m| SolDataUnit::new(PathBuf::from(m.as_str()), m.to_owned().into()))
.collect();
imports =
capture_outer_and_inner(content, &utils::RE_SOL_IMPORT, &["p1", "p2", "p3", "p4"])
.iter()
.map(|(cap, m)| {
SolDataUnit::new(PathBuf::from(m.as_str()), cap.to_owned().into())
})
.collect();
}
};
let license = content.lines().next().and_then(|line| {

View File

@ -1,4 +1,4 @@
//SPDX-License-Identifier: Unlicense
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;
contract Bar {}

View File

@ -1,4 +1,4 @@
//SPDX-License-Identifier: Unlicense
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;
import { Bar } from './Bar.sol';

View File

@ -1,4 +1,4 @@
//SPDX-License-Identifier: Unlicense
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;
import { Bar } from './Bar.sol';

View File

@ -0,0 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import { Lib } from "./Lib.sol";
// Intentionally erroneous code
contract Contract {
failure();
}

View File

@ -0,0 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
library Lib {}

View File

@ -375,6 +375,7 @@ fn can_flatten_file() {
assert!(result.is_ok());
let result = result.unwrap();
assert!(!result.contains("import"));
assert!(result.contains("contract Foo"));
assert!(result.contains("contract Bar"));
}
@ -393,6 +394,7 @@ fn can_flatten_file_with_external_lib() {
assert!(result.is_ok());
let result = result.unwrap();
assert!(!result.contains("import"));
assert!(result.contains("library console"));
assert!(result.contains("contract Greeter"));
}
@ -409,6 +411,7 @@ fn can_flatten_file_in_dapp_sample() {
assert!(result.is_ok());
let result = result.unwrap();
assert!(!result.contains("import"));
assert!(result.contains("contract DSTest"));
assert!(result.contains("contract Dapp"));
assert!(result.contains("contract DappTest"));
@ -416,7 +419,7 @@ fn can_flatten_file_in_dapp_sample() {
#[test]
fn can_flatten_file_with_duplicates() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/flatten-sample");
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-flatten-duplicates");
let paths = ProjectPathsConfig::builder().sources(root.join("contracts"));
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
@ -426,10 +429,44 @@ fn can_flatten_file_with_duplicates() {
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.matches("contract Foo {").count(), 1);
assert_eq!(result.matches("contract Bar {").count(), 1);
assert_eq!(result.matches("contract FooBar {").count(), 1);
assert_eq!(result.matches(';').count(), 1);
assert_eq!(
result,
r#"//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.0;
contract Bar {}
contract Foo {}
contract FooBar {}
"#
);
}
#[test]
fn can_flatten_on_solang_failure() {
let root =
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/test-flatten-solang-failure");
let paths = ProjectPathsConfig::builder().sources(&root.join("contracts"));
let project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();
let target = root.join("contracts/Contract.sol");
let result = project.flatten(&target);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(
result,
r#"// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
library Lib {}
// Intentionally erroneous code
contract Contract {
failure();
}
"#
);
}
#[test]