@tisankan/slop-check 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +222 -2
- package/action.yml +25 -0
- package/bin/slop-check.js +147 -0
- package/package.json +44 -4
- package/src/action-main.js +9 -0
- package/src/action.js +157 -0
- package/src/check.js +119 -0
- package/src/comments.js +53 -0
- package/src/config.js +133 -0
- package/src/diff.js +65 -0
- package/src/git.js +166 -0
- package/src/languages.js +54 -0
- package/src/report.js +105 -0
- package/src/rules.js +378 -0
- package/src/run.js +64 -0
- package/src/score.js +40 -0
package/src/rules.js
ADDED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
// Every rule slop-check knows. Each rule looks at one added line (or one line of a
|
|
2
|
+
// commit message) and answers yes or no. Weights: high 5, medium 3, low 1.
|
|
3
|
+
|
|
4
|
+
export const EM_DASH = String.fromCharCode(0x2014);
|
|
5
|
+
|
|
6
|
+
const HIGH = 5;
|
|
7
|
+
const MEDIUM = 3;
|
|
8
|
+
const LOW = 1;
|
|
9
|
+
|
|
10
|
+
const TYPESCRIPT_EXTENSIONS = ['ts', 'tsx', 'mts', 'cts'];
|
|
11
|
+
|
|
12
|
+
const PLACEHOLDER_CODE = [
|
|
13
|
+
/^(?:\.{3}|…)\s*(?:existing|rest of|remaining|other|previous|unchanged)\b/i,
|
|
14
|
+
/^todo:?\s*implement(?:\s+(?:this|me|it|here))?\s*\.?$/i,
|
|
15
|
+
/\byour code (?:goes )?here\b/i,
|
|
16
|
+
/\bimplementation goes here\b/i,
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const DEBUG_OUTPUT = [
|
|
20
|
+
/^\s*debugger\s*;?\s*$/,
|
|
21
|
+
/\bbreakpoint\(\)/,
|
|
22
|
+
/\bpdb\.set_trace\(\)/,
|
|
23
|
+
/\bconsole\.debug\(/,
|
|
24
|
+
/\bconsole\.log\(\s*(['"`])[A-Za-z]\w*:\s*\1\s*,/,
|
|
25
|
+
/\bconsole\.log\(\s*(['"`])[^'"`]*(?:\bdebug\b|\bhere\b|\btesting\b|\btest\b|>>>)/i,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
const TYPESCRIPT_ESCAPES = [/\bas any\b/, /:\s*any\b(?![\w$])/];
|
|
29
|
+
|
|
30
|
+
const HISTORY_CHANGE = /^(?:updated|changed|modified|refactored|replaced|switched|moved|converted)\s+(?:to|from)\b/i;
|
|
31
|
+
const HISTORY_VERB = /^(?:updated|changed|modified|added|removed|replaced|refactored|fixed)\s+([a-z]\S*)(?:\s+(\S+))?/i;
|
|
32
|
+
const HISTORY_NEXT_WORDS = [
|
|
33
|
+
'the', 'a', 'an', 'this', 'that', 'support', 'proper', 'missing', 'new', 'unused', 'extra',
|
|
34
|
+
'redundant', 'old',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const NARRATION = /^(?:let me|now let[’']s|as requested|as you requested|i[’']ve|i have|i[’']ll)/i;
|
|
38
|
+
|
|
39
|
+
const DISCLAIMERS = [
|
|
40
|
+
/\bin a real(?:[- ]world)? (?:app|application|project|scenario|system|implementation)\b/i,
|
|
41
|
+
/\bfor (?:demo|demonstration|illustration|illustrative|example) purposes\b/i,
|
|
42
|
+
/\bthis is a (?:simplified|basic|simple|naive|mock|placeholder)\b/i,
|
|
43
|
+
/\byou (?:may|might) want to\b/i,
|
|
44
|
+
/\bin production,? you (?:would|should|might)\b/i,
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const PLACEHOLDER_VALUES = [
|
|
48
|
+
/your[-_ ]?(?:api[-_ ]?key|secret|token|password)[-_ ]?here/i,
|
|
49
|
+
/\bYOUR_[A-Z_]*(?:KEY|TOKEN|SECRET|PASSWORD|ID|URL)\b/,
|
|
50
|
+
/(['"`])<your[-_ ][^>]+>\1/i,
|
|
51
|
+
/\bREPLACE_ME\b/,
|
|
52
|
+
/(['"`])changeme\1/i,
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
// The copyright, registered and trademark signs count as pictographic in Unicode.
|
|
56
|
+
const EMOJI = /(?![©®™])\p{Extended_Pictographic}/u;
|
|
57
|
+
const EMOJI_AT_START = /^(?![©®™])\p{Extended_Pictographic}/u;
|
|
58
|
+
const LOG_CALL = /\b(?:console\.(?:log|info|warn|error|debug)|logger\.\w+|log\.\w+|print|println|fmt\.Print\w*)\s*\(/;
|
|
59
|
+
const CONFIG_EXTENSIONS = ['yml', 'yaml', 'toml'];
|
|
60
|
+
|
|
61
|
+
const BUZZWORDS = [
|
|
62
|
+
/\bdelv(?:e|es|ed|ing) into\b/i,
|
|
63
|
+
/\btapestry\b/i,
|
|
64
|
+
/\bit[’']s worth noting\b/i,
|
|
65
|
+
/\bin today[’']s fast[- ]paced\b/i,
|
|
66
|
+
/\bgame[- ]changer\b/i,
|
|
67
|
+
/\bunleash(?:es|ed|ing)? the\b/i,
|
|
68
|
+
/\belevate your\b/i,
|
|
69
|
+
/\bembark(?:s|ed|ing)? on\b/i,
|
|
70
|
+
/\btestament to\b/i,
|
|
71
|
+
/\bever[- ]evolving\b/i,
|
|
72
|
+
/\bcutting[- ]edge\b/i,
|
|
73
|
+
/\bsupercharg(?:e|es|ed|ing)\b/i,
|
|
74
|
+
/\bnavigat(?:e|ing) the complexities\b/i,
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
// `catch {}` without a name and `except ImportError: pass` are deliberate choices
|
|
78
|
+
// in human code, so only a named catch or a broad Python except counts.
|
|
79
|
+
const EMPTY_CATCH_SAME_LINE = /\bcatch\s*\([^)]*\)\s*\{\s*\}/;
|
|
80
|
+
const CATCH_OPENS_BLOCK = /\bcatch\s*\([^)]*\)\s*\{\s*$/;
|
|
81
|
+
const BROAD_EXCEPT = String.raw`except(?:\s+(?:Exception|BaseException)(?:\s+as\s+\w+)?)?`;
|
|
82
|
+
const PYTHON_EXCEPT_PASS = new RegExp(String.raw`^\s*${BROAD_EXCEPT}\s*:\s*pass\s*$`);
|
|
83
|
+
const PYTHON_BROAD_EXCEPT_OPENS = new RegExp(String.raw`^\s*${BROAD_EXCEPT}\s*:\s*$`);
|
|
84
|
+
const PYTHON_EXCEPT_OPENS = /^\s*except\b([^:]*):\s*$/;
|
|
85
|
+
|
|
86
|
+
const RETHROW_SAME_LINE = /\bcatch\s*\(\s*(\w+)\s*\)\s*\{\s*throw\s+(\w+)\s*;?\s*\}/;
|
|
87
|
+
const CATCH_WITH_NAME_OPENS = /\bcatch\s*\(\s*(\w+)\s*\)\s*\{\s*$/;
|
|
88
|
+
|
|
89
|
+
const AI_ATTRIBUTION = [
|
|
90
|
+
/^co-authored-by:.*\b(?:claude|copilot|cursor|chatgpt|openai|gemini|codeium|windsurf|devin)\b/i,
|
|
91
|
+
/^co-authored-by:.*noreply@anthropic\.com/i,
|
|
92
|
+
/\bgenerated (?:with|by) \[?(?:claude|chatgpt|copilot|cursor|gemini|codex|windsurf)/i,
|
|
93
|
+
/^(?:created|written|authored|powered) (?:with|by) \[?(?:claude|chatgpt|copilot|cursor|gemini|codex)/i,
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
const STOP_WORDS = ['the', 'and', 'for', 'with', 'this', 'that', 'from', 'into', 'then', 'all', 'was'];
|
|
97
|
+
// Comments holding code (commented-out lines, examples) are not prose.
|
|
98
|
+
const CODE_IN_COMMENT = /[=(){}[\];`]/;
|
|
99
|
+
// Section banners such as `# ---- USERS ----` label a block, they do not restate a line.
|
|
100
|
+
const BANNER = /[─-╿]|[-=*#~_]{3,}|^[-=*#~_]{2,}.*[-=*#~_]{2,}$/;
|
|
101
|
+
const SCRIPT_FOLDERS = ['scripts', 'script', 'bin', 'tools', 'cli'];
|
|
102
|
+
|
|
103
|
+
// Code says some verbs with symbols, so `// increment the counter` matches `counter++`.
|
|
104
|
+
const SYMBOL_WORDS = [
|
|
105
|
+
[/\+\+/, 'increment'],
|
|
106
|
+
[/--/, 'decrement'],
|
|
107
|
+
[/\bif\s*\(/, 'check'],
|
|
108
|
+
[/[^=!<>]=[^=>]/, 'set'],
|
|
109
|
+
[/\bnew\s/, 'create'],
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
function matchesAny(text, patterns) {
|
|
113
|
+
for (const pattern of patterns) {
|
|
114
|
+
if (pattern.test(text)) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function isCodeFile(file) {
|
|
122
|
+
return file.style !== 'text';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Command line scripts print on purpose.
|
|
126
|
+
function isScriptFile(file) {
|
|
127
|
+
const folders = file.path.split('/').slice(0, -1);
|
|
128
|
+
return folders.some((folder) => SCRIPT_FOLDERS.includes(folder));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Blank out string contents so `'pick: any'` is not read as a type.
|
|
132
|
+
function withoutStrings(text) {
|
|
133
|
+
return text.replace(/(['"`])(?:\\.|(?!\1)[^\\])*\1/g, '""');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function isHistoryComment(comment) {
|
|
137
|
+
if (/^new:/i.test(comment) || HISTORY_CHANGE.test(comment)) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
const match = HISTORY_VERB.exec(comment);
|
|
141
|
+
if (!match) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
const nextWords = [match[1], match[2] || ''].map((word) => word.toLowerCase());
|
|
145
|
+
if (HISTORY_NEXT_WORDS.includes(nextWords[0])) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
return nextWords[1].endsWith('ing');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function isEmptyCatch(ctx) {
|
|
152
|
+
if (ctx.isCommentLine) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const code = withoutStrings(ctx.text);
|
|
156
|
+
const nextLine = ctx.next ? ctx.next.text.trim() : '';
|
|
157
|
+
if (ctx.file.extension === 'py') {
|
|
158
|
+
return PYTHON_EXCEPT_PASS.test(code) || (PYTHON_BROAD_EXCEPT_OPENS.test(code) && nextLine === 'pass');
|
|
159
|
+
}
|
|
160
|
+
return EMPTY_CATCH_SAME_LINE.test(code) || (CATCH_OPENS_BLOCK.test(code) && nextLine.startsWith('}'));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function isCatchRethrow(ctx) {
|
|
164
|
+
if (ctx.isCommentLine) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const code = withoutStrings(ctx.text);
|
|
168
|
+
const nextLine = ctx.next ? ctx.next.text.trim() : '';
|
|
169
|
+
if (ctx.file.extension === 'py') {
|
|
170
|
+
const opens = PYTHON_EXCEPT_OPENS.exec(code);
|
|
171
|
+
if (!opens) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
const name = /\bas\s+(\w+)\s*$/.exec(opens[1]);
|
|
175
|
+
return nextLine === 'raise' || (name !== null && nextLine === `raise ${name[1]}`);
|
|
176
|
+
}
|
|
177
|
+
const sameLine = RETHROW_SAME_LINE.exec(code);
|
|
178
|
+
if (sameLine) {
|
|
179
|
+
return sameLine[1] === sameLine[2];
|
|
180
|
+
}
|
|
181
|
+
const opens = CATCH_WITH_NAME_OPENS.exec(code);
|
|
182
|
+
return opens !== null && new RegExp(`^throw\\s+${opens[1]}\\s*;?$`).test(nextLine);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function commentWords(comment) {
|
|
186
|
+
const words = comment.toLowerCase().split(/[^a-z]+/);
|
|
187
|
+
return words.filter((word) => word.length >= 3 && !STOP_WORDS.includes(word));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function codeWords(code) {
|
|
191
|
+
const words = new Set();
|
|
192
|
+
const identifiers = code.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase().split(/[^a-z0-9]+/);
|
|
193
|
+
for (const word of identifiers) {
|
|
194
|
+
words.add(word);
|
|
195
|
+
}
|
|
196
|
+
for (const [pattern, word] of SYMBOL_WORDS) {
|
|
197
|
+
if (pattern.test(code)) {
|
|
198
|
+
words.add(word);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return words;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Go requires doc comments to start with the name they describe.
|
|
205
|
+
function isGoDocComment(ctx) {
|
|
206
|
+
if (ctx.file.extension !== 'go') {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
const firstWord = ctx.comment.split(/\s+/)[0].replace(/\W/g, '');
|
|
210
|
+
return firstWord !== '' && new RegExp(`\\b${firstWord}\\b`).test(ctx.next.text);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function isRestatingComment(ctx) {
|
|
214
|
+
if (!ctx.isCommentLine || ctx.previousIsComment || ctx.next === null || ctx.next.isCommentLine) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
if (CODE_IN_COMMENT.test(ctx.comment) || BANNER.test(ctx.comment) || isGoDocComment(ctx)) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
// Single-word section labels such as `// render` are a common human habit.
|
|
221
|
+
const words = commentWords(ctx.comment);
|
|
222
|
+
if (words.length < 2 || words.length > 6) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const code = codeWords(withoutStrings(ctx.next.text));
|
|
226
|
+
let matched = 0;
|
|
227
|
+
for (const word of words) {
|
|
228
|
+
if (code.has(word) || code.has(word.replace(/s$/, ''))) {
|
|
229
|
+
matched += 1;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return matched / words.length >= 0.75;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export const RULES = [
|
|
236
|
+
{
|
|
237
|
+
id: 'empty-catch',
|
|
238
|
+
weight: HIGH,
|
|
239
|
+
defaultOn: true,
|
|
240
|
+
scope: 'line',
|
|
241
|
+
message: 'Error swallowed. Log it, handle it, or let it throw.',
|
|
242
|
+
appliesTo: isCodeFile,
|
|
243
|
+
check: isEmptyCatch,
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
id: 'placeholder-code',
|
|
247
|
+
weight: HIGH,
|
|
248
|
+
defaultOn: true,
|
|
249
|
+
scope: 'line',
|
|
250
|
+
message: 'Unfinished code pasted from a chat.',
|
|
251
|
+
appliesTo: isCodeFile,
|
|
252
|
+
check: (ctx) => ctx.comment !== null && matchesAny(ctx.comment, PLACEHOLDER_CODE),
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
id: 'catch-rethrow',
|
|
256
|
+
weight: MEDIUM,
|
|
257
|
+
defaultOn: true,
|
|
258
|
+
scope: 'line',
|
|
259
|
+
message: 'This try/catch does nothing. Remove it.',
|
|
260
|
+
appliesTo: isCodeFile,
|
|
261
|
+
check: isCatchRethrow,
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
id: 'debug-leftover',
|
|
265
|
+
weight: MEDIUM,
|
|
266
|
+
defaultOn: true,
|
|
267
|
+
scope: 'line',
|
|
268
|
+
message: 'Debug output left behind.',
|
|
269
|
+
appliesTo: (file) => isCodeFile(file) && !file.isTest && !isScriptFile(file),
|
|
270
|
+
check: (ctx) => !ctx.isCommentLine && matchesAny(ctx.text, DEBUG_OUTPUT),
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
id: 'type-escape',
|
|
274
|
+
weight: MEDIUM,
|
|
275
|
+
defaultOn: true,
|
|
276
|
+
scope: 'line',
|
|
277
|
+
message: 'The type checker was silenced instead of satisfied.',
|
|
278
|
+
appliesTo: (file) => !file.isTest && (TYPESCRIPT_EXTENSIONS.includes(file.extension) || file.extension === 'py'),
|
|
279
|
+
check: (ctx) => {
|
|
280
|
+
if (ctx.file.extension === 'py') {
|
|
281
|
+
return ctx.comment !== null && /^type:\s*ignore\s*$/.test(ctx.comment);
|
|
282
|
+
}
|
|
283
|
+
if (ctx.comment !== null && ctx.comment.startsWith('@ts-ignore')) {
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
return !ctx.isCommentLine && matchesAny(withoutStrings(ctx.text), TYPESCRIPT_ESCAPES);
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
id: 'change-history-comment',
|
|
291
|
+
weight: MEDIUM,
|
|
292
|
+
defaultOn: true,
|
|
293
|
+
scope: 'line',
|
|
294
|
+
message: 'Comments describe the code, not your edit. Put history in the commit.',
|
|
295
|
+
appliesTo: isCodeFile,
|
|
296
|
+
check: (ctx) => ctx.comment !== null && !ctx.previousIsComment && isHistoryComment(ctx.comment),
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
id: 'narration-comment',
|
|
300
|
+
weight: MEDIUM,
|
|
301
|
+
defaultOn: true,
|
|
302
|
+
scope: 'line',
|
|
303
|
+
message: 'The AI is talking to you, not to the next reader.',
|
|
304
|
+
appliesTo: isCodeFile,
|
|
305
|
+
check: (ctx) => ctx.comment !== null && NARRATION.test(ctx.comment),
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
id: 'disclaimer-comment',
|
|
309
|
+
weight: MEDIUM,
|
|
310
|
+
defaultOn: true,
|
|
311
|
+
scope: 'line',
|
|
312
|
+
message: 'If it matters in production, do it now or open an issue.',
|
|
313
|
+
appliesTo: isCodeFile,
|
|
314
|
+
check: (ctx) => ctx.comment !== null && matchesAny(ctx.comment, DISCLAIMERS),
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
id: 'placeholder-value',
|
|
318
|
+
weight: MEDIUM,
|
|
319
|
+
defaultOn: true,
|
|
320
|
+
scope: 'line',
|
|
321
|
+
message: 'Placeholder value left in real code.',
|
|
322
|
+
appliesTo: (file) => isCodeFile(file) && !file.isTest && !file.isExample,
|
|
323
|
+
check: (ctx) => !ctx.isCommentLine && matchesAny(ctx.text, PLACEHOLDER_VALUES),
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
id: 'restating-comment',
|
|
327
|
+
weight: LOW,
|
|
328
|
+
defaultOn: true,
|
|
329
|
+
scope: 'line',
|
|
330
|
+
message: 'Comment repeats the next line. Say why, or delete it.',
|
|
331
|
+
appliesTo: isCodeFile,
|
|
332
|
+
check: isRestatingComment,
|
|
333
|
+
},
|
|
334
|
+
{
|
|
335
|
+
id: 'emoji-in-code',
|
|
336
|
+
weight: LOW,
|
|
337
|
+
defaultOn: true,
|
|
338
|
+
scope: 'line',
|
|
339
|
+
message: 'Emoji in code or logs.',
|
|
340
|
+
appliesTo: (file) => isCodeFile(file) && !file.isTest && !CONFIG_EXTENSIONS.includes(file.extension),
|
|
341
|
+
check: (ctx) => {
|
|
342
|
+
if (ctx.comment !== null && EMOJI_AT_START.test(ctx.comment)) {
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
return !ctx.isCommentLine && LOG_CALL.test(ctx.text) && EMOJI.test(ctx.text);
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
{
|
|
349
|
+
id: 'em-dash',
|
|
350
|
+
weight: LOW,
|
|
351
|
+
defaultOn: false,
|
|
352
|
+
scope: 'line',
|
|
353
|
+
message: 'The em dash is the best-known sign of unedited AI text.',
|
|
354
|
+
appliesTo: () => true,
|
|
355
|
+
check: (ctx) => ctx.text.includes(EM_DASH),
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
id: 'buzzword',
|
|
359
|
+
weight: LOW,
|
|
360
|
+
defaultOn: true,
|
|
361
|
+
scope: 'line',
|
|
362
|
+
message: 'Marketing words where plain words would do.',
|
|
363
|
+
appliesTo: () => true,
|
|
364
|
+
check: (ctx) => {
|
|
365
|
+
const text = ctx.file.style === 'text' ? ctx.text : ctx.comment;
|
|
366
|
+
return text !== null && matchesAny(text, BUZZWORDS);
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
id: 'ai-attribution',
|
|
371
|
+
weight: HIGH,
|
|
372
|
+
defaultOn: false,
|
|
373
|
+
scope: 'commit',
|
|
374
|
+
message: 'Tool credit in the commit message.',
|
|
375
|
+
appliesTo: () => false,
|
|
376
|
+
check: (ctx) => matchesAny(ctx.text.trim(), AI_ATTRIBUTION),
|
|
377
|
+
},
|
|
378
|
+
];
|
package/src/run.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The whole check in one call. The CLI and the GitHub Action both start here.
|
|
2
|
+
|
|
3
|
+
import { checkCommit, checkFile, MAX_LINE_LENGTH } from './check.js';
|
|
4
|
+
import { enabledRules, isIgnored, loadConfig } from './config.js';
|
|
5
|
+
import { parseDiff } from './diff.js';
|
|
6
|
+
import { findBase, findBaseFromSha, findRepoRoot, getAllFiles, getCommits, getDiffText, requireCommits } from './git.js';
|
|
7
|
+
import { getLanguage } from './languages.js';
|
|
8
|
+
import { scoreFindings } from './score.js';
|
|
9
|
+
|
|
10
|
+
function countScannedLines(files) {
|
|
11
|
+
let count = 0;
|
|
12
|
+
for (const file of files) {
|
|
13
|
+
for (const line of file.lines) {
|
|
14
|
+
if (line.text.trim() !== '' && line.text.length <= MAX_LINE_LENGTH) {
|
|
15
|
+
count += 1;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return count;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function runCheck(options) {
|
|
23
|
+
const root = findRepoRoot(options.cwd);
|
|
24
|
+
const config = options.config || loadConfig(root);
|
|
25
|
+
const enabled = enabledRules(config);
|
|
26
|
+
const isChecked = (path) => !isIgnored(path, config) && getLanguage(path) !== null;
|
|
27
|
+
|
|
28
|
+
let base = null;
|
|
29
|
+
let files;
|
|
30
|
+
let commits = [];
|
|
31
|
+
|
|
32
|
+
if (options.mode === 'all') {
|
|
33
|
+
files = getAllFiles(root, isChecked);
|
|
34
|
+
} else if (options.mode === 'staged') {
|
|
35
|
+
files = parseDiff(getDiffText(root, { mode: 'staged' })).filter((file) => isChecked(file.path));
|
|
36
|
+
} else {
|
|
37
|
+
requireCommits(root);
|
|
38
|
+
base = options.baseSha ? findBaseFromSha(root, options.baseSha) : findBase(root, options.ref);
|
|
39
|
+
const diffText = getDiffText(root, { mode: 'branch', base: base ? base.sha : null });
|
|
40
|
+
files = parseDiff(diffText).filter((file) => isChecked(file.path));
|
|
41
|
+
commits = base ? getCommits(root, base.sha) : [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const findings = [];
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
findings.push(...checkFile(file, enabled));
|
|
47
|
+
}
|
|
48
|
+
for (const commit of commits) {
|
|
49
|
+
findings.push(...checkCommit(commit, enabled));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const addedLines = countScannedLines(files);
|
|
53
|
+
const score = scoreFindings(findings, addedLines);
|
|
54
|
+
return {
|
|
55
|
+
base: base ? base.name : null,
|
|
56
|
+
mode: options.mode,
|
|
57
|
+
files: files.length,
|
|
58
|
+
addedLines,
|
|
59
|
+
findings,
|
|
60
|
+
score: score.score,
|
|
61
|
+
label: score.label,
|
|
62
|
+
color: score.color,
|
|
63
|
+
};
|
|
64
|
+
}
|
package/src/score.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Turns findings into one number: slop points per 100 added lines, capped at 100.
|
|
2
|
+
|
|
3
|
+
const MIN_LINES = 20;
|
|
4
|
+
const EM_DASH_CAP = 5;
|
|
5
|
+
|
|
6
|
+
const BANDS = [
|
|
7
|
+
{ max: 0, label: 'Hand-written', color: 'brightgreen' },
|
|
8
|
+
{ max: 10, label: 'Mostly human', color: 'green' },
|
|
9
|
+
{ max: 30, label: 'Needs a once-over', color: 'yellow' },
|
|
10
|
+
{ max: 60, label: 'Sloppy', color: 'orange' },
|
|
11
|
+
{ max: 100, label: 'Pure slop', color: 'red' },
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
export function scoreFindings(findings, addedLines) {
|
|
15
|
+
let points = 0;
|
|
16
|
+
// One long markdown file full of em dashes should not drown out real problems.
|
|
17
|
+
const emDashPointsByPlace = new Map();
|
|
18
|
+
|
|
19
|
+
for (const finding of findings) {
|
|
20
|
+
if (finding.rule !== 'em-dash') {
|
|
21
|
+
points += finding.weight;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const place = finding.file || `commit ${finding.commit}`;
|
|
25
|
+
const used = emDashPointsByPlace.get(place) || 0;
|
|
26
|
+
if (used < EM_DASH_CAP) {
|
|
27
|
+
emDashPointsByPlace.set(place, used + finding.weight);
|
|
28
|
+
points += finding.weight;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const lines = Math.max(addedLines, MIN_LINES);
|
|
33
|
+
const score = Math.min(100, Math.round((points * 100) / lines));
|
|
34
|
+
const band = BANDS.find((item) => score <= item.max);
|
|
35
|
+
return { score, points, label: band.label, color: band.color };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function badgeMarkdown(score, color) {
|
|
39
|
+
return `[](https://github.com/tisankanj/slop-check)`;
|
|
40
|
+
}
|