Skip to content

Commit bca3379

Browse files
mrmekualexeagle
andcommitted
feat(format): Add ability to ignore files (#210)
* feat: remove golangci-lint (#207) In preparation for 1.0, we want all linters to work correctly. In the discussion for #129 we decided that is a fatal bug, and no one is able to fix it in the short term. We're happy to add Go linting back to rules_lint when someone has a correct implementation that handles transitive srcs. The code will still be here in the git history when it's time to revive it. Closes #129 * Move changes from linting.md to formatting.md * Address review comments --------- Co-authored-by: Alex Eagle <[email protected]>
1 parent 07a0584 commit bca3379

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

docs/formatting.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ Assuming you installed with the typical layout:
7979

8080
`bazel run //:format some/file.md other/file.json`
8181

82+
### Ignoring files explicitly
83+
84+
Commonly, the underlying formatters that rules_lint invokes provide their own methods of excluding files (.prettierignore for example). At times when that is not the case, rules_lint provides its
85+
own escape hatch to exclude files from linting using attributes specified via [`.gitattributes` files](https://git-scm.com/docs/gitattributes).
86+
87+
If any of following attributes are set on a file it will be excluded:
88+
89+
- `rules-lint-ignored`
90+
- `gitlab-generated`
91+
- `linguist-generated`
92+
8293
### Install as a pre-commit hook
8394

8495
If you use [pre-commit.com](https://pre-commit.com/), add this in your `.pre-commit-config.yaml`:

example/.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Do not try and format generated code
2+
pnpm-lock.yaml linguist-generated
3+
4+
# Any of the below attributes can be set to ignore a file
5+
src/subdir/rules-lint-ignored.js rules-lint-ignored
6+
src/subdir/gitlab-generated.js gitlab-generated
7+
src/subdir/linguist-generated.js linguist-generated
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Ignored by https://github.com/aspect-build/rules_lint/blob/example/.gitattributes
2+
export var x = "white space issue and no semi colon"
3+

example/src/subdir/linguist-generated.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Ignored by https://github.com/aspect-build/rules_lint/blob/example/.gitattributes
2+
export var x = "white space issue and no semi colon"
3+

format/private/format.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,18 @@ function ls-files {
9494

9595
# TODO: determine which staged changes we should format; avoid formatting unstaged changes
9696
# TODO: try to format only modified regions of the file (where supported)
97-
git ls-files --cached --modified --other --exclude-standard "${patterns[@]}" "${patterns[@]/#/*/}" | {
98-
grep -vE "^$(git ls-files --deleted)$" || true;
99-
}
97+
files=$(git ls-files --cached --modified --other --exclude-standard "${patterns[@]}" "${patterns[@]/#/*/}" | {
98+
grep -vE \
99+
"^$(git ls-files --deleted)$" \
100+
|| true;
101+
})
102+
git_attributes=$(git check-attr -a -- $files)
103+
for file in $files; do
104+
# Check if any of the attributes we ignore are set for this file.
105+
if ! grep -qE "(^| )$file: (rules-lint-ignored|linguist-generated|gitlab-generated): set($| )" <<< $git_attributes; then
106+
echo $file
107+
fi
108+
done
100109
else
101110
# When given arguments, they are glob patterns of the superset of files to format.
102111
# We just need to filter those so we only select files for this language

0 commit comments

Comments
 (0)