feat(xatu): report empty let expressions
This commit is contained in:
@@ -10,6 +10,7 @@ rust_library(
|
||||
"rules.rs",
|
||||
"rules/deprecated_to_path.rs",
|
||||
"rules/empty_inherit.rs",
|
||||
"rules/empty_let_in.rs",
|
||||
"rules/unquoted_uri.rs",
|
||||
"xatu.rs",
|
||||
],
|
||||
@@ -33,6 +34,7 @@ rust_test(
|
||||
"testdata/clean.nix",
|
||||
"testdata/invalid.nix",
|
||||
"testdata/lint_findings.nix",
|
||||
"testdata/lint_interactions.nix",
|
||||
"testdata/lint_variants.nix",
|
||||
],
|
||||
deps = [
|
||||
|
||||
@@ -29,6 +29,7 @@ impl std::fmt::Display for SourceRange {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum NodeData {
|
||||
Apply(Apply),
|
||||
LetIn(LetIn),
|
||||
Inherit(Inherit),
|
||||
}
|
||||
|
||||
@@ -54,6 +55,12 @@ pub struct Select {
|
||||
pub attrs: Vec<Ident>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LetIn {
|
||||
pub entry_count: usize,
|
||||
pub inherit_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Inherit {
|
||||
pub attr_count: usize,
|
||||
|
||||
@@ -37,6 +37,7 @@ 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),
|
||||
_ => None,
|
||||
}
|
||||
@@ -102,6 +103,26 @@ impl TryFrom<rnix::ast::Select> for ast::Select {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
use rnix::ast::HasEntry;
|
||||
|
||||
Self {
|
||||
entry_count: let_in.entries().count(),
|
||||
inherit_count: let_in.inherits().count(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_inherit(node: rnix::SyntaxNode) -> Option<ast::Node> {
|
||||
let inherit = rnix::ast::Inherit::cast(node)?;
|
||||
|
||||
|
||||
@@ -2,10 +2,12 @@ use crate::FileId;
|
||||
use crate::ast;
|
||||
use crate::rules::deprecated_to_path::DeprecatedToPath;
|
||||
use crate::rules::empty_inherit::EmptyInherit;
|
||||
use crate::rules::empty_let_in::EmptyLetIn;
|
||||
use crate::rules::unquoted_uri::UnquotedUri;
|
||||
|
||||
pub mod deprecated_to_path;
|
||||
pub mod empty_inherit;
|
||||
pub mod empty_let_in;
|
||||
pub mod unquoted_uri;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -54,6 +56,7 @@ pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
||||
[
|
||||
run_node_rule::<DeprecatedToPath>(node),
|
||||
run_node_rule::<EmptyInherit>(node),
|
||||
run_node_rule::<EmptyLetIn>(node),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
|
||||
24
src/xatu/rules/empty_let_in.rs
Normal file
24
src/xatu/rules/empty_let_in.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::{ast, rules::NodeRule};
|
||||
|
||||
pub struct EmptyLetIn;
|
||||
|
||||
impl NodeRule for EmptyLetIn {
|
||||
type Node = ast::LetIn;
|
||||
|
||||
const ID: &'static str = "empty_let_in";
|
||||
|
||||
fn select(data: &ast::NodeData) -> Option<&Self::Node> {
|
||||
match data {
|
||||
ast::NodeData::LetIn(let_in) => Some(let_in),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn check(node: &Self::Node) -> Option<&'static str> {
|
||||
if node.entry_count == 0 && node.inherit_count == 0 {
|
||||
Some("let expression has no bindings")
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
5
src/xatu/testdata/clean.nix
vendored
5
src/xatu/testdata/clean.nix
vendored
@@ -1,6 +1,11 @@
|
||||
{ pkgs }:
|
||||
|
||||
{
|
||||
nonEmpty =
|
||||
let
|
||||
value = pkgs;
|
||||
in
|
||||
value;
|
||||
path = ./. + "/foo";
|
||||
url = "https://example.com";
|
||||
inherit pkgs;
|
||||
|
||||
1
src/xatu/testdata/lint_findings.nix
vendored
1
src/xatu/testdata/lint_findings.nix
vendored
@@ -2,6 +2,7 @@
|
||||
|
||||
{
|
||||
inherit;
|
||||
empty = let in src;
|
||||
path = builtins.toPath src;
|
||||
url = https://example.com;
|
||||
}
|
||||
|
||||
8
src/xatu/testdata/lint_interactions.nix
vendored
Normal file
8
src/xatu/testdata/lint_interactions.nix
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{ src }:
|
||||
|
||||
let
|
||||
inherit;
|
||||
in
|
||||
{
|
||||
value = src;
|
||||
}
|
||||
5
src/xatu/testdata/lint_variants.nix
vendored
5
src/xatu/testdata/lint_variants.nix
vendored
@@ -1,5 +1,10 @@
|
||||
{ src }:
|
||||
|
||||
{
|
||||
commentedEmpty =
|
||||
let
|
||||
# This comment must not suppress the finding.
|
||||
in
|
||||
src;
|
||||
path = toPath src;
|
||||
}
|
||||
|
||||
@@ -113,6 +113,51 @@ fn xatu_reports_empty_inherit() -> googletest::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_empty_let_in() -> googletest::Result<()> {
|
||||
let source = fixture_source("testdata/lint_findings.nix")?;
|
||||
let range_start = source.source.find("let in src").or_fail()?;
|
||||
let range_end = range_start + "let in src".len();
|
||||
let report = xatu::future_sight(vec![source]).or_fail()?;
|
||||
|
||||
let finding = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| &diagnostic.finding)
|
||||
.find(|finding| finding.rule_id == "empty_let_in")
|
||||
.or_fail()?;
|
||||
let expected_range = format!("{range_start}..{range_end}");
|
||||
expect_that!(finding.range.to_string(), eq(&expected_range));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_commented_empty_let_in() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_variants.nix")?]).or_fail()?;
|
||||
|
||||
let findings = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| diagnostic.finding.rule_id)
|
||||
.collect::<Vec<_>>();
|
||||
expect_that!(findings, contains(eq(&"empty_let_in")));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn empty_inherit_prevents_empty_let_in_finding() -> googletest::Result<()> {
|
||||
let report =
|
||||
xatu::future_sight(vec![fixture_source("testdata/lint_interactions.nix")?]).or_fail()?;
|
||||
|
||||
let findings = report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.map(|diagnostic| diagnostic.finding.rule_id)
|
||||
.collect::<Vec<_>>();
|
||||
expect_that!(findings, eq(&vec!["empty_inherit"]));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[gtest]
|
||||
fn xatu_reports_unquoted_uri() -> googletest::Result<()> {
|
||||
let report = xatu::future_sight(vec![fixture_source("testdata/lint_findings.nix")?]).or_fail()?;
|
||||
|
||||
Reference in New Issue
Block a user