-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check 'missing-patch-comment' using rust and rnix
- Loading branch information
Showing
19 changed files
with
705 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ast-checks/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.