Skip to content

Commit e73e11b

Browse files
authored
Merge pull request #335 from kimdv/kimdv/conform-string-to-expressible-as-protocols
Make `String` conform to `ExpressibleAs` protocols
2 parents 22c9714 + 674431e commit e73e11b

15 files changed

+136
-29
lines changed

Sources/SwiftSyntaxBuilder/Buildables.swift.gyb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,20 @@ extension ExpressibleAs${protocol} {
301301
}
302302

303303
% end
304+
% if 'TypeAnnotation' in conformances:
305+
extension ExpressibleAs${protocol} {
306+
public func createTypeAnnotation() -> TypeAnnotation {
307+
TypeAnnotation(type: self)
308+
}
309+
}
310+
311+
% end
312+
% if 'TypeExpr' in conformances:
313+
extension ExpressibleAs${protocol} {
314+
public func createTypeExpr() -> TypeExpr {
315+
TypeExpr(type: self)
316+
}
317+
}
318+
319+
% end
304320
% end

Sources/SwiftSyntaxBuilder/FunctionCallExprConvenienceInitializers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import Foundation
1515

1616
extension FunctionCallExpr {
1717
public init(
18-
_ calledExpression: IdentifierExpr,
18+
_ calledExpression: ExpressibleAsIdentifierExpr,
1919
leftParen: ExpressibleAsTokenSyntax? = nil,
2020
rightParen: ExpressibleAsTokenSyntax? = nil,
2121
trailingClosure: ExpressibleAsClosureExpr? = nil,
2222
@TupleExprElementListBuilder argumentListBuilder: () -> TupleExprElementList = { .empty },
2323
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
2424
) {
2525
self.init(
26-
calledExpression: calledExpression,
26+
calledExpression: calledExpression.createIdentifierExpr(),
2727
leftParen: leftParen,
2828
argumentList: argumentListBuilder(),
2929
rightParen: rightParen,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
extension String: ExpressibleAsSimpleTypeIdentifier {
14+
public func createSimpleTypeIdentifier() -> SimpleTypeIdentifier {
15+
SimpleTypeIdentifier(self)
16+
}
17+
}
18+
19+
extension String: ExpressibleAsIdentifierPattern {
20+
public func createIdentifierPattern() -> IdentifierPattern {
21+
IdentifierPattern(self)
22+
}
23+
}
24+
25+
extension String: ExpressibleAsIdentifierExpr {
26+
public func createIdentifierExpr() -> IdentifierExpr {
27+
IdentifierExpr(self)
28+
}
29+
}
30+
31+
extension String: ExpressibleAsStringLiteralExpr {
32+
public func createStringLiteralExpr() -> StringLiteralExpr {
33+
StringLiteralExpr(self)
34+
}
35+
}
36+
37+
/// Default conformance to `ExpressibleByTypeBuildable`
38+
extension String {
39+
public func createTypeBuildable() -> TypeBuildable {
40+
SimpleTypeIdentifier(self)
41+
}
42+
}
43+
44+
/// Default conformance to `ExpressibleByPatternBuildable`
45+
extension String {
46+
public func createPatternBuildable() -> PatternBuildable {
47+
IdentifierPattern(self)
48+
}
49+
}

Sources/SwiftSyntaxBuilder/VariableDeclConvenienceInitializers.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import SwiftSyntax
1414

1515
extension VariableDecl {
16-
public init(_ letOrVarKeyword: TokenSyntax, name: String, type: String) {
16+
public init(_ letOrVarKeyword: TokenSyntax,
17+
name: ExpressibleAsIdentifierPattern,
18+
type: ExpressibleAsTypeAnnotation) {
1719
self.init(letOrVarKeyword: letOrVarKeyword, bindingsBuilder: {
18-
PatternBinding(pattern: IdentifierPattern(name),
19-
typeAnnotation: TypeAnnotation(type))
20+
PatternBinding(pattern: name.createIdentifierPattern(),
21+
typeAnnotation: type.createTypeAnnotation())
2022
})
2123
}
2224
}

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -11450,3 +11450,15 @@ extension ExpressibleAsExprList {
1145011450
}
1145111451
}
1145211452

11453+
extension ExpressibleAsSimpleTypeIdentifier {
11454+
public func createTypeAnnotation() -> TypeAnnotation {
11455+
TypeAnnotation(type: self)
11456+
}
11457+
}
11458+
11459+
extension ExpressibleAsSimpleTypeIdentifier {
11460+
public func createTypeExpr() -> TypeExpr {
11461+
TypeExpr(type: self)
11462+
}
11463+
}
11464+

Tests/SwiftSyntaxBuilderTest/EnumCaseElementTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class EnumCaseElementTests: XCTestCase {
1515

1616
let initializerClause = InitializerClause(value: stringLiteralExpr)
1717

18-
let enumCase = EnumCaseElement(identifier: SyntaxFactory.makeIdentifier("TestEnum"),
18+
let enumCase = EnumCaseElement(identifier: "TestEnum",
1919
rawValue: initializerClause)
2020

2121
let test = enumCase.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

Tests/SwiftSyntaxBuilderTest/ExpressibleBuildablesTests.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ final class ExpressibleBuildablesTests: XCTestCase {
99

1010
// We use `MemberDeclListItem` to ensure and show we can combine it with `ExpressibleAsMemberDeclListItem`
1111
MemberDeclListItem(decl: VariableDecl(letOrVarKeyword: TokenSyntax.let, bindingsBuilder: {
12-
PatternBinding(pattern: IdentifierPattern(identifier: TokenSyntax.identifier("myOtherLet")),
13-
typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("String")))
12+
PatternBinding(pattern: "myOtherLet", typeAnnotation: "String")
1413
})
1514
)
1615

@@ -48,7 +47,7 @@ final class ExpressibleBuildablesTests: XCTestCase {
4847

4948
func testExpressibleAsSwitchStmt() {
5049
let versions = [("version_1", "1.0.0"), ("version_2", "2.0.0"), ("version_3", "3.0.0"), ("version_3_1", "3.1.0")]
51-
let expression = IdentifierExpr(identifier: SyntaxFactory.makeIdentifier("version"))
50+
let expression = IdentifierExpr("version")
5251

5352
let switchStmt = SwitchStmt(labelName: nil,
5453
expression: expression,

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ final class FunctionTests: XCTestCase {
77
let leadingTrivia = Trivia.garbageText("")
88

99
let input = ParameterClause(parameterListBuilder: {
10-
FunctionParameter(firstName: TokenSyntax.wildcard, secondName: TokenSyntax.identifier("n"), colon: TokenSyntax.colon, type: SimpleTypeIdentifier("Int"), attributesBuilder: {})
10+
FunctionParameter(firstName: TokenSyntax.wildcard, secondName: TokenSyntax.identifier("n"), colon: TokenSyntax.colon, type: "Int", attributesBuilder: {})
1111
})
1212

1313
let ifCodeBlock = CodeBlock(statementsBuilder: {
1414
ReturnStmt(expression: IntegerLiteralExpr(digits: "n"))
1515
})
1616

17-
let signature = FunctionSignature(input: input, output: ReturnClause(returnType: SimpleTypeIdentifier("Int")))
17+
let signature = FunctionSignature(input: input, output: ReturnClause(returnType: "Int"))
1818

1919

2020
let codeBlock = CodeBlock(statementsBuilder: {

Tests/SwiftSyntaxBuilderTest/IdentifierExprTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class IdentifierExprTests: XCTestCase {
66
func testStringLiteral() {
77
let leadingTrivia = Trivia.garbageText("")
88

9-
let testCases: [UInt: (IdentifierExpr, String)] = [
9+
let testCases: [UInt: (ExpressibleAsIdentifierExpr, String)] = [
1010
#line: (IdentifierExpr(identifier: .identifier("Test")), "␣Test"),
1111
#line: (IdentifierExpr(stringLiteral: "Test"), "␣Test"),
1212
#line: (IdentifierExpr("Test"), "␣Test"),
@@ -15,7 +15,8 @@ final class IdentifierExprTests: XCTestCase {
1515

1616
for (line, testCase) in testCases {
1717
let (builder, expected) = testCase
18-
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
18+
let identifierExpr = builder.createIdentifierExpr()
19+
let syntax = identifierExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
1920

2021
var text = ""
2122
syntax.write(to: &text)

Tests/SwiftSyntaxBuilderTest/IdentifierPatternTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class IdentifierPatternTests: XCTestCase {
66
func testStringLiteral() {
77
let leadingTrivia = Trivia.garbageText("")
88

9-
let testCases: [UInt: (IdentifierPattern, String)] = [
9+
let testCases: [UInt: (ExpressibleAsIdentifierPattern, String)] = [
1010
#line: (IdentifierPattern(identifier: .identifier("Test")), "␣Test"),
1111
#line: (IdentifierPattern(stringLiteral: "Test"), "␣Test"),
1212
#line: (IdentifierPattern("Test"), "␣Test"),
@@ -15,7 +15,8 @@ final class IdentifierPatternTests: XCTestCase {
1515

1616
for (line, testCase) in testCases {
1717
let (builder, expected) = testCase
18-
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
18+
let identifierPattern = builder.createIdentifierPattern()
19+
let syntax = identifierPattern.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
1920

2021
var text = ""
2122
syntax.write(to: &text)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class SimpleTypeIdentifierTests: XCTestCase {
6+
func testExpressibleAsSimpleTypeIdentifier() {
7+
let leadingTrivia = Trivia.garbageText("")
8+
9+
let testCases: [UInt: (ExpressibleAsSimpleTypeIdentifier, String)] = [
10+
#line: (SimpleTypeIdentifier(name: SyntaxFactory.makeIdentifier("Foo")), "␣Foo"),
11+
#line: (SimpleTypeIdentifier("Foo"), "␣Foo"),
12+
#line: ("Foo", "␣Foo")
13+
]
14+
15+
for (line, testCase) in testCases {
16+
let (builder, expected) = testCase
17+
let identifier = builder.createSimpleTypeIdentifier()
18+
let syntax = identifier.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
19+
20+
var text = ""
21+
syntax.write(to: &text)
22+
23+
XCTAssertEqual(text, expected, line: line)
24+
}
25+
}
26+
}

Tests/SwiftSyntaxBuilderTest/StringLiteralTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class StringLiteralTests: XCTestCase {
2828

2929
func testStringLiteralConvenienceInitializers() {
3030
let leadingTrivia = Trivia.garbageText("")
31-
let testCases: [UInt: (StringLiteralExpr, String)] = [
31+
let testCases: [UInt: (ExpressibleAsStringLiteralExpr, String)] = [
3232
#line: (StringLiteralExpr(""), #"␣"""#),
3333
#line: (StringLiteralExpr("asdf"), #"␣"asdf""#),
3434
#line: ("", #"␣"""#),
@@ -37,8 +37,8 @@ final class StringLiteralTests: XCTestCase {
3737

3838
for (line, testCase) in testCases {
3939
let (builder, expected) = testCase
40-
41-
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
40+
let stringLiteralExpr = builder.createStringLiteralExpr()
41+
let syntax = stringLiteralExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
4242

4343
var text = ""
4444
syntax.write(to: &text)

Tests/SwiftSyntaxBuilderTest/StructTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ final class StructTests: XCTestCase {
4949
if i.isMultiple(of: 2) {
5050
MemberDeclListItem(decl: VariableDecl(letOrVarKeyword: TokenSyntax.let, bindingsBuilder: {
5151
PatternBinding(
52-
pattern: IdentifierPattern(identifier: TokenSyntax.identifier("var\(i)")),
53-
typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("String"))
52+
pattern: IdentifierPattern("var\(i)"),
53+
typeAnnotation: "String"
5454
)
5555
}))
5656
}

Tests/SwiftSyntaxBuilderTest/TypeAnnotationTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ final class TypeAnnotationTests: XCTestCase {
66
func testStringLiteral() {
77
let leadingTrivia = Trivia.garbageText("")
88

9-
let testCases: [UInt: (TypeAnnotation, String)] = [
9+
let testCases: [UInt: (ExpressibleAsTypeAnnotation, String)] = [
10+
#line: (TypeAnnotation(type: "Test"), "␣: Test"),
1011
#line: (TypeAnnotation(type: SimpleTypeIdentifier("Test")), "␣: Test"),
1112
#line: (TypeAnnotation(stringLiteral: "Test"), "␣: Test"),
1213
#line: (TypeAnnotation("Test"), "␣: Test"),
14+
#line: (SimpleTypeIdentifier("Test"), "␣: Test"),
1315
#line: ("Test", "␣: Test")
1416
]
1517

1618
for (line, testCase) in testCases {
1719
let (builder, expected) = testCase
18-
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
20+
let typeAnnotation = builder.createTypeAnnotation()
21+
let syntax = typeAnnotation.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
1922

2023
var text = ""
2124
syntax.write(to: &text)

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ final class VariableTests: XCTestCase {
77
let leadingTrivia = Trivia.garbageText("")
88

99
let buildable = VariableDecl(letOrVarKeyword: .let, bindingsBuilder: {
10-
PatternBinding(pattern: IdentifierPattern(identifier: SyntaxFactory.makeIdentifier("color")),
11-
typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("UIColor")))
10+
PatternBinding(pattern: "color", typeAnnotation: "UIColor")
1211
})
1312

1413
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)
@@ -25,9 +24,8 @@ final class VariableTests: XCTestCase {
2524
let leadingTrivia = Trivia.garbageText("")
2625

2726
let buildable = VariableDecl(letOrVarKeyword: TokenSyntax.var, bindingsBuilder: {
28-
PatternBinding(pattern: IdentifierPattern(identifier: SyntaxFactory.makeIdentifier("number")),
29-
typeAnnotation: TypeAnnotation(type: SimpleTypeIdentifier("Int")),
30-
initializer: InitializerClause(value: IntegerLiteralExpr(digits: TokenSyntax.integerLiteral("123"))))
27+
PatternBinding(pattern: "number", typeAnnotation: "Int",
28+
initializer: InitializerClause(value: IntegerLiteralExpr(digits: "123")))
3129
})
3230

3331
let syntax = buildable.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

0 commit comments

Comments
 (0)