gritql icon indicating copy to clipboard operation
gritql copied to clipboard

Document comment patterns

Open joelburget opened this issue 1 year ago • 5 comments

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

joelburget avatar Nov 03 '24 14:11 joelburget

`/* TODO$_ */` finds different locations from `// TODO$_` -- both only match that specific type of comment 👎🏻

joelburget avatar Nov 03 '24 14:11 joelburget

While we're here I also tried the AST node example from the docs but wasn't able to make it work

Screenshot 2024-11-03 at 7 41 24 AM Screenshot 2024-11-03 at 7 41 40 AM Screenshot 2024-11-03 at 7 42 16 AM

joelburget avatar Nov 03 '24 15:11 joelburget

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: image

morgante avatar Nov 04 '24 01:11 morgante

@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.

jdfm avatar May 08 '25 13:05 jdfm

@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.

jdfm avatar May 08 '25 19:05 jdfm