fix(natu): preserve NixOS cfg bindings

This commit was merged in pull request #104.
This commit is contained in:
2026-09-26 23:24:54 -04:00
parent 5e5c3e2534
commit ac5cc8d659
4 changed files with 49 additions and 25 deletions

View File

@@ -8,36 +8,42 @@ pub struct Finding {
pub expected_order: Vec<String>,
}
pub fn check(graph: &ScopeGraph) -> Vec<Finding> {
pub fn check(graph: &ScopeGraph, ignore_cfg: bool) -> Vec<Finding> {
graph
.let_scopes()
.iter()
.flat_map(|let_scope| {
let expected = expected_order(graph, let_scope);
let is_ordered = expected.iter().enumerate().all(|(position, declaration)| {
let_scope
.bindings
.get(position)
.is_some_and(|binding| binding.declaration == *declaration)
});
let expected = expected_order(graph, let_scope)
.into_iter()
.filter(|declaration| !ignore_cfg || binding_name(let_scope, *declaration) != "cfg")
.collect::<Vec<_>>();
let actual = let_scope
.bindings
.iter()
.filter(|binding| !ignore_cfg || binding.name != "cfg")
.map(|binding| binding.declaration)
.collect::<Vec<_>>();
(!is_ordered).then(|| Finding {
(!actual.starts_with(&expected)).then(|| Finding {
range: let_scope.range,
expected_order: expected
.iter()
.filter_map(|declaration| {
let_scope
.bindings
.iter()
.find(|binding| binding.declaration == *declaration)
.map(|binding| binding.name.clone())
})
.map(|declaration| binding_name(let_scope, *declaration).to_owned())
.collect(),
})
})
.collect()
}
fn binding_name(let_scope: &LetScope, declaration: NodeId) -> &str {
&let_scope
.bindings
.iter()
.find(|binding| binding.declaration == declaration)
.expect("let declaration")
.name
}
fn expected_order(graph: &ScopeGraph, let_scope: &LetScope) -> Vec<NodeId> {
let mut order = Vec::new();
visit_references(graph, &let_scope.entry_references, let_scope, &mut order);