forked from tgstation/tgstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_grep.sh
296 lines (269 loc) · 8.93 KB
/
check_grep.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/bash
set -euo pipefail
#nb: must be bash to support shopt globstar
shopt -s globstar extglob
#ANSI Escape Codes for colors to increase contrast of errors
RED="\033[0;31m"
GREEN="\033[0;32m"
BLUE="\033[0;34m"
NC="\033[0m" # No Color
st=0
# check for ripgrep
if command -v rg >/dev/null 2>&1; then
grep=rg
pcre2_support=1
if [ ! rg -P '' >/dev/null 2>&1 ] ; then
pcre2_support=0
fi
code_files="code/**/**.dm"
map_files="_maps/**/**.dmm"
code_x_515="code/**/!(__byond_version_compat).dm"
else
pcre2_support=0
grep=grep
code_files="-r --include=code/**/**.dm"
map_files="-r --include=_maps/**/**.dmm"
code_x_515="-r --include=code/**/!(__byond_version_compat).dm"
fi
echo -e "${BLUE}Using grep provider at $(which $grep)${NC}"
part=0
section() {
echo -e "${BLUE}Checking for $1${NC}..."
part=0
}
part() {
part=$((part+1))
padded=$(printf "%02d" $part)
echo -e "${GREEN} $padded- $1${NC}"
}
section "map issues"
part "TGM"
if $grep -U '^".+" = \(.+\)' $map_files; then
echo
echo -e "${RED}ERROR: Non-TGM formatted map detected. Please convert it using Map Merger!${NC}"
st=1
fi;
part "comments"
if $grep '//' $map_files | $grep -v '//MAP CONVERTED BY dmm2tgm.py THIS HEADER COMMENT PREVENTS RECONVERSION, DO NOT REMOVE' | $grep -v 'name|desc'; then
echo
echo -e "${RED}ERROR: Unexpected commented out line detected in this map file. Please remove it.${NC}"
st=1
fi;
part "iconstate tags"
if $grep '^\ttag = "icon' $map_files; then
echo
echo -e "${RED}ERROR: Tag vars from icon state generation detected in maps, please remove them.${NC}"
st=1
fi;
part "invalid map procs"
if $grep '(new|newlist|icon|matrix|sound)\(.+\)' $map_files; then
echo
echo -e "${RED}ERROR: Using unsupported procs in variables in a map file! Please remove all instances of this.${NC}"
st=1
fi;
part "armor lists"
if $grep '\tarmor = list' $map_files; then
echo
echo -e "${RED}ERROR: Outdated armor list in map file.${NC}"
st=1
fi;
part "common spelling mistakes"
if $grep -i 'nanotransen' $map_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of Nanotrasen detected in maps, please remove the extra N(s).${NC}"
st=1
fi;
if $grep 'NanoTrasen' $map_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of Nanotrasen detected in maps, please uncapitalize the T(s).${NC}"
st=1
fi;
if $grep -i'centcomm' $map_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of CentCom detected in maps, please remove the extra M(s).${NC}"
st=1
fi;
section "whitespace issues"
part "space indentation"
if $grep '(^ {2})|(^ [^ * ])|(^ +)' $code_files; then
echo
echo -e "${RED}ERROR: Space indentation detected, please use tab indentation.${NC}"
st=1
fi;
part "mixed indentation"
if $grep '^\t+ [^ *]' $code_files; then
echo
echo -e "${RED}ERROR: Mixed <tab><space> indentation detected, please stick to tab indentation.${NC}"
st=1
fi;
section "unit tests"
unit_test_files="code/modules/unit_tests/**/**.dm"
part "mob/living/carbon/human usage"
if $grep 'allocate\(/mob/living/carbon/human[,\)]' $unit_test_files ||
$grep 'new /mob/living/carbon/human\s?\(' $unit_test_files ||
$grep 'var/mob/living/carbon/human/\w+\s?=\s?new' $unit_test_files ; then
echo
echo -e "${RED}ERROR: Usage of mob/living/carbon/human detected in a unit test, please use mob/living/carbon/human/consistent.${NC}"
st=1
fi;
section "common mistakes"
part "global vars"
if $grep '^/*var/' $code_files; then
echo
echo -e "${RED}ERROR: Unmanaged global var use detected in code, please use the helpers.${NC}"
st=1
fi;
part "proc args with var/"
if $grep '^/[\w/]\S+\(.*(var/|, ?var/.*).*\)' $code_files; then
echo
echo -e "${RED}ERROR: Changed files contains a proc argument starting with 'var'.${NC}"
st=1
fi;
part "can_perform_action argument check"
if $grep 'can_perform_action\(\s*\)' $code_files; then
echo
echo -e "${RED}ERROR: Found a can_perform_action() proc with improper arguments.${NC}"
st=1
fi;
part "src as a trait source" # ideally we'd lint / test for ANY datum reference as a trait source, but 'src' is the most common.
if $grep -i '(add_trait|remove_trait)\(.+,\s*.+,\s*src\)' $code_files; then
echo
echo -e "${RED}ERROR: Using 'src' as a trait source. Source must be a string key - dont't use references to datums as a source, perhaps use 'REF(src)'.${NC}"
st=1
fi;
if $grep -i '(add_traits|remove_traits)\(.+,\s*src\)' $code_files; then
echo
echo -e "${RED}ERROR: Using 'src' as trait sources. Source must be a string key - dont't use references to datums as sources, perhaps use 'REF(src)'.${NC}"
st=1
fi;
part "balloon_alert sanity"
if $grep 'balloon_alert\(".*"\)' $code_files; then
echo
echo -e "${RED}ERROR: Found a balloon alert with improper arguments.${NC}"
st=1
fi;
if $grep 'balloon_alert(.*span_)' $code_files; then
echo
echo -e "${RED}ERROR: Balloon alerts should never contain spans.${NC}"
st=1
fi;
part "balloon_alert idiomatic usage"
if $grep 'balloon_alert\(.*?, ?"[A-Z]' $code_files; then
echo
echo -e "${RED}ERROR: Balloon alerts should not start with capital letters. This includes text like 'AI'. If this is a false positive, wrap the text in UNLINT().${NC}"
st=1
fi;
part "update_icon_updates_onmob element usage"
if $grep 'AddElement\(/datum/element/update_icon_updates_onmob.+ITEM_SLOT_HANDS' $code_files; then
echo
echo -e "${RED}ERROR: update_icon_updates_onmob element automatically updates ITEM_SLOT_HANDS, this is redundant and should be removed.${NC}"
st=1
fi;
part "common spelling mistakes"
if $grep -i 'centcomm' $code_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of CentCom detected in code, please remove the extra M(s).${NC}"
st=1
fi;
if $grep -ni 'nanotransen' $code_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of Nanotrasen detected in code, please remove the extra N(s).${NC}"
st=1
fi;
if $grep 'NanoTrasen' $code_files; then
echo
echo -e "${RED}ERROR: Misspelling(s) of Nanotrasen detected in code, please uncapitalize the T(s).${NC}"
st=1
fi;
part "map json naming"
if ls _maps/*.json | $grep "[A-Z]"; then
echo
echo -e "${RED}ERROR: Uppercase in a map .JSON file detected, these must be all lowercase.${NC}"
st=1
fi;
part "map json sanity"
for json in _maps/*.json
do
map_path=$(jq -r '.map_path' $json)
while read map_file; do
filename="_maps/$map_path/$map_file"
if [ ! -f $filename ]
then
echo
echo -e "${RED}ERROR: Found an invalid file reference to $filename in _maps/$json ${NC}"
st=1
fi
done < <(jq -r '[.map_file] | flatten | .[]' $json)
done
part "updatepaths validity"
missing_txt_lines=$(find tools/UpdatePaths/Scripts -type f ! -name "*.txt" | wc -l)
if [ $missing_txt_lines -gt 0 ]; then
echo
echo -e "${RED}ERROR: Found an UpdatePaths File that doesn't end in .txt! Please add the proper file extension!${NC}"
st=1
fi;
number_prefix_lines=$(find tools/UpdatePaths/Scripts -type f | wc -l)
valid_number_prefix_lines=$(find tools/UpdatePaths/Scripts -type f | $grep -P "\d+_(.+)" | wc -l)
if [ $valid_number_prefix_lines -ne $number_prefix_lines ]; then
echo
echo -e "${RED}ERROR: Detected an UpdatePaths File that doesn't start with the PR number! Please add the proper number prefix!${NC}"
st=1
fi;
section "515 Proc Syntax"
part "proc ref syntax"
if $grep '\.proc/' $code_x_515 ; then
echo
echo -e "${RED}ERROR: Outdated proc reference use detected in code, please use proc reference helpers.${NC}"
st=1
fi;
if [ "$pcre2_support" -eq 1 ]; then
section "regexes requiring PCRE2"
part "empty variable values"
if $grep -PU '{\n\t},' $map_files; then
echo
echo -e "${RED}ERROR: Empty variable value list detected in map file. Please remove the curly brackets entirely.${NC}"
st=1
fi;
part "to_chat sanity"
if $grep -P 'to_chat\((?!.*,).*\)' $code_files; then
echo
echo -e "${RED}ERROR: to_chat() missing arguments.${NC}"
st=1
fi;
part "timer flag sanity"
if $grep -P 'addtimer\((?=.*TIMER_OVERRIDE)(?!.*TIMER_UNIQUE).*\)' $code_files; then
echo
echo -e "${RED}ERROR: TIMER_OVERRIDE used without TIMER_UNIQUE.${NC}"
st=1
fi
part "trailing newlines"
if $grep -PU '[^\n]$(?!\n)' $code_files; then
echo
echo -e "${RED}ERROR: File(s) with no trailing newline detected, please add one.${NC}"
st=1
fi
part "datum stockpart sanity"
if $grep -P 'for\b.*/obj/item/stock_parts/(?!cell)(?![\w_]+ in )' $code_files; then
echo
echo -e "${RED}ERROR: Should be using datum/stock_part instead"
st=1
fi;
part "improper atom initialize args"
if $grep -P '^/(obj|mob|turf|area|atom)/.+/Initialize\((?!mapload).*\)' $code_files; then
echo
echo -e "${RED}ERROR: Initialize override without 'mapload' argument.${NC}"
st=1
fi;
else
echo -e "${RED}pcre2 not supported, skipping checks requiring pcre2"
echo -e "if you want to run these checks install ripgrep with pcre2 support.${NC}"
fi
if [ $st = 0 ]; then
echo
echo -e "${GREEN}No errors found using $grep!${NC}"
fi;
if [ $st = 1 ]; then
echo
echo -e "${RED}Errors found, please fix them and try again.${NC}"
fi;
exit $st