feat(xatu): report unquoted URI literals

This commit was merged in pull request #81.
This commit is contained in:
2026-09-19 17:51:46 -04:00
parent ad00301ac9
commit be8744795c
8 changed files with 129 additions and 14 deletions

View File

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

View File

@@ -4,6 +4,7 @@ use crate::FileId;
pub struct ParsedFile { pub struct ParsedFile {
pub file_id: FileId, pub file_id: FileId,
pub nodes: Vec<Node>, pub nodes: Vec<Node>,
pub tokens: Vec<Token>,
pub errors: Vec<ParseError>, pub errors: Vec<ParseError>,
} }
@@ -58,6 +59,17 @@ pub struct Inherit {
pub attr_count: usize, pub attr_count: usize,
} }
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
pub range: SourceRange,
pub data: TokenData,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TokenData {
UnquotedUri(String),
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError { pub struct ParseError {
pub range: SourceRange, pub range: SourceRange,

View File

@@ -6,13 +6,12 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
let parse = rnix::Root::parse(source); let parse = rnix::Root::parse(source);
let source_len = source.len(); let source_len = source.len();
let (nodes, tokens) = convert_syntax(parse.syntax());
ast::ParsedFile { ast::ParsedFile {
file_id, file_id,
nodes: parse nodes,
.syntax() tokens,
.descendants()
.filter_map(convert_node)
.collect(),
errors: parse errors: parse
.errors() .errors()
.iter() .iter()
@@ -21,6 +20,20 @@ pub fn parse_file(file_id: FileId, source: &str) -> ast::ParsedFile {
} }
} }
fn convert_syntax(syntax: rnix::SyntaxNode) -> (Vec<ast::Node>, Vec<ast::Token>) {
let mut nodes = Vec::new();
let mut tokens = Vec::new();
for element in syntax.descendants_with_tokens() {
match element {
rnix::NodeOrToken::Node(node) => nodes.extend(convert_node(node)),
rnix::NodeOrToken::Token(token) => tokens.extend(convert_token(token)),
}
}
(nodes, tokens)
}
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_APPLY => convert_apply(node),
@@ -106,6 +119,16 @@ impl From<rnix::ast::Inherit> for ast::Inherit {
} }
} }
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
match token.kind() {
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
range: token.text_range().into(),
data: ast::TokenData::UnquotedUri(token.text().to_string()),
}),
_ => None,
}
}
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),

View File

@@ -2,9 +2,11 @@ use crate::FileId;
use crate::ast; use crate::ast;
use crate::rules::deprecated_to_path::DeprecatedToPath; use crate::rules::deprecated_to_path::DeprecatedToPath;
use crate::rules::empty_inherit::EmptyInherit; use crate::rules::empty_inherit::EmptyInherit;
use crate::rules::unquoted_uri::UnquotedUri;
pub mod deprecated_to_path; pub mod deprecated_to_path;
pub mod empty_inherit; pub mod empty_inherit;
pub mod unquoted_uri;
#[derive(Debug)] #[derive(Debug)]
pub struct Diagnostic { pub struct Diagnostic {
@@ -38,22 +40,40 @@ pub trait NodeRule {
fn check(node: &Self::Node) -> Option<&'static str>; 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> { pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
file.nodes let node_diagnostics = file.nodes.iter().flat_map(|node| {
.iter() [
.flat_map(|node| { run_node_rule::<DeprecatedToPath>(node),
[ run_node_rule::<EmptyInherit>(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() .into_iter()
.flatten() .flatten()
.map(|finding| Diagnostic { .map(|finding| Diagnostic {
file_id: file.file_id, file_id: file.file_id,
finding, finding,
}) })
}) });
.collect()
node_diagnostics.chain(token_diagnostics).collect()
} }
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding> fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
@@ -69,3 +89,17 @@ where
message, 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::UnquotedUri(uri) => Some(uri),
}
}
fn check(_token: &Self::Token) -> Option<&'static str> {
Some("URI literals should be quoted")
}
}

View File

@@ -2,5 +2,6 @@
{ {
path = ./. + "/foo"; path = ./. + "/foo";
url = "https://example.com";
inherit pkgs; inherit pkgs;
} }

View File

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

View File

@@ -112,3 +112,27 @@ fn xatu_reports_empty_inherit() -> googletest::Result<()> {
); );
Ok(()) 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(())
}