Skip to content

Commit 674431e

Browse files
committed
Make String conform to ExpressibleBy protocols
1 parent 22c9714 commit 674431e

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)

0 commit comments

Comments
 (0)