Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Feat): Parser prettier failure messages #589

Merged
merged 7 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
(Feat): Parser prettier failure messages
  • Loading branch information
pat-trunk-io committed Dec 12, 2023
commit 14abcaf87d7562e60668f41ea9656cc6704af019
8 changes: 6 additions & 2 deletions linters/prettier/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ lint:
- prettier_supported_configs
commands:
- name: format
output: rewrite
output: sarif
run: prettier -w ${target}
success_codes: [0]
read_output_from: stderr
success_codes: [0, 2]
batch: true
in_place: true
formatter: true
parser:
runtime: python
run: python3 ${plugin}/linters/prettier/prettier_to_sarif.py ${exit_code}
tools: [prettier]
suggest_if: files_present
direct_configs:
Expand Down
3 changes: 2 additions & 1 deletion linters/prettier/prettier.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { linterFmtTest } from "tests";
import { linterFmtTest, linterCheckTest } from "tests";

Check failure on line 1 in linters/prettier/prettier.test.ts

View check run for this annotation

Trunk-Staging.io / Trunk Check

eslint(simple-import-sort/imports)

[new] Run autofix to sort these imports!

Check failure on line 1 in linters/prettier/prettier.test.ts

View workflow job for this annotation

GitHub Actions / Trunk Check

eslint(simple-import-sort/imports)

[new] Run autofix to sort these imports!
import { TrunkLintDriver } from "tests/driver";

// Grab the root .prettierrc.yaml
Expand All @@ -12,3 +12,4 @@

// TODO(Tyler): We will eventually need to add a couple more test cases involving other file types.
linterFmtTest({ linterName: "prettier", preCheck });
linterCheckTest({ linterName: "prettier", preCheck });
66 changes: 66 additions & 0 deletions linters/prettier/prettier_to_sarif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python3

import json
import re
import sys


def to_result_sarif(path: str, description: str, line: int = 0, column: int = 0):
return {
"level": "error",
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": path,
},
"region": {
"startColumn": column,
"startLine": line,
},
}
}
],
"message": {
"text": description,
},
"ruleId": "SyntaxError",
}


def main(argv):
if len(argv) < 2:
print("Usage: trivy_to_sarif.py <exit_code>)")
sys.exit(1)

if argv[1] == "0":
results = []
sarif = {
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
"version": "2.1.0",
"runs": [{"results": results}],
}

print(json.dumps(sarif, indent=2))
sys.exit(0)

first_line = sys.stdin.readline()
m = re.match(r"\[error\] (.*): SyntaxError:(.*)\((\d+):(\d+)\)", first_line)
if m is None:
print("Unexpected output from prettier")
sys.exit(1)

results = []
results.append(to_result_sarif(m[1], m[2], int(m[3]), int(m[4])))

sarif = {
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
"version": "2.1.0",
"runs": [{"results": results}],
}

print(json.dumps(sarif, indent=2))


if __name__ == "__main__":
main(sys.argv)
39 changes: 39 additions & 0 deletions linters/prettier/test_data/error.in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
invoice : 34843
date : 2001-01-23
]
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
-

sku : BL394D
? quantity
: 4
description : Basketball
? price
: 450.00


-
sku : BL4438H
quantity : 1
description: Super Hoop
price : 2392.00


tax : 251.42
total : 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
30 changes: 30 additions & 0 deletions linters/prettier/test_data/prettier_v2.6.2_basic.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter prettier test basic 1`] = `
{
"issues": [],
"lintActions": [
{
"command": "format",
"fileGroupName": "yaml",
"linter": "prettier",
"paths": [
"test_data/basic.in.yaml",
],
"verb": "TRUNK_VERB_FMT",
},
],
"taskFailures": [],
"unformattedFiles": [
{
"column": "1",
"file": "test_data/basic.in.yaml",
"issueClass": "ISSUE_CLASS_UNFORMATTED",
"level": "LEVEL_HIGH",
"line": "1",
"linter": "prettier",
"message": "Incorrect formatting, autoformat by running 'trunk fmt'",
},
],
}
`;
31 changes: 31 additions & 0 deletions linters/prettier/test_data/prettier_v2.6.2_error.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter prettier test error 1`] = `
{
"issues": [
{
"code": "SyntaxError",
"column": "1",
"file": "test_data/error.in.yaml",
"level": "LEVEL_HIGH",
"line": "4",
"linter": "prettier",
"message": "Implicit map keys need to be followed by map values",
"targetType": "yaml",
},
],
"lintActions": [
{
"command": "format",
"fileGroupName": "yaml",
"linter": "prettier",
"paths": [
"test_data/error.in.yaml",
],
"verb": "TRUNK_VERB_FMT",
},
],
"taskFailures": [],
"unformattedFiles": [],
}
`;
44 changes: 44 additions & 0 deletions linters/prettier/test_data/prettier_v2.6.2_error.fmt.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing formatter prettier test error 1`] = `
"---
invoice : 34843
date : 2001-01-23
]
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
-

sku : BL394D
? quantity
: 4
description : Basketball
? price
: 450.00


-
sku : BL4438H
quantity : 1
description: Super Hoop
price : 2392.00


tax : 251.42
total : 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
"
`;
Loading