Skip to content

Add helper for getting arguments #3046

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -64,6 +64,21 @@ let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
}

// Custom implementations for nodes with special handling.
DeclSyntax(
"""
extension FunctionParameterClauseSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
parse(from: &parser) {
$0.parseParameterClause(RawFunctionParameterClauseSyntax.self) { parser in
parser.parseFunctionParameter()
}
}
}
}
"""
)

try! ExtensionDeclSyntax("fileprivate extension Parser") {
DeclSyntax(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ let syntaxExpressibleByStringInterpolationConformancesFile = SourceFileSyntax(le
"""
)
}

// Due to we cannot parse the `FunctionParameterClauseSyntax` from string interpolation in the normal way
// we need to hand-write the conformance.
DeclSyntax("extension FunctionParameterClauseSyntax: SyntaxExpressibleByStringInterpolation {}")
DeclSyntax(
"""
#if compiler(>=6)
extension FunctionParameterClauseSyntax: Swift.ExpressibleByStringInterpolation {}
#endif
"""
)
}
10 changes: 10 additions & 0 deletions Sources/SwiftParser/generated/LayoutNodes+Parsable.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 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 LabeledExprListSyntax {
public func matchArguments(
against parameters: FunctionParameterClauseSyntax,
isSubscript: Bool = false
) -> MatchedArguments {
var expressions: [[ExprSyntax]] = []

var iterator = makeIterator()

var elements: [ExprSyntax] = []
while let element = iterator.next() {
elements.append(element.expression)

guard element.colon != nil else {
continue
}

expressions.append(elements)
}

var result: [String: [ExprSyntax]] = [:]
for (expressions, parameter) in zip(expressions, parameters.parameters) {
let key = parameter.name

result[key] = expressions
}

return MatchedArguments(matchedArguments: result)
}
}

public struct MatchedArguments {
let matchedArguments: [String: [ExprSyntax]]

public func argument(for internalLabel: String) -> [ExprSyntax]? {
return matchedArguments[internalLabel]
}
}

extension FunctionParameterSyntax {
fileprivate var name: String {
return secondName?.text ?? firstName.text
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 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
import SwiftSyntaxBuilder
import XCTest

final class FunctionParameterClauseSyntaxTests: XCTestCase {
func testFunctionParameterClauseSyntax() {
let builder = FunctionParameterClauseSyntax("(x: Int)")
assertBuildResult(
builder,
"""
(x: Int)
"""
)
}
}
112 changes: 112 additions & 0 deletions Tests/SwiftSyntaxMacroExpansionTest/MatchedArgumentsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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 SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import XCTest
import _SwiftSyntaxTestSupport

final class MatchedArgumentsTests: XCTestCase {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started to add some test cases that you send to me @ahoppen.

func testWithSingleName() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(label: "x", expression: ExprSyntax("2"))
}

let arguments = arg.matchArguments(against: "(x: Int)")

XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
}

func testWithTwoNames() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(
label: "x",
expression: ExprSyntax("2")
)
}

let arguments = arg.matchArguments(against: "(x y: Int)")

XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
XCTAssertNil(arguments.argument(for: "x"))
}

func testWithMultipleArguments() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(
label: "x",
expression: ExprSyntax("2")
)

LabeledExprSyntax(
label: "x",
expression: ExprSyntax("3")
)
}

let arguments = arg.matchArguments(against: "(x y: Int, x z: Int)")

XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
XCTAssertEqual(arguments.argument(for: "z")?.first?.description, "3")
}

func testWithVariadicArguments() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(
label: "x",
expression: ExprSyntax("2")
)

LabeledExprSyntax(
expression: ExprSyntax("3")
)
}

let arguments = arg.matchArguments(against: "(x: Int...)")

XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
XCTAssertEqual(arguments.argument(for: "x")?.last?.description, "3")
}

func testWithSubscript() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(
label: "x",
expression: ExprSyntax("2")
)
}

let arguments = arg.matchArguments(against: "(x: Int)", isSubscript: true)

XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
}

func testWithMultipleArgumentsWithNoLabel() {
let arg = LabeledExprListSyntax {
LabeledExprSyntax(
expression: ExprSyntax("2")
)

LabeledExprSyntax(
expression: ExprSyntax("3")
)
}

let arguments = arg.matchArguments(against: "(_ x: Int, _ y: Int)")

XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
XCTAssertEqual(arguments.argument(for: "z")?.first?.description, "3")
}
}
Loading