feat(xatu): report unquoted URI literals

This commit is contained in:
2026-09-19 17:27:43 -04:00
parent ad00301ac9
commit 2572c4a56b
7 changed files with 120 additions and 9 deletions

View File

@@ -10,6 +10,7 @@ rust_library(
"rules.rs",
"rules/deprecated_to_path.rs",
"rules/empty_inherit.rs",
"rules/unquoted_uri.rs",
"xatu.rs",
],
deps = [

View File

@@ -4,6 +4,7 @@ use crate::FileId;
pub struct ParsedFile {
pub file_id: FileId,
pub nodes: Vec<Node>,
pub tokens: Vec<Token>,
pub errors: Vec<ParseError>,
}
@@ -31,6 +32,17 @@ pub enum NodeData {
Inherit(Inherit),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
pub range: SourceRange,
pub data: TokenData,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TokenData {
Uri(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Apply {
pub function: Expr,

View File

@@ -13,6 +13,11 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
.descendants()
.filter_map(convert_node)
.collect(),
tokens: parse
.syntax()
.descendants_with_tokens()
.filter_map(convert_token)
.collect(),
errors: parse
.errors()
.iter()
@@ -29,6 +34,21 @@ fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
}
}
fn convert_token(element: rnix::SyntaxElement) -> Option<ast::Token> {
let token = match element {
rnix::NodeOrToken::Token(token) => token,
rnix::NodeOrToken::Node(_) => return None,
};
match token.kind() {
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
range: token.text_range().into(),
data: ast::TokenData::Uri(token.text().to_string()),
}),
_ => None,
}
}
fn convert_apply(node: rnix::SyntaxNode) -> Option<ast::Node> {
let apply = rnix::ast::Apply::cast(node)?;
let range = apply.syntax().text_range().into();

View File

@@ -2,9 +2,11 @@ use crate::FileId;
use crate::ast;
use crate::rules::deprecated_to_path::DeprecatedToPath;
use crate::rules::empty_inherit::EmptyInherit;
use crate::rules::unquoted_uri::UnquotedUri;
pub mod deprecated_to_path;
pub mod empty_inherit;
pub mod unquoted_uri;
#[derive(Debug)]
pub struct Diagnostic {
@@ -38,22 +40,40 @@ pub trait NodeRule {
fn check(node: &Self::Node) -> Option<&'static str>;
}
pub trait TokenRule {
type Token;
const ID: &'static str;
fn select(data: &ast::TokenData) -> Option<&Self::Token>;
fn check(token: &Self::Token) -> Option<&'static str>;
}
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
file.nodes
.iter()
.flat_map(|node| {
[
run_node_rule::<DeprecatedToPath>(node),
run_node_rule::<EmptyInherit>(node),
]
let node_diagnostics = file.nodes.iter().flat_map(|node| {
[
run_node_rule::<DeprecatedToPath>(node),
run_node_rule::<EmptyInherit>(node),
]
.into_iter()
.flatten()
.map(|finding| Diagnostic {
file_id: file.file_id,
finding,
})
});
let token_diagnostics = file.tokens.iter().flat_map(|token| {
[run_token_rule::<UnquotedUri>(token)]
.into_iter()
.flatten()
.map(|finding| Diagnostic {
file_id: file.file_id,
finding,
})
})
.collect()
});
node_diagnostics.chain(token_diagnostics).collect()
}
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
@@ -69,3 +89,17 @@ where
message,
})
}
fn run_token_rule<R>(token: &ast::Token) -> Option<Finding>
where
R: TokenRule,
{
let typed_token = R::select(&token.data)?;
let message = R::check(typed_token)?;
Some(Finding {
rule_id: R::ID,
range: token.range,
message,
})
}

View File

@@ -0,0 +1,19 @@
use crate::{ast, rules::TokenRule};
pub struct UnquotedUri;
impl TokenRule for UnquotedUri {
type Token = String;
const ID: &'static str = "unquoted_uri";
fn select(data: &ast::TokenData) -> Option<&Self::Token> {
match data {
ast::TokenData::Uri(uri) => Some(uri),
}
}
fn check(_token: &Self::Token) -> Option<&'static str> {
Some("URI literals should be quoted")
}
}

View File

@@ -3,4 +3,5 @@
{
inherit;
path = builtins.toPath src;
url = https://example.com;
}

View File

@@ -112,3 +112,27 @@ fn xatu_reports_empty_inherit() -> googletest::Result<()> {
);
Ok(())
}
#[gtest]
fn xatu_reports_unquoted_uri() -> googletest::Result<()> {
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
let findings = report
.diagnostics
.iter()
.map(|diagnostic| {
(
report.files.get(diagnostic.file_id).path.clone(),
diagnostic.finding.rule_id,
)
})
.collect::<Vec<_>>();
expect_that!(
findings,
contains(eq(&(
PathBuf::from("testdata/lint_findings.nix"),
"unquoted_uri"
)))
);
Ok(())
}