fix: check all import styles (#634)

This commit is contained in:
Matthias Seitz 2021-11-29 22:45:07 +01:00 committed by GitHub
parent 5dfc1bd171
commit 133382d034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -10,7 +10,7 @@ use walkdir::WalkDir;
/// statement with the named groups "path", "id". /// statement with the named groups "path", "id".
pub static RE_SOL_IMPORT: Lazy<Regex> = Lazy::new(|| { pub static RE_SOL_IMPORT: Lazy<Regex> = Lazy::new(|| {
// Adapted from https://github.com/nomiclabs/hardhat/blob/cced766c65b25d3d0beb39ef847246ac9618bdd9/packages/hardhat-core/src/internal/solidity/parse.ts#L100 // Adapted from https://github.com/nomiclabs/hardhat/blob/cced766c65b25d3d0beb39ef847246ac9618bdd9/packages/hardhat-core/src/internal/solidity/parse.ts#L100
Regex::new(r#"import\s+(?:(?:"(?P<path>[^;]*)"|'([^;]*)')(?:;|\s+as\s+(?P<id>[^;]*);)|.+from\s+(?:"(.*)"|'(.*)');)"#).unwrap() Regex::new(r#"import\s+(?:(?:"(?P<p1>[^;]*)"|'([^;]*)')(?:;|\s+as\s+(?P<id>[^;]*);)|.+from\s+(?:"(?P<p2>.*)"|'(?P<p3>.*)');)"#).unwrap()
}); });
/// A regex that matches the version part of a solidity pragma /// A regex that matches the version part of a solidity pragma
@ -27,7 +27,7 @@ pub static RE_SOL_PRAGMA_VERSION: Lazy<Regex> =
pub fn find_import_paths(contract: &str) -> Vec<&str> { pub fn find_import_paths(contract: &str) -> Vec<&str> {
RE_SOL_IMPORT RE_SOL_IMPORT
.captures_iter(contract) .captures_iter(contract)
.filter_map(|cap| cap.name("path")) .filter_map(|cap| cap.name("p1").or_else(|| cap.name("p2")).or_else(|| cap.name("p3")))
.map(|m| m.as_str()) .map(|m| m.as_str())
.collect() .collect()
} }
@ -153,8 +153,13 @@ mod tests {
pragma solidity ^0.8.0; pragma solidity ^0.8.0;
import "hardhat/console.sol"; import "hardhat/console.sol";
import "../contract/Contract.sol"; import "../contract/Contract.sol";
import { T } from "../Test.sol";
import { T } from '../Test2.sol';
"##; "##;
assert_eq!(vec!["hardhat/console.sol", "../contract/Contract.sol"], find_import_paths(s)); assert_eq!(
vec!["hardhat/console.sol", "../contract/Contract.sol", "../Test.sol", "../Test2.sol"],
find_import_paths(s)
);
} }
#[test] #[test]
fn can_find_version() { fn can_find_version() {