Skip to content

Commit a4889ed

Browse files
committed
Remove withKind from TokenSyntax
`with(\.tokenKind, newValue)` should be used instead.
1 parent 6f9502e commit a4889ed

File tree

6 files changed

+16
-22
lines changed

6 files changed

+16
-22
lines changed

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/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/SyntaxOtherNodes.swift renamed to Sources/SwiftSyntax/TokenSyntax.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
6363
return tokenKind.text
6464
}
6565

66-
/// Returns a new TokenSyntax with its kind replaced
67-
/// by the provided token kind.
68-
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
69-
guard raw.kind == .token else {
70-
fatalError("TokenSyntax must have token as its raw")
71-
}
72-
let arena = SyntaxArena()
73-
let newRaw = tokenView.withKind(tokenKind, arena: arena)
74-
let newData = data.replacingSelf(newRaw, arena: arena)
75-
return TokenSyntax(newData)
76-
}
77-
7866
/// The leading trivia (spaces, newlines, etc.) associated with this token.
7967
public var leadingTrivia: Trivia {
8068
get {
@@ -101,7 +89,13 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
10189
return tokenView.formKind()
10290
}
10391
set {
104-
self = withKind(newValue)
92+
guard raw.kind == .token else {
93+
fatalError("TokenSyntax must have token as its raw")
94+
}
95+
let arena = SyntaxArena()
96+
let newRaw = tokenView.withKind(newValue, arena: arena)
97+
let newData = data.replacingSelf(newRaw, arena: arena)
98+
self = TokenSyntax(newData)
10599
}
106100
}
107101

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)