Skip to content

Commit 581dea4

Browse files
committed
Added TokenSyntax identifier property
This acts as a convenience property to convert a TokenSyntax to an Identifier
1 parent 33a58e6 commit 581dea4

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

Sources/SwiftSyntax/Identifier.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import Foundation
1414

15-
public struct Identifier {
15+
public struct Identifier: Equatable {
1616
public let name: String
1717

1818
public init(_ token: TokenSyntax) {

Sources/SwiftSyntax/TokenSyntax.swift

+5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
153153
}
154154
}
155155

156+
/// An identifier created from `self`.
157+
public var identifier: Identifier {
158+
Identifier(self)
159+
}
160+
156161
/// A token by itself has no structure, so we represent its structure by an
157162
/// empty layout node.
158163
///
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import XCTest
15+
16+
class TokenSyntaxTests: XCTestCase {
17+
18+
public func testTokenSyntaxIdentifier() {
19+
let tokenSyntax = TokenSyntax(stringLiteral: "sometoken")
20+
XCTAssertEqual(tokenSyntax.identifier, Identifier(tokenSyntax))
21+
}
22+
}

0 commit comments

Comments
 (0)