Compare commits

18 Commits

Author SHA1 Message Date
0cd7c4bfb7 test(xatu): inline finding assertions 2026-09-19 14:48:35 -04:00
798a6dc3a3 test(xatu): organize lint fixtures 2026-09-19 14:45:23 -04:00
575b0fde0d test(xatu): relax diagnostic count assertions 2026-09-19 11:10:16 -04:00
460e7e25ad test(xatu): simplify future sight tests 2026-09-19 11:06:14 -04:00
22e8c896c0 test(xatu): keep multiline fixtures in testdata 2026-09-19 11:02:12 -04:00
7b5559595e test(xatu): format inline fixtures 2026-09-19 10:59:29 -04:00
825a1b9773 test(xatu): use explicit path construction fixture 2026-09-19 10:58:28 -04:00
46c1ca65b7 test(xatu): inline short future sight fixtures 2026-09-19 10:53:57 -04:00
0485e5eb88 style(xatu): order rule dispatch list 2026-09-19 10:49:43 -04:00
8d4018c5dd refactor(xatu): order parser conversions by use 2026-09-19 10:43:07 -04:00
d455dcfb46 refactor(xatu): use conversion traits in parser 2026-09-19 10:36:57 -04:00
0b67331ca3 refactor(xatu): clarify parser conversions 2026-09-19 10:34:47 -04:00
d2fc9cb0af style(rust): drop declaration ordering rule 2026-09-19 10:30:53 -04:00
5190d1ed01 style(rust): document declaration ordering 2026-09-19 10:29:05 -04:00
efb2fd3234 style(xatu): order AST declarations by use 2026-09-19 10:23:09 -04:00
c790bf06fc style(xatu): order AST support declarations 2026-09-19 10:22:04 -04:00
731ad83ae9 style(xatu): order AST declarations 2026-09-19 10:18:24 -04:00
f8966fe2a8 feat(xatu): report deprecated toPath calls 2026-09-18 23:57:08 -04:00
13 changed files with 249 additions and 82 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,8 +30,9 @@ rust_test(
srcs = ["xatu_test.rs"], srcs = ["xatu_test.rs"],
data = [ data = [
"testdata/clean.nix", "testdata/clean.nix",
"testdata/empty_inherit.nix",
"testdata/invalid.nix", "testdata/invalid.nix",
"testdata/lint_findings.nix",
"testdata/lint_variants.nix",
], ],
deps = [ deps = [
":xatu_lib", ":xatu_lib",

View File

@@ -1,13 +1,16 @@
use crate::FileId; use crate::FileId;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug)]
pub struct Inherit { pub struct ParsedFile {
pub attr_count: usize, pub file_id: FileId,
pub nodes: Vec<Node>,
pub errors: Vec<ParseError>,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeData { pub struct Node {
Inherit(Inherit), pub range: SourceRange,
pub data: NodeData,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -23,9 +26,36 @@ impl std::fmt::Display for SourceRange {
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node { pub enum NodeData {
pub range: SourceRange, Apply(Apply),
pub data: NodeData, Inherit(Inherit),
}
#[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)]
pub struct Inherit {
pub attr_count: usize,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -33,10 +63,3 @@ pub struct ParseError {
pub range: SourceRange, pub range: SourceRange,
pub message: String, pub message: String,
} }
#[derive(Debug)]
pub struct ParsedFile {
pub file_id: FileId,
pub nodes: Vec<Node>,
pub errors: Vec<ParseError>,
}

View File

@@ -1,14 +1,5 @@
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileId(usize);
#[derive(Debug)]
pub struct SourceFile {
pub path: PathBuf,
pub source: String,
}
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct FileStore { pub struct FileStore {
files: Vec<SourceFile>, files: Vec<SourceFile>,
@@ -31,3 +22,12 @@ impl From<Vec<SourceFile>> for FileStore {
Self { files } Self { files }
} }
} }
#[derive(Debug)]
pub struct SourceFile {
pub path: PathBuf,
pub source: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileId(usize);

View File

@@ -21,6 +21,91 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
} }
} }
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
match node.kind() {
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
_ => None,
}
}
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
let apply = rnix::ast::Apply::cast(node)?;
let range = apply.syntax().text_range().into();
let data = apply.try_into().ok()?;
Some(ast::Node {
range,
data: ast::NodeData::Apply(data),
})
}
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.into())),
rnix::ast::Expr::Select(select) => Ok(Self::Select(Box::new(select.try_into()?))),
_ => Err(()),
}
}
}
impl From<rnix::ast::Ident> for ast::Ident {
fn from(ident: rnix::ast::Ident) -> Self {
Self {
name: ident.to_string(),
}
}
}
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) => Ok(ident.into()),
_ => Err(()),
})
.collect::<Result<Vec<_>, _>>()?,
})
}
}
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
let inherit = rnix::ast::Inherit::cast(node)?;
Some(ast::Node {
range: inherit.syntax().text_range().into(),
data: ast::NodeData::Inherit(inherit.into()),
})
}
impl From<rnix::ast::Inherit> for ast::Inherit {
fn from(inherit: rnix::ast::Inherit) -> Self {
Self {
attr_count: inherit.attrs().count(),
}
}
}
fn convert_parse_error(error: &rnix::ParseError, source_len: usize) -> ast::ParseError { fn convert_parse_error(error: &rnix::ParseError, source_len: usize) -> ast::ParseError {
ast::ParseError { ast::ParseError {
range: parse_error_range(error, source_len), range: parse_error_range(error, source_len),
@@ -50,22 +135,6 @@ fn parse_error_range(error: &rnix::ParseError, source_len: usize) -> ast::Source
} }
} }
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
match node.kind() {
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
_ => None,
}
}
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
let inherit = rnix::ast::Inherit::cast(node)?;
Some(ast::Node {
range: inherit.syntax().text_range().into(),
data: ast::NodeData::Inherit(inherit.into()),
})
}
impl From<rnix::TextRange> for ast::SourceRange { impl From<rnix::TextRange> for ast::SourceRange {
fn from(range: rnix::TextRange) -> Self { fn from(range: rnix::TextRange) -> Self {
Self { Self {
@@ -74,11 +143,3 @@ impl From<rnix::TextRange> for ast::SourceRange {
} }
} }
} }
impl From<rnix::ast::Inherit> for ast::Inherit {
fn from(inherit: rnix::ast::Inherit) -> Self {
Self {
attr_count: inherit.attrs().count(),
}
}
}

View File

@@ -1,9 +1,17 @@
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)]
pub struct Diagnostic {
pub file_id: FileId,
pub finding: Finding,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Finding { pub struct Finding {
pub rule_id: &'static str, pub rule_id: &'static str,
@@ -11,12 +19,6 @@ pub struct Finding {
pub message: &'static str, pub message: &'static str,
} }
#[derive(Debug)]
pub struct Diagnostic {
pub file_id: FileId,
pub finding: Finding,
}
impl std::fmt::Display for Diagnostic { impl std::fmt::Display for Diagnostic {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
@@ -54,7 +56,10 @@ 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)] [
run_node_rule::<DeprecatedToPath>(node),
run_node_rule::<EmptyInherit>(node),
]
.into_iter() .into_iter()
.flatten() .flatten()
.map(|finding| Diagnostic { .map(|finding| Diagnostic {

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

@@ -1 +1,6 @@
{ inherit foo; } { pkgs }:
{
path = ./. + "/foo";
inherit pkgs;
}

View File

@@ -1 +0,0 @@
{ inherit; }

View File

@@ -1 +1,4 @@
{ pkgs }:
{ {
inherit pkgs;

6
src/xatu/testdata/lint_findings.nix vendored Normal file
View File

@@ -0,0 +1,6 @@
{ src }:
{
inherit;
path = builtins.toPath src;
}

5
src/xatu/testdata/lint_variants.nix vendored Normal file
View File

@@ -0,0 +1,5 @@
{ src }:
{
path = toPath src;
}

View File

@@ -3,14 +3,14 @@ use std::path::{Path, PathBuf};
use status::Code; use status::Code;
use xatu::SourceFile; use xatu::SourceFile;
fn source_file(path: &str) -> SourceFile { fn fixture_source(path: &str) -> SourceFile {
SourceFile { SourceFile {
path: PathBuf::from(path), path: PathBuf::from(path),
source: std::fs::read_to_string(testdata_path(path)).expect("fixture should be readable"), source: std::fs::read_to_string(fixture_path(path)).expect("fixture should be readable"),
} }
} }
fn testdata_path(path: &str) -> PathBuf { fn fixture_path(path: &str) -> PathBuf {
if Path::new(path).exists() { if Path::new(path).exists() {
return PathBuf::from(path); return PathBuf::from(path);
} }
@@ -23,7 +23,7 @@ fn testdata_path(path: &str) -> PathBuf {
#[test] #[test]
fn future_sight_reports_clean_sources_as_clean() { fn future_sight_reports_clean_sources_as_clean() {
let report = xatu::future_sight(vec![source_file("testdata/clean.nix")]) let report = xatu::future_sight(vec![fixture_source("testdata/clean.nix")])
.expect("fixture should parse"); .expect("fixture should parse");
assert!(report.is_clean()); assert!(report.is_clean());
@@ -31,23 +31,41 @@ fn future_sight_reports_clean_sources_as_clean() {
#[test] #[test]
fn future_sight_rejects_parse_errors() { fn future_sight_rejects_parse_errors() {
let error = xatu::future_sight(vec![source_file("testdata/invalid.nix")]) let error = xatu::future_sight(vec![fixture_source("testdata/invalid.nix")])
.expect_err("fixture should fail to parse"); .expect_err("fixture should fail to parse");
assert_eq!(error.code(), Code::FailedPrecondition); assert_eq!(error.code(), Code::FailedPrecondition);
} }
#[test] #[test]
fn future_sight_reports_empty_inherit_findings() { fn future_sight_reports_deprecated_to_path_qualified_findings() {
let report = xatu::future_sight(vec![source_file("testdata/empty_inherit.nix")]) let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")])
.expect("fixture should parse"); .expect("fixture should parse");
assert_eq!(report.diagnostics.len(), 1); assert!(report.diagnostics.iter().any(|diagnostic| {
report.files.get(diagnostic.file_id).path.as_path() == Path::new("testdata/lint_findings.nix")
let diagnostic = &report.diagnostics[0]; && diagnostic.finding.rule_id == "deprecated_to_path"
assert_eq!( }));
report.files.get(diagnostic.file_id).path, }
PathBuf::from("testdata/empty_inherit.nix"),
); #[test]
assert_eq!(diagnostic.finding.rule_id, "empty_inherit"); fn future_sight_reports_deprecated_to_path_unqualified_findings() {
let report = xatu::future_sight(vec![fixture_source("testdata/lint_variants.nix")])
.expect("fixture should parse");
assert!(report.diagnostics.iter().any(|diagnostic| {
report.files.get(diagnostic.file_id).path.as_path() == Path::new("testdata/lint_variants.nix")
&& diagnostic.finding.rule_id == "deprecated_to_path"
}));
}
#[test]
fn future_sight_reports_empty_inherit_findings() {
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")])
.expect("fixture should parse");
assert!(report.diagnostics.iter().any(|diagnostic| {
report.files.get(diagnostic.file_id).path.as_path() == Path::new("testdata/lint_findings.nix")
&& diagnostic.finding.rule_id == "empty_inherit"
}));
} }