Skip to content

Commit 2c46bac

Browse files
authored
Merge pull request #1057 from ibrahimoktay/bugfix/remapping-default-to-identifier
Add remapping to identifier when parsing argument label
2 parents 79c49bd + 539d315 commit 2c46bac

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

Sources/SwiftParser/Names.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ extension Parser {
3131
self.missingToken(.identifier, text: nil)
3232
)
3333
} else {
34-
return (nil, self.consumeAnyToken())
34+
if let wildcardToken = self.consume(if: .wildcardKeyword) {
35+
return (nil, wildcardToken)
36+
}
37+
return (nil, self.consumeAnyToken(remapping: .identifier))
3538
}
3639
}
3740
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ final class DeclarationTests: XCTestCase {
7373
)
7474

7575
AssertParse("func /^ (lhs: Int, rhs: Int) -> Int { 1 / 2 }")
76+
77+
AssertParse(
78+
"""
79+
func name(_ default: Int) {}
80+
""",
81+
substructure: Syntax(FunctionParameterSyntax(
82+
attributes: nil,
83+
modifiers: nil,
84+
firstName: .wildcardKeyword(),
85+
secondName: .identifier("default"),
86+
colon: TokenSyntax.colonToken(),
87+
type: TypeSyntax(SimpleTypeIdentifierSyntax(name: TokenSyntax.identifier("Int"), genericArgumentClause: nil)),
88+
ellipsis: nil,
89+
defaultArgument: nil,
90+
trailingComma: nil))
91+
)
7692
}
7793

7894
func testFuncAfterUnbalancedClosingBrace() {

Tests/SwiftParserTest/StatementTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,30 @@ final class StatementTests: XCTestCase {
520520
func testDefaultIdentIdentifierInReturnStmt() {
521521
AssertParse("return FileManager.default")
522522
}
523+
524+
func testDefaultAsIdentifierInSubscript() {
525+
AssertParse(
526+
"""
527+
data[position, default: 0]
528+
""",
529+
substructure: Syntax(ExprSyntax(SubscriptExprSyntax(
530+
calledExpression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("data"), declNameArguments: nil)),
531+
leftBracket: .leftSquareBracketToken(),
532+
argumentList: TupleExprElementListSyntax([
533+
TupleExprElementSyntax(
534+
label: nil,
535+
colon: nil,
536+
expression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("position"), declNameArguments: nil)),
537+
trailingComma: .commaToken()),
538+
TupleExprElementSyntax(
539+
label: .identifier("default"),
540+
colon: .colonToken(),
541+
expression: ExprSyntax(IntegerLiteralExprSyntax(0)),
542+
trailingComma: nil),
543+
]),
544+
rightBracket: .rightSquareBracketToken(),
545+
trailingClosure: nil,
546+
additionalTrailingClosures: nil)))
547+
)
548+
}
523549
}

0 commit comments

Comments
 (0)