feat(xatu): report deprecated toPath calls

This commit is contained in:
2026-09-18 23:57:08 -04:00
parent a6fe84190e
commit f8966fe2a8
9 changed files with 173 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ rust_library(
"file_store.rs", "file_store.rs",
"parser.rs", "parser.rs",
"rules.rs", "rules.rs",
"rules/deprecated_to_path.rs",
"rules/empty_inherit.rs", "rules/empty_inherit.rs",
"xatu.rs", "xatu.rs",
], ],
@@ -29,6 +30,8 @@ rust_test(
srcs = ["xatu_test.rs"], srcs = ["xatu_test.rs"],
data = [ data = [
"testdata/clean.nix", "testdata/clean.nix",
"testdata/deprecated_to_path_ident.nix",
"testdata/deprecated_to_path_select.nix",
"testdata/empty_inherit.nix", "testdata/empty_inherit.nix",
"testdata/invalid.nix", "testdata/invalid.nix",
], ],

View File

@@ -1,5 +1,27 @@
use crate::FileId; use crate::FileId;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Apply {
pub function: Expr,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr {
Ident(Ident),
Select(Box<Select>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ident {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Select {
pub receiver: Expr,
pub attrs: Vec<Ident>,
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Inherit { pub struct Inherit {
pub attr_count: usize, pub attr_count: usize,
@@ -7,6 +29,7 @@ pub struct Inherit {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeData { pub enum NodeData {
Apply(Apply),
Inherit(Inherit), Inherit(Inherit),
} }

View File

@@ -52,11 +52,22 @@ fn parse_error_range(error: &rnix::ParseError, source_len: usize) -> ast::Source
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> { fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
match node.kind() { match node.kind() {
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node), rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
_ => None, _ => None,
} }
} }
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
let apply = rnix::ast::Apply::cast(node)?;
let range = apply.syntax().text_range().into();
Some(ast::Node {
range,
data: ast::NodeData::Apply(apply.try_into().ok()?),
})
}
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> { fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
let inherit = rnix::ast::Inherit::cast(node)?; let inherit = rnix::ast::Inherit::cast(node)?;
@@ -75,6 +86,57 @@ impl From<rnix::TextRange> for ast::SourceRange {
} }
} }
impl TryFrom<rnix::ast::Apply> for ast::Apply {
type Error = ();
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
Ok(Self {
function: apply.lambda().ok_or(())?.try_into()?,
})
}
}
impl TryFrom<rnix::ast::Expr> for ast::Expr {
type Error = ();
fn try_from(expr: rnix::ast::Expr) -> Result<Self, Self::Error> {
match expr {
rnix::ast::Expr::Ident(ident) => Ok(Self::Ident(ident.try_into()?)),
rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))),
_ => Err(()),
}
}
}
impl TryFrom<rnix::ast::Ident> for ast::Ident {
type Error = ();
fn try_from(ident: rnix::ast::Ident) -> Result<Self, Self::Error> {
Ok(Self {
name: ident.ident_token().ok_or(())?.text().to_owned(),
})
}
}
impl TryFrom<rnix::ast::Select> for ast::Select {
type Error = ();
fn try_from(select: rnix::ast::Select) -> Result<Self, Self::Error> {
Ok(Self {
receiver: select.expr().ok_or(())?.try_into()?,
attrs: select
.attrpath()
.ok_or(())?
.attrs()
.map(|attr| match attr {
rnix::ast::Attr::Ident(ident) => ident.try_into(),
_ => Err(()),
})
.collect::<Result<Vec<_>, _>>()?,
})
}
}
impl From<rnix::ast::Inherit> for ast::Inherit { impl From<rnix::ast::Inherit> for ast::Inherit {
fn from(inherit: rnix::ast::Inherit) -> Self { fn from(inherit: rnix::ast::Inherit) -> Self {
Self { Self {

View File

@@ -1,7 +1,9 @@
use crate::FileId; use crate::FileId;
use crate::ast; use crate::ast;
use crate::rules::deprecated_to_path::DeprecatedToPath;
use crate::rules::empty_inherit::EmptyInherit; use crate::rules::empty_inherit::EmptyInherit;
pub mod deprecated_to_path;
pub mod empty_inherit; pub mod empty_inherit;
#[derive(Debug)] #[derive(Debug)]
@@ -54,13 +56,16 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
file.nodes file.nodes
.iter() .iter()
.flat_map(|node| { .flat_map(|node| {
[run_node_rule::<EmptyInherit>(node)] [
.into_iter() run_node_rule::<EmptyInherit>(node),
.flatten() run_node_rule::<DeprecatedToPath>(node),
.map(|finding| Diagnostic { ]
file_id: file.file_id, .into_iter()
finding, .flatten()
}) .map(|finding| Diagnostic {
file_id: file.file_id,
finding,
})
}) })
.collect() .collect()
} }

View File

@@ -0,0 +1,39 @@
use crate::{ast, rules::NodeRule};
pub struct DeprecatedToPath;
impl NodeRule for DeprecatedToPath {
type Node = ast::Apply;
const ID: &'static str = "deprecated_to_path";
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
match data {
ast::NodeData::Apply(apply) => Some(apply),
_ => None,
}
}
fn check(node: &Self::Node) -> Option<&'static str> {
if calls_to_path(&node.function) {
Some("builtins.toPath is deprecated")
} else {
None
}
}
}
fn calls_to_path(function: &ast::Expr) -> bool {
match function {
ast::Expr::Ident(ident) => ident.name == "toPath",
ast::Expr::Select(select) => is_builtins_to_path(select),
}
}
fn is_builtins_to_path(select: &ast::Select) -> bool {
matches!(
&select.receiver,
ast::Expr::Ident(ident) if ident.name == "builtins"
) && select.attrs.len() == 1
&& select.attrs[0].name == "toPath"
}

View File

@@ -10,6 +10,7 @@ impl NodeRule for EmptyInherit {
fn select(data: &ast::NodeData) -> Option<&Self::Node> { fn select(data: &ast::NodeData) -> Option<&Self::Node> {
match data { match data {
ast::NodeData::Inherit(inherit) => Some(inherit), ast::NodeData::Inherit(inherit) => Some(inherit),
_ => None,
} }
} }

View File

@@ -0,0 +1 @@
toPath x

View File

@@ -0,0 +1 @@
builtins.toPath x

View File

@@ -37,10 +37,40 @@ fn future_sight_rejects_parse_errors() {
assert_eq!(error.code(), Code::FailedPrecondition); assert_eq!(error.code(), Code::FailedPrecondition);
} }
#[test]
fn future_sight_reports_deprecated_to_path_ident_findings() {
let report = xatu::future_sight(vec![source_file("testdata/deprecated_to_path_ident.nix")])
.expect("fixture should parse");
assert_eq!(report.diagnostics.len(), 1);
let diagnostic = &report.diagnostics[0];
assert_eq!(
report.files.get(diagnostic.file_id).path,
PathBuf::from("testdata/deprecated_to_path_ident.nix"),
);
assert_eq!(diagnostic.finding.rule_id, "deprecated_to_path");
}
#[test]
fn future_sight_reports_deprecated_to_path_select_findings() {
let report = xatu::future_sight(vec![source_file("testdata/deprecated_to_path_select.nix")])
.expect("fixture should parse");
assert_eq!(report.diagnostics.len(), 1);
let diagnostic = &report.diagnostics[0];
assert_eq!(
report.files.get(diagnostic.file_id).path,
PathBuf::from("testdata/deprecated_to_path_select.nix"),
);
assert_eq!(diagnostic.finding.rule_id, "deprecated_to_path");
}
#[test] #[test]
fn future_sight_reports_empty_inherit_findings() { fn future_sight_reports_empty_inherit_findings() {
let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")]) let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")])
.expect("fixture should parse"); .expect("fixture should parse");
assert_eq!(report.diagnostics.len(), 1); assert_eq!(report.diagnostics.len(), 1);