Skip to content

Commit

Permalink
Use result builder to fix indentation (#5507)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Mar 24, 2024
1 parent e6bb673 commit dfa7f2f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
58 changes: 32 additions & 26 deletions Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

enum AutoApply: MemberMacro {
Expand Down Expand Up @@ -34,41 +35,46 @@ enum AutoApply: MemberMacro {
let elementNames = annotatedVarDecls.compactMap {
$0.0.bindings.first?.pattern.as(IdentifierPatternSyntax.self)?.identifier.text
}
let inlinedOptionsUpdate = elementNames[firstIndexWithoutKey...].map {
"""
do {
try \($0).apply(configuration, ruleID: Parent.identifier)
try $\($0).performAfterParseOperations()
} catch let issue as Issue where issue == Issue.nothingApplied(ruleID: Parent.identifier) {
// Acceptable. Continue.
}
"""
}
let nonInlinedOptionsUpdate = elementNames[..<firstIndexWithoutKey].map {
"""
if $\($0).key.isEmpty {
$\($0).key = "\($0.snakeCased)"
}
try \($0).apply(configuration[$\($0).key], ruleID: Parent.identifier)
try $\($0).performAfterParseOperations()
"""
}
return [
"""
mutating func apply(configuration: Any) throws {
\(raw: inlinedOptionsUpdate.joined())
DeclSyntax(try FunctionDeclSyntax("mutating func apply(configuration: Any) throws") {
let inlinedOptions = elementNames[firstIndexWithoutKey...]
for option in inlinedOptions {
"""
do {
try \(raw: option).apply(configuration, ruleID: Parent.identifier)
try $\(raw: option).performAfterParseOperations()
} catch let issue as Issue where issue == Issue.nothingApplied(ruleID: Parent.identifier) {
// Acceptable. Continue.
}
"""
}
"""
guard let configuration = configuration as? [String: Any] else {
\(raw: inlinedOptionsUpdate.isEmpty
\(raw: inlinedOptions.isEmpty
? "throw Issue.invalidConfiguration(ruleID: Parent.description.identifier)"
: "return")
}
\(raw: nonInlinedOptionsUpdate.joined())
"""
for option in elementNames[..<firstIndexWithoutKey] {
"""
if $\(raw: option).key.isEmpty {
$\(raw: option).key = "\(raw: option.snakeCased)"
}
"""
"""
try \(raw: option).apply(configuration[$\(raw: option).key], ruleID: Parent.identifier)
"""
"""
try $\(raw: option).performAfterParseOperations()
"""
}
"""
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
}
}
"""
"""
})
]
}
}
Expand Down
15 changes: 6 additions & 9 deletions Tests/MacroTests/AutoApplyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ final class AutoApplyTests: XCTestCase {
struct S {
mutating func apply(configuration: Any) throws {
guard let configuration = configuration as? [String: Any] else {
throw Issue.invalidConfiguration(ruleID: Parent.description.identifier)
}
if !supportedKeys.isSuperset(of: configuration.keys) {
let unknownKeys = Set(configuration.keys).subtracting(supportedKeys)
throw Issue.invalidConfigurationKeys(ruleID: Parent.identifier, keys: unknownKeys)
Expand Down Expand Up @@ -73,12 +71,11 @@ final class AutoApplyTests: XCTestCase {
var eB = 2
mutating func apply(configuration: Any) throws {
guard let configuration = configuration as? [String: Any] else {
throw Issue.invalidConfiguration(ruleID: Parent.description.identifier)
}
if $eA.key.isEmpty {
$eA.key = "e_a"
$eA.key = "e_a"
}
try eA.apply(configuration[$eA.key], ruleID: Parent.identifier)
try $eA.performAfterParseOperations()
Expand Down Expand Up @@ -123,16 +120,16 @@ final class AutoApplyTests: XCTestCase {
mutating func apply(configuration: Any) throws {
do {
try eB.apply(configuration, ruleID: Parent.identifier)
try $eB.performAfterParseOperations()
try eB.apply(configuration, ruleID: Parent.identifier)
try $eB.performAfterParseOperations()
} catch let issue as Issue where issue == Issue.nothingApplied(ruleID: Parent.identifier) {
// Acceptable. Continue.
}
// Acceptable. Continue.
}
guard let configuration = configuration as? [String: Any] else {
return
}
if $eA.key.isEmpty {
$eA.key = "e_a"
$eA.key = "e_a"
}
try eA.apply(configuration[$eA.key], ruleID: Parent.identifier)
try $eA.performAfterParseOperations()
Expand Down

0 comments on commit dfa7f2f

Please sign in to comment.