Skip to content

Commit

Permalink
Add rule suggesting use of const instead of global
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Dec 4, 2023
1 parent 5d03c29 commit 51b7847
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
40 changes: 40 additions & 0 deletions rule-tests/__snapshots__/use-const-snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
id: use-const
snapshots:
global a = b"";:
fixed: const a = b"";
labels:
- source: global
style: primary
start: 0
end: 6
- source: b""
style: secondary
start: 11
end: 14
- source: b""
style: secondary
start: 11
end: 14
- source: global a = b"";
style: secondary
start: 0
end: 15
global x = 1;:
fixed: const x = 1;
labels:
- source: global
style: primary
start: 0
end: 6
- source: '1'
style: secondary
start: 11
end: 12
- source: '1'
style: secondary
start: 11
end: 12
- source: global x = 1;
style: secondary
start: 0
end: 13
8 changes: 8 additions & 0 deletions rule-tests/use-const.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
id: use-const
valid:
- "global x: m::X;"
- "global x: m::X = [];"
- "const x = 1;"
invalid:
- "global x = 1;"
- 'global a = b"";'
19 changes: 19 additions & 0 deletions rules/use-const.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
id: use-const
message: Prefer 'const' over 'global' if possible
severity: hint
language: spicy
rule:
kind: "linkage"
regex: "global"
inside:
kind: "var_decl"
has:
field: "init"
kind: "expression"
any:
- has:
kind: "bytes"
- has:
kind: "integer"

fix: "const"

0 comments on commit 51b7847

Please sign in to comment.