Skip to content

Commit 8f4cb4c

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 8f4cb4c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Sources/SwiftSyntax/Identifier.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
/// An abstraction for sanitized values on a token.
14+
public struct Identifier {
15+
/// The sanitized `text` of a token.
16+
public let name: String
17+
18+
public init(_ token: TokenSyntax) {
19+
let text = token.text
20+
21+
self.name =
22+
if text.contains("`") {
23+
String(text.trimmingCharacters("`"))
24+
} else {
25+
text
26+
}
27+
}
28+
}

Sources/SwiftSyntax/Utils.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,38 @@ extension RawUnexpectedNodesSyntax {
120120
self.init(raw: raw)
121121
}
122122
}
123+
124+
extension String {
125+
func removingCharactersStartingWith(_ charactersToTrim: String) -> Substring {
126+
var startCharactersToDrop = 0
127+
128+
for character in self {
129+
if charactersToTrim.contains(character) {
130+
startCharactersToDrop += 1
131+
} else {
132+
break
133+
}
134+
}
135+
136+
return dropFirst(startCharactersToDrop)
137+
}
138+
139+
func removingCharactersEndingWith(_ charactersToTrim: String) -> Substring {
140+
var endCharactersToDrop = 0
141+
142+
for character in reversed() {
143+
if charactersToTrim.contains(character) {
144+
endCharactersToDrop += 1
145+
} else {
146+
break
147+
}
148+
}
149+
150+
return dropLast(endCharactersToDrop)
151+
}
152+
153+
func trimmingCharacters(_ characters: String) -> Substring {
154+
String(removingCharactersStartingWith(characters))
155+
.removingCharactersEndingWith(characters)
156+
}
157+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
let multiBacktickedToken = TokenSyntax(stringLiteral: "```backtickedtoken```")
25+
XCTAssertEqual(Identifier(multiBacktickedToken).name, "backtickedtoken")
26+
}
27+
}

0 commit comments

Comments
 (0)