Skip to content

Commit 0c05205

Browse files
committed
Add convenience initializer for àBinaryOperatorExpr`
1 parent f17a5b5 commit 0c05205

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 BinaryOperatorExpr {
16+
public init(_ text: String) {
17+
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
18+
}
19+
}

Sources/SwiftSyntaxBuilder/StringConvenienceInitializers.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ extension String: ExpressibleAsStringLiteralExpr {
3434
}
3535
}
3636

37+
extension String: ExpressibleAsBinaryOperatorExpr {
38+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
39+
BinaryOperatorExpr(self)
40+
}
41+
}
42+
3743
/// Default conformance to `ExpressibleByTypeBuildable`
3844
extension String {
3945
public func createTypeBuildable() -> TypeBuildable {

Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12318,7 +12318,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
1231812318
}
1231912319
}
1232012320

12321-
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList {
12321+
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr {
1232212322
}
1232312323

1232412324
// MARK: - Syntax Collection buildable expressible as conformances
@@ -12583,6 +12583,12 @@ extension ExpressibleAsMemberDeclList {
1258312583
}
1258412584
}
1258512585

12586+
extension TokenSyntax {
12587+
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
12588+
BinaryOperatorExpr(operatorToken: self)
12589+
}
12590+
}
12591+
1258612592
extension ExpressibleAsDeclBuildable {
1258712593
public func createCodeBlockItem() -> CodeBlockItem {
1258812594
CodeBlockItem(item: self)
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 BinaryOperatorExprTests: XCTestCase {
6+
func testBinaryOperatorExprConvenienceInitializers() {
7+
let leadingTrivia = Trivia.garbageText("")
8+
let testCases: [UInt: (ExpressibleAsBinaryOperatorExpr, String)] = [
9+
#line: (BinaryOperatorExpr(stringLiteral: "=="), "␣ == "),
10+
#line: (BinaryOperatorExpr("=="), "␣ == "),
11+
#line: (TokenSyntax.unspacedBinaryOperator("=="), "␣=="),
12+
#line: ("==", "␣ == "),
13+
]
14+
15+
for (line, testCase) in testCases {
16+
let (builder, expected) = testCase
17+
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
18+
let syntax = binaryOperatorExpr.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/FunctionTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class FunctionTests: XCTestCase {
2121
IfStmt(conditions: ExprList([
2222
IntegerLiteralExpr(digits: "n"),
2323

24-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("<=")),
24+
BinaryOperatorExpr("<="),
2525

2626
IntegerLiteralExpr(1)
2727
]), body: ifCodeBlock)
@@ -31,19 +31,19 @@ final class FunctionTests: XCTestCase {
3131
TupleExprElement(expression: SequenceExpr(elementsBuilder: {
3232
IntegerLiteralExpr(digits: "n")
3333

34-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
34+
BinaryOperatorExpr("-")
3535

3636
IntegerLiteralExpr(1)
3737
}))
3838
})
3939

40-
BinaryOperatorExpr(operatorToken: .spacedBinaryOperator("+"))
40+
BinaryOperatorExpr("+")
4141

4242
FunctionCallExpr("fibonacci", leftParen: .leftParen, rightParen: .rightParen, argumentListBuilder: {
4343
TupleExprElement(expression: SequenceExpr(elementsBuilder: {
4444
IntegerLiteralExpr(digits: "n")
4545

46-
BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
46+
BinaryOperatorExpr("-")
4747

4848
IntegerLiteralExpr(2)
4949
}))
@@ -56,10 +56,10 @@ final class FunctionTests: XCTestCase {
5656

5757
XCTAssertEqual(syntax.description, """
5858
func fibonacci(_ n: Int)-> Int{
59-
if n<=1{
59+
if n <= 1{
6060
return n
6161
}
62-
return fibonacci(n-1) + fibonacci(n-2)
62+
return fibonacci(n - 1) + fibonacci(n - 2)
6363
}
6464
""")
6565
}

0 commit comments

Comments
 (0)