feat(xatu): add manual inherit from lint
This commit was merged in pull request #87.
This commit is contained in:
@@ -9,6 +9,7 @@ rust_library(
|
||||
"parser.rs",
|
||||
"rules.rs",
|
||||
"rules/manual_inherit.rs",
|
||||
"rules/manual_inherit_from.rs",
|
||||
"rules/unquoted_uri.rs",
|
||||
"xatu.rs",
|
||||
],
|
||||
|
||||
@@ -34,7 +34,7 @@ pub enum NodeData {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Binding {
|
||||
pub key: Ident,
|
||||
pub value: Ident,
|
||||
pub value: Expr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -42,6 +42,18 @@ pub struct Ident {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Expr {
|
||||
Ident(Ident),
|
||||
Select(Select),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Select {
|
||||
pub receiver: String,
|
||||
pub attr: Ident,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Token {
|
||||
pub range: SourceRange,
|
||||
|
||||
@@ -67,15 +67,24 @@ impl TryFrom<rnix::ast::AttrpathValue> for ast::Binding {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let value = match binding.value().ok_or(())? {
|
||||
rnix::ast::Expr::Ident(ident) => ident.into(),
|
||||
_ => return Err(()),
|
||||
};
|
||||
let value = binding.value().ok_or(())?.try_into()?;
|
||||
|
||||
Ok(Self { key, value })
|
||||
}
|
||||
}
|
||||
|
||||
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(select.try_into()?)),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rnix::ast::Ident> for ast::Ident {
|
||||
fn from(ident: rnix::ast::Ident) -> Self {
|
||||
Self {
|
||||
@@ -84,6 +93,31 @@ 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> {
|
||||
if select.or_token().is_some() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let mut attrs = select.attrpath().ok_or(())?.attrs();
|
||||
let attr = match attrs.next().ok_or(())? {
|
||||
Attr::Ident(ident) => ident.into(),
|
||||
_ => return Err(()),
|
||||
};
|
||||
|
||||
if attrs.next().is_some() {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
receiver: select.expr().ok_or(())?.syntax().to_string(),
|
||||
attr,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_token(token: rnix::SyntaxToken) -> Option<ast::Token> {
|
||||
match token.kind() {
|
||||
rnix::SyntaxKind::TOKEN_URI => Some(ast::Token {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::FileId;
|
||||
use crate::ast;
|
||||
use crate::rules::manual_inherit::ManualInherit;
|
||||
use crate::rules::manual_inherit_from::ManualInheritFrom;
|
||||
use crate::rules::unquoted_uri::UnquotedUri;
|
||||
|
||||
pub mod manual_inherit;
|
||||
pub mod manual_inherit_from;
|
||||
pub mod unquoted_uri;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -51,7 +53,12 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
||||
let node_diagnostics = file
|
||||
.nodes
|
||||
.iter()
|
||||
.flat_map(|node| [run_node_rule::<ManualInherit>(node)])
|
||||
.flat_map(|node| {
|
||||
[
|
||||
run_node_rule::<ManualInherit>(node),
|
||||
run_node_rule::<ManualInheritFrom>(node),
|
||||
]
|
||||
})
|
||||
.flatten();
|
||||
let token_diagnostics = file
|
||||
.tokens
|
||||
|
||||
@@ -14,10 +14,11 @@ impl NodeRule for ManualInherit {
|
||||
}
|
||||
|
||||
fn check(node: &Self::Node) -> Option<&'static str> {
|
||||
if node.key == node.value {
|
||||
Some("Use `inherit` for this assignment")
|
||||
} else {
|
||||
None
|
||||
match &node.value {
|
||||
ast::Expr::Ident(value) if node.key == *value => {
|
||||
Some("Use `inherit` for this assignment")
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
src/xatu/rules/manual_inherit_from.rs
Normal file
24
src/xatu/rules/manual_inherit_from.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::{ast, rules::NodeRule};
|
||||
|
||||
pub struct ManualInheritFrom;
|
||||
|
||||
impl NodeRule for ManualInheritFrom {
|
||||
type Node = ast::Binding;
|
||||
|
||||
const ID: &'static str = "manual_inherit_from";
|
||||
|
||||
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
|
||||
let ast::NodeData::Binding(binding) = data;
|
||||
|
||||
Some(binding)
|
||||
}
|
||||
|
||||
fn check(node: &Self::Node) -> Option<&'static str> {
|
||||
match &node.value {
|
||||
ast::Expr::Select(select) if node.key == select.attr => {
|
||||
Some("Use `inherit` for this selected attribute")
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
3
src/xatu/testdata/clean.nix
vendored
3
src/xatu/testdata/clean.nix
vendored
@@ -8,5 +8,6 @@
|
||||
nested.package = nested.package;
|
||||
"quoted" = quoted;
|
||||
${dynamic} = dynamic;
|
||||
from = pkgs.from;
|
||||
from = pkgs.from.other;
|
||||
fallback = pkgs.fallback or null;
|
||||
}
|
||||
|
||||
1
src/xatu/testdata/lint_findings.nix
vendored
1
src/xatu/testdata/lint_findings.nix
vendored
@@ -1,4 +1,5 @@
|
||||
{
|
||||
package = package;
|
||||
from = pkgs.from;
|
||||
url = https://example.com;
|
||||
}
|
||||
|
||||
@@ -77,3 +77,16 @@ fn xatu_reports_manual_inherit() -> googletest::Result<()> {
|
||||
expect_that!(manual_inherit_count, eq(1));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_manual_inherit_from() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
|
||||
|
||||
let manual_inherit_from_count = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.filter(|diagnostic| diagnostic.finding.rule_id == "manual_inherit_from")
|
||||
.count();
|
||||
expect_that!(manual_inherit_from_count, eq(1));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user