Skip to content

Commit

Permalink
rename Rewriter to PostProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 17, 2024
1 parent 1b86e4b commit 6661e4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ extension EnumeratorMacroType: MemberMacro {
}
let postProcessedSyntaxes = syntaxes.compactMap {
(syntax, codeSyntax) -> DeclSyntax? in
let rewriter = Rewriter()
let newSyntax = rewriter.rewrite(syntax)
let postProcessor = PostProcessor()
let newSyntax = postProcessor.rewrite(syntax)
guard let declSyntax = DeclSyntax(newSyntax) else {
context.diagnose(
Diagnostic(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftSyntax

final class Rewriter: SyntaxRewriter {
final class PostProcessor: SyntaxRewriter {
override func visit(_ node: SwitchCaseSyntax) -> SwitchCaseSyntax {
self.removeUnusedLet(
self.removeUnusedArguments(
Expand Down Expand Up @@ -45,34 +45,42 @@ final class Rewriter: SyntaxRewriter {
let identifier = patternExpr.pattern.as(IdentifierPatternSyntax.self) else {
continue
}
/// The arg is already a wildcard so no need to modify
if identifier.identifier.tokenKind == .wildcard {
allArgsAreWildcards = allArgsAreWildcards && true
/// Only try to do something if the `tokenKind` is `.identifier`.
guard case .identifier = identifier.identifier.tokenKind else {
let isWildCard = identifier.identifier.tokenKind == .wildcard
allArgsAreWildcards = allArgsAreWildcards && isWildCard
continue
} else {
let presenceDetector = PresenceDetector(toDetect: identifier.identifier.tokenKind)
presenceDetector.walk(node)
if presenceDetector.detectCount < 2 {
didModify = true
allArgsAreWildcards = allArgsAreWildcards && true
let idx = arguments.index(at: idx)
arguments[idx] = arguments[idx].with(
\.expression,
ExprSyntax(
patternExpr.with(
\.pattern,
PatternSyntax(
identifier.with(
\.identifier,
.wildcardToken()
)
)
)
)
)
} else {
allArgsAreWildcards = false
}
}

let presenceDetector = PresenceDetector(
toDetect: identifier.identifier.tokenKind
)
presenceDetector.walk(node)
guard presenceDetector.detectCount < 2 else {
allArgsAreWildcards = false
continue
}

if presenceDetector.detectCount < 2 {
/// Doesn't logically do anything, so commented out.
/* allArgsAreWildcards = allArgsAreWildcards && true */

didModify = true
let idx = arguments.index(at: idx)
arguments[idx] = arguments[idx].with(
\.expression,
ExprSyntax(
patternExpr.with(
\.pattern,
PatternSyntax(
identifier.with(
\.identifier,
.wildcardToken()
)
)
)
)
)
}
}

Expand Down

0 comments on commit 6661e4e

Please sign in to comment.