Document comment patterns
The docs don't cover matching comments. It seems like there is support and I was able to reverse-engineer a bit of it, but wasn't able to figure out how to match comments with a specific format (e.g. TODO\((.*)\) where the capture group is a bug identifier).
`// TODO`matches only comments whose text is exactly "TODO"`// TODO$_`matches all comments whose prefix is "TODO"`// r"TODO"`,`// r"TODO.*"`,`r"// TODO"`,`r"// TODO.*"`,`// TODO.*`, and`// TODO*`don't match anything
`/* TODO$_ */` finds different locations from `// TODO$_` -- both only match that specific type of comment 👎🏻
While we're here I also tried the AST node example from the docs but wasn't able to make it work
By default, Grit treats block comments as different from line comments - since sometimes you want to transform one into the other. We can consider adding an equivalence class, but if you use the AST node it works well.
This should do what you want:
engine marzano(0.1)
language js
string(fragment="str")
I'm unable to replicate your concern with AST nodes:
@joelburget This may be a bit late to the party, but, I was able to match block comments with the following pattern:
r"/\*(?s).*?(?-s)\*/"
The above pattern properly captures the following cases (and more):
/* single line */
/** single line with some extra asteristks **/
/*
multi line
*/
/**
*
* jsdoc-like multi-line
*
*/
This makes use of Rust's regex (which GritQL uses for regexes) group flags feature to turn on (and eventually off in the same pattern) the allow . to match \n feature between the opening and closing comment indicators, thus allowing us to match everything in block comments for a single case of block comments at a time.
I was working on something that could help me find cases where we're doing stuff like eslint-ignore or biome-ignore or istanbul-ignore etc... and this helps us capture the relevant comment sections that have these kinds of indicators.
@joelburget I decided to dig a little deeper into the patterns that are part of the standard library of gritql patterns and it seems there is some code that matches comments via, what looks like, a comments ast matcher.
The code in question would be found in .grit/.gritmodules/github.com/getgrit/stdlib/.grit/patterns/js/no_commented_out_code.md in a standard gritql installation.
I'm still a fledgling GritQL user, so I'm still trying to understand how it all works, but, it may help out with your original use case.
In the mean time, it seems the code in the aforementioned gritql pattern also accounts for *-ignore comments (which is what I was trying to do) so I'll try learning how everything in this file is connected.
I really wish there was more extensive documentation on the AST nodes that we can match against in the documentation instead of relying on access to the GritQL Studio.