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

docs: explain how users can customize linting behavior of rules #2062

Merged
merged 1 commit into from
Sep 1, 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
68 changes: 68 additions & 0 deletions docs/users/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,71 @@ triggered.rules = [
DisableSyntax
]
```

## Overriding the linting behavior

By default, only lints raised with `LintSeverity.Error` will result in a
non-zero exit code. To force Scalafix to return a non-zero exit code for other
diagnostics, it is possible to promote non-error diagnostics to the error level
without touching the rules(s) source code, thanks to `lint.error`.

This key accepts as value either
* a string, interpreted as a regular expression to match diagnostics using the
`<rule_name>[.<lint_id>]` pattern,
* a list of strings, treated as several regular expressions like above,
* an object, with `include` and `exclude` attributes, each treated as regular
expressions like above.

As an example, to force all linter reports to trigger an error, except for unused
suppressions, any of the 3 configuration files below can be used, as they are
effectively equivalent.

```scala
// .scalafix.conf
lint.error = "^((?!UnusedScalafixSuppression).)*$"
```

```scala
// .scalafix.conf
lint.error = [
"^((?!UnusedScalafixSuppression).)*$"
]
```

```scala
// .scalafix.conf
lint.error = {
includes = ".*"
excludes = "UnusedScalafixSuppression"
}
```

Similarly, diagnostics can be controlled through `lint.ignore`, `lint.info`,
and `lint.warning`, which respectively suppress matching diagnostics, force
them to be at info level, and force them to be at warning level.

## Passing configuration as CLI arguments

It is possible to pass configuration flags with scalar values as CLI arguments,
overriding any potential value set in the configuration file, by prefixing the
qualified name of each attribute with `--settings.`.

For example, the configuration file below

```scala
// .scalafix.conf
DisableSyntax {
noFinalize = true
}
lint.error.includes = ".*"
lint.error.excludes = "UnusedScalafixSuppression"
```

is equivalent to the following CLI arguments

```
scalafix ... \
--settings.DisableSyntax.noFinalize=true \
--settings.lint.error.includes=.* \
Copy link
Collaborator Author

@bjaglin bjaglin Sep 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a shell context, this needs to be wrapped in single quotes so that * is not processed.

Suggested change
--settings.lint.error.includes=.* \
'--settings.lint.error.includes=.*' \

However, this syntax is not supported on sbt-scalafix, so I'd rather document the sbt-scalafix one, and let CLI power users figure out by themselves (instead of being very verbose about it here and confusing sbt-scalafix users).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out that multiline syntax does not work on sbt, so I am taking back what I wrote above and will target CLI/shell syntax instead in another PR

--settings.lint.error.excludes=UnusedScalafixSuppression
```
11 changes: 9 additions & 2 deletions docs/users/suppression.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ id: suppression
title: Suppressing rules
---

Sometimes you need to ignore the output from Scalafix. Scalafix provides two
methods for suppressing rules: annotations and comments.
Sometimes you need to ignore the output from Scalafix.

For linters, and for linters only, an easy way to achieve that is to
selectively ignore some diagnostics via
[`lint.ignore`](configuration.md#overriding-the-linting-behavior).
However, this toggle is global, which is rarely what is needed.

Scalafix provides two methods for suppressing rules in source code: annotations
and comments.

- Annotations: great for disabling regions of code but some source code
locations are impossible to annotate.
Expand Down