Skip to content

Commit 2ad3886

Browse files
committed
chore: add semgrep python-enforce-file-variable-suffix rule (#14)
Signed-off-by: Ronan Abhamon <ronan.abhamon@vates.tech>
1 parent 31fc654 commit 2ad3886

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
rules:
2+
- id: python-enforce-file-variable-suffix
3+
languages: [python]
4+
severity: WARNING
5+
message: "File object `$FILE` must be named `file` or end with `_file`."
6+
patterns:
7+
- pattern-either:
8+
- pattern: |
9+
with open(...) as $FILE:
10+
...
11+
- pattern: |
12+
with ..., open(...) as $FILE, ...:
13+
...
14+
- pattern: |
15+
with Path(...).open(...) as $FILE:
16+
...
17+
- pattern: |
18+
with ..., Path(...).open(...) as $FILE, ...:
19+
...
20+
- pattern: $FILE = open(...)
21+
- pattern: $FILE = Path(...).open(...)
22+
- patterns:
23+
- pattern-either:
24+
- pattern: |
25+
with $PATH.open(...) as $FILE:
26+
...
27+
- pattern: |
28+
with ..., $PATH.open(...) as $FILE, ...:
29+
...
30+
- pattern: $FILE = $PATH.open(...)
31+
- metavariable-regex:
32+
metavariable: $PATH
33+
regex: '^(.*[._])?path$'
34+
35+
- focus-metavariable: $FILE
36+
37+
- metavariable-regex:
38+
metavariable: $FILE
39+
regex: '^(?!(file|\w+_file)$)'

0 commit comments

Comments
 (0)