Skip to content

Commit 2e2cbc3

Browse files
fzakariaalexeagle
andauthored
feat: Add keep-sorted as a linter (#463)
* 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]>
1 parent 50c151a commit 2e2cbc3

File tree

16 files changed

+425
-29
lines changed

16 files changed

+425
-29
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ Features:
2121

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

24+
Linters which are not language-specific:
25+
26+
- [keep-sorted]
27+
2428
| Language | Formatter | Linter(s) |
25-
| ---------------------- | --------------------- |----------------------------------|
29+
| ---------------------- | --------------------- | -------------------------------- |
2630
| C / C++ | [clang-format] | [clang-tidy] |
2731
| Cuda | [clang-format] | |
2832
| CSS, Less, Sass | [Prettier] | [Stylelint] |
@@ -59,6 +63,7 @@ New tools are being added frequently, so check this page again!
5963
[swiftformat]: https://github.com/nicklockwood/SwiftFormat
6064
[terraform]: https://github.com/hashicorp/terraform
6165
[buf]: https://docs.buf.build/format/usage
66+
[keep-sorted]: https://github.com/google/keep-sorted
6267
[ktfmt]: https://github.com/facebook/ktfmt
6368
[ktlint]: https://github.com/pinterest/ktlint
6469
[buildifier]: https://github.com/keith/buildifier-prebuilt

docs/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ stardoc_with_diff_test(
2222
bzl_library_target = "//lint:flake8",
2323
)
2424

25+
stardoc_with_diff_test(
26+
name = "keep_sorted",
27+
bzl_library_target = "//lint:keep_sorted",
28+
)
29+
2530
stardoc_with_diff_test(
2631
name = "pmd",
2732
bzl_library_target = "//lint:pmd",

docs/keep_sorted.md

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/.aspect/cli/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ lint:
1111
- //tools/lint:linters.bzl%checkstyle
1212
- //tools/lint:linters.bzl%clang_tidy
1313
- //tools/lint:linters.bzl%spotbugs
14+
- //tools/lint:linters.bzl%keep_sorted

example/.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Automatically apply --config=linux, --config=windows etc
22
common --enable_platform_specific_config
33

4+
# Permit MODULE.bazel to use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
5+
common --experimental_isolated_extension_usages
6+
47
# Aspect recommended Bazel flags when using rules_java and rules_jvm_external
58

69
# Pin java versions to desired language level

example/MODULE.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ bazel_dep(name = "toolchains_llvm", version = "0.10.3")
1111
bazel_dep(name = "toolchains_protoc", version = "0.3.0")
1212
bazel_dep(name = "rules_java", version = "8.5.0")
1313
bazel_dep(name = "rules_jvm_external", version = "6.5")
14-
bazel_dep(name = "rules_go", version = "0.42.0", repo_name = "io_bazel_rules_go")
14+
bazel_dep(name = "rules_go", version = "0.52.0", repo_name = "io_bazel_rules_go")
1515
bazel_dep(name = "rules_proto", version = "6.0.0")
1616
bazel_dep(name = "rules_python", version = "0.26.0")
1717
bazel_dep(name = "rules_rust", version = "0.50.1")
1818
bazel_dep(name = "buildifier_prebuilt", version = "6.3.3")
1919
bazel_dep(name = "platforms", version = "0.0.8")
2020
bazel_dep(name = "rules_kotlin", version = "1.9.0")
2121
bazel_dep(name = "rules_cc", version = "0.0.9")
22+
bazel_dep(name = "gazelle", version = "0.41.0")
2223

2324
local_path_override(
2425
module_name = "aspect_rules_lint",
@@ -74,10 +75,14 @@ use_repo(pip, "pip")
7475
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
7576
go_sdk.download(
7677
name = "go_sdk",
77-
version = "1.20.3",
78+
version = "1.23.5",
7879
)
7980
use_repo(go_sdk, "go_sdk")
8081

82+
keep_sorted_deps = use_extension("@gazelle//:extensions.bzl", "go_deps", isolate = True)
83+
keep_sorted_deps.from_file(go_mod = "@aspect_rules_lint//lint/keep-sorted:go.mod")
84+
use_repo(keep_sorted_deps, "com_github_google_keep_sorted")
85+
8186
# Java and other JVM languages:
8287
# https://github.com/bazelbuild/rules_jvm_external/blob/master/examples/bzlmod/MODULE.bazel
8388
# https://github.com/bazelbuild/rules_jvm_external#pinning-artifacts-and-integration-with-bazels-downloader

example/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if [ $machine == "Windows" ]; then
3737
# avoid missing linters on windows platform
3838
args=("--aspects=$(echo //tools/lint:linters.bzl%{flake8,pmd,ruff,vale,clang_tidy} | tr ' ' ',')")
3939
else
40-
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,ktlint,pmd,ruff,shellcheck,stylelint,vale,clang_tidy,spotbugs} | tr ' ' ',')")
40+
args=("--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,keep_sorted,ktlint,pmd,ruff,shellcheck,stylelint,vale,clang_tidy,spotbugs} | tr ' ' ',')")
4141
fi
4242

4343
# NB: perhaps --remote_download_toplevel is needed as well with remote execution?

example/src/Bar.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
import java.io.BufferedInputStream;
66

77
public class Bar {
8+
9+
enum MyEnum {
10+
// keep-sorted start
11+
B(),
12+
A(),
13+
D(),
14+
C(),
15+
// keep-sorted end
16+
}
17+
818
// Max line length set to 20, so this should raise issue.
919
protected void finalize(int a) {}
1020
}

example/tools/lint/linters.bzl

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load("@aspect_rules_lint//lint:checkstyle.bzl", "lint_checkstyle_aspect")
55
load("@aspect_rules_lint//lint:clang_tidy.bzl", "lint_clang_tidy_aspect")
66
load("@aspect_rules_lint//lint:eslint.bzl", "lint_eslint_aspect")
77
load("@aspect_rules_lint//lint:flake8.bzl", "lint_flake8_aspect")
8+
load("@aspect_rules_lint//lint:keep_sorted.bzl", "lint_keep_sorted_aspect")
89
load("@aspect_rules_lint//lint:ktlint.bzl", "lint_ktlint_aspect")
910
load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test")
1011
load("@aspect_rules_lint//lint:pmd.bzl", "lint_pmd_aspect")
@@ -15,76 +16,76 @@ load("@aspect_rules_lint//lint:stylelint.bzl", "lint_stylelint_aspect")
1516
load("@aspect_rules_lint//lint:vale.bzl", "lint_vale_aspect")
1617

1718
buf = lint_buf_aspect(
18-
config = "@@//:buf.yaml",
19+
config = Label("@//:buf.yaml"),
1920
)
2021

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

3233
eslint_test = lint_test(aspect = eslint)
3334

3435
stylelint = lint_stylelint_aspect(
35-
binary = "@@//tools/lint:stylelint",
36-
config = "@@//:stylelintrc",
36+
binary = Label("@//tools/lint:stylelint"),
37+
config = Label("@//:stylelintrc"),
3738
)
3839

3940
flake8 = lint_flake8_aspect(
40-
binary = "@@//tools/lint:flake8",
41-
config = "@@//:.flake8",
41+
binary = Label("@//tools/lint:flake8"),
42+
config = Label("@//:.flake8"),
4243
)
4344

4445
flake8_test = lint_test(aspect = flake8)
4546

4647
pmd = lint_pmd_aspect(
47-
binary = "@@//tools/lint:pmd",
48-
rulesets = ["@@//:pmd.xml"],
48+
binary = Label("@//tools/lint:pmd"),
49+
rulesets = [Label("@//:pmd.xml")],
4950
)
5051

5152
pmd_test = lint_test(aspect = pmd)
5253

5354
checkstyle = lint_checkstyle_aspect(
54-
binary = "@@//tools/lint:checkstyle",
55-
config = "@@//:checkstyle.xml",
56-
data = ["@@//:checkstyle-suppressions.xml"],
55+
binary = Label("@//tools/lint:checkstyle"),
56+
config = Label("@//:checkstyle.xml"),
57+
data = [Label("@//:checkstyle-suppressions.xml")],
5758
)
5859

5960
checkstyle_test = lint_test(aspect = checkstyle)
6061

6162
ruff = lint_ruff_aspect(
6263
binary = "@multitool//tools/ruff",
6364
configs = [
64-
"@@//:.ruff.toml",
65-
"@@//src/subdir:ruff.toml",
65+
Label("@//:.ruff.toml"),
66+
Label("@//src/subdir:ruff.toml"),
6667
],
6768
)
6869

6970
ruff_test = lint_test(aspect = ruff)
7071

7172
shellcheck = lint_shellcheck_aspect(
7273
binary = "@multitool//tools/shellcheck",
73-
config = "@@//:.shellcheckrc",
74+
config = Label("@//:.shellcheckrc"),
7475
)
7576

7677
shellcheck_test = lint_test(aspect = shellcheck)
7778

7879
vale = lint_vale_aspect(
79-
binary = "@@//tools/lint:vale",
80-
config = "@@//:.vale.ini",
81-
styles = "@@//tools/lint:vale_styles",
80+
binary = Label("@//tools/lint:vale"),
81+
config = Label("@//:.vale.ini"),
82+
styles = Label("@//tools/lint:vale_styles"),
8283
)
8384

8485
ktlint = lint_ktlint_aspect(
85-
binary = "@@com_github_pinterest_ktlint//file",
86-
editorconfig = "@@//:.editorconfig",
87-
baseline_file = "@@//:ktlint-baseline.xml",
86+
binary = Label("@com_github_pinterest_ktlint//file"),
87+
editorconfig = Label("@//:.editorconfig"),
88+
baseline_file = Label("@//:ktlint-baseline.xml"),
8889
)
8990

9091
ktlint_test = lint_test(aspect = ktlint)
@@ -113,8 +114,14 @@ clang_tidy_global_config = lint_clang_tidy_aspect(
113114
)
114115

115116
spotbugs = lint_spotbugs_aspect(
116-
binary = "@@//tools/lint:spotbugs",
117-
exclude_filter = "@@//:spotbugs-exclude.xml",
117+
binary = Label("@//tools/lint:spotbugs"),
118+
exclude_filter = Label("@//:spotbugs-exclude.xml"),
118119
)
119120

120121
spotbugs_test = lint_test(aspect = spotbugs)
122+
123+
keep_sorted = lint_keep_sorted_aspect(
124+
binary = Label("@com_github_google_keep_sorted//:keep-sorted"),
125+
)
126+
127+
keep_sorted_test = lint_test(aspect = keep_sorted)

lint/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ bzl_library(
161161
deps = ["//lint/private:lint_aspect"],
162162
)
163163

164+
bzl_library(
165+
name = "keep_sorted",
166+
srcs = ["keep_sorted.bzl"],
167+
visibility = ["//visibility:public"],
168+
deps = ["//lint/private:lint_aspect"],
169+
)
170+
164171
bzl_library(
165172
name = "pmd",
166173
srcs = ["pmd.bzl"],

0 commit comments

Comments
 (0)