diff --git a/Sources/EnumeratorMacroImpl/MacroError.swift b/Sources/EnumeratorMacroImpl/MacroError.swift index 2526813..79c4a89 100644 --- a/Sources/EnumeratorMacroImpl/MacroError.swift +++ b/Sources/EnumeratorMacroImpl/MacroError.swift @@ -79,7 +79,7 @@ enum MacroError: Error, CustomStringConvertible { case let .declaredHere(name): "\(name) declared here:" case .redundantKeyValuesFunctionCall: - "Redundant 'keyValues' function used. The array is already of type '\(EArray.self)'" + "Redundant 'keyValues' function used. The array is already of type '[KeyValue]'" } } } diff --git a/Sources/EnumeratorMacroImpl/Visitors/ExcessiveTriviaRemover.swift b/Sources/EnumeratorMacroImpl/Visitors/ExcessiveTriviaRemover.swift index 040d110..d3a9eaa 100644 --- a/Sources/EnumeratorMacroImpl/Visitors/ExcessiveTriviaRemover.swift +++ b/Sources/EnumeratorMacroImpl/Visitors/ExcessiveTriviaRemover.swift @@ -1,18 +1,21 @@ import SwiftSyntax final class ExcessiveTriviaRemover: SyntaxRewriter { - /// Remove empty lines. + /// Removes empty lines. override func visitAny(_ node: Syntax) -> Syntax? { var node = node let leadingModified, trailingModified: Bool - (leadingModified, node) = self.handleTrivia(node, at: \.leadingTrivia) - (trailingModified, node) = self.handleTrivia(node, at: \.trailingTrivia) + (leadingModified, node) = self.removeEmptyLineTrivia(from: node, at: \.leadingTrivia) + (trailingModified, node) = self.removeEmptyLineTrivia(from: node, at: \.trailingTrivia) let modified = leadingModified || trailingModified + /// Recursively call `rewrite(_:)` if we are not returning `nil`. + /// Because `SyntaxRewriter`'s implementation will skip + /// calling `visitAny(_:)` on the children of the node. return modified ? self.rewrite(node) : nil } - func handleTrivia( - _ node: Syntax, + func removeEmptyLineTrivia( + from node: Syntax, at keyPath: WritableKeyPath ) -> (modified: Bool, syntax: Syntax) { var node = node @@ -29,7 +32,7 @@ final class ExcessiveTriviaRemover: SyntaxRewriter { if previousWasNewLines, idx + 1 < pieces.count, case .newlines = pieces[idx + 1] { - /// Previous and next are both `newlines`. + /// Previous and next are both `newlines`, so remove this `spaces`. toBeRemoved.append(idx) } previousWasNewLines = false diff --git a/Sources/EnumeratorMacroImpl/Visitors/SwitchErrorsRewriter.swift b/Sources/EnumeratorMacroImpl/Visitors/SwitchErrorsRewriter.swift index b14eceb..25a88e2 100644 --- a/Sources/EnumeratorMacroImpl/Visitors/SwitchErrorsRewriter.swift +++ b/Sources/EnumeratorMacroImpl/Visitors/SwitchErrorsRewriter.swift @@ -1,6 +1,26 @@ import SwiftSyntax final class SwitchErrorsRewriter: SyntaxRewriter { + /// Rewrites and removes the trailing comma of the last case which is an error in Swift. + /// For example, it rewrites: + /// ```swift + /// switch self { + /// case + /// .a, + /// .b, + /// : + /// return x + /// ``` + /// to: + /// ```swift + /// switch self { + /// case + /// .a, + /// .b + /// : + /// return x + /// ``` + /// so it removes the `,` after `b` which is the last case. override func visit(_ node: SwitchCaseSyntax) -> SwitchCaseSyntax { guard let label = node.label.as(SwitchCaseLabelSyntax.self) else { return node diff --git a/Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift b/Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift index 3a9ea8f..24acf8b 100644 --- a/Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift +++ b/Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift @@ -579,7 +579,7 @@ final class EnumeratorMacroTests: XCTestCase { domain: "EnumeratorMacro.MacroError", id: "redundantKeyValuesFunctionCall" ), - message: "Redundant 'keyValues' function used. The array is already of type 'EArray'", + message: "Redundant 'keyValues' function used. The array is already of type '[KeyValue]'", line: 1, column: 13, severity: .error