Skip to content

Commit c9e33bd

Browse files
authored
Merge pull request #1494 from ahoppen/ahoppen/5.9/wrong-token-kind
[5.9] Fix cases where syntax nodes were constructed with incorrect token kinds
2 parents c167401 + ed8d9b7 commit c9e33bd

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

Sources/SwiftOperators/SyntaxSynthesis.swift

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@
1212

1313
import SwiftSyntax
1414

15+
fileprivate extension OperatorKind {
16+
var keyword: Keyword {
17+
switch self {
18+
case .infix: return .infix
19+
case .prefix: return .prefix
20+
case .postfix: return .postfix
21+
}
22+
}
23+
}
24+
1525
extension Operator {
1626
/// Synthesize a syntactic representation of this operator based on its
1727
/// semantic definition.
1828
public func synthesizedSyntax() -> OperatorDeclSyntax {
1929
let modifiers = ModifierListSyntax(
20-
[DeclModifierSyntax(name: .identifier("\(kind)"))]
30+
[DeclModifierSyntax(name: .keyword(kind.keyword))]
2131
)
2232
let operatorKeyword = TokenSyntax.keyword(.operator, leadingTrivia: .space)
2333
let identifierSyntax =
24-
TokenSyntax.identifier(name, leadingTrivia: .space)
34+
TokenSyntax.binaryOperator(name, leadingTrivia: .space)
2535
let precedenceGroupSyntax = precedenceGroup.map { groupName in
2636
OperatorPrecedenceAndTypesSyntax(
2737
colon: .colonToken(),
@@ -64,6 +74,16 @@ extension PrecedenceRelation {
6474
}
6575
}
6676

77+
fileprivate extension Associativity {
78+
var keyword: Keyword {
79+
switch self {
80+
case .none: return .none
81+
case .left: return .left
82+
case .right: return .right
83+
}
84+
}
85+
}
86+
6787
extension PrecedenceGroup {
6888
/// Synthesize a syntactic representation of this precedence group based on
6989
/// its semantic definition.
@@ -83,12 +103,12 @@ extension PrecedenceGroup {
83103
.init(
84104
PrecedenceGroupAssociativitySyntax(
85105
associativityKeyword:
86-
.identifier(
87-
"associativity",
106+
.keyword(
107+
.associativity,
88108
leadingTrivia: [.newlines(1), .spaces(indentation)]
89109
),
90110
colon: .colonToken(),
91-
value: .identifier("\(associativity)", leadingTrivia: .space)
111+
value: .keyword(associativity.keyword, leadingTrivia: .space)
92112
)
93113
)
94114
)
@@ -103,8 +123,8 @@ extension PrecedenceGroup {
103123
.init(
104124
PrecedenceGroupAssignmentSyntax(
105125
assignmentKeyword:
106-
.identifier(
107-
"assignment",
126+
.keyword(
127+
.assignment,
108128
leadingTrivia: [.newlines(1), .spaces(indentation)]
109129
),
110130
colon: .colonToken(),

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ extension ExpressibleByLiteralSyntax where Self: FloatingPoint, Self: LosslessSt
265265
case .negativeInfinity, .negativeZero:
266266
return ExprSyntax(
267267
PrefixOperatorExprSyntax(
268-
operatorToken: "-",
268+
operatorToken: .prefixOperator("-"),
269269
postfixExpression: (-self).makeLiteralSyntax()
270270
)
271271
)

Tests/SwiftSyntaxBuilderTest/FloatLiteralTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ final class FloatLiteralTests: XCTestCase {
1919
let testCases: [UInt: (FloatLiteralExprSyntax, String)] = [
2020
#line: (FloatLiteralExprSyntax(floatingDigits: .floatingLiteral(String(123.321))), "123.321"),
2121
#line: (FloatLiteralExprSyntax(floatingDigits: .floatingLiteral(String(-123.321))), "-123.321"),
22-
#line: (FloatLiteralExprSyntax(floatingDigits: "2_123.321"), "2_123.321"),
23-
#line: (FloatLiteralExprSyntax(floatingDigits: "-2_123.321"), "-2_123.321"),
22+
#line: (FloatLiteralExprSyntax(floatingDigits: .floatingLiteral("2_123.321")), "2_123.321"),
23+
#line: (FloatLiteralExprSyntax(floatingDigits: .floatingLiteral("-2_123.321")), "-2_123.321"),
2424
#line: (FloatLiteralExprSyntax(2_123.321), "2123.321"),
2525
#line: (FloatLiteralExprSyntax(-2_123.321), "-2123.321"),
2626
#line: (2_123.321, "2123.321"),

Tests/SwiftSyntaxBuilderTest/IntegerLiteralTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ final class IntegerLiteralTests: XCTestCase {
1919
let testCases: [UInt: (IntegerLiteralExprSyntax, String)] = [
2020
#line: (IntegerLiteralExprSyntax(digits: .integerLiteral(String(123))), "123"),
2121
#line: (IntegerLiteralExprSyntax(digits: .integerLiteral(String(-123))), "-123"),
22-
#line: (IntegerLiteralExprSyntax(digits: "1_000"), "1_000"),
23-
#line: (IntegerLiteralExprSyntax(digits: "-1_000"), "-1_000"),
22+
#line: (IntegerLiteralExprSyntax(digits: .integerLiteral("1_000")), "1_000"),
23+
#line: (IntegerLiteralExprSyntax(digits: .integerLiteral("-1_000")), "-1_000"),
2424
#line: (IntegerLiteralExprSyntax(1_000), "1000"),
2525
#line: (IntegerLiteralExprSyntax(-1_000), "-1000"),
2626
#line: (1_000, "1000"),

Tests/SwiftSyntaxParserTest/SyntaxComparisonTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public class SyntaxComparisonTests: XCTestCase {
4141
}
4242

4343
public func testDifferentTokenKind() throws {
44-
let expected = Syntax(makeFunc(identifier: .identifier("f"), keyword: .keyword(.class)))
44+
let expected = Syntax(makeFunc(identifier: .binaryOperator("f")))
4545

4646
func expectations(_ diff: TreeDifference?, file: StaticString = #filePath, line: UInt = #line) throws {
4747
let diff = try XCTUnwrap(diff, file: file, line: line)
4848
XCTAssertEqual(diff.reason, .token)
49-
XCTAssertEqual(Syntax(diff.baseline).as(TokenSyntax.self)?.tokenKind, .keyword(.class))
50-
XCTAssertEqual(Syntax(diff.node).as(TokenSyntax.self)?.tokenKind, .keyword(.func))
49+
XCTAssertEqual(Syntax(diff.baseline).as(TokenSyntax.self)?.tokenKind, .binaryOperator("f"))
50+
XCTAssertEqual(Syntax(diff.node).as(TokenSyntax.self)?.tokenKind, .identifier("f"))
5151
}
5252

5353
let actual = Syntax(makeFunc(identifier: .identifier("f")))
@@ -98,7 +98,7 @@ public class SyntaxComparisonTests: XCTestCase {
9898
body: CodeBlockSyntax(
9999
leftBrace: .leftBraceToken(presence: .missing),
100100
statements: CodeBlockItemListSyntax([]),
101-
rightBrace: .leftBraceToken(presence: .missing)
101+
rightBrace: .rightBraceToken(presence: .missing)
102102
)
103103
)
104104
)

0 commit comments

Comments
 (0)