-
-
Notifications
You must be signed in to change notification settings - Fork 50
feat: support bulk suppressions #292
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
Open
viddo
wants to merge
1
commit into
webpack:main
Choose a base branch
from
viddo:fix-not-fail-on-suppressions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const fs = require("node:fs"); | ||
| const { dirname, isAbsolute, resolve } = require("node:path"); | ||
|
|
||
| /** @typedef {import('eslint').ESLint.LintResult} LintResult */ | ||
| /** @typedef {import('./options').Options} Options */ | ||
| /** | ||
| * @typedef {Record<string, Record<string, { count: number }>>} SuppressedViolations | ||
| * @typedef {new (options: { filePath: string, cwd: string }) => { load: () => Promise<SuppressedViolations>, applySuppressions: (results: LintResult[], suppressions: SuppressedViolations) => { results: LintResult[], unused: SuppressedViolations } }} SuppressionsService | ||
| */ | ||
|
|
||
| /** | ||
| * Try to load SuppressionsService from ESLint.. | ||
| * Returns null if not available, e.g. older ESLint versions. | ||
| * @returns {SuppressionsService | null} SuppressionsService instance or null | ||
| */ | ||
| function getSuppressionsService() { | ||
| // ESLint doesn't export SuppressionsService in package.json exports, | ||
| // so we need to resolve the path directly | ||
| const eslintPath = require.resolve("eslint"); | ||
| const eslintDir = eslintPath.replace(/\/lib\/api\.js$/, ""); | ||
|
|
||
| try { | ||
| const { SuppressionsService } = require( | ||
| `${eslintDir}/lib/services/suppressions-service.js`, | ||
| ); | ||
|
|
||
| return SuppressionsService; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {LintResult[]} results results | ||
| * @param {Options} options options | ||
| * @returns {Promise<LintResult[]>} suppressed results | ||
| */ | ||
| async function applySuppressions(results, options) { | ||
| const SuppressionsService = getSuppressionsService(); | ||
| if (!SuppressionsService) { | ||
| return results; | ||
| } | ||
|
|
||
| const { context } = options; | ||
| if (!context) { | ||
| return results; | ||
| } | ||
|
|
||
| const suppressionsLocation = | ||
| options.suppressionsLocation || "eslint-suppressions.json"; | ||
| const filePath = isAbsolute(suppressionsLocation) | ||
| ? suppressionsLocation | ||
| : resolve(context, suppressionsLocation); | ||
|
|
||
| // Only apply suppressions if the file exists | ||
| if (!fs.existsSync(filePath)) { | ||
| return results; | ||
| } | ||
|
|
||
| // cwd must be the directory containing the suppressions file, | ||
| // since paths in the file are relative to that location | ||
| const suppressionsCwd = dirname(filePath); | ||
|
|
||
| const suppressions = new SuppressionsService({ | ||
| filePath, | ||
| cwd: suppressionsCwd, | ||
| }); | ||
|
|
||
| try { | ||
| const suppressionData = await suppressions.load(); | ||
| const { results: filteredResults } = suppressions.applySuppressions( | ||
| results, | ||
| suppressionData, | ||
| ); | ||
| return filteredResults; | ||
| } catch { | ||
| // Return original results if loading/applying suppressions fails | ||
| return results; | ||
| } | ||
| } | ||
|
|
||
| module.exports = applySuppressions; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import './suppressed-error'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // This file has errors that should be suppressed | ||
| var foo = undefinedVariable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| import './suppressed-error'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // This file has errors that should be suppressed | ||
| var foo = undefinedVariable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| require('./suppressed-error'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // This file has errors that should be suppressed | ||
| var foo = undefinedVariable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { existsSync, unlinkSync, writeFileSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import pack from "./utils/pack"; | ||
|
|
||
| const testDir = join(__dirname, "fixtures"); | ||
|
|
||
| describe("suppressions", () => { | ||
| const suppressionsFile = join(testDir, "eslint-suppressions.json"); | ||
|
|
||
| afterEach(() => { | ||
| if (existsSync(suppressionsFile)) { | ||
| unlinkSync(suppressionsFile); | ||
| } | ||
| }); | ||
|
|
||
| it("should report errors when no suppressions file exists", async () => { | ||
| const compiler = pack("suppressed-error", { | ||
| cwd: testDir, | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(true); | ||
| }); | ||
|
|
||
| it("should suppress errors when suppressions file exists", async () => { | ||
| // Create suppressions file that matches the violations in suppressed-error.js | ||
| // The file has: var foo = undefinedVariable | ||
| // Which triggers: no-var (error), no-undef (error), no-unused-vars (error) | ||
| const suppressions = { | ||
| "suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| "no-undef": { count: 1 }, | ||
| "no-unused-vars": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync(suppressionsFile, JSON.stringify(suppressions, null, 2)); | ||
|
|
||
| const compiler = pack("suppressed-error", { | ||
| cwd: testDir, | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(false); | ||
| }); | ||
|
|
||
| it("should support custom suppressionsLocation option", async () => { | ||
| const customSuppressionsFile = join(testDir, "custom-suppressions.json"); | ||
|
|
||
| const suppressions = { | ||
| "suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| "no-undef": { count: 1 }, | ||
| "no-unused-vars": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync( | ||
| customSuppressionsFile, | ||
| JSON.stringify(suppressions, null, 2), | ||
| ); | ||
|
|
||
| try { | ||
| const compiler = pack("suppressed-error", { | ||
| cwd: testDir, | ||
| suppressionsLocation: "custom-suppressions.json", | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(false); | ||
| } finally { | ||
| if (existsSync(customSuppressionsFile)) { | ||
| unlinkSync(customSuppressionsFile); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| it("should still report unsuppressed errors", async () => { | ||
| // Only suppress some of the violations, not suppressing no-undef and | ||
| // no-unused-vars | ||
| const suppressions = { | ||
| "suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync(suppressionsFile, JSON.stringify(suppressions, null, 2)); | ||
|
|
||
| const compiler = pack("suppressed-error", { | ||
| cwd: testDir, | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(true); | ||
| }); | ||
|
|
||
| describe("with context as subdirectory", () => { | ||
| const subdirTestDir = join(testDir, "subdir"); | ||
| const parentSuppressionsFile = join(testDir, "eslint-suppressions.json"); | ||
|
|
||
| afterEach(() => { | ||
| if (existsSync(parentSuppressionsFile)) { | ||
| unlinkSync(parentSuppressionsFile); | ||
| } | ||
| }); | ||
|
|
||
| it("should suppress errors with suppressionsLocation pointing to parent directory", async () => { | ||
| // Suppressions file is at test/fixtures/eslint-suppressions.json | ||
| // Context is test/fixtures/subdir/ | ||
| // suppressionsLocation is ../eslint-suppressions.json | ||
| // | ||
| // Paths in suppressions file are relative to the suppressions file location (test/fixtures/) | ||
| const suppressions = { | ||
| "subdir/suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| "no-undef": { count: 1 }, | ||
| "no-unused-vars": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync( | ||
| parentSuppressionsFile, | ||
| JSON.stringify(suppressions, null, 2), | ||
| ); | ||
|
|
||
| const compiler = pack("subdir/suppressed-error", { | ||
| context: subdirTestDir, | ||
| suppressionsLocation: "../eslint-suppressions.json", | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(false); | ||
| }); | ||
|
|
||
| it("should suppress errors with absolute suppressionsLocation path", async () => { | ||
| const suppressions = { | ||
| "subdir/suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| "no-undef": { count: 1 }, | ||
| "no-unused-vars": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync( | ||
| parentSuppressionsFile, | ||
| JSON.stringify(suppressions, null, 2), | ||
| ); | ||
|
|
||
| const compiler = pack("subdir/suppressed-error", { | ||
| context: subdirTestDir, | ||
| suppressionsLocation: parentSuppressionsFile, // Absolute path | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(false); | ||
| }); | ||
|
|
||
| it("should report errors when suppressionsLocation is relative but file does not exist", async () => { | ||
| const compiler = pack("subdir/suppressed-error", { | ||
| context: subdirTestDir, | ||
| suppressionsLocation: "../nonexistent-suppressions.json", | ||
| }); | ||
|
|
||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasErrors()).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("with context omitted (defaults to webpack context)", () => { | ||
| it("should suppress errors with default suppressionsLocation", async () => { | ||
| const suppressions = { | ||
| "suppressed-error.js": { | ||
| "no-var": { count: 1 }, | ||
| "no-undef": { count: 1 }, | ||
| "no-unused-vars": { count: 1 }, | ||
| }, | ||
| }; | ||
|
|
||
| writeFileSync(suppressionsFile, JSON.stringify(suppressions, null, 2)); | ||
|
|
||
| const compiler = pack("suppressed-error", {}); | ||
| const stats = await compiler.runAsync(); | ||
| expect(stats.hasWarnings()).toBe(false); | ||
| expect(stats.hasErrors()).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to open an issue in eslint repo, ideally they should export or provide API for such things