diff --git a/Sources/SwiftSyntax/Identifier.swift b/Sources/SwiftSyntax/Identifier.swift new file mode 100644 index 00000000000..420252abd78 --- /dev/null +++ b/Sources/SwiftSyntax/Identifier.swift @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 Foundation + +public struct Identifier { + public let name: String + + public init(_ token: TokenSyntax) { + self.name = sanitizing(token.text) + } +} + +private func sanitizing(_ name: String) -> String { + guard name.contains("`") else { + return name + } + + return name.trimmingCharacters(in: CharacterSet(charactersIn: "`")) +} diff --git a/Tests/SwiftSyntaxTest/IdentifierTests.swift b/Tests/SwiftSyntaxTest/IdentifierTests.swift new file mode 100644 index 00000000000..293eb22cfeb --- /dev/null +++ b/Tests/SwiftSyntaxTest/IdentifierTests.swift @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// 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 IdentifierTests: XCTestCase { + public func testInit() { + let basicToken = TokenSyntax(stringLiteral: "sometoken") + XCTAssertEqual(Identifier(basicToken).name, "sometoken") + + let backtickedToken = TokenSyntax(stringLiteral: "`backtickedtoken`") + XCTAssertEqual(Identifier(backtickedToken).name, "backtickedtoken") + } +}