Skip to content

Commit 2d4f1b6

Browse files
leogdionclaude
andcommitted
Refactor: deprecate HTML escaping for console-only skit tool
- Mark HTML escaping functions as @available(*, unavailable) to preserve compatibility - Update TokenVisitor to output plain text instead of HTML entities - Remove HTML markup generation from trivia processing - Simplify data structures to store raw Swift code text - Clean JSON output now preserves special characters (&, <, >) as-is - Improves skit command-line tool usability for console consumption 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3d37753 commit 2d4f1b6

File tree

7 files changed

+23
-40
lines changed

7 files changed

+23
-40
lines changed

Sources/SyntaxParser/String.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extension String {
5757
private static let whitespace = "whitespace"
5858
private static let newlineClass = "newline"
5959

60+
@available(*, unavailable, message: "HTML escaping is not needed for console output")
6061
internal func escapeHTML() -> String {
6162
var string = self
6263
let specialCharacters = [
@@ -77,6 +78,7 @@ extension String {
7778
return string
7879
}
7980

81+
@available(*, unavailable, message: "HTML formatting is not needed for console output")
8082
internal func replaceInvisiblesWithHTML() -> String {
8183
self
8284
.replacingOccurrences(of: Self.space, with: Self.nonBreakingSpace)
@@ -89,6 +91,7 @@ extension String {
8991
.replacingOccurrences(of: Self.newline, with: Self.newlineSymbol)
9092
}
9193

94+
@available(*, unavailable, message: "HTML formatting is not needed for console output")
9295
internal func replaceHTMLWhitespacesWithSymbols() -> String {
9396
self
9497
.replacingOccurrences(
@@ -103,6 +106,7 @@ extension String {
103106
)
104107
}
105108

109+
@available(*, unavailable, message: "HTML formatting is not needed for console output")
106110
internal func replaceHTMLWhitespacesToSymbols() -> String {
107111
self
108112
.replacingOccurrences(

Sources/SyntaxParser/StructureProperty.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ internal struct StructureProperty: Codable, Equatable {
3535
internal let ref: String?
3636

3737
internal init(name: String, value: StructureValue? = nil, ref: String? = nil) {
38-
self.name = name.escapeHTML()
38+
self.name = name
3939
self.value = value
40-
self.ref = ref?.escapeHTML()
40+
self.ref = ref
4141
}
4242
}

Sources/SyntaxParser/StructureValue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal struct StructureValue: Codable, Equatable {
3434
internal let kind: String?
3535

3636
internal init(text: String, kind: String? = nil) {
37-
self.text = text.escapeHTML().replaceHTMLWhitespacesToSymbols()
38-
self.kind = kind?.escapeHTML()
37+
self.text = text
38+
self.kind = kind
3939
}
4040
}

Sources/SyntaxParser/Token.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,4 @@ internal struct Token: Codable, Equatable {
3333
internal let kind: String
3434
internal var leadingTrivia: String
3535
internal var trailingTrivia: String
36-
37-
internal init(kind: String, leadingTrivia: String, trailingTrivia: String) {
38-
self.kind = kind.escapeHTML()
39-
self.leadingTrivia = leadingTrivia
40-
self.trailingTrivia = trailingTrivia
41-
}
4236
}

Sources/SyntaxParser/TokenVisitor+Helpers.swift

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,32 @@ extension TokenVisitor {
6161
kind = Self.keywordNormalized
6262
}
6363

64-
let sourceRange = token.sourceRange(converter: locationConverter)
65-
let start = sourceRange.start
66-
let end = sourceRange.end
67-
let text =
68-
token.presence == .present || showMissingTokens ? token.text : TokenVisitor.emptyString
64+
// No longer needed for plain text output
65+
_ = token.sourceRange(converter: locationConverter)
66+
_ = token.presence == .present || showMissingTokens ? token.text : TokenVisitor.emptyString
6967
}
7068

7169
internal func processTriviaPiece(_ piece: TriviaPiece) -> String {
72-
func wrapWithSpanTag(class className: String, text: String) -> String {
73-
"\(Self.spanClass)\(className.escapeHTML())' "
74-
+ "\(Self.dataTitle)\("\(piece)".escapeHTML().replaceInvisiblesWithSymbols())' "
75-
+ "\(Self.dataContent)\(className.escapeHTML().replaceInvisiblesWithHTML())' "
76-
+ "\(Self.dataType)\(Self.trivia)'>\(text.escapeHTML().replaceInvisiblesWithHTML())\(Self.spanEnd)"
77-
}
78-
7970
var trivia = TokenVisitor.emptyString
8071
switch piece {
8172
case .spaces(let count):
82-
trivia += String(repeating: Self.nonBreakingSpace, count: count)
73+
trivia += String(repeating: " ", count: count)
8374
case .tabs(let count):
84-
trivia += String(
85-
repeating: Self.nonBreakingSpace,
86-
count: count * Self.whitespaceSpacer)
75+
trivia += String(repeating: "\t", count: count)
8776
case .verticalTabs, .formfeeds:
8877
break
8978
case .newlines(let count), .carriageReturns(let count), .carriageReturnLineFeeds(let count):
90-
trivia += String(repeating: Self.lineBreak, count: count)
79+
trivia += String(repeating: "\n", count: count)
9180
case .lineComment(let text):
92-
trivia += wrapWithSpanTag(class: Self.lineComment, text: text)
81+
trivia += text
9382
case .blockComment(let text):
94-
trivia += wrapWithSpanTag(class: Self.blockComment, text: text)
83+
trivia += text
9584
case .docLineComment(let text):
96-
trivia += wrapWithSpanTag(class: Self.docLineComment, text: text)
85+
trivia += text
9786
case .docBlockComment(let text):
98-
trivia += wrapWithSpanTag(class: Self.docBlockComment, text: text)
87+
trivia += text
9988
case .unexpectedText(let text):
100-
trivia += wrapWithSpanTag(class: Self.unexpectedText, text: text)
89+
trivia += text
10190
case .backslashes(let count):
10291
trivia += String(repeating: Self.backslash, count: count)
10392
case .pounds(let count):

Sources/SyntaxParser/TokenVisitor.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,20 @@ internal final class TokenVisitor: SyntaxRewriter {
183183
}
184184

185185
override internal func visit(_ token: TokenSyntax) -> TokenSyntax {
186-
current.text = token
187-
.text
188-
.escapeHTML()
189-
.replaceInvisiblesWithHTML()
190-
.replaceHTMLWhitespacesWithSymbols()
186+
current.text = token.text
191187

192188
current.token = Token(
193189
kind: "\(token.tokenKind)", leadingTrivia: Self.emptyString,
194190
trailingTrivia: Self.emptyString)
195191

196192
for piece in token.leadingTrivia {
197193
let trivia = processTriviaPiece(piece)
198-
current.token?.leadingTrivia += trivia.replaceHTMLWhitespacesWithSymbols()
194+
current.token?.leadingTrivia += trivia
199195
}
200196
processToken(token)
201197
for piece in token.trailingTrivia {
202198
let trivia = processTriviaPiece(piece)
203-
current.token?.trailingTrivia += trivia.replaceHTMLWhitespacesWithSymbols()
199+
current.token?.trailingTrivia += trivia
204200
}
205201

206202
return token

Sources/SyntaxParser/TreeNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal final class TreeNode: Codable {
4646

4747
internal init(id: Int, text: String, range: SourceRange, type: SyntaxType) {
4848
self.id = id
49-
self.text = text.escapeHTML()
49+
self.text = text
5050
self.range = range
5151
self.type = type
5252
}

0 commit comments

Comments
 (0)