Skip to content

Commit f5f167a

Browse files
committed
Add literalEscaped utility function
1 parent 217363d commit f5f167a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Sources/SwiftSyntaxBuilderGeneration/SyntaxBuildableChild.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ extension Child {
100100
assertChoices.append(SequenceExpr {
101101
MemberAccessExpr(base: type.forceUnwrapped(expr: varName), name: "text")
102102
BinaryOperatorExpr("==")
103-
StringLiteralExpr(textChoice.replacingOccurrences(of: "\\", with: "\\\\"))
103+
StringLiteralExpr(literalEscaped(string: textChoice))
104104
})
105105
}
106106
let disjunction = ExprList(assertChoices.flatMap { [$0, BinaryOperatorExpr("||")] }.dropLast())

Sources/SwiftSyntaxBuilderGeneration/Utils.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ func flattened(indentedDocumentation: String) -> String {
7070
.replacingOccurrences(of: "\n", with: "")
7171
.trimmingCharacters(in: .whitespacesAndNewlines)
7272
}
73+
74+
/// Escapes a string for use in a string literal.
75+
func literalEscaped(string: String) -> String {
76+
string
77+
.replacingOccurrences(of: "\\", with: "\\\\")
78+
.replacingOccurrences(of: "\"", with: "\\\"")
79+
}

0 commit comments

Comments
 (0)