Skip to content

Commit

Permalink
(Feat): Parser prettier failure messages (#589)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Clearwater <[email protected]>
  • Loading branch information
pat-trunk-io and det authored Jan 5, 2024
1 parent e6ed22c commit 3b669eb
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ updates:
schedule:
interval: weekly
day: sunday
time: 08:00 # UTC
# trunk-ignore(yamllint/quoted-strings)
time: "08:00" # UTC
labels: [🤖 dependabot]
open-pull-requests-limit: 2

Expand All @@ -14,7 +15,8 @@ updates:
schedule:
interval: weekly
day: sunday
time: 08:00 # UTC
# trunk-ignore(yamllint/quoted-strings)
time: "08:00" # UTC
labels: [🤖 dependabot]
groups:
dependencies:
Expand Down
1 change: 1 addition & 0 deletions .trunk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
plugins
user_trunk.yaml
user.yaml
tmp
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 0.1

# version used for local trunk runs and testing
cli:
version: 1.18.2-beta.8
version: 1.18.2-beta.14
shell_hooks:
enforce: true

Expand Down
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
5 changes: 3 additions & 2 deletions linters/prettier/prettier.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { linterFmtTest } from "tests";
import { linterCheckTest, linterFmtTest } from "tests";
import { TrunkLintDriver } from "tests/driver";

// Grab the root .prettierrc.yaml
const preCheck = (driver: TrunkLintDriver) => {
driver.writeFile(
".trunk/configs/.prettierrc.yaml",
`printWidth: 100
proseWrap: always`
proseWrap: always`,
);
};

// 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(int(argv[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.
"
`;

0 comments on commit 3b669eb

Please sign in to comment.