Skip to content

Commit

Permalink
Remove backticks when creating Identifier
Browse files Browse the repository at this point in the history
Added a new Identifier type which contains a name property

This name property contains a sanitized version of the TokenSyntax's
text property which for now only consists of trimming backticks
  • Loading branch information
adammcarter committed Mar 28, 2024
1 parent cfd0487 commit 33a58e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/SwiftSyntax/Identifier.swift
Original file line number Diff line number Diff line change
@@ -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: "`"))
}
24 changes: 24 additions & 0 deletions Tests/SwiftSyntaxTest/IdentifierTests.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 33a58e6

Please sign in to comment.