@tisankan/slop-check 1.0.0 → 1.1.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/README.md +4 -3
- package/action.yml +4 -0
- package/package.json +1 -1
- package/src/action.js +46 -0
- package/src/check.js +18 -1
- package/src/rules.js +39 -0
package/README.md
CHANGED
|
@@ -109,13 +109,14 @@ jobs:
|
|
|
109
109
|
max-score: 30
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
-
The action writes the report to the job summary
|
|
113
|
-
pull request up to date.
|
|
112
|
+
The action writes the report to the job summary, prints inline annotations on
|
|
113
|
+
the diff, and keeps one comment on the pull request up to date.
|
|
114
114
|
|
|
115
115
|
| Input | Default | What it does |
|
|
116
116
|
| --- | --- | --- |
|
|
117
117
|
| `max-score` | empty | Fail the job when the score is above this number. Empty never fails. |
|
|
118
118
|
| `comment` | `true` | Post one PR comment and update it on every push. |
|
|
119
|
+
| `annotations` | `true` | Show findings as inline annotations on the diff. |
|
|
119
120
|
| `github-token` | `github.token` | Token used for the comment. |
|
|
120
121
|
|
|
121
122
|
Output: `score`. Pull requests from forks get a read-only token, so they get the
|
|
@@ -125,7 +126,7 @@ job summary but no comment.
|
|
|
125
126
|
|
|
126
127
|
| Rule | Weight | Catches |
|
|
127
128
|
| --- | --- | --- |
|
|
128
|
-
| `empty-catch` | high | `catch (e) {}
|
|
129
|
+
| `empty-catch` | high | `catch (e) {}`, `except: pass`, `if err != nil {}` and empty Ruby `rescue` |
|
|
129
130
|
| `placeholder-code` | high | `// ... existing code ...`, `// TODO: implement` |
|
|
130
131
|
| `catch-rethrow` | medium | A catch block that only rethrows |
|
|
131
132
|
| `debug-leftover` | medium | `debugger`, `console.log('user:', user)`, `breakpoint()` |
|
package/action.yml
CHANGED
|
@@ -13,6 +13,10 @@ inputs:
|
|
|
13
13
|
description: Post one PR comment with the score and keep it updated. Set to false to only write the job summary.
|
|
14
14
|
required: false
|
|
15
15
|
default: 'true'
|
|
16
|
+
annotations:
|
|
17
|
+
description: Print inline annotations for findings on the PR diff. Set to false to disable.
|
|
18
|
+
required: false
|
|
19
|
+
default: 'true'
|
|
16
20
|
github-token:
|
|
17
21
|
description: Token used to post the PR comment.
|
|
18
22
|
required: false
|
package/package.json
CHANGED
package/src/action.js
CHANGED
|
@@ -13,12 +13,15 @@ const MARKER = '<!-- slop-check -->';
|
|
|
13
13
|
const ZERO_SHA = '0'.repeat(40);
|
|
14
14
|
// GitHub rejects comments over 65536 characters.
|
|
15
15
|
const MAX_REPORT_LENGTH = 60000;
|
|
16
|
+
// GitHub shows at most 10 warning annotations per step.
|
|
17
|
+
const MAX_ANNOTATIONS = 10;
|
|
16
18
|
|
|
17
19
|
function readInputs(env) {
|
|
18
20
|
const maxScoreText = (env['INPUT_MAX-SCORE'] || '').trim();
|
|
19
21
|
return {
|
|
20
22
|
maxScore: maxScoreText === '' ? null : Number(maxScoreText),
|
|
21
23
|
comment: (env.INPUT_COMMENT || 'true').trim() !== 'false',
|
|
24
|
+
annotations: (env.INPUT_ANNOTATIONS || 'true').trim().toLowerCase() !== 'false',
|
|
22
25
|
token: env['INPUT_GITHUB-TOKEN'] || '',
|
|
23
26
|
};
|
|
24
27
|
}
|
|
@@ -57,6 +60,46 @@ function writeLog(text) {
|
|
|
57
60
|
process.stdout.write(`::stop-commands::${token}\n${text}::${token}::\n`);
|
|
58
61
|
}
|
|
59
62
|
|
|
63
|
+
function escapeMessage(value) {
|
|
64
|
+
return String(value)
|
|
65
|
+
.replaceAll('%', '%25')
|
|
66
|
+
.replaceAll('\r', '%0D')
|
|
67
|
+
.replaceAll('\n', '%0A');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function escapeProperty(value) {
|
|
71
|
+
return escapeMessage(value)
|
|
72
|
+
.replaceAll(':', '%3A')
|
|
73
|
+
.replaceAll(',', '%2C');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function byWeightThenLocation(a, b) {
|
|
77
|
+
if (a.weight !== b.weight) {
|
|
78
|
+
return b.weight - a.weight;
|
|
79
|
+
}
|
|
80
|
+
if (a.file !== b.file) {
|
|
81
|
+
return a.file < b.file ? -1 : 1;
|
|
82
|
+
}
|
|
83
|
+
return a.line - b.line;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function formatAnnotations(findings) {
|
|
87
|
+
const fileFindings = findings.filter((finding) => finding.file && Number.isInteger(finding.line));
|
|
88
|
+
fileFindings.sort(byWeightThenLocation);
|
|
89
|
+
return fileFindings.slice(0, MAX_ANNOTATIONS).map((finding) => {
|
|
90
|
+
const file = escapeProperty(finding.file);
|
|
91
|
+
const title = escapeProperty(finding.rule);
|
|
92
|
+
const message = escapeMessage(finding.message);
|
|
93
|
+
return `::warning file=${file},line=${finding.line},title=${title}::${message}`;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function writeAnnotations(findings) {
|
|
98
|
+
for (const line of formatAnnotations(findings)) {
|
|
99
|
+
process.stdout.write(`${line}\n`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
60
103
|
function buildReport(result) {
|
|
61
104
|
let text = formatText(result, { color: false });
|
|
62
105
|
if (text.length > MAX_REPORT_LENGTH) {
|
|
@@ -125,6 +168,9 @@ export async function runAction({ env, fetchFn, cwd }) {
|
|
|
125
168
|
}
|
|
126
169
|
|
|
127
170
|
writeLog(formatText(result, { color: false }));
|
|
171
|
+
if (inputs.annotations) {
|
|
172
|
+
writeAnnotations(result.findings);
|
|
173
|
+
}
|
|
128
174
|
const report = buildReport(result);
|
|
129
175
|
if (env.GITHUB_STEP_SUMMARY) {
|
|
130
176
|
appendFileSync(env.GITHUB_STEP_SUMMARY, report);
|
package/src/check.js
CHANGED
|
@@ -47,6 +47,23 @@ function readComments(lines, style) {
|
|
|
47
47
|
return result;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
// Rules that read a block (a Ruby rescue with blank lines in it) need more than one
|
|
51
|
+
// line below. Only lines this change also added, in an unbroken run, are returned.
|
|
52
|
+
const LOOKAHEAD_LINES = 4;
|
|
53
|
+
|
|
54
|
+
function readLookahead(lines, comments, index) {
|
|
55
|
+
const result = [];
|
|
56
|
+
let expected = lines[index].number + 1;
|
|
57
|
+
for (let i = index + 1; i < lines.length && result.length < LOOKAHEAD_LINES; i += 1) {
|
|
58
|
+
if (lines[i].number !== expected) {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
result.push({ number: lines[i].number, text: lines[i].text, ...comments[i] });
|
|
62
|
+
expected += 1;
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
50
67
|
export function checkFile(file, enabled) {
|
|
51
68
|
const language = getLanguage(file.path);
|
|
52
69
|
if (!language) {
|
|
@@ -79,7 +96,7 @@ export function checkFile(file, enabled) {
|
|
|
79
96
|
|
|
80
97
|
const previous = file.lines[i - 1];
|
|
81
98
|
const previousIsComment = Boolean(previous && previous.number === line.number - 1 && comments[i - 1].isCommentLine);
|
|
82
|
-
const ctx = { text: line.text, ...comments[i], previousIsComment, next, file: info };
|
|
99
|
+
const ctx = { text: line.text, ...comments[i], previousIsComment, next, lookahead: readLookahead(file.lines, comments, i), file: info };
|
|
83
100
|
for (const rule of rules) {
|
|
84
101
|
if (rule.check(ctx)) {
|
|
85
102
|
findings.push({ rule: rule.id, weight: rule.weight, message: rule.message, file: file.path, line: line.number });
|
package/src/rules.js
CHANGED
|
@@ -78,11 +78,22 @@ const BUZZWORDS = [
|
|
|
78
78
|
// in human code, so only a named catch or a broad Python except counts.
|
|
79
79
|
const EMPTY_CATCH_SAME_LINE = /\bcatch\s*\([^)]*\)\s*\{\s*\}/;
|
|
80
80
|
const CATCH_OPENS_BLOCK = /\bcatch\s*\([^)]*\)\s*\{\s*$/;
|
|
81
|
+
const GO_EMPTY_ERR_SAME_LINE = /\bif\s+err\s*!=\s*nil\s*\{\s*\}/;
|
|
82
|
+
const GO_ERR_OPENS_BLOCK = /\bif\s+err\s*!=\s*nil\s*\{\s*$/;
|
|
81
83
|
const BROAD_EXCEPT = String.raw`except(?:\s+(?:Exception|BaseException)(?:\s+as\s+\w+)?)?`;
|
|
82
84
|
const PYTHON_EXCEPT_PASS = new RegExp(String.raw`^\s*${BROAD_EXCEPT}\s*:\s*pass\s*$`);
|
|
83
85
|
const PYTHON_BROAD_EXCEPT_OPENS = new RegExp(String.raw`^\s*${BROAD_EXCEPT}\s*:\s*$`);
|
|
84
86
|
const PYTHON_EXCEPT_OPENS = /^\s*except\b([^:]*):\s*$/;
|
|
85
87
|
|
|
88
|
+
// Ruby `rescue` with no class, or only StandardError or Exception, catches every
|
|
89
|
+
// error. A named class such as `rescue LoadError` is a deliberate choice, like
|
|
90
|
+
// `except ImportError: pass`. `x rescue nil` is not a block, `rescue => @error`
|
|
91
|
+
// saves the error on the object, and a comment explains why it is dropped.
|
|
92
|
+
const RUBY_BROAD_RESCUE = String.raw`rescue(?:\s+(?:::)?(?:StandardError|Exception))?(?:\s*=>\s*\w+)?`;
|
|
93
|
+
const RUBY_RESCUE_OPENS = new RegExp(String.raw`^\s*${RUBY_BROAD_RESCUE}\s*$`);
|
|
94
|
+
const RUBY_RESCUE_SAME_LINE = new RegExp(String.raw`(?:^|[\s;])${RUBY_BROAD_RESCUE}\s*;\s*end\b`);
|
|
95
|
+
const RUBY_END_LINE = /^end\s*(?:#.*)?$/;
|
|
96
|
+
|
|
86
97
|
const RETHROW_SAME_LINE = /\bcatch\s*\(\s*(\w+)\s*\)\s*\{\s*throw\s+(\w+)\s*;?\s*\}/;
|
|
87
98
|
const CATCH_WITH_NAME_OPENS = /\bcatch\s*\(\s*(\w+)\s*\)\s*\{\s*$/;
|
|
88
99
|
|
|
@@ -148,6 +159,28 @@ function isHistoryComment(comment) {
|
|
|
148
159
|
return nextWords[1].endsWith('ing');
|
|
149
160
|
}
|
|
150
161
|
|
|
162
|
+
// True when only blank lines sit between `rescue` and `end`. A comment or any
|
|
163
|
+
// expression (such as `nil`) means the rescue explains or returns something.
|
|
164
|
+
function rubyBodyIsEmpty(lookahead) {
|
|
165
|
+
for (const line of lookahead) {
|
|
166
|
+
const text = line.text.trim();
|
|
167
|
+
if (RUBY_END_LINE.test(text)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (text !== '') {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function isEmptyRubyRescue(ctx, code) {
|
|
178
|
+
if (RUBY_RESCUE_SAME_LINE.test(code)) {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
return RUBY_RESCUE_OPENS.test(code) && rubyBodyIsEmpty(ctx.lookahead || []);
|
|
182
|
+
}
|
|
183
|
+
|
|
151
184
|
function isEmptyCatch(ctx) {
|
|
152
185
|
if (ctx.isCommentLine) {
|
|
153
186
|
return false;
|
|
@@ -157,6 +190,12 @@ function isEmptyCatch(ctx) {
|
|
|
157
190
|
if (ctx.file.extension === 'py') {
|
|
158
191
|
return PYTHON_EXCEPT_PASS.test(code) || (PYTHON_BROAD_EXCEPT_OPENS.test(code) && nextLine === 'pass');
|
|
159
192
|
}
|
|
193
|
+
if (ctx.file.extension === 'go') {
|
|
194
|
+
return GO_EMPTY_ERR_SAME_LINE.test(code) || (GO_ERR_OPENS_BLOCK.test(code) && nextLine === '}');
|
|
195
|
+
}
|
|
196
|
+
if (ctx.file.extension === 'rb') {
|
|
197
|
+
return isEmptyRubyRescue(ctx, code);
|
|
198
|
+
}
|
|
160
199
|
return EMPTY_CATCH_SAME_LINE.test(code) || (CATCH_OPENS_BLOCK.test(code) && nextLine.startsWith('}'));
|
|
161
200
|
}
|
|
162
201
|
|