-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matching comments is not supported #5931
Comments
This issue is synced in Linear at https://linear.app/r2c/issue/PA-1775/matching-python-comments-is-not-well-supported. Note: this link is for r2c use only and is not accessible publicly. |
I think comments are generally ignored by semgrep in any language. Today I also ran into a use case where I want to match comments, but in PHP: the comments contain some documentation on parameters, and I want to cross-check that against the code with semgrep. So matching comments would be desirable. |
This issue is being marked |
Hi there. It's not impossible that we could add some support for matching comments but I haven't found a simple approach that's also generic yet. Thanks for the clear use case, it's helpful. Preliminary work to have comments in the AST is almost done: semgrep/ocaml-tree-sitter-core#48 |
Chiming in with another use case - catching and preventing linter exemption comments. In ruby, Rubocop rules can be disabled with comments like |
It's pretty much the same for Python, tooling generally supports a way to opt out (e.g. OTOH the command of these while parseable is generally not regular, so I think |
Problem description
It would be nice if Semgrep could be used to match python comments in a simpler way. For example, I wanted to match python functions with the
@route
decorator, did not have the@protected_endpoint
decorator, and did not have a specific comment (# Hello
in the examples below).This was the best solution I came up with (https://semgrep.dev/s/pwr0).
The
pattern-regex
works as follows:# Hello
)def
, i.e., until the beginning of a function ([\s\S]*?\ndef.*
)(^[ \t]+[^\n]*\n?)+
)Problems:
Solution I'd like
I would like to be able to match comments with Semgrep. For example, the rule could look something like this (https://semgrep.dev/s/2Dyq):
The text was updated successfully, but these errors were encountered: