Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: overhaul of format.sh #105

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading