feat(xatu): add manual inherit lint #86
@@ -8,6 +8,7 @@ rust_library(
|
||||
"file_store.rs",
|
||||
"parser.rs",
|
||||
"rules.rs",
|
||||
"rules/manual_inherit.rs",
|
||||
"rules/unquoted_uri.rs",
|
||||
"xatu.rs",
|
||||
],
|
||||
|
||||
@@ -28,20 +28,13 @@ impl std::fmt::Display for SourceRange {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum NodeData {
|
||||
Apply(Apply),
|
||||
LetIn(LetIn),
|
||||
Inherit(Inherit),
|
||||
Binding(Binding),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Apply {
|
||||
pub function: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Expr {
|
||||
Ident(Ident),
|
||||
Select(Box<Select>),
|
||||
pub struct Binding {
|
||||
pub key: Ident,
|
||||
pub value: Ident,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -49,22 +42,6 @@ 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 LetIn {
|
||||
pub binding_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Inherit {
|
||||
pub attr_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Token {
|
||||
pub range: SourceRange,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use rnix::ast::HasEntry;
|
||||
use rnix::ast::Attr;
|
||||
use rowan::ast::AstNode;
|
||||
|
||||
use crate::{FileId, ast};
|
||||
@@ -37,43 +37,42 @@ fn convert_syntax(syntax: rnix::SyntaxNode) -> (Vec<ast::Node>, Vec<ast::Token>)
|
||||
|
||||
fn convert_node(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
match node.kind() {
|
||||
rnix::SyntaxKind::NODE_APPLY => convert_apply(node),
|
||||
rnix::SyntaxKind::NODE_LET_IN => convert_let_in(node),
|
||||
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
||||
rnix::SyntaxKind::NODE_ATTRPATH_VALUE => convert_binding(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()?;
|
||||
fn convert_binding(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let binding = rnix::ast::AttrpathValue::cast(node)?;
|
||||
let range = binding.syntax().text_range().into();
|
||||
let data = binding.try_into().ok()?;
|
||||
|
||||
Some(ast::Node {
|
||||
range,
|
||||
data: ast::NodeData::Apply(data),
|
||||
data: ast::NodeData::Binding(data),
|
||||
})
|
||||
}
|
||||
|
||||
impl TryFrom<rnix::ast::Apply> for ast::Apply {
|
||||
impl TryFrom<rnix::ast::AttrpathValue> for ast::Binding {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(apply: rnix::ast::Apply) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
function: apply.lambda().ok_or(())?.try_into()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
fn try_from(binding: rnix::ast::AttrpathValue) -> Result<Self, Self::Error> {
|
||||
let mut attrs = binding.attrpath().ok_or(())?.attrs();
|
||||
let key = match attrs.next().ok_or(())? {
|
||||
Attr::Ident(ident) => ident.into(),
|
||||
_ => return Err(()),
|
||||
};
|
||||
|
||||
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(()),
|
||||
if attrs.next().is_some() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let value = match binding.value().ok_or(())? {
|
||||
rnix::ast::Expr::Ident(ident) => ident.into(),
|
||||
_ => return Err(()),
|
||||
};
|
||||
|
||||
Ok(Self { key, value })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,63 +84,6 @@ impl From<rnix::ast::Ident> for ast::Ident {
|
||||
}
|
||||
}
|
||||
|
||||
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_let_in(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let let_in = rnix::ast::LetIn::cast(node)?;
|
||||
|
||||
Some(ast::Node {
|
||||
range: let_in.syntax().text_range().into(),
|
||||
data: ast::NodeData::LetIn(let_in.into()),
|
||||
})
|
||||
}
|
||||
|
||||
impl From<rnix::ast::LetIn> for ast::LetIn {
|
||||
fn from(let_in: rnix::ast::LetIn) -> Self {
|
||||
Self {
|
||||
binding_count: let_in.attrpath_values().count()
|
||||
+ let_in
|
||||
.inherits()
|
||||
.map(|inherit| inherit.attrs().count())
|
||||
.sum::<usize>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
||||
match token.kind() {
|
||||
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use crate::FileId;
|
||||
use crate::ast;
|
||||
use crate::rules::manual_inherit::ManualInherit;
|
||||
use crate::rules::unquoted_uri::UnquotedUri;
|
||||
|
||||
pub mod manual_inherit;
|
||||
pub mod unquoted_uri;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -27,7 +29,6 @@ impl std::fmt::Display for Diagnostic {
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(dead_code, reason = "retained for planned node rules")]
|
||||
pub trait NodeRule {
|
||||
type Node;
|
||||
|
||||
@@ -47,7 +48,11 @@ pub trait TokenRule {
|
||||
}
|
||||
|
||||
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
||||
let node_diagnostics = file.nodes.iter().flat_map(|_| std::iter::empty());
|
||||
let node_diagnostics = file
|
||||
.nodes
|
||||
.iter()
|
||||
.flat_map(|node| [run_node_rule::<ManualInherit>(node)])
|
||||
.flatten();
|
||||
let token_diagnostics = file
|
||||
.tokens
|
||||
.iter()
|
||||
@@ -63,7 +68,6 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[expect(dead_code, reason = "retained for planned node rules")]
|
||||
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
|
||||
where
|
||||
R: NodeRule,
|
||||
|
||||
23
src/xatu/rules/manual_inherit.rs
Normal file
23
src/xatu/rules/manual_inherit.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::{ast, rules::NodeRule};
|
||||
|
||||
pub struct ManualInherit;
|
||||
|
||||
impl NodeRule for ManualInherit {
|
||||
type Node = ast::Binding;
|
||||
|
||||
const ID: &'static str = "manual_inherit";
|
||||
|
||||
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
|
||||
let ast::NodeData::Binding(binding) = data;
|
||||
|
||||
Some(binding)
|
||||
}
|
||||
|
||||
fn check(node: &Self::Node) -> Option<&'static str> {
|
||||
if node.key == node.value {
|
||||
Some("Use `inherit` for this assignment")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/xatu/testdata/clean.nix
vendored
5
src/xatu/testdata/clean.nix
vendored
@@ -4,4 +4,9 @@
|
||||
path = ./. + "/foo";
|
||||
url = "https://example.com";
|
||||
inherit pkgs;
|
||||
different = other;
|
||||
nested.package = nested.package;
|
||||
"quoted" = quoted;
|
||||
${dynamic} = dynamic;
|
||||
from = pkgs.from;
|
||||
}
|
||||
|
||||
1
src/xatu/testdata/lint_findings.nix
vendored
1
src/xatu/testdata/lint_findings.nix
vendored
@@ -1,3 +1,4 @@
|
||||
{
|
||||
package = package;
|
||||
url = https://example.com;
|
||||
}
|
||||
|
||||
@@ -64,3 +64,16 @@ fn xatu_reports_unquoted_uri() -> googletest::Result<()> {
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_manual_inherit() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
|
||||
|
||||
let manual_inherit_count = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.filter(|diagnostic| diagnostic.finding.rule_id == "manual_inherit")
|
||||
.count();
|
||||
expect_that!(manual_inherit_count, eq(1));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user