Skip to content

Commit c41e48c

Browse files
committed
feat: Adds parsing single require and require with comments
Signed-off-by: Farhaan Bukhsh <farhaan.bukhsh@gmail.com>
1 parent 9b4914f commit c41e48c

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/licenses.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ struct PackageJson {
8686
impl PackageJson {
8787
fn get_all_dependencies(self) -> HashMap<String, String> {
8888
let mut all_dependencies: HashMap<String, String> = HashMap::new();
89-
if let Some(deps) = self.dev_dependencies { all_dependencies.extend(deps) };
90-
if let Some(deps) = self.dependencies { all_dependencies.extend(deps) };
89+
if let Some(deps) = self.dev_dependencies {
90+
all_dependencies.extend(deps)
91+
};
92+
if let Some(deps) = self.dependencies {
93+
all_dependencies.extend(deps)
94+
};
9195
all_dependencies
9296
}
9397
}
@@ -231,12 +235,17 @@ pub fn analyze_go_licenses(go_mod_path: &str) -> Vec<LicenseInfo> {
231235
}
232236

233237
pub fn get_go_dependencies(content_string: String) -> Vec<GoPackages> {
234-
let re =
235-
Regex::new(r"require\s*\(\s*((?:[\w./-]+\s+v[\d]+(?:\.\d+)*(?:-\S+)?\s*)+)\)").unwrap();
238+
let re_comment = Regex::new(r"(?m)^(.*?)\s*(//|#).*?$").unwrap(); // Matches comments after dependencies
239+
let cleaned = re_comment.replace_all(content_string.as_str(), "$1"); // Removes everything after // or #
240+
let re = Regex::new(
241+
r"require\s*(?:\(\s*)?((?:[\w./-]+\s+v[\d][\w\d.-]+(?:-\w+)?(?:\+\w+)?\s*)+)\)?",
242+
)
243+
.unwrap();
244+
let re_dependency = Regex::new(r"([\w./-]+)\s+(v[\d]+(?:\.\d+)*(?:-\S+)?)").unwrap();
245+
236246
let mut dependency = vec![];
237-
for cap in re.captures_iter(content_string.as_str()) {
247+
for cap in re.captures_iter(&cleaned) {
238248
let dependency_block = &cap[1];
239-
let re_dependency = Regex::new(r"([\w./-]+)\s+(v[\d]+(?:\.\d+)*(?:-\S+)?)").unwrap();
240249
for dep_cap in re_dependency.captures_iter(dependency_block) {
241250
dependency.push(GoPackages {
242251
name: dep_cap[1].to_string(),

src/parser.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn find_project_roots(root_path: impl AsRef<Path>) -> Vec<ProjectRoot> {
5353
let mut project_roots = Vec::new();
5454

5555
for entry in Walk::new(root_path).flatten() {
56-
if !entry.file_type().map_or(false, |ft| ft.is_file()) {
56+
if !entry.file_type().is_some_and(|ft| ft.is_file()) {
5757
continue;
5858
}
5959

@@ -217,21 +217,28 @@ mod tests {
217217
github.com/aws/aws-sdk-go-v2/credentials v1.13.43
218218
github.com/aws/aws-sdk-go-v2/service/route53 v1.30.2
219219
github.com/cespare/cp v0.1.0
220-
github.com/cloudflare/cloudflare-go v0.79.0
221-
github.com/cockroachdb/pebble v1.1.2
222-
github.com/consensys/gnark-crypto v0.14.0
223-
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a
220+
github.com/cloudflare/cloudflare-go v0.79.0 //indirect
221+
github.com/cockroachdb/pebble v1.1.2 //indirect
224222
github.com/crate-crypto/go-kzg-4844 v1.1.0
225-
github.com/davecgh/go-spew v1.1.1
226-
)"#;
223+
github.com/davecgh/go-spew v1.1.1 # Another comment
224+
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a
225+
github.com/consensys/gnark-crypto v0.14.0
226+
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // Check the version
227+
)
228+
229+
require example.com/theirmodule v1.3.4
230+
231+
require (github.com/some/module v1.0.0)
232+
require (github.com/another/module v2.3.4)
233+
require (github.com/mixed-case/Module v3.5.7-beta)"#;
227234
fs::write(&go_mod_path, dependencies).unwrap();
228235

229236
let result = parse_dependencies(&ProjectRoot {
230237
path: temp_dir.path().to_path_buf(),
231238
project_type: Language::Go("go.mod"),
232239
});
233240
let parsed = get_go_dependencies(dependencies.to_string());
234-
assert!(parsed.len() == 14);
241+
assert!(parsed.len() == 19);
235242
assert!(result.len() == parsed.len());
236243
}
237244

0 commit comments

Comments
 (0)