Skip to content

Commit

Permalink
minor refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 29, 2024
1 parent d7c6fdf commit ef945f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/EnumeratorMacroImpl/MacroError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<EKeyValue>.self)'"
"Redundant 'keyValues' function used. The array is already of type '[KeyValue]'"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Syntax, Trivia>
) -> (modified: Bool, syntax: Syntax) {
var node = node
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions Sources/EnumeratorMacroImpl/Visitors/SwitchErrorsRewriter.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<EKeyValue>'",
message: "Redundant 'keyValues' function used. The array is already of type '[KeyValue]'",
line: 1,
column: 13,
severity: .error
Expand Down

0 comments on commit ef945f2

Please sign in to comment.