Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid duplicated variable under if-macro syntax #279

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Models/IfMacroModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
final class IfMacroModel: Model {
let name: String
let offset: Int64
let entities: [Model]
let entities: [(String, Model)]

var modelType: ModelType {
return .macro
}

var fullName: String {
return entities.map {$0.fullName}.joined(separator: "_")
return entities.map {$0.0}.joined(separator: "_")
}

init(name: String,
offset: Int64,
entities: [Model]) {
entities: [(String, Model)]) {
self.name = name
self.entities = entities
self.offset = offset
Expand Down
10 changes: 8 additions & 2 deletions Sources/MockoloFramework/Parsers/SwiftSyntaxExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ extension IfConfigDeclSyntax {
}
}
}

let macroModel = IfMacroModel(name: name, offset: self.offset, entities: subModels)

let uniqueSubModels = uniqueEntities(
in: subModels,
exclude: [:],
fullnames: []
).map({ $0 })

let macroModel = IfMacroModel(name: name, offset: self.offset, entities: uniqueSubModels)
return (macroModel, attrDesc, hasInit)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Templates/IfMacroTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ extension IfMacroModel {
func applyMacroTemplate(name: String,
context: RenderContext,
arguments: GenerationArguments,
entities: [Model]) -> String {
entities: [(String, Model)]) -> String {
let rendered = entities
.compactMap { model in
model.render(
model.1.render(
context: .init(
overloadingResolvedName: model.name, // FIXME: the name is not resolving overload
overloadingResolvedName: model.0,
enclosingType: context.enclosingType,
annotatedTypeKind: context.annotatedTypeKind
),
Expand Down
15 changes: 11 additions & 4 deletions Tests/TestMacros/FixtureMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,27 @@ protocol PresentableListener: AnyObject {
"""

let macroInFuncWithOverloadMock = """



class PresentableListenerMock: PresentableListener {
init() { }

#if DEBUG

private(set) var runCallCount = 0
var runHandler: ((Int) -> ())?
func run(value: Int) {
var runHandler: ((Int) -> ())?
func run(value: Int) {
runCallCount += 1
if let runHandler = runHandler {
runHandler(value)
}

}

private(set) var runValueCallCount = 0
var runValueHandler: ((String) -> ())?
func run(value: String) {
var runValueHandler: ((String) -> ())?
func run(value: String) {
runValueCallCount += 1
if let runValueHandler = runValueHandler {
runValueHandler(value)
Expand All @@ -305,4 +311,5 @@ class PresentableListenerMock: PresentableListener {
}
#endif
}

"""
10 changes: 2 additions & 8 deletions Tests/TestMacros/MacroTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import XCTest

final class MacroTests: MockoloTestCase {
func testMacroInFunc() {
verify(srcContent: macroInFunc,
dstContent: macroInFuncMock)
}

#if os(macOS)
func testMacroInFuncWithOverload() {
XCTExpectFailure("Resolving overloading in #if is broken.") {
verify(srcContent: macroInFuncWithOverload,
dstContent: macroInFuncWithOverloadMock)
}
verify(srcContent: macroInFuncWithOverload,
dstContent: macroInFuncWithOverloadMock)
}
#endif

func testMacroImports() {
verify(srcContent: macroImports,
Expand Down