You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Introduce a way to suppress violations (#19159)
* feat: Suppress violations
* Add more examples and fix headings
* Cleanup suppressions file after each test
* Use posix format regardless of the OS
* Apply posix format at the relative path
* Output unused rules when in debug mode
* Minor simplifications and more docs
* Add more test for --fix and pruning
* Move messages to suppressed messages along with the reason.
* Adjust counters for fatalErrorCount, fixableErrorCount and fixableWarningCount
* Prunes suppressions when fixes were applied
* Report both errors and message about pruning
* Extract and re-use calculateStatsPerFile
* Do not automatically prune suppressions when running fix
* Cleanup and align with the project standards.
* Use the async APIs rather than the sync APIs
* More code cleanups
* Remove unused options - these are CLIEngine constructor options
* Update entries for the files that were linted in the current run and leave the others unchanged
* Add dedicated page for suppressions
* Simplify
* Apply suggestions
* More suggestions
* Import types and ignore null ruleIds
* Drop the dot
* Add quotes to all errors and warnings
* Drop the dot from the directory file, and throw an error if the file doesn't exist
* Update docs
* Update docs/src/use/command-line-interface.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update docs/src/use/command-line-interface.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update docs/src/use/command-line-interface.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update docs/src/use/command-line-interface.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update docs/src/use/command-line-interface.md
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Re-calculate stats only once per file
* displays an error when the suppressions file doesn't exist
* Update lib/services/suppressions-service.js
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Add more tests about stdin, cli args and multiple rules
* Cover warnings as well
* Re-calculate only when violations were suppressed
* Simplify further
* Re-format code
* Tiding up
---------
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Suppresses existing violations, so that they are not being reported in subsequent runs. It allows you to enable one or more lint rules and be notified only when new violations show up. The suppressions are stored in `eslint-suppressions.json` by default, unless otherwise specified by `--suppressions-location`. The file gets updated with the new suppressions.
854
+
855
+
-**Argument Type**: No argument.
856
+
857
+
##### `--suppress-all` example
858
+
859
+
{{ npx_tabs ({
860
+
package: "eslint",
861
+
args: ["\"src/**/*.js\"", "--suppress-all"]
862
+
}) }}
863
+
864
+
#### `--suppress-rule`
865
+
866
+
Suppresses violations for specific rules, so that they are not being reported in subsequent runs. Similar to `--suppress-all`, the suppressions are stored in `eslint-suppressions.json` by default, unless otherwise specified by `--suppressions-location`. The file gets updated with the new suppressions.
Specify the path to the suppressions location. Can be a file or a directory.
881
+
882
+
-**Argument Type**: String. Path to file. If a directory is specified, a cache file is created inside the specified folder. The name of the file is based on the hash of the current working directory, e.g.: `suppressions_hashOfCWD`
883
+
-**Multiple Arguments**: No
884
+
-**Default Value**: If no location is specified, `eslint-suppressions.json` is used. The file is created in the directory where the `eslint` command is executed.
Enabling a new lint rule as `"error"` can be challenging when the codebase has many violations and the rule isn't auto-fixable. Unless the rule is enabled during the early stages of the project, it becomes harder and harder to enable it as the codebase grows. Existing violations must be resolved before enabling the rule, but while doing that other violations may occur.
11
+
12
+
To address this, ESLint provides a way to suppress existing violations for one or more rules. While the rule will be enforced for new code, the existing violations will not be reported. This way, you can address the existing violations at your own pace.
13
+
14
+
::: important
15
+
Only rules configured as `"error"` are suppressed. If a rule is enabled as `"warn"`, ESLint will not suppress the violations.
16
+
:::
17
+
18
+
After you enable a rule as `"error"` in your configuration file, you can suppress all the existing violations at once by using the `--suppress-all` flag. It is recommended to execute the command with the `--fix` flag so that you don't suppress violations that can be auto-fixed.
19
+
20
+
```bash
21
+
eslint --fix --suppress-all
22
+
```
23
+
24
+
This command will suppress all the existing violations of all the rules that are enabled as `"error"`. Running the `eslint` command again will not report these violations.
25
+
26
+
If you would like to suppress violations of a specific rule, you can use the `--suppress-rule` flag.
When you suppress violations, ESLint creates a `eslint-suppressions.json` file in the root of the project. This file contains the list of rules that have been suppressed. You should commit this file to the repository so that the suppressions are shared with all the developers.
41
+
42
+
If necessary, you can change the location of the suppressions file by using the `--suppressions-location` argument. Note that the argument must be provided not only when suppressing violations but also when running ESLint. This is necessary so that ESLint picks up the correct suppressions file.
You can address any of the reported violations by making the necessary changes to the code as usual. If you run ESLint again you will notice that a warning is reported about unused suppressions. This is because the violations have been resolved but the suppressions are still in place.
51
+
52
+
```bash
53
+
> eslint
54
+
There are suppressions left that do not occur anymore. Consider re-running the command with `--prune-suppressions`.
55
+
```
56
+
57
+
To remove the suppressions that are no longer needed, you can use the `--prune-suppressions` flag.
58
+
59
+
```bash
60
+
eslint --prune-suppressions
61
+
```
62
+
63
+
For more information on the available CLI options, refer to [Command Line Interface](./command-line-interface).
0 commit comments