Skip to content

Commit 4b46c2e

Browse files
committed
Shorten geters and setters of syntax nodes
This makes the `Syntax*Nodes.swift` more succinct and easier to read
1 parent a63faeb commit 4b46c2e

File tree

7 files changed

+3258
-10947
lines changed

7 files changed

+3258
-10947
lines changed

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,14 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
187187
% end
188188
public var ${child.swift_name}: ${child_type}${optional_mark} {
189189
get {
190-
let childData = data.child(at: ${idx}, parent: Syntax(self))
191190
% if child.is_optional:
192-
if childData == nil { return nil }
191+
return data.child(at: ${idx}, parent: Syntax(self)).map(${child_type}.init)
192+
% else:
193+
return ${child_type}(data.child(at: ${idx}, parent: Syntax(self))!)
193194
% end
194-
return ${child_type}(childData!)
195195
}
196196
set(value) {
197-
let arena = SyntaxArena()
198-
% if child.is_optional:
199-
let raw = value?.raw
200-
% else:
201-
let raw = value.raw
202-
% end
203-
let newData = data.replacingChild(at: ${idx}, with: raw, arena: arena)
204-
self = ${node.name}(newData)
197+
self = ${node.name}(data.replacingChild(at: ${idx}, with: value${"?" if child.is_optional else ""}.raw, arena: SyntaxArena()))
205198
}
206199
}
207200
%

0 commit comments

Comments
 (0)