Skip to content

Improvements in FunctionCallExpr convenience initializer #452

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
merged 1 commit into from
May 31, 2022
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 @@ -15,15 +15,15 @@ import SwiftSyntax

extension FunctionCallExpr {
public init(
_ calledExpression: ExpressibleAsIdentifierExpr,
leftParen: TokenSyntax? = nil,
rightParen: TokenSyntax? = nil,
_ calledExpression: ExpressibleAsExprBuildable,
leftParen: TokenSyntax? = .leftParen,
rightParen: TokenSyntax? = .rightParen,
trailingClosure: ExpressibleAsClosureExpr? = nil,
@TupleExprElementListBuilder argumentListBuilder: () -> ExpressibleAsTupleExprElementList = { TupleExprElementList([]) },
@MultipleTrailingClosureElementListBuilder additionalTrailingClosuresBuilder: () -> MultipleTrailingClosureElementList? = { nil }
) {
self.init(
calledExpression: calledExpression.createIdentifierExpr(),
calledExpression: calledExpression.createExprBuildable(),
leftParen: leftParen,
argumentList: argumentListBuilder(),
rightParen: rightParen,
Expand Down
30 changes: 11 additions & 19 deletions Sources/SwiftSyntaxBuilderGeneration/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import SwiftSyntaxBuilder
private let format = Format(indentWidth: 2)

private func createSpacingCall() -> FunctionCallExpr {
FunctionCallExpr(calledExpression: MemberAccessExpr(name: "spaces"), leftParen: .leftParen, argumentList: TupleExprElement(expression: IntegerLiteralExpr(1)), rightParen: .rightParen)
FunctionCallExpr(MemberAccessExpr(name: "spaces"), argumentListBuilder: { TupleExprElement(expression: IntegerLiteralExpr(1)) })
}

private func createWithLeadingTriviaCall() -> FunctionCallExpr {
FunctionCallExpr(calledExpression: MemberAccessExpr(name: "withLeadingTrivia"), leftParen: .leftParen, argumentList: TupleExprElement.init(expression: createSpacingCall()), rightParen: .rightParen)
FunctionCallExpr(MemberAccessExpr(name: "withLeadingTrivia"), argumentListBuilder: { TupleExprElement(expression: createSpacingCall()) })
}

private func createWithTrailingTriviaCall() -> FunctionCallExpr {
FunctionCallExpr(calledExpression: MemberAccessExpr(name: "withTrailingTrivia"), leftParen: .leftParen, argumentList: TupleExprElement(expression: createSpacingCall()), rightParen: .rightParen)
FunctionCallExpr(MemberAccessExpr(name: "withTrailingTrivia"), argumentListBuilder: { TupleExprElement(expression: createSpacingCall()) })
}

private func createTokenSyntaxPatternBinding(_ pattern: ExpressibleAsPatternBuildable, accessor: ExpressibleAsSyntaxBuildable) -> PatternBinding {
Expand Down Expand Up @@ -72,9 +72,7 @@ let tokensFile = SourceFile {
// We need to use `CodeBlock` here to ensure there is braces around.

let accessor = CodeBlock {
FunctionCallExpr(calledExpression: MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Keyword"),
leftParen: .leftParen,
rightParen: .rightParen)
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Keyword"))

if token.requiresLeadingSpace {
createWithLeadingTriviaCall()
Expand All @@ -99,9 +97,7 @@ let tokensFile = SourceFile {
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let accessor = CodeBlock {
FunctionCallExpr(calledExpression: MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Token"),
leftParen: .leftParen,
rightParen: .rightParen)
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)Token"))

if token.requiresLeadingSpace {
createWithLeadingTriviaCall()
Expand All @@ -115,7 +111,7 @@ let tokensFile = SourceFile {
createTokenSyntaxPatternBinding("`\(token.name.withFirstCharacterLowercased)`", accessor: accessor)
})
} else {
let signature = FunctionSignature(input: ParameterClause(leftParen: TokenSyntax.leftParen, parameterList: FunctionParameter(attributes: nil, firstName: .wildcard, secondName: .identifier("text"), colon: .colon, type: "String"), rightParen: .rightParen.withTrailingTrivia(.spaces(1))), output: "TokenSyntax")
let signature = FunctionSignature(input: ParameterClause(parameterList: FunctionParameter(attributes: nil, firstName: .wildcard, secondName: .identifier("text"), colon: .colon, type: "String"), rightParen: .rightParen.withTrailingTrivia(.spaces(1))), output: "TokenSyntax")

FunctionDecl(identifier: .identifier("`\(token.name.withFirstCharacterLowercased)`"),
signature: signature,
Expand All @@ -127,7 +123,7 @@ let tokensFile = SourceFile {
}
},
bodyBuilder: {
FunctionCallExpr(calledExpression: MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)"), leftParen: .leftParen, rightParen: .rightParen, trailingClosure: nil, argumentListBuilder: {
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "make\(token.name)"), argumentListBuilder: {
TupleExprElement(expression: IdentifierExpr("text"))
})

Expand All @@ -146,12 +142,10 @@ let tokensFile = SourceFile {
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr(calledExpression: MemberAccessExpr(base: "SyntaxFactory", name: "makeToken"),
leftParen: .leftParen,
rightParen: .rightParen,
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "makeToken"),
argumentListBuilder: {
TupleExprElement(expression: FunctionCallExpr(calledExpression: MemberAccessExpr(name: "eof")), trailingComma: .comma)
TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: FunctionCallExpr(calledExpression: MemberAccessExpr(name: "present")))
TupleExprElement(expression: MemberAccessExpr(name: "eof"), trailingComma: .comma)
TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: MemberAccessExpr(name: "present"))
})
}

Expand All @@ -162,9 +156,7 @@ let tokensFile = SourceFile {
bindingsBuilder: {
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr(calledExpression: MemberAccessExpr(base: "SyntaxFactory", name: "makeContextualKeyword"),
leftParen: .leftParen,
rightParen: .rightParen,
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "makeContextualKeyword"),
argumentListBuilder: {
TupleExprElement(expression: StringLiteralExpr("open"))
})
Expand Down
4 changes: 1 addition & 3 deletions Tests/SwiftSyntaxBuilderTest/ExtensionDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ final class ExtensionDeclTests: XCTestCase {
let keywords = ["associatedtype", "class"].map { keyword -> VariableDecl in
// We need to use `CodeBlock` here to ensure there is braces around.
let body = CodeBlock {
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword",
leftParen: .leftParen,
rightParen: .rightParen)
FunctionCallExpr("SyntaxFactory.make\(keyword)Keyword")
}

return VariableDecl(letOrVarKeyword: .var,
Expand Down
6 changes: 3 additions & 3 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class FunctionTests: XCTestCase {
]), body: ifCodeBlock)

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

Expand All @@ -36,7 +36,7 @@ final class FunctionTests: XCTestCase {

BinaryOperatorExpr("+")

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

Expand All @@ -56,7 +56,7 @@ final class FunctionTests: XCTestCase {
if n <= 1{
return n
}
return fibonacci(n - 1) + fibonacci(n - 2)
return fibonacci(n - 1) + self.fibonacci(n - 2)
}
""")
}
Expand Down