Skip to content

Commit

Permalink
Merge pull request #137 from PadraigK/main
Browse files Browse the repository at this point in the history
Permit implictly unwrapped optionals when using @SceneTree
  • Loading branch information
migueldeicaza authored Sep 29, 2023
2 parents a5ae598 + afe095b commit 39689af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Sources/SwiftGodotMacroLibrary/SceneTreeMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public struct SceneTreeMacro: AccessorMacro {
return []
}

guard let optional = nodeType.as(OptionalTypeSyntax.self) else {
let unwrappedType = nodeType.as(OptionalTypeSyntax.self)?.wrappedType ?? nodeType.as(ImplicitlyUnwrappedOptionalTypeSyntax.self)?.wrappedType

guard let unwrappedType else {
let newOptional = OptionalTypeSyntax(wrappedType: nodeType)
let addOptionalFix = FixIt(message: MarkOptionalMessage(),
changes: [.replace(oldNode: Syntax(nodeType), newNode: Syntax(newOptional))])
Expand All @@ -90,7 +92,7 @@ public struct SceneTreeMacro: AccessorMacro {

return [
"""
get { getNodeOrNull(path: NodePath(stringLiteral: \(argument))) as? \(optional.wrappedType) }
get { getNodeOrNull(path: NodePath(stringLiteral: \(argument))) as? \(unwrappedType) }
"""
]
}
Expand Down
23 changes: 23 additions & 0 deletions Tests/SwiftGodotMacrosTests/SceneTreeMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ final class SceneTreeMacroTests: XCTestCase {
macros: testMacros
)
}

func testMacroExpansionWithImplicitlyUnwrappedOptional() {
assertMacroExpansion(
"""
class MyNode: Node {
@SceneTree(path: "Entities/CharacterBody2D")
var character: CharacterBody2D!
}
""",
expandedSource: """
class MyNode: Node {
var character: CharacterBody2D! {
get {
getNodeOrNull(path: NodePath(stringLiteral: "Entities/CharacterBody2D")) as? CharacterBody2D
}
}
}
""",
macros: testMacros
)
}

func testMacroMissingPathDiagnostic() {
assertMacroExpansion(
Expand Down Expand Up @@ -88,4 +109,6 @@ final class SceneTreeMacroTests: XCTestCase {
macros: testMacros
)
}


}

0 comments on commit 39689af

Please sign in to comment.