Skip to content

Commit 794949d

Browse files
authored
Merge pull request swiftlang#76 from nkcsgexi/accessor-as-modifier
API: child syntax should be able to be modified by using accessors
2 parents c725aec + 901bfec commit 794949d

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Sources/SwiftSyntax/SyntaxNodes.swift.gyb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
101101
/// ${line}
102102
% end
103103
public var ${child.swift_name}: ${ret_type} {
104+
get {
104105
let child = data.child(at: Cursor.${child.swift_name}, parent: self)
105106
% if child.is_optional:
106107
if child == nil { return nil }
@@ -115,6 +116,10 @@ public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
115116
return makeSyntax(child!) ${cast}
116117
% end
117118
}
119+
set(value) {
120+
self = with${child.name}(value)
121+
}
122+
}
118123
% if child_node and child_node.is_syntax_collection():
119124
% child_elt = child_node.collection_element_name
120125
% child_elt_type = child_node.collection_element_type

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ XCTMain({ () -> [XCTestCaseEntry] in
1414
testCase(SyntaxAPITestCase.allTests),
1515
testCase(SyntaxVisitorTestCase.allTests),
1616
testCase(TokenSyntaxTestCase.allTests),
17+
testCase(SyntaxTreeModifierTests.allTests),
1718
]
1819
return testCases
1920
}())
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import XCTest
2+
import SwiftSyntax
3+
4+
fileprivate func cannedVarDecl() -> VariableDeclSyntax {
5+
let Pattern = SyntaxFactory.makePatternBinding(
6+
pattern: SyntaxFactory.makeIdentifierPattern(
7+
identifier: SyntaxFactory.makeIdentifier("a").withLeadingTrivia(.spaces(1))),
8+
typeAnnotation: SyntaxFactory.makeTypeAnnotation(
9+
colon: SyntaxFactory.makeColonToken().withTrailingTrivia(.spaces(1)),
10+
type: SyntaxFactory.makeTypeIdentifier("Int")),
11+
initializer: nil, accessor: nil, trailingComma: nil)
12+
return VariableDeclSyntax {
13+
$0.useLetOrVarKeyword(SyntaxFactory.makeLetKeyword())
14+
$0.addPatternBinding(Pattern)
15+
}
16+
}
17+
18+
public class SyntaxTreeModifierTests: XCTestCase {
19+
20+
public static let allTests = [
21+
("testAccessorAsModifier", testAccessorAsModifier)
22+
]
23+
24+
public func testAccessorAsModifier() {
25+
var VD = cannedVarDecl()
26+
XCTAssertEqual("\(VD)", "let a: Int")
27+
VD.letOrVarKeyword = SyntaxFactory.makeVarKeyword().withTrailingTrivia(.spaces(0))
28+
XCTAssertEqual("\(VD)", "var a: Int")
29+
}
30+
}

0 commit comments

Comments
 (0)