Skip to content

Commit 680fd22

Browse files
committed
Add new constructor variable
1 parent 88564b2 commit 680fd22

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

designs/2025-bulk-suppressions-api/README.md

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,40 @@ This RFC aims to resolve this discrepancy by adding support for bulk suppression
2323

2424
This proposal integrates the existing bulk suppression functionality into the `ESLint` and `LegacyESLint` Node.js API classes by leveraging the internal `SuppressionsService`. A new `suppressionsLocation` option is introduced in the constructors.
2525

26-
1. New Constructor Option (`suppressionsLocation`)
27-
- Both `ESLintOptions` and `LegacyESLintOptions` will accept a new optional property: `suppressionsLocation`.
28-
- `suppressionsLocation: string | undefined`: Specifies the path to the suppressions file (`eslint-suppressions.json`). This path can be absolute or relative to the `cwd`.
29-
- If `suppressionsLocation` is provided, ESLint will attempt to load suppressions from that specific file path.
30-
- If `suppressionsLocation` is not provided (or is `undefined`), ESLint will default to looking for `eslint-suppressions.json` in the `cwd`.
26+
1. New Constructor Options
27+
- `suppressionsLocation`
28+
- Both `ESLintOptions` and `LegacyESLintOptions` will accept a new optional property: `suppressionsLocation`.
29+
- `suppressionsLocation: string | undefined`: Specifies the path to the suppressions file (`eslint-suppressions.json`). This path can be absolute or relative to the `cwd`.
30+
- If `suppressionsLocation` is provided, ESLint will attempt to load suppressions from that specific file path.
31+
- If `suppressionsLocation` is not provided (or is `undefined`), ESLint will default to looking for `eslint-suppressions.json` in the `cwd`.
32+
- `applySuppressions`
33+
- Controls whether suppressions are automatically applied to results from `lintText()` and `lintFiles()`.
34+
- If `true`, suppressions are automatically applied to lint results before returning.
35+
- If `false`, results are returned as-is without applying suppressions.
36+
- If not provided (or is `undefined`), defaults to `true`.
3137

3238
2. Service Instantiation and Configuration
3339
- Upon instantiation, the `ESLint` and `LegacyESLint` constructors will create an instance of `SuppressionsService`.
34-
- A new constructor option, `suppressionsLocation` (string, optional), will be added to both classes.
35-
- If provided and relative, this path (relative to `cwd`) specifies the suppression file.
36-
- If provided and absolute, this path (relative to `/`) specifies the suppression file.
37-
- If not provided, ESLint defaults to searching for `eslint-suppressions.json` in the `cwd`.
38-
- The constructor will resolve the final absolute path to the suppression file (using `suppressionsLocation` or the default) and pass it to the `SuppressionsService` constructor.
40+
- 2 new constructor options will be added to both classes.
41+
- `suppressionsLocation` (string, optional)
42+
- If provided and relative, this path (relative to `cwd`) specifies the suppression file.
43+
- If provided and absolute, this path (relative to `/`) specifies the suppression file.
44+
- If not provided, ESLint defaults to searching for `eslint-suppressions.json` in the `cwd`.
45+
- The constructor will resolve the final absolute path to the suppression file (using `suppressionsLocation` or the default) and pass it to the `SuppressionsService` constructor.
46+
- `applySuppressions` (boolean, optional)
47+
- If `true`, suppressions will be applied automatically in `lintText()` and `lintFiles()`.
48+
- If `false`, suppressions will not be applied.
49+
- If not provided, defaults to `true`.
3950

4051
3. Applying Suppressions
41-
- Within the `lintFiles()` and `lintText()` methods of both classes, *after* the initial linting results are obtained, the `applySuppressions` method of the instantiated `SuppressionsService` will be called.
42-
- This method takes the raw linting results and the loaded suppressions (from the resolved file path) and returns the results with suppressions applied, along with any unused suppressions.
43-
- The final, suppression-applied results will be returned to the user.
52+
- When `applySuppressions` is `true` (the default), the `lintFiles()` and `lintText()` methods will automatically apply suppressions to results before returning.
53+
- Within these methods, *after* the initial linting results are obtained, the `applySuppressions` method of the instantiated `SuppressionsService` will be called.
54+
- This method takes the raw linting results and the loaded suppressions (from the resolved file path) and returns the results with suppressions applied.
55+
- When `applySuppressions` is `false`, the raw linting results are returned without applying suppressions, allowing consumers to handle suppressions themselves.
4456

4557
4. Changes to ESLint CLI
46-
- With the integration of suppression handling into the `ESLint` and `LegacyESLint` APIs, the ESLint CLI (`lib/cli.js`) will be updated.
47-
- Specifically, direct calls to `SuppressionsService` within the CLI will be removed. The CLI will now leverage the updated API methods to handle bulk suppressions, ensuring that the CLI's behavior is consistent with the API's new capabilities. This change simplifies the CLI's implementation by delegating suppression logic to the core API.
58+
- The CLI will instantiate `ESLint` with `applySuppressions: false` to receive raw linting results.
59+
- The CLI will continue to call `SuppressionsService` directly to handle suppression-related flags (`--suppress-all`, `--suppress-rule`, `--prune-suppressions`) and to apply suppressions before output.
4860

4961
### Example code of `lib/eslint/eslint.js`
5062

@@ -61,6 +73,7 @@ class ESLint {
6173
#suppressionsService;
6274

6375
constructor(options = {}) {
76+
// options includes: suppressionsLocation, applySuppressions, cwd, ...
6477
const processedOptions = processOptions(options);
6578

6679
// ... existing constructor logic to initialize options, linter, cache, configLoader ...
@@ -78,8 +91,7 @@ class ESLint {
7891
}
7992

8093
async lintFiles(patterns) {
81-
const { option, lintResultCache, /* ... other needed members */ } = privateMembers.get(this);
82-
let suppressionResults = null;
94+
const { options, lintResultCache, /* ... other needed members */ } = privateMembers.get(this);
8395

8496
// Existing lint logic to get initial `results` (LintResult[])
8597
const results = /* LintResult[] */
@@ -91,13 +103,13 @@ class ESLint {
91103

92104
const finalResults = results.filter(result => !!result);
93105

94-
if (!fs.existsSync(suppressionsFilePath)) {
106+
if (!fs.existsSync(suppressionsFilePath) || options.applySuppressions === false) {
95107
return finalResults;
96-
}
108+
}
97109

98110
return this.#suppressionsService.applySuppressions(
99111
finalResults,
100-
await suppressions.load(),
112+
await this.#suppressionsService.load(),
101113
);
102114
}
103115

@@ -131,16 +143,15 @@ class ESLint {
131143
}),
132144
);
133145

134-
if (!fs.existsSync(suppressionsFilePath)) {
146+
if (!fs.existsSync(suppressionsFilePath) || eslintOptions.applySuppressions === false) {
135147
return processLintReport(this, { results });
136148
}
137149

138150
const suppressedResults = this.#suppressionsService.applySuppressions(
139151
results,
140-
await suppressions.load(),
152+
await this.#suppressionsService.load(),
141153
);
142154

143-
// Return the suppression-applied results
144155
return processLintReport(this, { results: suppressedResults });
145156
}
146157
}
@@ -151,28 +162,30 @@ class ESLint {
151162
The documentation updates will reflect that this change aligns the Node.js API behavior with the existing CLI functionality.
152163

153164
1. API Documentation (`ESLint`/`LegacyESLint` classes):
154-
- Add the new `suppressionsLocation` option to the constructor options documentation for both `ESLint` and `LegacyESLint`, explaining its purpose (specifying the suppression file path) and behavior (relative to `cwd`, default lookup).
155-
- Add a note to the descriptions of `lintText()` and `lintFiles()` methods stating that suppressions are automatically applied based on the resolved suppression file path (either from `suppressionsLocation` or the default `eslint-suppressions.json` in `cwd`). Example: "Applies suppressions from the resolved suppression file (`suppressionsLocation` option or `eslint-suppressions.json` in `cwd`), if found."
165+
- Add the new `suppressionsLocation` and `applySuppressions` options to the constructor options documentation for both `ESLint` and `LegacyESLint`.
166+
- Document `suppressionsLocation`: its purpose (specifying the suppression file path) and behavior (relative to `cwd`, default lookup).
167+
- Document `applySuppressions`: controls whether suppressions are automatically applied (defaults to `true`).
168+
- Add a note to the descriptions of `lintText()` and `lintFiles()` methods stating that suppressions are automatically applied when `applySuppressions` is `true` (the default), based on the resolved suppression file path.
156169

157170
2. Bulk Suppressions User Guide Page:
158171
- Update the existing user guide page for Bulk Suppressions.
159172
- Add a section or note clarifying that the feature is now also available when using the `ESLint` and `LegacyESLint` Node.js APIs.
160173
- Explicitly mention how the suppression file is located when using the API: "Note: When using the Node.js API, ESLint searches for the suppression file specified by the `suppressionsLocation` constructor option. If this option is not provided, it defaults to looking for `eslint-suppressions.json` in the `cwd` (current working directory)."
161174

162175
3. Release Notes:
163-
- Include an entry in the release notes announcing the availability of bulk suppressions in the Node.js API, **highlighting the new `suppressionsLocation` option**.
176+
- Include an entry in the release notes announcing the availability of bulk suppressions in the Node.js API, **highlighting the new `suppressionsLocation` and `applySuppressions` options**.
164177

165178
## Drawbacks
166179

167-
- API Complexity: Introduces a new option (`suppressionsLocation`) to the constructor API surface for both `ESLint` and `LegacyESLint`, slightly increasing complexity compared to only supporting the default file location.
180+
- API Complexity: Introduces two new options (`suppressionsLocation` and `applySuppressions`) to the constructor API surface for both `ESLint` and `LegacyESLint`, slightly increasing complexity.
168181
- Performance: The overhead of potentially resolving `suppressionsLocation` and then searching for/parsing the suppression file is introduced. However, this aligns the API\'s behavior and capabilities with the CLI.
169182
- Complexity: Introduces `SuppressionsService` interaction into `ESLint`/`LegacyESLint`, but reuses existing internal logic.
170183

171184
## Backwards Compatibility Analysis
172185

173186
This change is designed to be backward-compatible.
174187

175-
- New Option is Optional: The new `suppressionsLocation` option is optional. Existing code that does not provide this option will continue to work, defaulting to the behavior of looking for `eslint-suppressions.json` in the `cwd`.
188+
- New Options are Optional: The new `suppressionsLocation` and `applySuppressions` options are optional. Existing code that does not provide these options will continue to work, with `applySuppressions` defaulting to `true` and `suppressionsLocation` defaulting to looking for `eslint-suppressions.json` in the `cwd`.
176189
- Automatic Application: By integrating bulk suppression handling directly into the existing `lintText()` and `lintFiles()` methods, users who utilize a suppression file (either at the default location or specified via the new option) will automatically benefit simply by updating their ESLint version.
177190
- Alignment with CLI: This approach aligns the Node.js API behavior *and configuration options* more closely with the established CLI behavior.
178191
- Non-Breaking: Since the core change only alters behavior when a suppression file is found (based on the new option or default location), it is considered a non-breaking change for existing API consumers.

0 commit comments

Comments
 (0)