Skip to content

Commit 03123cb

Browse files
authored
fix(parser): manifest depends properly parse comment (#84)
fix #83
1 parent f6f60a4 commit 03123cb

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

src/index.rs

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ impl Index {
595595

596596
fn parse_dependencies(manifest: &Path) -> anyhow::Result<Box<[Symbol<ModuleEntry>]>> {
597597
query! {
598-
ManifestQuery(Depends);
598+
ManifestQuery(DependsList);
599599

600600
((dictionary
601601
(pair
602602
(string (string_content) @_depends)
603-
(list ((string) @DEPENDS ","?)*)
603+
(list) @DEPENDS_LIST
604604
)
605605
) (#eq? @_depends "depends"))
606606
}
@@ -621,12 +621,17 @@ fn parse_dependencies(manifest: &Path) -> anyhow::Result<Box<[Symbol<ModuleEntry
621621

622622
for (match_, idx) in cursor.captures(ManifestQuery::query(), root, &contents[..]) {
623623
let capture = match_.captures[idx];
624-
// for capture in match_.captures {
625-
if let Some(ManifestQuery::Depends) = ManifestQuery::from(capture.index) {
626-
let dep = String::from_utf8_lossy(&contents[capture.node.byte_range().shrink(1)]);
627-
deps.push(_I(dep).into());
624+
if let Some(ManifestQuery::DependsList) = ManifestQuery::from(capture.index) {
625+
let list_node = capture.node;
626+
let mut child_cursor = list_node.walk();
627+
for child in list_node.children(&mut child_cursor) {
628+
// Only process string nodes, skip comments, commas, brackets, etc.
629+
if child.kind() == "string" {
630+
let dep = String::from_utf8_lossy(&contents[child.byte_range().shrink(1)]);
631+
deps.push(_I(dep).into());
632+
}
633+
}
628634
}
629-
// }
630635
}
631636

632637
Ok(deps.into_boxed_slice())
@@ -1196,4 +1201,62 @@ class TransifexCodeTranslation(models.Model):
11961201
}]
11971202
));
11981203
}
1204+
1205+
#[test]
1206+
fn test_depends_manifest_parsing() {
1207+
use crate::test_utils;
1208+
1209+
if let Ok(mut fs) = test_utils::fs::TEST_FS.write() {
1210+
fs.insert(
1211+
"foobar/__manifest__.py".into(),
1212+
r#"{
1213+
"name": "foobar",
1214+
"depends": [
1215+
"foo",
1216+
# test dependencies
1217+
"bar", # this was not recognized
1218+
# "skip",
1219+
],
1220+
}
1221+
"#
1222+
.as_bytes(),
1223+
);
1224+
}
1225+
1226+
let manifest_path = std::path::Path::new("foobar/__manifest__.py");
1227+
let result = parse_dependencies(manifest_path);
1228+
1229+
match result {
1230+
Ok(deps) => {
1231+
println!("Dependencies found: {} total", deps.len());
1232+
for (i, dep) in deps.iter().enumerate() {
1233+
println!(" {}. {:?}", i + 1, dep);
1234+
}
1235+
1236+
assert_eq!(
1237+
deps.len(),
1238+
3,
1239+
"Expected to find 3 dependencies (base + foo + bar), but found {}",
1240+
deps.len()
1241+
);
1242+
1243+
let dep_names: Vec<String> = deps.iter().map(|d| format!("{:?}", d)).collect();
1244+
assert!(
1245+
dep_names.contains(&"Symbol<ModuleEntry>(\"base\")".to_string()),
1246+
"Expected to find 'base' dependency"
1247+
);
1248+
assert!(
1249+
dep_names.contains(&"Symbol<ModuleEntry>(\"foo\")".to_string()),
1250+
"Expected to find 'foo' dependency"
1251+
);
1252+
assert!(
1253+
dep_names.contains(&"Symbol<ModuleEntry>(\"bar\")".to_string()),
1254+
"Expected to find 'bar' dependency (should now be recognized despite comment)"
1255+
);
1256+
}
1257+
Err(e) => {
1258+
panic!("Parsing should succeed but with incomplete results, got error: {}", e);
1259+
}
1260+
}
1261+
}
11991262
}

0 commit comments

Comments
 (0)