Skip to content
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
36 changes: 36 additions & 0 deletions Sources/SwiftSyntax/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@
//
//===----------------------------------------------------------------------===//

extension EnumCaseParameterSyntax {

/// Creates an `EnumCaseParameterSyntax` with a `firstName`, and automatically adds a `colon` to it.
///
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
///
public init(
leadingTrivia: Trivia? = nil,
modifiers: DeclModifierListSyntax = [],
firstName: TokenSyntax,
secondName: TokenSyntax? = nil,
colon: TokenSyntax = TokenSyntax.colonToken(),
type: some TypeSyntaxProtocol,
defaultValue: InitializerClauseSyntax? = nil,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
modifiers: modifiers,
firstName: firstName as TokenSyntax?,
secondName: secondName,
colon: colon,
type: type,
defaultValue: defaultValue,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}

extension MemberAccessExprSyntax {
/// Creates a new ``MemberAccessExprSyntax`` where the accessed member is represented by
/// an identifier without specifying argument labels.
Expand Down Expand Up @@ -45,3 +76,8 @@ extension MemberAccessExprSyntax {
)
}
}

//==========================================================================//
// IMPORTANT: If you are tempted to add an extension here, please insert //
// it in alphabetical order above //
//==========================================================================//
8 changes: 8 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,12 @@ public class SyntaxTests: XCTestCase {
XCTAssertEqual(funcKW.endPosition, AbsolutePosition(utf8Offset: 7))
XCTAssertEqual(funcKW.trimmedLength, SourceLength(utf8Length: 4))
}

public func testEnumCaseParameterSyntaxConvenienceInit() {
let noFirstName = EnumCaseParameterSyntax(type: TypeSyntax("MyType"))
XCTAssertEqual(noFirstName.formatted().description, "MyType")

let node = EnumCaseParameterSyntax(firstName: "label", type: TypeSyntax("MyType"))
XCTAssertEqual(node.formatted().description, "label: MyType")
}
}