Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip attrsets in patterns #130

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}