Skip to content

Commit

Permalink
Added TokenSyntax identifier property
Browse files Browse the repository at this point in the history
This acts as a convenience property to convert a TokenSyntax to an
Identifier
  • Loading branch information
adammcarter committed Mar 28, 2024
1 parent 33a58e6 commit 581dea4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/Identifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import Foundation

public struct Identifier {
public struct Identifier: Equatable {
public let name: String

public init(_ token: TokenSyntax) {
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/TokenSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
}
}

/// An identifier created from `self`.
public var identifier: Identifier {
Identifier(self)
}

/// A token by itself has no structure, so we represent its structure by an
/// empty layout node.
///
Expand Down
22 changes: 22 additions & 0 deletions Tests/SwiftSyntaxTest/TokenSyntaxTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import XCTest

class TokenSyntaxTests: XCTestCase {

public func testTokenSyntaxIdentifier() {
let tokenSyntax = TokenSyntax(stringLiteral: "sometoken")
XCTAssertEqual(tokenSyntax.identifier, Identifier(tokenSyntax))
}
}

0 comments on commit 581dea4

Please sign in to comment.