Skip to content

Commit

Permalink
Add a checkstyle linter (#374)
Browse files Browse the repository at this point in the history
* initial checkstyle

* support suppressions

* generate checkstyle docs

* regen docs

* update readme

* update java-format test

* add format
  • Loading branch information
vinnybod authored Aug 28, 2024
1 parent 1e27f34 commit b9068fc
Show file tree
Hide file tree
Showing 17 changed files with 395 additions and 28 deletions.
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,38 @@ Features:

New tools are being added frequently, so check this page again!

| Language | Formatter | Linter(s) |
| ---------------------- | --------------------- | ---------------- |
| C / C++ | [clang-format] | [clang-tidy] |
| Cuda | [clang-format] | |
| CSS, Less, Sass | [Prettier] | [Stylelint] |
| Go | [gofmt] or [gofumpt] | |
| GraphQL | [Prettier] | |
| HCL (Hashicorp Config) | [terraform] fmt | |
| HTML | [Prettier] | |
| JSON | [Prettier] | |
| Java | [google-java-format] | [pmd] |
| JavaScript | [Prettier] | [ESLint] |
| Jsonnet | [jsonnetfmt] | |
| Kotlin | [ktfmt] | [ktlint] |
| Markdown | [Prettier] | [Vale] |
| Protocol Buffer | [buf] | [buf lint] |
| Python | [ruff] | [flake8], [ruff] |
| Rust | [rustfmt] | |
| SQL | [prettier-plugin-sql] | |
| Scala | [scalafmt] | |
| Shell | [shfmt] | [shellcheck] |
| Starlark | [Buildifier] | |
| Swift | [SwiftFormat] (1) | |
| TSX | [Prettier] | [ESLint] |
| TypeScript | [Prettier] | [ESLint] |
| YAML | [yamlfmt] | |
| Language | Formatter | Linter(s) |
| ---------------------- | --------------------- |----------------------|
| C / C++ | [clang-format] | [clang-tidy] |
| Cuda | [clang-format] | |
| CSS, Less, Sass | [Prettier] | [Stylelint] |
| Go | [gofmt] or [gofumpt] | |
| GraphQL | [Prettier] | |
| HCL (Hashicorp Config) | [terraform] fmt | |
| HTML | [Prettier] | |
| JSON | [Prettier] | |
| Java | [google-java-format] | [pmd] , [Checkstyle] |
| JavaScript | [Prettier] | [ESLint] |
| Jsonnet | [jsonnetfmt] | |
| Kotlin | [ktfmt] | [ktlint] |
| Markdown | [Prettier] | [Vale] |
| Protocol Buffer | [buf] | [buf lint] |
| Python | [ruff] | [flake8], [ruff] |
| Rust | [rustfmt] | |
| SQL | [prettier-plugin-sql] | |
| Scala | [scalafmt] | |
| Shell | [shfmt] | [shellcheck] |
| Starlark | [Buildifier] | |
| Swift | [SwiftFormat] (1) | |
| TSX | [Prettier] | [ESLint] |
| TypeScript | [Prettier] | [ESLint] |
| YAML | [yamlfmt] | |

[prettier]: https://prettier.io
[google-java-format]: https://github.com/google/google-java-format
[flake8]: https://flake8.pycqa.org/en/latest/index.html
[pmd]: https://docs.pmd-code.org/latest/index.html
[checkstyle]: https://checkstyle.sourceforge.io/cmdline.html
[buf lint]: https://buf.build/docs/lint/overview
[eslint]: https://eslint.org/
[swiftformat]: https://github.com/nicklockwood/SwiftFormat
Expand Down
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ stardoc_with_diff_test(
bzl_library_target = "//lint:pmd",
)

stardoc_with_diff_test(
name = "checkstyle",
bzl_library_target = "//lint:checkstyle",
)

stardoc_with_diff_test(
name = "format",
bzl_library_target = "//format:defs",
Expand Down
111 changes: 111 additions & 0 deletions docs/checkstyle.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/.aspect/cli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ lint:
- //tools/lint:linters.bzl%stylelint
- //tools/lint:linters.bzl%ruff
- //tools/lint:linters.bzl%vale
- //tools/lint:linters.bzl%checkstyle
2 changes: 2 additions & 0 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ exports_files(
"buf.yaml",
".flake8",
"pmd.xml",
"checkstyle.xml",
"checkstyle-suppressions.xml",
".ruff.toml",
".shellcheckrc",
".scalafmt.conf",
Expand Down
4 changes: 4 additions & 0 deletions example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ fetch_ktfmt()

fetch_swiftformat()

load("@aspect_rules_lint//lint:checkstyle.bzl", "fetch_checkstyle")

fetch_checkstyle()

load("@aspect_rules_lint//lint:pmd.bzl", "fetch_pmd")

fetch_pmd()
Expand Down
4 changes: 4 additions & 0 deletions example/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ load("@aspect_rules_lint//lint:pmd.bzl", "fetch_pmd")

fetch_pmd()

load("@aspect_rules_lint//lint:checkstyle.bzl", "fetch_checkstyle")

fetch_checkstyle()

load("@aspect_rules_lint//lint:vale.bzl", "fetch_vale")

fetch_vale()
Expand Down
9 changes: 9 additions & 0 deletions example/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
<suppress files="Bar.java" checks="UnusedImports" />
</suppressions>
19 changes: 19 additions & 0 deletions example/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">

<module name="TreeWalker">
<module name="UnusedImports"/>
</module>

<module name="LineLength">
<property name="max" value="20"/>
</module>

<module name="SuppressionFilter">
<property name="file" value="checkstyle-suppressions.xml"/>
</module>

</module>
5 changes: 5 additions & 0 deletions example/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ java_library(
srcs = ["Foo.java"],
)

java_library(
name = "bar",
srcs = ["Bar.java"],
)

sh_library(
name = "hello_shell",
srcs = ["hello.sh"],
Expand Down
10 changes: 10 additions & 0 deletions example/src/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package src;

// Unused imports are suppressed in suppressions.xml, so this should not raise issue.
import java.util.Objects;
import java.io.BufferedInputStream;

public class Bar {
// Max line length set to 20, so this should raise issue.
protected void finalize(int a) {}
}
8 changes: 7 additions & 1 deletion example/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load("@aspect_rules_lint//format:defs.bzl", "format_test")
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//tools/lint:linters.bzl", "eslint_test", "flake8_test", "pmd_test", "ruff_test", "shellcheck_test")
load("//tools/lint:linters.bzl", "checkstyle_test", "eslint_test", "flake8_test", "pmd_test", "ruff_test", "shellcheck_test")

write_file(
name = "ts_code_generator",
Expand Down Expand Up @@ -94,6 +94,12 @@ pmd_test(
tags = ["manual"],
)

checkstyle_test(
name = "checkstyle",
srcs = ["//src:bar"],
tags = ["manual"],
)

eslint_test(
name = "eslint",
# NB: we must lint the `ts_typings` target that has the .ts files in srcs,
Expand Down
6 changes: 6 additions & 0 deletions example/tools/lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ java_binary(
runtime_deps = ["@net_sourceforge_pmd"],
)

java_binary(
name = "checkstyle",
main_class = "com.puppycrawl.tools.checkstyle.Main",
runtime_deps = ["@com_puppycrawl_tools_checkstyle//jar"],
)

native_binary(
name = "vale",
src = select(
Expand Down
9 changes: 9 additions & 0 deletions example/tools/lint/linters.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Define linter aspects"

load("@aspect_rules_lint//lint:buf.bzl", "lint_buf_aspect")
load("@aspect_rules_lint//lint:checkstyle.bzl", "lint_checkstyle_aspect")
load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect")
load("@aspect_rules_lint//lint:eslint.bzl", "lint_eslint_aspect")
load("@aspect_rules_lint//lint:flake8.bzl", "lint_flake8_aspect")
Expand Down Expand Up @@ -48,6 +49,14 @@ pmd = lint_pmd_aspect(

pmd_test = lint_test(aspect = pmd)

checkstyle = lint_checkstyle_aspect(
binary = "@@//tools/lint:checkstyle",
config = "@@//:checkstyle.xml",
data = ["@@//:checkstyle-suppressions.xml"],
)

checkstyle_test = lint_test(aspect = checkstyle)

ruff = lint_ruff_aspect(
binary = "@multitool//tools/ruff",
configs = [
Expand Down
2 changes: 1 addition & 1 deletion format/test/format_test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bats_load_library "bats-assert"
run bazel run //format/test:format_Java_with_java-format
assert_success

assert_output --partial "+ java-format --replace example/src/Foo.java"
assert_output --partial "+ java-format --replace example/src/Bar.java example/src/Foo.java"
}

@test "should run ktfmt on Kotlin" {
Expand Down
9 changes: 9 additions & 0 deletions lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ bzl_library(
],
)

bzl_library(
name = "checkstyle",
srcs = ["checkstyle.bzl"],
visibility = ["//visibility:public"],
deps = _BAZEL_TOOLS + [
"//lint/private:lint_aspect",
],
)

bzl_library(
name = "ruff",
srcs = ["ruff.bzl"],
Expand Down
Loading

0 comments on commit b9068fc

Please sign in to comment.