Skip to content

Add convenience initializers to VariableDecl #308

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
Expand Up @@ -10,8 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension BooleanLiteralExpr {
public init(_ value: Bool) {
self.init(booleanLiteral: value ? .true : .false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension FloatLiteralExpr {
public init(_ value: Float) {
self.init(floatingDigits: String(value))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

extension IdentifierPattern {
init(_ identifier: String) {
self.init(identifier: .identifier(identifier))
}
}

extension IdentifierPattern: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ extension SimpleTypeIdentifier {
genericArgumentClause: nil)
}
}

extension SimpleTypeIdentifier: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

extension TypeAnnotation {
public init(_ type: String) {
self.init(type: SimpleTypeIdentifier(type))
}
}

extension TypeAnnotation: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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 VariableDecl {
public init(_ letOrVarKeyword: TokenSyntax, name: String, type: String) {
self.init(letOrVarKeyword: letOrVarKeyword, bindingsBuilder: {
PatternBinding(pattern: IdentifierPattern(name),
typeAnnotation: TypeAnnotation(type))
})
}
}
17 changes: 17 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,21 @@ final class VariableTests: XCTestCase {
␣var number: Int = 123
""")
}

func testConvenienceInitializer() {
let leadingTrivia = Trivia.garbageText("")

let testCases: [UInt: (TokenSyntax, String, String, String)] = [
#line: (TokenSyntax.let, "foo", "Int", "␣let foo: Int"),
#line: (TokenSyntax.var, "bar", "Baz", "␣var bar: Baz")
]

for (line, testCase) in testCases {
let (keyword, name, type, expected) = testCase
let builder = VariableDecl(keyword, name: name, type: type)
let syntax = builder.buildSyntax(format: Format(), leadingTrivia: leadingTrivia)

XCTAssertEqual(syntax.description, expected, line: line)
}
}
}