feat(xatu): add manual inherit lint
This commit is contained in:
@@ -8,6 +8,7 @@ rust_library(
|
|||||||
"file_store.rs",
|
"file_store.rs",
|
||||||
"parser.rs",
|
"parser.rs",
|
||||||
"rules.rs",
|
"rules.rs",
|
||||||
|
"rules/manual_inherit.rs",
|
||||||
"rules/unquoted_uri.rs",
|
"rules/unquoted_uri.rs",
|
||||||
"xatu.rs",
|
"xatu.rs",
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ impl std::fmt::Display for SourceRange {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum NodeData {
|
pub enum NodeData {
|
||||||
Apply(Apply),
|
Apply(Apply),
|
||||||
|
AttrpathValue(AttrpathValue),
|
||||||
LetIn(LetIn),
|
LetIn(LetIn),
|
||||||
Inherit(Inherit),
|
Inherit(Inherit),
|
||||||
}
|
}
|
||||||
@@ -38,6 +39,12 @@ pub struct Apply {
|
|||||||
pub function: Expr,
|
pub function: Expr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct AttrpathValue {
|
||||||
|
pub key: Ident,
|
||||||
|
pub value: Ident,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Ident(Ident),
|
Ident(Ident),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use rnix::ast::HasEntry;
|
use rnix::ast::{Attr, HasEntry};
|
||||||
use rowan::ast::AstNode;
|
use rowan::ast::AstNode;
|
||||||
|
|
||||||
use crate::{FileId, ast};
|
use crate::{FileId, ast};
|
||||||
@@ -38,6 +38,7 @@ fn convert_syntax(syntax: rnix::SyntaxNode) -> (Vec<ast::Node>, Vec<ast::Token>)
|
|||||||
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),
|
||||||
|
rnix::SyntaxKind::NODE_ATTRPATH_VALUE => convert_attrpath_value(node),
|
||||||
rnix::SyntaxKind::NODE_LET_IN => convert_let_in(node),
|
rnix::SyntaxKind::NODE_LET_IN => convert_let_in(node),
|
||||||
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
rnix::SyntaxKind::NODE_INHERIT => convert_inherit(node),
|
||||||
_ => None,
|
_ => None,
|
||||||
@@ -65,6 +66,40 @@ impl TryFrom<rnix::ast::Apply> for ast::Apply {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn convert_attrpath_value(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||||
|
let attrpath_value = rnix::ast::AttrpathValue::cast(node)?;
|
||||||
|
let range = attrpath_value.syntax().text_range().into();
|
||||||
|
let data = attrpath_value.try_into().ok()?;
|
||||||
|
|
||||||
|
Some(ast::Node {
|
||||||
|
range,
|
||||||
|
data: ast::NodeData::AttrpathValue(data),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<rnix::ast::AttrpathValue> for ast::AttrpathValue {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(attrpath_value: rnix::ast::AttrpathValue) -> Result<Self, Self::Error> {
|
||||||
|
let mut attrs = attrpath_value.attrpath().ok_or(())?.attrs();
|
||||||
|
let key = match attrs.next().ok_or(())? {
|
||||||
|
Attr::Ident(ident) => ident.into(),
|
||||||
|
_ => return Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if attrs.next().is_some() {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = match attrpath_value.value().ok_or(())? {
|
||||||
|
rnix::ast::Expr::Ident(ident) => ident.into(),
|
||||||
|
_ => return Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Self { key, value })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl TryFrom<rnix::ast::Expr> for ast::Expr {
|
impl TryFrom<rnix::ast::Expr> for ast::Expr {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
use crate::FileId;
|
use crate::FileId;
|
||||||
use crate::ast;
|
use crate::ast;
|
||||||
|
use crate::rules::manual_inherit::ManualInherit;
|
||||||
use crate::rules::unquoted_uri::UnquotedUri;
|
use crate::rules::unquoted_uri::UnquotedUri;
|
||||||
|
|
||||||
|
pub mod manual_inherit;
|
||||||
pub mod unquoted_uri;
|
pub mod unquoted_uri;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -27,7 +29,6 @@ impl std::fmt::Display for Diagnostic {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[expect(dead_code, reason = "retained for planned node rules")]
|
|
||||||
pub trait NodeRule {
|
pub trait NodeRule {
|
||||||
type Node;
|
type Node;
|
||||||
|
|
||||||
@@ -47,7 +48,11 @@ pub trait TokenRule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
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
|
let token_diagnostics = file
|
||||||
.tokens
|
.tokens
|
||||||
.iter()
|
.iter()
|
||||||
@@ -63,7 +68,6 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[expect(dead_code, reason = "retained for planned node rules")]
|
|
||||||
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
|
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
|
||||||
where
|
where
|
||||||
R: NodeRule,
|
R: NodeRule,
|
||||||
|
|||||||
24
src/xatu/rules/manual_inherit.rs
Normal file
24
src/xatu/rules/manual_inherit.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use crate::{ast, rules::NodeRule};
|
||||||
|
|
||||||
|
pub struct ManualInherit;
|
||||||
|
|
||||||
|
impl NodeRule for ManualInherit {
|
||||||
|
type Node = ast::AttrpathValue;
|
||||||
|
|
||||||
|
const ID: &'static str = "manual_inherit";
|
||||||
|
|
||||||
|
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
|
||||||
|
match data {
|
||||||
|
ast::NodeData::AttrpathValue(attrpath_value) => Some(attrpath_value),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(node: &Self::Node) -> Option<&'static str> {
|
||||||
|
if node.key == node.value {
|
||||||
|
Some("This assignment is better written with `inherit`")
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/xatu/testdata/clean.nix
vendored
5
src/xatu/testdata/clean.nix
vendored
@@ -4,4 +4,9 @@
|
|||||||
path = ./. + "/foo";
|
path = ./. + "/foo";
|
||||||
url = "https://example.com";
|
url = "https://example.com";
|
||||||
inherit pkgs;
|
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;
|
url = https://example.com;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,3 +64,16 @@ fn xatu_reports_unquoted_uri() -> googletest::Result<()> {
|
|||||||
);
|
);
|
||||||
Ok(())
|
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