Skip to content

Commit

Permalink
Restrict FixItApplier to Swift 6 and above (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM authored Jul 23, 2024
1 parent a7fe5b6 commit e112ec5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/// FixItApplier not available in older versions of SwiftSyntax.
#if compiler(>=6.0)
@_spi(FixItApplier) import SwiftIDEUtils
#endif
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacros
Expand Down Expand Up @@ -116,6 +119,7 @@ extension EnumeratorMacroType: MemberMacro {
var statement = statement
var diagnostics = ParseDiagnosticsGenerator.diagnostics(for: statement)

#if compiler(>=6.0)
/// Returns if anything changed at all.
func tryApplyFixIts() -> Bool {
guard diagnostics.contains(where: { !$0.fixIts.isEmpty }) else {
Expand Down Expand Up @@ -143,6 +147,7 @@ extension EnumeratorMacroType: MemberMacro {
return true
}
}
#endif

/// Returns if anything changed at all.
func tryManuallyFixErrors() -> Bool {
Expand All @@ -165,9 +170,11 @@ extension EnumeratorMacroType: MemberMacro {
}
}

#if compiler(>=6.0)
if tryApplyFixIts() {
diagnostics = ParseDiagnosticsGenerator.diagnostics(for: statement)
}
#endif

if diagnostics.containsError, tryManuallyFixErrors() {
diagnostics = ParseDiagnosticsGenerator.diagnostics(for: statement)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftSyntax

#if compiler(>=6.0)
final class PlaceholderDetector: SyntaxVisitor {
var containedPlaceholder = false

Expand All @@ -15,3 +16,4 @@ final class PlaceholderDetector: SyntaxVisitor {
return .visitChildren
}
}
#endif
5 changes: 4 additions & 1 deletion Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,9 @@ final class EnumeratorMacroTests: XCTestCase {
)
}

func testAppliesFixIts() {
/// FixItApplier not available in older versions of SwiftSyntax.
#if compiler(>=6.0)
func testAppliesFixIts() throws {
let unterminatedString = """
let unterminated = "This is unterminated
"""
Expand All @@ -1177,6 +1179,7 @@ final class EnumeratorMacroTests: XCTestCase {
macros: EnumeratorMacroEntryPoint.macros
)
}
#endif
}

@Enumerator("""
Expand Down

0 comments on commit e112ec5

Please sign in to comment.