From 3b669ebca634c090a400a989f3a753a33fb09003 Mon Sep 17 00:00:00 2001 From: pat-trunk-io Date: Fri, 5 Jan 2024 14:30:01 -0500 Subject: [PATCH] (Feat): Parser prettier failure messages (#589) Co-authored-by: Chris Clearwater --- .github/dependabot.yaml | 6 +- .trunk/.gitignore | 1 + .trunk/trunk.yaml | 2 +- linters/prettier/plugin.yaml | 8 ++- linters/prettier/prettier.test.ts | 5 +- linters/prettier/prettier_to_sarif.py | 66 +++++++++++++++++++ linters/prettier/test_data/error.in.yaml | 39 +++++++++++ .../prettier_v2.6.2_basic.check.shot | 30 +++++++++ .../prettier_v2.6.2_error.check.shot | 31 +++++++++ .../test_data/prettier_v2.6.2_error.fmt.shot | 44 +++++++++++++ 10 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 linters/prettier/prettier_to_sarif.py create mode 100644 linters/prettier/test_data/error.in.yaml create mode 100644 linters/prettier/test_data/prettier_v2.6.2_basic.check.shot create mode 100644 linters/prettier/test_data/prettier_v2.6.2_error.check.shot create mode 100644 linters/prettier/test_data/prettier_v2.6.2_error.fmt.shot diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml index 57c8b48ec..17adf3ee6 100644 --- a/.github/dependabot.yaml +++ b/.github/dependabot.yaml @@ -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 @@ -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: diff --git a/.trunk/.gitignore b/.trunk/.gitignore index 1e2465290..15966d087 100644 --- a/.trunk/.gitignore +++ b/.trunk/.gitignore @@ -6,3 +6,4 @@ plugins user_trunk.yaml user.yaml +tmp diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 9e86738b3..6cfa0e5c0 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -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 diff --git a/linters/prettier/plugin.yaml b/linters/prettier/plugin.yaml index 07ac90e38..d7bf82fe9 100644 --- a/linters/prettier/plugin.yaml +++ b/linters/prettier/plugin.yaml @@ -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: diff --git a/linters/prettier/prettier.test.ts b/linters/prettier/prettier.test.ts index 9bdfb57ba..1f7b295a9 100644 --- a/linters/prettier/prettier.test.ts +++ b/linters/prettier/prettier.test.ts @@ -1,4 +1,4 @@ -import { linterFmtTest } from "tests"; +import { linterCheckTest, linterFmtTest } from "tests"; import { TrunkLintDriver } from "tests/driver"; // Grab the root .prettierrc.yaml @@ -6,9 +6,10 @@ 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 }); diff --git a/linters/prettier/prettier_to_sarif.py b/linters/prettier/prettier_to_sarif.py new file mode 100644 index 000000000..342794755 --- /dev/null +++ b/linters/prettier/prettier_to_sarif.py @@ -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 )") + 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) diff --git a/linters/prettier/test_data/error.in.yaml b/linters/prettier/test_data/error.in.yaml new file mode 100644 index 000000000..263c320ed --- /dev/null +++ b/linters/prettier/test_data/error.in.yaml @@ -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. diff --git a/linters/prettier/test_data/prettier_v2.6.2_basic.check.shot b/linters/prettier/test_data/prettier_v2.6.2_basic.check.shot new file mode 100644 index 000000000..604e474ae --- /dev/null +++ b/linters/prettier/test_data/prettier_v2.6.2_basic.check.shot @@ -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'", + }, + ], +} +`; diff --git a/linters/prettier/test_data/prettier_v2.6.2_error.check.shot b/linters/prettier/test_data/prettier_v2.6.2_error.check.shot new file mode 100644 index 000000000..0cbb342dd --- /dev/null +++ b/linters/prettier/test_data/prettier_v2.6.2_error.check.shot @@ -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": [], +} +`; diff --git a/linters/prettier/test_data/prettier_v2.6.2_error.fmt.shot b/linters/prettier/test_data/prettier_v2.6.2_error.fmt.shot new file mode 100644 index 000000000..ecb6f9c15 --- /dev/null +++ b/linters/prettier/test_data/prettier_v2.6.2_error.fmt.shot @@ -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. +" +`;