Skip to content

Commit 84358d0

Browse files
committed
Remove withKind from TokenSyntax
`with(\.tokenKind, newValue)` should be used instead.
1 parent ee5fcba commit 84358d0

File tree

8 files changed

+18
-24
lines changed

8 files changed

+18
-24
lines changed

Examples/Sources/AddOneToIntegerLiterals/AddOneToIntegerLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private class AddOneToIntegerLiterals: SyntaxRewriter {
3131
let int = Int(integerText)!
3232

3333
// Create a new integer literal token with `int + 1` as its text.
34-
let newIntegerLiteralToken = token.withKind(.integerLiteral("\(int + 1)"))
34+
let newIntegerLiteralToken = token.with(\.tokenKind, .integerLiteral("\(int + 1)"))
3535

3636
// Return the new integer literal.
3737
return newIntegerLiteralToken

Sources/SwiftParserDiagnostics/LexerDiagnosticMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public extension SwiftSyntax.TokenDiagnostic {
204204
.replacingFirstOccurence(of: "", with: #"""#)
205205
.replacingLastOccurence(of: "", with: #"""#)
206206

207-
let fixedToken = token.withKind(TokenKind.fromRaw(kind: rawKind, text: replacedText))
207+
let fixedToken = token.with(\.tokenKind, TokenKind.fromRaw(kind: rawKind, text: replacedText))
208208
return [
209209
FixIt(message: .replaceCurlyQuoteByNormalQuote, changes: [.replace(oldNode: Syntax(token), newNode: Syntax(fixedToken))])
210210
]

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fileprivate extension TokenSyntax {
2020
var negatedAvailabilityKeyword: TokenSyntax {
2121
switch self.tokenKind {
2222
case .poundAvailableKeyword:
23-
return self.withKind(.poundUnavailableKeyword)
23+
return self.with(\.tokenKind, .poundUnavailableKeyword)
2424
case .poundUnavailableKeyword:
25-
return self.withKind(.poundAvailableKeyword)
25+
return self.with(\.tokenKind, .poundAvailableKeyword)
2626
default:
2727
preconditionFailure("The availability token of an AvailabilityConditionSyntax should always be #available or #unavailable")
2828
}

Sources/SwiftRefactor/AddSeparatorsToIntegerLiteral.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public struct AddSeparatorsToIntegerLiteral: RefactoringProvider {
5151
formattedText += value.byAddingGroupSeparators(at: lit.idealGroupSize)
5252
return
5353
lit
54-
.with(\.digits, lit.digits.withKind(.integerLiteral(formattedText)))
54+
.with(\.digits, lit.digits.with(\.tokenKind, .integerLiteral(formattedText)))
5555
}
5656
}
5757

Sources/SwiftRefactor/FormatRawStringLiteral.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ public struct FormatRawStringLiteral: RefactoringProvider {
4949
guard maximumHashes > 0 else {
5050
return
5151
lit
52-
.with(\.openDelimiter, lit.openDelimiter?.withKind(.rawStringDelimiter("")))
53-
.with(\.closeDelimiter, lit.closeDelimiter?.withKind(.rawStringDelimiter("")))
52+
.with(\.openDelimiter, lit.openDelimiter?.with(\.tokenKind, .rawStringDelimiter("")))
53+
.with(\.closeDelimiter, lit.closeDelimiter?.with(\.tokenKind, .rawStringDelimiter("")))
5454
}
5555

5656
let delimiters = String(repeating: "#", count: maximumHashes + 1)
5757
return
5858
lit
59-
.with(\.openDelimiter, lit.openDelimiter?.withKind(.rawStringDelimiter(delimiters)))
60-
.with(\.closeDelimiter, lit.closeDelimiter?.withKind(.rawStringDelimiter(delimiters)))
59+
.with(\.openDelimiter, lit.openDelimiter?.with(\.tokenKind, .rawStringDelimiter(delimiters)))
60+
.with(\.closeDelimiter, lit.closeDelimiter?.with(\.tokenKind, .rawStringDelimiter(delimiters)))
6161
}
6262
}
6363

Sources/SwiftRefactor/RemoveSeparatorsFromIntegerLiteral.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public struct RemoveSeparatorsFromIntegerLiteral: RefactoringProvider {
3232
return lit
3333
}
3434
let formattedText = lit.digits.text.filter({ $0 != "_" })
35-
return lit.with(\.digits, lit.digits.withKind(.integerLiteral(formattedText)))
35+
return lit.with(\.digits, lit.digits.with(\.tokenKind, .integerLiteral(formattedText)))
3636
}
3737
}

Sources/SwiftSyntax/TokenSyntax.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
6868
return tokenKind.text
6969
}
7070

71-
/// Returns a new TokenSyntax with its kind replaced
72-
/// by the provided token kind.
73-
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
74-
guard raw.kind == .token else {
75-
fatalError("TokenSyntax must have token as its raw")
76-
}
77-
let arena = SyntaxArena()
78-
let newRaw = tokenView.withKind(tokenKind, arena: arena)
79-
let newData = data.replacingSelf(newRaw, arena: arena)
80-
return TokenSyntax(newData)
81-
}
82-
8371
/// The leading trivia (spaces, newlines, etc.) associated with this token.
8472
public var leadingTrivia: Trivia {
8573
get {
@@ -106,7 +94,13 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
10694
return tokenView.formKind()
10795
}
10896
set {
109-
self = withKind(newValue)
97+
guard raw.kind == .token else {
98+
fatalError("TokenSyntax must have token as its raw")
99+
}
100+
let arena = SyntaxArena()
101+
let newRaw = tokenView.withKind(newValue, arena: arena)
102+
let newData = data.replacingSelf(newRaw, arena: arena)
103+
self = TokenSyntax(newData)
110104
}
111105
}
112106

Tests/SwiftSyntaxTest/RawSyntaxTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class RawSyntaxTests: XCTestCase {
142142
XCTAssertEqual(ident.description, "\nfoo ")
143143

144144
let identSyntax = Syntax(raw: ident.raw).as(TokenSyntax.self)!
145-
let barIdentSyntax = identSyntax.withKind(.keyword(.open))
145+
let barIdentSyntax = identSyntax.with(\.tokenKind, .keyword(.open))
146146
let barIdent = barIdentSyntax.raw.as(RawTokenSyntax.self)!
147147

148148
XCTAssertEqual(barIdent.tokenKind, .keyword)

0 commit comments

Comments
 (0)