refactor(natu): rename xatu
This commit was merged in pull request #97.
This commit is contained in:
71
src/natu/rules/let_order.rs
Normal file
71
src/natu/rules/let_order.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use crate::{
|
||||
ast,
|
||||
scope_graph::{LetScope, NodeId, ScopeGraph},
|
||||
};
|
||||
|
||||
pub struct Finding {
|
||||
pub range: ast::SourceRange,
|
||||
pub binding: String,
|
||||
}
|
||||
|
||||
pub fn check(graph: &ScopeGraph) -> Vec<Finding> {
|
||||
graph
|
||||
.let_scopes()
|
||||
.iter()
|
||||
.flat_map(|let_scope| {
|
||||
let expected = expected_order(graph, let_scope);
|
||||
let positions = expected
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(position, declaration)| (*declaration, position))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let_scope
|
||||
.bindings
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(move |(position, binding)| {
|
||||
positions
|
||||
.iter()
|
||||
.find(|(declaration, _)| *declaration == binding.declaration)
|
||||
.filter(|(_, expected_position)| position != *expected_position)
|
||||
.map(|_| Finding {
|
||||
range: binding.range,
|
||||
binding: binding.name.clone(),
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
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);
|
||||
order
|
||||
}
|
||||
|
||||
fn visit_references(
|
||||
graph: &ScopeGraph,
|
||||
references: &[NodeId],
|
||||
let_scope: &LetScope,
|
||||
order: &mut Vec<NodeId>,
|
||||
) {
|
||||
for reference in references {
|
||||
for declaration in graph.resolve(*reference) {
|
||||
let Some(binding) = let_scope
|
||||
.bindings
|
||||
.iter()
|
||||
.find(|binding| binding.declaration == declaration)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if order.contains(&declaration) {
|
||||
continue;
|
||||
}
|
||||
|
||||
order.push(declaration);
|
||||
visit_references(graph, &binding.references, let_scope, order);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user