Skip to content

Commit d0ab5da

Browse files
author
Colin McNeil
committed
Finalize eslint output
1 parent 986de42 commit d0ab5da

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

prompts/eslint/scripts/lint.sh

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
PROJECT_DIR="/project"
44

@@ -13,14 +13,40 @@ ESLINT_VERSION=$(echo $ARGS | jq -r '.version')
1313

1414
echo "Linting: eslint@$ESLINT_VERSION $ESLINT_ARGS"
1515

16-
# Run eslint but suppress the output
17-
npx --no-install "eslint@$ESLINT_VERSION" $ESLINT_ARGS > /dev/null
18-
19-
# If eslint fails, exit with the exit code
20-
if [ $? -ne 0 ]; then
21-
echo "ESLint exited with an error code: $?"
22-
exit $?
23-
else
24-
echo "ESLint passed with no errors"
25-
exit 0
26-
fi
16+
ESLINT_JSON=$(npx --no-install "eslint@$ESLINT_VERSION" --format json "$ESLINT_ARGS")
17+
18+
VIOLATIONS_BY_ID="{}"
19+
20+
FILE_PATHS=$(echo $ESLINT_JSON | jq -r '.[].filePath')
21+
ALL_MESSAGES=$(echo $ESLINT_JSON | jq -r -c '.[].messages')
22+
23+
# convert to array
24+
IFS=$'\n' FILE_PATHS=($FILE_PATHS)
25+
IFS=$'\n' ALL_MESSAGES=($ALL_MESSAGES)
26+
# Iterate over file paths
27+
for index in "${!FILE_PATHS[@]}"; do
28+
file_path=${FILE_PATHS[$index]}
29+
# Get the messages for the file path
30+
messages=${ALL_MESSAGES[$index]}
31+
# Remove duplicates
32+
messages=$(echo $messages | jq -r -c 'unique_by(.ruleId)')
33+
messages=$(echo $messages | jq -r -c '.[]')
34+
IFS=$'\n' messages=($messages)
35+
36+
# Iterate over messages
37+
for message in "${messages[@]}"; do
38+
# Get the message id
39+
ID=$(echo $message | jq -r '.ruleId')
40+
41+
# If the violations_by_id[id] is null, set it to empty array
42+
if [[ $(echo $VIOLATIONS_BY_ID | jq -r "has(\"$ID\")") == 'false' ]]; then
43+
VIOLATIONS_BY_ID=$(echo $VIOLATIONS_BY_ID | jq --arg id "$ID" '.[$id] = []')
44+
fi
45+
# Add the fileid to the violations object key
46+
VIOLATIONS_BY_ID=$(echo $VIOLATIONS_BY_ID | jq --arg id "$ID" --arg file_path "$file_path" '.[$id] += [$file_path]')
47+
done
48+
done
49+
50+
echo $VIOLATIONS_BY_ID
51+
52+
exit 0

0 commit comments

Comments
 (0)