Skip to content

[SwiftFixIt] Allow filtering diagnostics by category #8620

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

Merged
merged 1 commit into from
May 6, 2025
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
16 changes: 15 additions & 1 deletion Sources/SwiftFixIt/SwiftFixit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ package struct SwiftFixIt /*: ~Copyable */ {

package init(
diagnosticFiles: [AbsolutePath],
categories: Set<String> = [],
fileSystem: any FileSystem
) throws {
// Deserialize the diagnostics.
Expand All @@ -97,11 +98,16 @@ package struct SwiftFixIt /*: ~Copyable */ {
return try TSCUtility.SerializedDiagnostics(bytes: fileContents).diagnostics
}.lazy.joined()

self = try SwiftFixIt(diagnostics: diagnostics, fileSystem: fileSystem)
self = try SwiftFixIt(
diagnostics: diagnostics,
categories: categories,
fileSystem: fileSystem
)
}

init(
diagnostics: some Collection<some AnyDiagnostic>,
categories: Set<String> = [],
fileSystem: any FileSystem
) throws {
self.fileSystem = fileSystem
Expand Down Expand Up @@ -143,6 +149,14 @@ package struct SwiftFixIt /*: ~Copyable */ {
continue
}

if !categories.isEmpty {
guard let category = convertedDiagnostic.diagMessage.category?.name,
categories.contains(category)
else {
continue
}
}

diagnosticsPerFile[consume sourceFile, default: []].append(consume convertedDiagnostic)
}

Expand Down
98 changes: 93 additions & 5 deletions Tests/SwiftFixItTests/SwiftFixItTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,18 @@ final class SwiftFixItTests: XCTestCase {

private func _testAPI(
_ sourceFilePathsAndEdits: [(AbsolutePath, SourceFileEdit)],
_ diagnostics: [TestDiagnostic]
_ diagnostics: [TestDiagnostic],
_ categories: Set<String>,
) throws {
for (path, edit) in sourceFilePathsAndEdits {
try localFileSystem.writeFileContents(path, string: edit.input)
}

let swiftFixIt = try SwiftFixIt(diagnostics: diagnostics, fileSystem: localFileSystem)
let swiftFixIt = try SwiftFixIt(
diagnostics: diagnostics,
categories: categories,
fileSystem: localFileSystem
)
try swiftFixIt.applyFixIts()

for (path, edit) in sourceFilePathsAndEdits {
Expand All @@ -75,6 +80,7 @@ final class SwiftFixItTests: XCTestCase {

// Cannot use variadic generics: crashes.
private func testAPI1File(
categories: Set<String> = [],
_ getTestCase: (String) -> TestCase<SourceFileEdit>
) throws {
try testWithTemporaryDirectory { fixturePath in
Expand All @@ -84,13 +90,15 @@ final class SwiftFixItTests: XCTestCase {

try self._testAPI(
[(sourceFilePath, testCase.edits)],
testCase.diagnostics
testCase.diagnostics,
categories
)
}
}

private func testAPI2Files(
_ getTestCase: (String, String) -> TestCase<(SourceFileEdit, SourceFileEdit)>
categories: Set<String> = [],
_ getTestCase: (String, String) -> TestCase<(SourceFileEdit, SourceFileEdit)>,
) throws {
try testWithTemporaryDirectory { fixturePath in
let sourceFilePath1 = fixturePath.appending(self.uniqueSwiftFileName())
Expand All @@ -100,7 +108,8 @@ final class SwiftFixItTests: XCTestCase {

try self._testAPI(
[(sourceFilePath1, testCase.edits.0), (sourceFilePath2, testCase.edits.1)],
testCase.diagnostics
testCase.diagnostics,
categories
)
}
}
Expand Down Expand Up @@ -129,6 +138,85 @@ extension SwiftFixItTests {
}
}

func testCategoryFiltering() throws {
// Check that the fix-it gets ignored because category doesn't match
try self.testAPI1File(categories: ["Test"]) { (filename: String) in
.init(
edits: .init(input: "var x = 1", result: "var x = 1"),
diagnostics: [
TestDiagnostic(
text: "error",
level: .error,
location: .init(filename: filename, line: 1, column: 1, offset: 0),
fixIts: [
.init(
start: .init(filename: filename, line: 1, column: 1, offset: 0),
end: .init(filename: filename, line: 1, column: 4, offset: 0),
text: "let"
),
]
),
]
)
}

// Check that the fix-it gets ignored because category doesn't match
try self.testAPI1File(categories: ["Test"]) { (filename: String) in
.init(
edits: .init(input: "var x = 1", result: "var x = 1"),
diagnostics: [
TestDiagnostic(
text: "error",
level: .error,
location: .init(filename: filename, line: 1, column: 1, offset: 0),
category: "Other",
fixIts: [
.init(
start: .init(filename: filename, line: 1, column: 1, offset: 0),
end: .init(filename: filename, line: 1, column: 4, offset: 0),
text: "let"
),
]
),
]
)
}

try self.testAPI1File(categories: ["Other", "Test"]) { (filename: String) in
.init(
edits: .init(input: "var x = 1", result: "let _ = 1"),
diagnostics: [
TestDiagnostic(
text: "error",
level: .error,
location: .init(filename: filename, line: 1, column: 1, offset: 0),
category: "Test",
fixIts: [
.init(
start: .init(filename: filename, line: 1, column: 1, offset: 0),
end: .init(filename: filename, line: 1, column: 4, offset: 0),
text: "let"
),
]
),
TestDiagnostic(
text: "error",
level: .error,
location: .init(filename: filename, line: 1, column: 4, offset: 0),
category: "Other",
fixIts: [
.init(
start: .init(filename: filename, line: 1, column: 5, offset: 0),
end: .init(filename: filename, line: 1, column: 6, offset: 0),
text: "_"
),
]
),
]
)
}
}

func testFixItIgnoredDiagnostic() throws {
try self.testAPI1File { (filename: String) in
.init(
Expand Down