Skip to content

Commit

Permalink
Merge pull request #130 from fgaz/skip-patterns
Browse files Browse the repository at this point in the history
fix: skip attrsets in patterns
  • Loading branch information
infinisil authored Aug 25, 2024
2 parents 3712175 + d861687 commit 203aa64
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,13 @@ fn collect_entries(root: rnix::Root, prefix: &str, category: &str) -> Vec<Manual
// we only need a single level of scope for this.
// since only the body can export a function we don't need to implement
// mutually recursive resolution.
for ev in root.syntax().preorder() {
let mut preorder = root.syntax().preorder();
while let Some(ev) = preorder.next() {
match ev {
// Skip patterns. See test/patterns.nix for the reason why.
WalkEvent::Enter(n) if n.kind() == SyntaxKind::NODE_PATTERN => {
preorder.skip_subtree();
}
WalkEvent::Enter(n) if n.kind() == SyntaxKind::NODE_LET_IN => {
return collect_bindings(
LetIn::cast(n.clone()).unwrap().body().unwrap().syntax(),
Expand Down
7 changes: 7 additions & 0 deletions src/snapshots/nixdoc__test__patterns.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: src/test.rs
expression: output
---
## `lib.debug.iAmTopLevel` {#function-library-lib.debug.iAmTopLevel}

This binding is in the top-level attrset
19 changes: 19 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,22 @@ fn test_empty_prefix() {
assert_eq!(ident, "test.mapSimple-prime");
assert_eq!(title, "test.mapSimple'");
}

#[test]
fn test_patterns() {
let mut output = Vec::new();
let src = fs::read_to_string("test/patterns.nix").unwrap();
let nix = rnix::Root::parse(&src).ok().expect("failed to parse input");
let prefix = "lib";
let category = "debug";

for entry in collect_entries(nix, prefix, category) {
entry
.write_section(&Default::default(), &mut output)
.expect("Failed to write section")
}

let output = String::from_utf8(output).expect("not utf8");

insta::assert_snapshot!(output);
}
13 changes: 13 additions & 0 deletions test/patterns.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
pkgs ? import <nixpkgs>
# | This attrset must be skipped -- it is not the top-level attrset
# ↓ even though it comes first!
{ }
}:

{
/**
This binding is in the top-level attrset
*/
iAmTopLevel = null;
}

0 comments on commit 203aa64

Please sign in to comment.