Skip to content

Commit 0fac163

Browse files
author
Colin McNeil
committed
Add complaint output mode
1 parent dc80285 commit 0fac163

File tree

4 files changed

+60
-81
lines changed

4 files changed

+60
-81
lines changed

prompts/eslint/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ functions:
1414
type: number
1515
description: The ESLint version, 7-9 supported. Use 8 if unsure.
1616
outputLevel:
17-
type: number
18-
description: Accepts [0,1,2]. 0 to only output number of violations, 1 is condensed json grouped by violation id, 2 is full lint json grouped by file.
17+
type: string
18+
description: Nullish values return a summary from the linter. `complaint` returns a list of editor complaints. `condensed` returns violations grouped by violation id. `json` returns the raw JSON output from the linter.
1919
required:
2020
- args
2121
- version

prompts/eslint/scripts/lint-standardjs.sh

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,18 @@ else
2323
FILES=$(echo $ARGS | jq -r '.files[]')
2424
fi
2525

26-
TOTAL_VIOLATIONS=0
27-
2826
# If typescript is false, run standard
2927
if [ $TYPESCRIPT == 'false' ]; then
30-
echo "Running standard"
3128
$LINT_ARGS="$FILES"
3229
# If FIX
3330
if [ $FIX == "true" ]; then
3431
LINT_ARGS="--fix $FILES"
3532
fi
3633
# Pass files array as args to standard
37-
OUTPUT=$(standard $LINT_ARGS | standard-json)
38-
39-
# Count total violations
40-
TOTAL_VIOLATIONS=$(echo $OUTPUT | jq -r '.results[].messages | length')
41-
42-
if [ $OUTPUT_LEVEL == "0" ]; then
43-
echo "Linting with StandardJS complete. $TOTAL_VIOLATIONS violations found."
44-
fi
45-
46-
if [ $OUTPUT_LEVEL == "1" ]; then
47-
echo "Linting with StandardJS complete. Outputting condensed JSON."
48-
echo $OUTPUT | /remap_lint.sh
49-
fi
50-
51-
if [ $OUTPUT_LEVEL == "2" ]; then
52-
echo "Linting with StandardJS complete. Outputting JSON."
53-
echo $OUTPUT
54-
fi
34+
echo standard 2>/dev/null | standard-json | /remap_lint.sh "$OUTPUT_LEVEL"
35+
exit $?
5536
fi
5637

57-
echo "Running ts-standard..."
58-
5938
TS_FILES=$(echo "$FILES" | grep -E "\.ts$|\.tsx$")
6039

6140
TS_ROOTS=$(fd -d 3 tsconfig.json)
@@ -77,7 +56,6 @@ for TS_ROOT in $TS_ROOTS; do
7756
cd $root_dirname
7857
# Filter all TS_FILES in root_dirname
7958
TS_FILES_IN_ROOT=$(echo "$TS_FILES" | grep -E $root_dirname)
80-
echo "Linting TS Files in $root_dirname"
8159
# If no TS_FILES in root_dirname, skip
8260
if [ -z "$TS_FILES_IN_ROOT" ]; then
8361
continue
@@ -88,33 +66,14 @@ for TS_ROOT in $TS_ROOTS; do
8866
else
8967
LINT_ARGS="$TS_FILES_IN_ROOT"
9068
fi
91-
92-
TS_OUTPUT+=$(ts-standard $LINT_ARGS | standard-json)
93-
94-
# Count total violations
95-
TOTAL_VIOLATIONS=$(echo $TS_OUTPUT | jq -r '.results[].messages | length')
69+
TS_OUTPUT+=$(ts-standard 2>/dev/null | standard-json | /remap_lint.sh "$OUTPUT_LEVEL")
9670
# If ts-standard failed and EXIT_CODE is 0, set EXIT_CODE
9771
if [ $? -ne 0 ] && [ $EXIT_CODE -eq 0 ]; then
9872
EXIT_CODE=$?
9973
fi
10074
cd $PROJECT_DIR
10175
done
10276

103-
104-
if [ $OUTPUT_LEVEL == "0" ]; then
105-
echo "Linting with StandardJS (TS) complete. $TOTAL_VIOLATIONS violations found."
106-
fi
107-
108-
if [ $OUTPUT_LEVEL == "1" ]; then
109-
echo "Linting with StandardJS (TS) complete. Outputting condensed JSON."
110-
echo $TS_OUTPUT | /remap_lint.sh
111-
fi
112-
113-
if [ $OUTPUT_LEVEL == "2" ]; then
114-
echo "Linting with StandardJS (TS) complete. Outputting JSON."
115-
echo $TS_OUTPUT
116-
fi
117-
11877
echo $TS_OUTPUT
11978

12079
exit $EXIT_CODE

prompts/eslint/scripts/lint.sh

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,4 @@ OUTPUT_LEVEL=$(echo $ARGS | jq -r '.outputLevel')
1616

1717
echo "Linting: eslint@$ESLINT_VERSION $ESLINT_ARGS"
1818

19-
ESLINT_JSON=$(npx --no-install "eslint@$ESLINT_VERSION" --format json "$ESLINT_ARGS")
20-
21-
22-
if [ $OUTPUT_LEVEL == "0" ]; then
23-
echo "Linting with ESLint v$ESLINT_VERSION complete."
24-
TOTAL_VIOLATIONS=$(echo $ESLINT_JSON | jq -r '.[].messages | length')
25-
fi
26-
27-
if [ $OUTPUT_LEVEL == "1" ]; then
28-
echo "Linting with ESLint v$ESLINT_VERSION complete. Outputting condensed JSON."
29-
echo $ESLINT_JSON | /remap_lint.sh
30-
fi
31-
32-
if [ $OUTPUT_LEVEL == "2" ]; then
33-
echo "Linting with ESLint v$ESLINT_VERSION complete. Outputting JSON."
34-
echo $ESLINT_JSON
35-
fi
19+
ESLINT_JSON=$(npx --no-install "eslint@$ESLINT_VERSION" --format json "$ESLINT_ARGS" | /remap_lint.sh "$OUTPUT_LEVEL")
Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
#!/bin/bash
2+
13
# Get piped stdin as input
24
INPUT=$(cat)
35

4-
VIOLATIONS_BY_ID="{}"
6+
OUTPUT="{}"
7+
8+
OUTPUT_MODE=$1 # summary, complaints, condensed, json
9+
10+
# If not set
11+
if [ -z "$OUTPUT_MODE" ]; then
12+
OUTPUT_MODE="summary"
13+
fi
514

615
FILE_PATHS=$(echo $INPUT | jq -r '.[].filePath')
716
ALL_MESSAGES=$(echo $INPUT | jq -r -c '.[].messages')
@@ -14,25 +23,52 @@ for index in "${!FILE_PATHS[@]}"; do
1423
file_path=${FILE_PATHS[$index]}
1524
# Get the messages for the file path
1625
messages=${ALL_MESSAGES[$index]}
17-
# Remove duplicates
18-
messages=$(echo $messages | jq -r -c 'unique_by(.ruleId)')
19-
messages=$(echo $messages | jq -r -c '.[]')
20-
IFS=$'\n' messages=($messages)
21-
22-
# Iterate over messages
23-
for message in "${messages[@]}"; do
24-
# Get the message id
25-
ID=$(echo $message | jq -r '.ruleId')
26-
27-
# If the violations_by_id[id] is null, set it to empty array
28-
if [[ $(echo $VIOLATIONS_BY_ID | jq -r "has(\"$ID\")") == 'false' ]]; then
29-
VIOLATIONS_BY_ID=$(echo $VIOLATIONS_BY_ID | jq --arg id "$ID" '.[$id] = []')
26+
27+
if [ $OUTPUT_MODE == "complaints" ]; then
28+
29+
# Complaint: {filePath: "path", "start": [line, column], "end": [line, column], "message": "message", "severity": "severity", "ruleId": "ruleId"}
30+
messages=$(echo $messages | jq -r -c '.[]')
31+
IFS=$'\n' messages=($messages)
32+
for message in "${messages[@]}"; do
33+
# If endLine is null, set it to line
34+
message_parsed=$(echo $message | jq -r -c 'if .endLine == null then .endLine = .line end')
35+
# If endColumn is null, set it to convert column to number and add 1
36+
message_parsed=$(echo $message_parsed | jq -r -c 'if .endColumn == null then .endColumn = (.column | tonumber + 1) end')
37+
COMPLAINT=$(echo $message_parsed | jq -r -c --arg file_path $file_path '{filePath: $file_path, start: [.line, .column], end: [.endLine, .endColumn], message: .message, severity: .severity, ruleId: .ruleId}')
38+
echo $COMPLAINT
39+
done
40+
elif [ $OUTPUT_MODE == "condensed" ]; then
41+
# Remove duplicates
42+
messages=$(echo $messages | jq -r -c 'unique_by(.ruleId)')
43+
messages=$(echo $messages | jq -r -c '.[]')
44+
IFS=$'\n' messages=($messages)
45+
# Iterate over messages
46+
for message in "${messages[@]}"; do
47+
# Get the message id
48+
ID=$(echo $message | jq -r '.ruleId')
49+
50+
# If the OUTPUT[id] is null, set it to empty array
51+
if [[ $(echo $VIOLATIONS_BY_ID | jq -r "has(\"$ID\")") == 'false' ]]; then
52+
OUTPUT=$(echo $OUTPUT | jq --arg id "$ID" '.[$id] = []')
53+
fi
54+
# Add the fileid to the violations object key
55+
OUTPUT=$(echo $OUTPUT | jq --arg id "$ID" --arg file_path "$file_path" '.[$id] += [$file_path]')
56+
done
57+
elif [ $OUTPUT_MODE == "json" ]; then
58+
OUTPUT=$(echo $INPUT)
59+
else
60+
# Summary
61+
AFFECTED_FILE_COUNT=$(echo $INPUT | jq -r 'length')
62+
TOTAL_VIOLATION_COUNT=$(echo $INPUT | jq -r '.[].messages | length' | jq -s add)
63+
64+
if [[ $TOTAL_VIOLATION_COUNT -gt 0 ]]; then
65+
OUTPUT="Found $TOTAL_VIOLATION_COUNT violations in $AFFECTED_FILE_COUNT files."
66+
else
67+
OUTPUT="No violations found."
3068
fi
31-
# Add the fileid to the violations object key
32-
VIOLATIONS_BY_ID=$(echo $VIOLATIONS_BY_ID | jq --arg id "$ID" --arg file_path "$file_path" '.[$id] += [$file_path]')
33-
done
69+
fi
3470
done
3571

36-
echo $VIOLATIONS_BY_ID
72+
echo $OUTPUT
3773

3874
exit 0

0 commit comments

Comments
 (0)