ast-grep / ast-grep

⚡A CLI tool for code structural search, lint and rewriting. Written in Rust
https://ast-grep.github.io/
MIT License
7.37k stars 167 forks source link

[bug] Unable to suppress errors for rule based on Python module kind #1541

Open seamuswn opened 22 hours ago

seamuswn commented 22 hours ago

Please read the FAQ for the bug you encountered.

⏯ Playground Link

https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InB5dGhvbiIsInF1ZXJ5IjoiY29uc29sZS5sb2coJE1BVENIKSIsInJld3JpdGUiOiJsb2dnZXIubG9nKCRNQVRDSCkiLCJzdHJpY3RuZXNzIjoic21hcnQiLCJzZWxlY3RvciI6IiIsImNvbmZpZyI6Ii0tLVxuaWQ6IGVtcHR5X2luaXRfZmlsZXNcbmxhbmd1YWdlOiBQeXRob25cbnNldmVyaXR5OiBlcnJvclxubWVzc2FnZTogfFxuICBgX19pbml0X18ucHlgIGZpbGVzIHNob3VsZCBiZSBlbXB0eSBhc2lkZSBmcm9tIGltcG9ydHNcblxucnVsZTpcbiAga2luZDogbW9kdWxlXG4gIGhhczpcbiAgICBub3Q6XG4gICAgICBhbnk6XG4gICAgICAgIC0ga2luZDogaW1wb3J0X2Zyb21fc3RhdGVtZW50XG4gICAgICAgIC0ga2luZDogaW1wb3J0X3N0YXRlbWVudFxuXG5maWxlczpcbiAgLSAnKiovX19pbml0X18ucHknXG4iLCJzb3VyY2UiOiIjIGFzdC1ncmVwLWlnbm9yZTogZW1wdHlfaW5pdF9maWxlc1xuZnJvbSBmb28gaW1wb3J0IGJhclxuaW1wb3J0IGZvb1xuXG5mb28gPSBiYXIifQ==

💻 Code

---
id: empty_init_files
language: Python
severity: error
message: |
  `__init__.py` files should be empty aside from imports

rule:
  kind: module
  has:
    not:
      any:
        - kind: import_from_statement
        - kind: import_statement

files:
 - '**/__init__.py'

🙁 Actual behavior

With rule above, and # ast-grep-ignore: empty_init_files as first line in the module, ast-grep complains about /my/path/__init__.py:1:1: error[empty_init_files]:init.pyfiles should be empty aside from imports but also /my/path/__init__.py:1:1: help[unused-suppression]: Unused '@ast-grep-ignore' directive.

🙂 Expected behavior

Placing module suppression on very first line should result in this rule being ignored and ast-grep scan passing.

Additional information about the issue

No response

HerringtonDarkholme commented 20 hours ago

At the moment ast-grep does not support file level suppression. You can try using exclude to suppress warning.

Another issue here is that comment suppression seems to be unable suppress module, the root AST. I will investigate it later.

HerringtonDarkholme commented 11 minutes ago

The issue here is how ast-grep handle ignore comment

# ast-grep-ignore
'this only ignores the next line'
'this only ignores the current line' # ast-grep-ignore

So the leading comment does not ignore the module ast