Skip to content

Commit

Permalink
feat: Add keep-sorted as a linter (#463)
Browse files Browse the repository at this point in the history
* feat: Add keep-sorted as a linter

Add Google's keep-sorted a linter.

* improvements:

wire up fixer
supply the tools.go from rules_lint to make user install shorter
isolate the go_deps module from anything the user already installed

* tweaks

* chore: docgen

* chore: undo test change

* fix: don't collide on patch_cfg filenames

---------

Co-authored-by: Alex Eagle <[email protected]>
  • Loading branch information
fzakaria and alexeagle authored Jan 25, 2025
1 parent 50c151a commit 2e2cbc3
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 29 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ Features:

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

Linters which are not language-specific:

- [keep-sorted]

| Language | Formatter | Linter(s) |
| ---------------------- | --------------------- |----------------------------------|
| ---------------------- | --------------------- | -------------------------------- |
| C / C++ | [clang-format] | [clang-tidy] |
| Cuda | [clang-format] | |
| CSS, Less, Sass | [Prettier] | [Stylelint] |
Expand Down Expand Up @@ -59,6 +63,7 @@ New tools are being added frequently, so check this page again!
[swiftformat]: https://github.com/nicklockwood/SwiftFormat
[terraform]: https://github.com/hashicorp/terraform
[buf]: https://docs.buf.build/format/usage
[keep-sorted]: https://github.com/google/keep-sorted
[ktfmt]: https://github.com/facebook/ktfmt
[ktlint]: https://github.com/pinterest/ktlint
[buildifier]: https://github.com/keith/buildifier-prebuilt
Expand Down
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ stardoc_with_diff_test(
bzl_library_target = "//lint:flake8",
)

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

stardoc_with_diff_test(
name = "pmd",
bzl_library_target = "//lint:pmd",
Expand Down
105 changes: 105 additions & 0 deletions docs/keep_sorted.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 @@ -11,3 +11,4 @@ lint:
- //tools/lint:linters.bzl%checkstyle
- //tools/lint:linters.bzl%clang_tidy
- //tools/lint:linters.bzl%spotbugs
- //tools/lint:linters.bzl%keep_sorted
3 changes: 3 additions & 0 deletions example/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Automatically apply --config=linux, --config=windows etc
common --enable_platform_specific_config

# Permit MODULE.bazel to use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
common --experimental_isolated_extension_usages

# Aspect recommended Bazel flags when using rules_java and rules_jvm_external

# Pin java versions to desired language level
Expand Down
9 changes: 7 additions & 2 deletions example/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ bazel_dep(name = "toolchains_llvm", version = "0.10.3")
bazel_dep(name = "toolchains_protoc", version = "0.3.0")
bazel_dep(name = "rules_java", version = "8.5.0")
bazel_dep(name = "rules_jvm_external", version = "6.5")
bazel_dep(name = "rules_go", version = "0.42.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_go", version = "0.52.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_proto", version = "6.0.0")
bazel_dep(name = "rules_python", version = "0.26.0")
bazel_dep(name = "rules_rust", version = "0.50.1")
bazel_dep(name = "buildifier_prebuilt", version = "6.3.3")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_kotlin", version = "1.9.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "gazelle", version = "0.41.0")

local_path_override(
module_name = "aspect_rules_lint",
Expand Down Expand Up @@ -74,10 +75,14 @@ use_repo(pip, "pip")
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(
name = "go_sdk",
version = "1.20.3",
version = "1.23.5",
)
use_repo(go_sdk, "go_sdk")

keep_sorted_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
keep_sorted_deps.from_file(go_mod = "@aspect_rules_lint//lint/keep-sorted:go.mod")
use_repo(keep_sorted_deps, "com_github_google_keep_sorted")

# Java and other JVM languages:
# https://github.com/bazelbuild/rules_jvm_external/blob/master/examples/bzlmod/MODULE.bazel
# https://github.com/bazelbuild/rules_jvm_external#pinning-artifacts-and-integration-with-bazels-downloader
Expand Down
2 changes: 1 addition & 1 deletion example/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if [ $machine == "Windows" ]; then
# avoid missing linters on windows platform
args=("--aspects=$(echo //tools/lint:linters.bzl%{flake8,pmd,ruff,vale,clang_tidy} | tr ' ' ',')")
else
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,ktlint,pmd,ruff,shellcheck,stylelint,vale,clang_tidy,spotbugs} | tr ' ' ',')")
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,keep_sorted,ktlint,pmd,ruff,shellcheck,stylelint,vale,clang_tidy,spotbugs} | tr ' ' ',')")
fi

# NB: perhaps --remote_download_toplevel is needed as well with remote execution?
Expand Down
10 changes: 10 additions & 0 deletions example/src/Bar.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
import java.io.BufferedInputStream;

public class Bar {

enum MyEnum {
// keep-sorted start
B(),
A(),
D(),
C(),
// keep-sorted end
}

// Max line length set to 20, so this should raise issue.
protected void finalize(int a) {}
}
55 changes: 31 additions & 24 deletions example/tools/lint/linters.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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")
load("@aspect_rules_lint//lint:keep_sorted.bzl", "lint_keep_sorted_aspect")
load("@aspect_rules_lint//lint:ktlint.bzl", "lint_ktlint_aspect")
load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test")
load("@aspect_rules_lint//lint:pmd.bzl", "lint_pmd_aspect")
Expand All @@ -15,76 +16,76 @@ load("@aspect_rules_lint//lint:stylelint.bzl", "lint_stylelint_aspect")
load("@aspect_rules_lint//lint:vale.bzl", "lint_vale_aspect")

buf = lint_buf_aspect(
config = "@@//:buf.yaml",
config = Label("@//:buf.yaml"),
)

eslint = lint_eslint_aspect(
binary = "@@//tools/lint:eslint",
binary = Label("@//tools/lint:eslint"),
# ESLint will resolve the configuration file by looking in the working directory first.
# See https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file-resolution
# We must also include any other config files we expect eslint to be able to locate, e.g. tsconfigs
configs = [
"@@//:eslintrc",
"@@//src:tsconfig",
Label("@//:eslintrc"),
Label("@//src:tsconfig"),
],
)

eslint_test = lint_test(aspect = eslint)

stylelint = lint_stylelint_aspect(
binary = "@@//tools/lint:stylelint",
config = "@@//:stylelintrc",
binary = Label("@//tools/lint:stylelint"),
config = Label("@//:stylelintrc"),
)

flake8 = lint_flake8_aspect(
binary = "@@//tools/lint:flake8",
config = "@@//:.flake8",
binary = Label("@//tools/lint:flake8"),
config = Label("@//:.flake8"),
)

flake8_test = lint_test(aspect = flake8)

pmd = lint_pmd_aspect(
binary = "@@//tools/lint:pmd",
rulesets = ["@@//:pmd.xml"],
binary = Label("@//tools/lint:pmd"),
rulesets = [Label("@//:pmd.xml")],
)

pmd_test = lint_test(aspect = pmd)

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

checkstyle_test = lint_test(aspect = checkstyle)

ruff = lint_ruff_aspect(
binary = "@multitool//tools/ruff",
configs = [
"@@//:.ruff.toml",
"@@//src/subdir:ruff.toml",
Label("@//:.ruff.toml"),
Label("@//src/subdir:ruff.toml"),
],
)

ruff_test = lint_test(aspect = ruff)

shellcheck = lint_shellcheck_aspect(
binary = "@multitool//tools/shellcheck",
config = "@@//:.shellcheckrc",
config = Label("@//:.shellcheckrc"),
)

shellcheck_test = lint_test(aspect = shellcheck)

vale = lint_vale_aspect(
binary = "@@//tools/lint:vale",
config = "@@//:.vale.ini",
styles = "@@//tools/lint:vale_styles",
binary = Label("@//tools/lint:vale"),
config = Label("@//:.vale.ini"),
styles = Label("@//tools/lint:vale_styles"),
)

ktlint = lint_ktlint_aspect(
binary = "@@com_github_pinterest_ktlint//file",
editorconfig = "@@//:.editorconfig",
baseline_file = "@@//:ktlint-baseline.xml",
binary = Label("@com_github_pinterest_ktlint//file"),
editorconfig = Label("@//:.editorconfig"),
baseline_file = Label("@//:ktlint-baseline.xml"),
)

ktlint_test = lint_test(aspect = ktlint)
Expand Down Expand Up @@ -113,8 +114,14 @@ clang_tidy_global_config = lint_clang_tidy_aspect(
)

spotbugs = lint_spotbugs_aspect(
binary = "@@//tools/lint:spotbugs",
exclude_filter = "@@//:spotbugs-exclude.xml",
binary = Label("@//tools/lint:spotbugs"),
exclude_filter = Label("@//:spotbugs-exclude.xml"),
)

spotbugs_test = lint_test(aspect = spotbugs)

keep_sorted = lint_keep_sorted_aspect(
binary = Label("@com_github_google_keep_sorted//:keep-sorted"),
)

keep_sorted_test = lint_test(aspect = keep_sorted)
7 changes: 7 additions & 0 deletions lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ bzl_library(
deps = ["//lint/private:lint_aspect"],
)

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

bzl_library(
name = "pmd",
srcs = ["pmd.bzl"],
Expand Down
1 change: 1 addition & 0 deletions lint/keep-sorted/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty package
15 changes: 15 additions & 0 deletions lint/keep-sorted/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/aspect-build/rules_lint/lint/keep-sorted

go 1.23.5

require github.com/google/keep-sorted v0.6.0

require (
github.com/Workiva/go-datastructures v1.0.53 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/zerolog v1.31.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 2e2cbc3

Please sign in to comment.