Skip to content

Commit 5538362

Browse files
committed
Remove backticks when creating Identifier
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
1 parent cfd0487 commit 5538362

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Sources/SwiftSyntax/Identifier.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Foundation
14+
15+
/// An abstraction for sanitized values on a token.
16+
public struct Identifier {
17+
/// The sanitized `text` of a token.
18+
public let name: String
19+
20+
public init(_ token: TokenSyntax) {
21+
self.name = sanitizing(token.text)
22+
}
23+
}
24+
25+
private func sanitizing(_ name: String) -> String {
26+
guard name.contains("`") else {
27+
return name
28+
}
29+
30+
return name.trimmingCharacters(in: CharacterSet(charactersIn: "`"))
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 IdentifierTests: XCTestCase {
17+
public func testInit() {
18+
let basicToken = TokenSyntax(stringLiteral: "sometoken")
19+
XCTAssertEqual(Identifier(basicToken).name, "sometoken")
20+
21+
let backtickedToken = TokenSyntax(stringLiteral: "`backtickedtoken`")
22+
XCTAssertEqual(Identifier(backtickedToken).name, "backtickedtoken")
23+
}
24+
}

0 commit comments

Comments
 (0)