Skip to content

Add convenience initializer for BinaryOperatorExpr #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension BinaryOperatorExpr {
public init(_ text: String) {
self.init(operatorToken: TokenSyntax.spacedBinaryOperator(text))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ extension String: ExpressibleAsStringLiteralExpr {
}
}

extension String: ExpressibleAsBinaryOperatorExpr {
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
BinaryOperatorExpr(self)
}
}

/// Default conformance to `ExpressibleByTypeBuildable`
extension String {
public func createTypeBuildable() -> TypeBuildable {
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftSyntaxBuilder/gyb_generated/Buildables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12318,7 +12318,7 @@ extension VersionTuple: ExpressibleAsVersionTuple {
}
}

extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList {
extension TokenSyntax: ExpressibleAsIdentifierList, ExpressibleAsTokenList, ExpressibleAsNonEmptyTokenList, ExpressibleAsBinaryOperatorExpr {
}

// MARK: - Syntax Collection buildable expressible as conformances
Expand Down Expand Up @@ -12583,6 +12583,12 @@ extension ExpressibleAsMemberDeclList {
}
}

extension TokenSyntax {
public func createBinaryOperatorExpr() -> BinaryOperatorExpr {
BinaryOperatorExpr(operatorToken: self)
}
}

extension ExpressibleAsDeclBuildable {
public func createCodeBlockItem() -> CodeBlockItem {
CodeBlockItem(item: self)
Expand Down
25 changes: 25 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/BinaryOperatorExprTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class BinaryOperatorExprTests: XCTestCase {
func testBinaryOperatorExprConvenienceInitializers() {
let leadingTrivia = Trivia.garbageText("␣")
let testCases: [UInt: (ExpressibleAsBinaryOperatorExpr, String)] = [
#line: (BinaryOperatorExpr("=="), "␣ == "),
#line: (TokenSyntax.unspacedBinaryOperator("=="), "␣=="),
#line: ("==", "␣ == "),
]

for (line, testCase) in testCases {
let (builder, expected) = testCase
let binaryOperatorExpr = builder.createBinaryOperatorExpr()
let syntax = binaryOperatorExpr.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

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

XCTAssertEqual(text, expected, line: line)
}
}
}
12 changes: 6 additions & 6 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class FunctionTests: XCTestCase {
IfStmt(conditions: ExprList([
IntegerLiteralExpr(digits: "n"),

BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("<=")),
BinaryOperatorExpr("<="),

IntegerLiteralExpr(1)
]), body: ifCodeBlock)
Expand All @@ -31,19 +31,19 @@ final class FunctionTests: XCTestCase {
TupleExprElement(expression: SequenceExpr(elementsBuilder: {
IntegerLiteralExpr(digits: "n")

BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
BinaryOperatorExpr("-")

IntegerLiteralExpr(1)
}))
})

BinaryOperatorExpr(operatorToken: .spacedBinaryOperator("+"))
BinaryOperatorExpr("+")

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

BinaryOperatorExpr(operatorToken: .unspacedBinaryOperator("-"))
BinaryOperatorExpr("-")

IntegerLiteralExpr(2)
}))
Expand All @@ -56,10 +56,10 @@ final class FunctionTests: XCTestCase {

XCTAssertEqual(syntax.description, """
func fibonacci(_ n: Int)-> Int{
if n<=1{
if n <= 1{
return n
}
return fibonacci(n-1) + fibonacci(n-2)
return fibonacci(n - 1) + fibonacci(n - 2)
}
""")
}
Expand Down