Skip to content

Commit

Permalink
Fix ruff parsing for source files with syntax errors (#914)
Browse files Browse the repository at this point in the history
Ruff now reports syntax errors as a diagnostic with a `null` code.
  • Loading branch information
det authored Nov 12, 2024
1 parent d86b62c commit c45aa94
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion linters/ruff/ruff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { linterCheckTest, linterFmtTest } from "tests";
import { TrunkLintDriver } from "tests/driver";
import { skipOS } from "tests/utils";

linterCheckTest({ linterName: "ruff", namedTestPrefixes: ["basic", "interface"] });
linterCheckTest({ linterName: "ruff", namedTestPrefixes: ["basic", "interface", "syntax"] });

const skipJupyterTestIf = (version?: string) => {
if (!version || !semver.valid(version)) {
Expand Down
3 changes: 2 additions & 1 deletion linters/ruff/ruff_to_sarif.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def get_region(entry, column_offset=0):
continue

filepath = result["filename"]
rule_id = result["code"]
# Ruff will set code to null for syntax errors
rule_id = result["code"] or "E999"
message = result["message"]

sarif_result = {
Expand Down
43 changes: 43 additions & 0 deletions linters/ruff/test_data/ruff_v0.2.1_syntax.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter ruff test syntax 1`] = `
{
"issues": [
{
"code": "E999",
"column": "1",
"file": "test_data/syntax.in.py",
"issueClass": "ISSUE_CLASS_EXISTING",
"issueUrl": "https://docs.astral.sh/ruff/rules/#E999",
"level": "LEVEL_HIGH",
"line": "2",
"linter": "ruff",
"message": "SyntaxError: unexpected EOF while parsing",
"targetType": "python",
},
],
"lintActions": [
{
"command": "lint",
"fileGroupName": "python",
"linter": "ruff",
"paths": [
"test_data/syntax.in.py",
],
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "lint",
"fileGroupName": "python",
"linter": "ruff",
"paths": [
"test_data/syntax.in.py",
],
"upstream": true,
"verb": "TRUNK_VERB_CHECK",
},
],
"taskFailures": [],
"unformattedFiles": [],
}
`;
1 change: 1 addition & 0 deletions linters/ruff/test_data/syntax.in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def f(): {

0 comments on commit c45aa94

Please sign in to comment.