diff --git a/Makefile b/Makefile index 724a262..fdef7be 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,16 @@ -.PHONY: all check lint test format +.PHONY: all check format format-check lint test -check: format lint test - -test: - @scripts/test - -lint: - @scripts/lint +all: format check +check: format-check lint test format: - @stylua . + @stylua $(or $(FILES),.) + +format-check: + @stylua --check $(or $(FILES),.) + +lint: + @scripts/lint $(FILES) + +test: + @scripts/test $(FILES) diff --git a/scripts/lint b/scripts/lint index ee5646b..f94e831 100755 --- a/scripts/lint +++ b/scripts/lint @@ -3,15 +3,55 @@ set -uo pipefail cd "$(dirname "$0")/.." || exit 1 -emmylua_check --warnings-as-errors --output-format=json . 2> >(grep -v '^Check finished$' >&2) \ - | jq -r ' +filter='.' +if [[ $# -gt 0 ]]; then + files_json=$(printf '%s\n' "$@" | jq -R -s 'split("\n")[:-1]') + read -r -d "" filter <<'jq' +def selected($path; $rel): + any($files[]; . as $target + | ($target | sub("/+$"; "") | sub("^\\./"; "")) as $clean + | (if $clean | startswith("/") then $clean else $cwd + "/" + $clean end) as $abs + | $path == $clean + or $rel == $clean + or $path == $abs + or ($rel | startswith($clean + "/")) + or ($path | startswith($abs + "/")) + ); +map( + .file as $f + | ($f | sub("^" + $cwd + "/"; "")) as $rel + | select(selected($f; $rel)) +) +jq +fi + +json=$(emmylua_check --warnings-as-errors --output-format=json . 2> >(grep -v '^Check finished$' >&2)) +check_status=$? +if ! jq -e type >/dev/null <<<"$json"; then + if [[ $check_status -ne 0 ]]; then + exit "$check_status" + fi + exit 1 +fi +if ! filtered=$(jq --arg cwd "$PWD" --argjson files "${files_json:-[]}" "$filter" <<<"$json"); then + exit 1 +fi + +jq -r ' .[] | .file as $f | .diagnostics[] | "\($f):\(.range.start.line + 1):\(.range.start.character + 1)" + ": \(["error","warning","info","hint"][.severity-1])" + ": \(.message | rtrimstr(" ")) [\(.code)]" - ' \ + ' <<<"$filtered" \ | sed "s|^$PWD/||" -exit "${PIPESTATUS[0]}" +if [[ $# -gt 0 ]]; then + if jq -e 'any(.[]?.diagnostics[]?; .severity <= 2)' >/dev/null <<<"$filtered"; then + exit 1 + fi + exit 0 +fi + +exit "$check_status"