Skip to content

Commit

Permalink
Add check 'missing-patch-comment' using rust and rnix
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcgibbo authored and jtojnar committed Feb 1, 2021
1 parent 525c176 commit 0864566
Show file tree
Hide file tree
Showing 19 changed files with 705 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
uses: cachix/install-nix-action@v12

- name: Run tests
run: nix-shell --run ./run-tests.py
run: nix run -c ./run-tests.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ast-checks/target
222 changes: 222 additions & 0 deletions ast-checks/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ast-checks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "nixpkgs-hammer-ast-checks"
version = "0.0.0"
authors = ["Robert T. McGibbon <[email protected]>"]
edition = "2018"

[[bin]]
name = "missing-patch-comment"
path = "src/missing-patch-comment.rs"

[dependencies]
codespan = "0.11"
rnix = "0.7.0"
rowan = { version = "0.6.2", features = [ "serde1" ] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
34 changes: 34 additions & 0 deletions ast-checks/src/comment_finders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::tree_utils::{next_siblings, prev_siblings, walk_kind};
use rnix::{NodeOrToken, SyntaxElement, SyntaxKind::*, SyntaxNode};

pub fn find_comment_within(node: &SyntaxNode) -> Option<String> {
// Find the first `TOKEN_COMMENT` within.
let tok = walk_kind(node, TOKEN_COMMENT).next()?;
// Iterate over sibling comments and concatenate them.
let node_iter = next_siblings(&tok);
let doc = collect_comment_text(node_iter);
Some(doc).filter(|it| !it.is_empty())
}

pub fn find_comment_above(node: &SyntaxNode) -> Option<String> {
// Note: The `prev_siblings` iterator includes self, which
// is not a `TOKEN_COMMENT`, so we need to skip one.
let node_iter = prev_siblings(&NodeOrToken::Node(node.clone())).skip(1);
let doc = collect_comment_text(node_iter);
Some(doc).filter(|it| !it.is_empty())
}

fn collect_comment_text(node_iter: impl Iterator<Item = SyntaxElement>) -> String {
// Take text of all immediately subsequent `TOKEN_COMMENT`s,
// skipping over whitespace-only tokens.
// Note this would be more clearly written using `map_while`, but that
// does not seem to be in Rust stable yet.
node_iter
.filter_map(|node| node.into_token())
.take_while(|tok| tok.kind() == TOKEN_COMMENT || tok.kind().is_trivia())
.map(|tok| tok.text().to_string())
.map(|s| s.trim_start_matches('#').trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("\n")
}
16 changes: 16 additions & 0 deletions ast-checks/src/common_structs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct SourceLocation {
pub column: usize,
pub line: usize,
pub file: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NixpkgsHammerMessage {
pub cond: bool,
pub locations: Vec<SourceLocation>,
pub msg: &'static str,
pub name: &'static str,
}
Loading

0 comments on commit 0864566

Please sign in to comment.