Skip to content

Commit

Permalink
refactor: overhaul of format.sh (#105)
Browse files Browse the repository at this point in the history
* refactor: overhaul of format.sh

Use GitHub Linguist to define the file extensions for each language, getting much more accurate.
Extract a helper function for listing files we want to format to reduce duplication.

* Update mirror_linguist_languages.sh
  • Loading branch information
alexeagle authored Jan 19, 2024
1 parent d7d7304 commit 175da17
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 142 deletions.
2 changes: 1 addition & 1 deletion docs/format.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
66 changes: 33 additions & 33 deletions example/test/lint_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@ bats_load_library "bats-support"
bats_load_library "bats-assert"

function assert_lints() {
# Shellcheck
echo <<"EOF" | assert_output --partial
# Shellcheck
echo <<"EOF" | assert_output --partial
In src/hello.sh line 3:
[ -z $THING ] && echo "hello world"
^----^ SC2086 (info): Double quote to prevent globbing and word splitting.
EOF

# Ruff
echo <<"EOF" | assert_output --partial
# Ruff
echo <<"EOF" | assert_output --partial
src/unused_import.py:13:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 fixable with the `--fix` option.
EOF

# Flake8
assert_output --partial "src/unused_import.py:13:1: F401 'os' imported but unused"
# Flake8
assert_output --partial "src/unused_import.py:13:1: F401 'os' imported but unused"

# PMD
assert_output --partial 'src/Foo.java:9: FinalizeOverloaded: Finalize methods should not be overloaded'
# PMD
assert_output --partial 'src/Foo.java:9: FinalizeOverloaded: Finalize methods should not be overloaded'

# ESLint
assert_output --partial 'src/file.ts:2:7: Type string trivially inferred from a string literal, remove type annotation [error from @typescript-eslint/no-inferrable-types]'
# ESLint
assert_output --partial 'src/file.ts:2:7: Type string trivially inferred from a string literal, remove type annotation [error from @typescript-eslint/no-inferrable-types]'

# Buf
assert_output --partial 'src/file.proto:1:1:Import "src/unused.proto" is unused.'
# Buf
assert_output --partial 'src/file.proto:1:1:Import "src/unused.proto" is unused.'

# Golangci-lint
assert_output --partial 'src/hello.go:13:2: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)'
# Golangci-lint
assert_output --partial 'src/hello.go:13:2: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)'
}

@test "should produce reports" {
run $BATS_TEST_DIRNAME/../lint.sh //src:all
assert_success
assert_lints
run $BATS_TEST_DIRNAME/../lint.sh //src:all
assert_success
assert_lints

run $BATS_TEST_DIRNAME/../lint.sh --fix --dry-run //src:all
# Check that we created a 'patch -p1' format file that fixes the ESLint violation
run cat bazel-bin/src/ESLint.ts.aspect_rules_lint.patch
assert_success
echo <<"EOF" | assert_output --partial
run $BATS_TEST_DIRNAME/../lint.sh --fix --dry-run //src:all
# Check that we created a 'patch -p1' format file that fixes the ESLint violation
run cat bazel-bin/src/ESLint.ts.aspect_rules_lint.patch
assert_success
echo <<"EOF" | assert_output --partial
--- a/src/file.ts
+++ b/src/file.ts
@@ -1,3 +1,3 @@
Expand All @@ -51,10 +51,10 @@ EOF
console.log(a);
EOF

# Check that we created a 'patch -p1' format file that fixes the ruff violation
run cat bazel-bin/src/ruff.unused_import.aspect_rules_lint.patch
assert_success
echo <<"EOF" | assert_output --partial
# Check that we created a 'patch -p1' format file that fixes the ruff violation
run cat bazel-bin/src/ruff.unused_import.aspect_rules_lint.patch
assert_success
echo <<"EOF" | assert_output --partial
--- a/src/unused_import.py
+++ b/src/unused_import.py
@@ -10,4 +10,3 @@
Expand All @@ -66,14 +66,14 @@ EOF
}

@test "should fail when --fail-on-violation is passed" {
run $BATS_TEST_DIRNAME/../lint.sh --fail-on-violation //src:all
assert_failure
assert_lints
run $BATS_TEST_DIRNAME/../lint.sh --fail-on-violation //src:all
assert_failure
assert_lints
}

@test "should use nearest ancestor .eslintrc file" {
run $BATS_TEST_DIRNAME/../lint.sh //src/subdir:eslint-override
assert_success
# This lint check is disabled in the .eslintrc.cjs file
refute_output --partial "Unexpected 'debugger' statement"
run $BATS_TEST_DIRNAME/../lint.sh //src/subdir:eslint-override
assert_success
# This lint check is disabled in the .eslintrc.cjs file
refute_output --partial "Unexpected 'debugger' statement"
}
37 changes: 37 additions & 0 deletions format/private/filter.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Filter languages we understand
with_entries(select(.key | IN(
"C++",
"CSS",
"Markdown",
"Go",
"HCL",
"HTML",
"Java",
"JavaScript",
"Jsonnet",
"JSON",
"Kotlin",
"Protocol Buffer",
"Python",
"Scala",
"Shell",
"SQL",
"Starlark",
"Swift",
"TSX",
"TypeScript"
)))

# Convert values to filenames and extensions with star prefix
| with_entries(.value = (
.value.filenames + (.value.extensions | map("*" + .))
))

# Render each language as a line in a Bash case statement, e.g.
# 'Jsonnet') patterns=('*.jsonnet' '*.libsonnet') ;;
| to_entries | map(
"'" + .key + "') patterns=(" + (.value | map("'" + . + "'") | join(" ")) + ") ;;"
)

# Suitable for --raw-output
|.[]
Loading

0 comments on commit 175da17

Please sign in to comment.