Skip to content

Commit f61c0ae

Browse files
committed
Add ExtensionDecl test
1 parent 3a97187 commit f61c0ae

14 files changed

+242
-25
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ extension ${node.syntax_kind}: ${expressible_as_type} {
237237

238238
% end
239239
% end
240-
public protocol ExpressibleAsTokenSyntax {
240+
public protocol ExpressibleAsTokenSyntax: ExpressibleAsDeclModifier {
241241
func createTokenSyntax() -> TokenSyntax
242242
}
243243

@@ -281,5 +281,61 @@ extension ${protocol} {
281281
}
282282
}
283283

284+
% end
285+
% if 'ExpressibleAsMemberDeclBlock' in conformances:
286+
extension ${protocol} {
287+
public func createMemberDeclBlock() -> MemberDeclBlock {
288+
MemberDeclBlock(members: self)
289+
}
290+
}
291+
292+
% end
293+
% if 'ExpressibleAsPatternBindingList' in conformances:
294+
extension ${protocol} {
295+
public func createPatternBindingList() -> PatternBindingList {
296+
PatternBindingList([self])
297+
}
298+
}
299+
300+
% end
301+
% if 'ExpressibleAsTypeExpr' in conformances:
302+
extension ${protocol} {
303+
public func createTypeExpr() -> TypeExpr {
304+
TypeExpr(type: self)
305+
}
306+
}
307+
308+
% end
309+
% if 'ExpressibleAsTypeAnnotation' in conformances:
310+
extension ${protocol} {
311+
public func createTypeAnnotation() -> TypeAnnotation {
312+
TypeAnnotation(type: self)
313+
}
314+
}
315+
316+
% end
317+
% if 'ExpressibleAsDeclModifier' in conformances:
318+
extension ${protocol} {
319+
public func createDeclModifier() -> DeclModifier {
320+
DeclModifier(name: self)
321+
}
322+
}
323+
324+
% end
325+
% if 'ExpressibleAsAccessorBlock' in conformances:
326+
extension ${protocol} {
327+
public func createAccessorBlock() -> AccessorBlock {
328+
AccessorBlock(accessors: self)
329+
}
330+
}
331+
332+
% end
333+
% if 'ExpressibleAsAccessorList' in conformances:
334+
extension ${protocol} {
335+
public func createAccessorList() -> AccessorList {
336+
AccessorList([self])
337+
}
338+
}
339+
284340
% end
285341
% end

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension FunctionCallExpr {
1919
leftParen: ExpressibleAsTokenSyntax? = nil,
2020
rightParen: ExpressibleAsTokenSyntax? = nil,
2121
trailingClosure: ExpressibleAsClosureExpr? = nil,
22-
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
22+
@TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList.empty },
2323
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
2424
) {
2525
self.init(

Sources/SwiftSyntaxBuilder/IdentifierPatternConvenienceInitializers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ extension IdentifierPattern: ExpressibleByStringLiteral {
2121
self.init(value)
2222
}
2323
}
24+
25+
extension String: ExpressibleAsIdentifierPattern {
26+
public func createPatternBuildable() -> PatternBuildable {
27+
IdentifierPattern(self)
28+
}
29+
30+
public func createIdentifierPattern() -> IdentifierPattern {
31+
IdentifierPattern(self)
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension ReturnStmt {
16+
public init(
17+
returnKeyword: ExpressibleAsTokenSyntax = TokenSyntax.`return`,
18+
@ExprListBuilder expressionBuilder: () -> ExpressibleAsExprBuildable? = { nil }
19+
) {
20+
self.init(returnKeyword: returnKeyword,
21+
expression: expressionBuilder())
22+
}
23+
}

Sources/SwiftSyntaxBuilder/SimpleTypeIdentifierConvenienceInitializers.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ extension SimpleTypeIdentifier: ExpressibleByStringLiteral {
2424
self.init(value)
2525
}
2626
}
27+
28+
extension String: ExpressibleAsSimpleTypeIdentifier {
29+
public func createSimpleTypeIdentifier() -> SimpleTypeIdentifier {
30+
SimpleTypeIdentifier(self)
31+
}
32+
33+
public func createTypeBuildable() -> TypeBuildable {
34+
SimpleTypeIdentifier(self)
35+
}
36+
}

Sources/SwiftSyntaxBuilder/VariableDeclConvenienceInitializers.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import SwiftSyntax
1414

1515
extension VariableDecl {
1616
public init(_ letOrVarKeyword: TokenSyntax, name: String, type: String) {
17-
self.init(letOrVarKeyword: letOrVarKeyword, bindingsBuilder: {
18-
PatternBinding(pattern: IdentifierPattern(name),
19-
typeAnnotation: TypeAnnotation(type))
20-
})
17+
self.init(letOrVarKeyword: letOrVarKeyword, bindings: PatternBinding(pattern: name,
18+
typeAnnotation: type))
2119
}
2220
}

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,7 +4686,7 @@ public struct MemberDeclList: SyntaxBuildable {
46864686
}
46874687
}
46884688

4689-
public protocol ExpressibleAsMemberDeclList {
4689+
public protocol ExpressibleAsMemberDeclList: ExpressibleAsMemberDeclBlock {
46904690
func createMemberDeclList() -> MemberDeclList
46914691
}
46924692

@@ -5464,7 +5464,7 @@ public struct AccessorDecl: DeclBuildable {
54645464
}
54655465
}
54665466

5467-
public protocol ExpressibleAsAccessorDecl {
5467+
public protocol ExpressibleAsAccessorDecl: ExpressibleAsAccessorList {
54685468
func createAccessorDecl() -> AccessorDecl
54695469
}
54705470

@@ -5502,7 +5502,7 @@ public struct AccessorList: SyntaxBuildable {
55025502
}
55035503
}
55045504

5505-
public protocol ExpressibleAsAccessorList {
5505+
public protocol ExpressibleAsAccessorList: ExpressibleAsAccessorBlock {
55065506
func createAccessorList() -> AccessorList
55075507
}
55085508

@@ -5602,7 +5602,7 @@ public struct PatternBinding: SyntaxBuildable {
56025602
}
56035603
}
56045604

5605-
public protocol ExpressibleAsPatternBinding {
5605+
public protocol ExpressibleAsPatternBinding: ExpressibleAsPatternBindingList {
56065606
func createPatternBinding() -> PatternBinding
56075607
}
56085608

@@ -9548,7 +9548,7 @@ public struct SimpleTypeIdentifier: TypeBuildable {
95489548
}
95499549
}
95509550

9551-
public protocol ExpressibleAsSimpleTypeIdentifier {
9551+
public protocol ExpressibleAsSimpleTypeIdentifier: ExpressibleAsTypeAnnotation, ExpressibleAsTypeBuildable, ExpressibleAsTypeExpr {
95529552
func createSimpleTypeIdentifier() -> SimpleTypeIdentifier
95539553
}
95549554

@@ -10624,7 +10624,7 @@ public struct IdentifierPattern: PatternBuildable {
1062410624
}
1062510625
}
1062610626

10627-
public protocol ExpressibleAsIdentifierPattern {
10627+
public protocol ExpressibleAsIdentifierPattern: ExpressibleAsPatternBuildable {
1062810628
func createIdentifierPattern() -> IdentifierPattern
1062910629
}
1063010630

@@ -11166,7 +11166,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
1116611166
}
1116711167
}
1116811168

11169-
public protocol ExpressibleAsTokenSyntax {
11169+
public protocol ExpressibleAsTokenSyntax: ExpressibleAsDeclModifier {
1117011170
func createTokenSyntax() -> TokenSyntax
1117111171
}
1117211172

@@ -11178,15 +11178,15 @@ extension TokenSyntax: ExpressibleAsTokenSyntax {
1117811178

1117911179
// MARK: - Syntax buildable expressible as conformances
1118011180

11181-
extension ExpressibleAsStmtBuildable {
11182-
public func createCodeBlockItem() -> CodeBlockItem {
11183-
CodeBlockItem(item: self)
11181+
extension ExpressibleAsPatternBinding {
11182+
public func createPatternBindingList() -> PatternBindingList {
11183+
PatternBindingList([self])
1118411184
}
1118511185
}
1118611186

11187-
extension ExpressibleAsExprList {
11188-
public func createConditionElement() -> ConditionElement {
11189-
ConditionElement(condition: self)
11187+
extension ExpressibleAsStmtBuildable {
11188+
public func createCodeBlockItem() -> CodeBlockItem {
11189+
CodeBlockItem(item: self)
1119011190
}
1119111191
}
1119211192

@@ -11202,9 +11202,51 @@ extension ExpressibleAsDeclBuildable {
1120211202
}
1120311203
}
1120411204

11205+
extension ExpressibleAsAccessorDecl {
11206+
public func createAccessorList() -> AccessorList {
11207+
AccessorList([self])
11208+
}
11209+
}
11210+
11211+
extension ExpressibleAsTokenSyntax {
11212+
public func createDeclModifier() -> DeclModifier {
11213+
DeclModifier(name: self)
11214+
}
11215+
}
11216+
11217+
extension ExpressibleAsMemberDeclList {
11218+
public func createMemberDeclBlock() -> MemberDeclBlock {
11219+
MemberDeclBlock(members: self)
11220+
}
11221+
}
11222+
1120511223
extension ExpressibleAsConditionElement {
1120611224
public func createConditionElementList() -> ConditionElementList {
1120711225
ConditionElementList([self])
1120811226
}
1120911227
}
1121011228

11229+
extension ExpressibleAsSimpleTypeIdentifier {
11230+
public func createTypeExpr() -> TypeExpr {
11231+
TypeExpr(type: self)
11232+
}
11233+
}
11234+
11235+
extension ExpressibleAsSimpleTypeIdentifier {
11236+
public func createTypeAnnotation() -> TypeAnnotation {
11237+
TypeAnnotation(type: self)
11238+
}
11239+
}
11240+
11241+
extension ExpressibleAsAccessorList {
11242+
public func createAccessorBlock() -> AccessorBlock {
11243+
AccessorBlock(accessors: self)
11244+
}
11245+
}
11246+
11247+
extension ExpressibleAsExprList {
11248+
public func createConditionElement() -> ConditionElement {
11249+
ConditionElement(condition: self)
11250+
}
11251+
}
11252+

Sources/SwiftSyntaxBuilder/gyb_generated/Tokens.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ public extension ExpressibleAsTokenSyntax where Self == TokenSyntax {
674674

675675
static func `spacedBinaryOperator`(_ text: String) -> TokenSyntax {
676676
SyntaxFactory.makeSpacedBinaryOperator(text)
677+
.withLeadingTrivia(.spaces(1))
678+
.withTrailingTrivia(.spaces(1))
677679
}
678680

679681
static func `postfixOperator`(_ text: String) -> TokenSyntax {

Tests/SwiftSyntaxBuilderTest/ExpressibleBuildablesTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,20 @@ final class ExpressibleBuildablesTests: XCTestCase {
8282
}
8383
""")
8484
}
85+
86+
func testStringAsExpressibleAsSimpleTypeIdentifier() {
87+
let identifier: ExpressibleAsSimpleTypeIdentifier = "Foo"
88+
let buildable = identifier.createSimpleTypeIdentifier()
89+
let syntax = buildable.buildSyntax(format: Format())
90+
91+
XCTAssertEqual(syntax.description, "Foo")
92+
}
93+
94+
func testStringAsExpressibleAsIdentifierPattern() {
95+
let identifier: ExpressibleAsIdentifierPattern = "Foo"
96+
let buildable = identifier.createIdentifierPattern()
97+
let syntax = buildable.buildSyntax(format: Format())
98+
99+
XCTAssertEqual(syntax.description, "Foo")
100+
}
85101
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class ExtensionDeclTests: XCTestCase {
6+
func testExtensionDecl() {
7+
let leadingTrivia = Trivia.garbageText("")
8+
let genericWhereClause = GenericWhereClause(whereKeyword: TokenSyntax.where.withLeadingTrivia(.spaces(1))) {
9+
GenericRequirement(body: ConditionElement(condition: ExprList([
10+
TypeExpr(type: SimpleTypeIdentifier("Self")),
11+
BinaryOperatorExpr(operatorToken: TokenSyntax.spacedBinaryOperator("==")),
12+
TypeExpr(type: SimpleTypeIdentifier("TokenSyntax"))
13+
])))
14+
}
15+
let keywords = ["associatedtype", "class"].map { keyword -> VariableDecl in
16+
let body = CodeBlock(statementsBuilder: {
17+
ReturnStmt(expressionBuilder: {
18+
FunctionCallExpr(IdentifierExpr("SyntaxFactory.make\(keyword)Keyword"),
19+
leftParen: TokenSyntax.leftParen,
20+
rightParen: TokenSyntax.rightParen)
21+
})
22+
})
23+
24+
return VariableDecl(letOrVarKeyword: TokenSyntax.let,
25+
modifiersBuilder: { TokenSyntax.public },
26+
bindingsBuilder: {
27+
PatternBinding(pattern: "`\(keyword)`",
28+
typeAnnotation: "TokenSyntax",
29+
initializer: nil,
30+
accessor: body,
31+
trailingComma: nil)
32+
33+
})
34+
}
35+
let members = MemberDeclList(keywords)
36+
let buildable = ExtensionDecl(modifiers: nil,
37+
extendedType: "ExpressibleAsTokenSyntax",
38+
genericWhereClause: genericWhereClause,
39+
members: members)
40+
41+
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
42+
43+
var text = ""
44+
syntax.write(to: &text)
45+
46+
print(text)
47+
48+
XCTAssertEqual(text, """
49+
␣extension ExpressibleAsTokenSyntax where Self == TokenSyntax{
50+
public let `associatedtype`: TokenSyntax{
51+
return SyntaxFactory.makeAssociatedtypeKeyword()
52+
.withTrailingTrivia(.spaces(1))
53+
}
54+
public let `class`: TokenSyntax{
55+
return n
56+
}
57+
}
58+
""")
59+
}
60+
}

0 commit comments

Comments
 (0)