Skip to content

Commit f720b72

Browse files
authored
Merge pull request #2468 from RoBo-Inc/fix_escaped_newline_symbol_bug
Fix "escaped newline symbol" bug in `CodeGeneration` module
2 parents 865e3ba + 1617bcb commit f720b72

File tree

7 files changed

+14
-45
lines changed

7 files changed

+14
-45
lines changed

CodeGeneration/Sources/SyntaxSupport/Utils.swift

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,19 @@ public func lowercaseFirstWord(name: String) -> String {
4242
extension SwiftSyntax.Trivia {
4343
/// Make a new trivia from a (possibly multi-line) string, prepending `///`
4444
/// to each line and creating a `.docLineComment` trivia piece for each line.
45-
public static func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
46-
guard let string else {
47-
return []
48-
}
49-
50-
let lines = string.split(separator: "\n", omittingEmptySubsequences: false)
51-
let pieces = lines.enumerated().map { (index, line) in
52-
var line = line
53-
if index != lines.count - 1 {
54-
line = "\(line)\n"
55-
}
56-
return SwiftSyntax.TriviaPiece.docLineComment("/// \(line)")
57-
}
58-
return SwiftSyntax.Trivia(pieces: pieces)
45+
public static func docCommentTrivia(from string: String?) -> Self {
46+
guard let string else { return [] }
47+
let lines = string.split(separator: "\n", omittingEmptySubsequences: false).map { "/// \($0)" }
48+
return .init(pieces: lines.flatMap { [.docLineComment($0), .newlines(1)] })
5949
}
6050

6151
/// Make a new trivia by joining together ``SwiftSyntax/TriviaPiece``s from `joining`,
6252
/// and gluing them together with pieces from `separator`.
6353
public init(
6454
joining items: [SwiftSyntax.Trivia],
65-
separator: SwiftSyntax.Trivia = SwiftSyntax.Trivia(pieces: [TriviaPiece.newlines(1), TriviaPiece.docLineComment("///"), TriviaPiece.newlines(1)])
55+
separator: SwiftSyntax.Trivia = .init(pieces: [.docLineComment("///"), .newlines(1)])
6656
) {
67-
68-
self.init(
69-
pieces:
70-
items
71-
.filter { !$0.isEmpty }
72-
.joined(separator: separator)
73-
)
57+
self.init(pieces: items.filter { !$0.isEmpty }.joined(separator: separator))
7458
}
7559
}
7660

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxBaseNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
173173

174174
try! StructDeclSyntax(
175175
"""
176-
\(documentation)
176+
\(documentation)\
177177
\(node.apiAttributes())\
178178
public struct \(node.kind.syntaxType): \(node.kind.protocolType), SyntaxHashable
179179
"""

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2626

2727
try! StructDeclSyntax(
2828
"""
29-
\(documentation)
29+
\(documentation)\
3030
\(node.node.apiAttributes())\
3131
public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable
3232
"""

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
2727
"""
2828
// MARK: - \(node.kind.syntaxType)
2929
30-
\(SwiftSyntax.Trivia(joining: [node.documentation, node.experimentalDocNote, node.grammar, node.containedIn]))
30+
\(SwiftSyntax.Trivia(joining: [node.documentation, node.experimentalDocNote, node.grammar, node.containedIn]))\
3131
\(node.node.apiAttributes())\
3232
public struct \(node.kind.syntaxType): \(node.baseType.syntaxBaseName)Protocol, SyntaxHashable, \(node.base.leafProtocolType)
3333
"""
@@ -55,7 +55,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
5555

5656
try! InitializerDeclSyntax(
5757
"""
58-
\(node.generateInitializerDocComment())
58+
\(node.generateInitializerDocComment())\
5959
\(node.generateInitializerDeclHeader())
6060
"""
6161
) {
@@ -137,7 +137,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
137137

138138
try! VariableDeclSyntax(
139139
"""
140-
\(child.documentation)
140+
\(child.documentation)\
141141
\(child.apiAttributes)public var \(child.varOrCaseName.backtickedIfNeeded): \(type)
142142
"""
143143
) {

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxTraitsFile.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2121
"""
2222
// MARK: - \(trait.protocolName)
2323
24-
\(trait.documentation)
24+
\(trait.documentation)\
2525
public protocol \(trait.protocolName): SyntaxProtocol\(raw:
26-
trait.baseKind != nil
27-
? ", \(trait.baseKind!.protocolType)"
28-
: ""
26+
trait.baseKind == nil ? "" : ", \(trait.baseKind!.protocolType)"
2927
)
3028
"""
3129
) {
@@ -34,7 +32,7 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
3432

3533
DeclSyntax(
3634
"""
37-
\(child.documentation)
35+
\(child.documentation)\
3836
\(child.apiAttributes)var \(child.varOrCaseName): \(child.syntaxNodeKind.syntaxType)\(raw: questionMark) { get set }
3937
"""
4038
)

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
2323

2424
try! StructDeclSyntax(
2525
"""
26-
2726
// MARK: - \(type.resultBuilderType)
2827
2928
@resultBuilder

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
// MARK: - BracedSyntax
1616

17-
1817
public protocol BracedSyntax: SyntaxProtocol {
1918
/// ### Tokens
2019
///
@@ -62,7 +61,6 @@ public extension SyntaxProtocol {
6261

6362
// MARK: - DeclGroupSyntax
6463

65-
6664
public protocol DeclGroupSyntax: SyntaxProtocol, DeclSyntaxProtocol {
6765
var attributes: AttributeListSyntax {
6866
get
@@ -120,7 +118,6 @@ public extension SyntaxProtocol {
120118

121119
// MARK: - EffectSpecifiersSyntax
122120

123-
124121
public protocol EffectSpecifiersSyntax: SyntaxProtocol {
125122
var unexpectedBeforeAsyncSpecifier: UnexpectedNodesSyntax? {
126123
get
@@ -182,7 +179,6 @@ public extension SyntaxProtocol {
182179

183180
// MARK: - FreestandingMacroExpansionSyntax
184181

185-
186182
public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
187183
/// ### Tokens
188184
///
@@ -266,7 +262,6 @@ public extension SyntaxProtocol {
266262

267263
// MARK: - NamedDeclSyntax
268264

269-
270265
public protocol NamedDeclSyntax: SyntaxProtocol {
271266
/// ### Tokens
272267
///
@@ -350,7 +345,6 @@ public extension SyntaxProtocol {
350345

351346
// MARK: - ParenthesizedSyntax
352347

353-
354348
public protocol ParenthesizedSyntax: SyntaxProtocol {
355349
/// ### Tokens
356350
///
@@ -398,7 +392,6 @@ public extension SyntaxProtocol {
398392

399393
// MARK: - WithAttributesSyntax
400394

401-
402395
public protocol WithAttributesSyntax: SyntaxProtocol {
403396
var attributes: AttributeListSyntax {
404397
get
@@ -435,7 +428,6 @@ public extension SyntaxProtocol {
435428

436429
// MARK: - WithCodeBlockSyntax
437430

438-
439431
public protocol WithCodeBlockSyntax: SyntaxProtocol {
440432
var body: CodeBlockSyntax {
441433
get
@@ -518,7 +510,6 @@ public extension SyntaxProtocol {
518510

519511
// MARK: - WithModifiersSyntax
520512

521-
522513
public protocol WithModifiersSyntax: SyntaxProtocol {
523514
var modifiers: DeclModifierListSyntax {
524515
get
@@ -555,7 +546,6 @@ public extension SyntaxProtocol {
555546

556547
// MARK: - WithOptionalCodeBlockSyntax
557548

558-
559549
public protocol WithOptionalCodeBlockSyntax: SyntaxProtocol {
560550
var body: CodeBlockSyntax? {
561551
get
@@ -592,7 +582,6 @@ public extension SyntaxProtocol {
592582

593583
// MARK: - WithStatementsSyntax
594584

595-
596585
public protocol WithStatementsSyntax: SyntaxProtocol {
597586
var statements: CodeBlockItemListSyntax {
598587
get
@@ -629,7 +618,6 @@ public extension SyntaxProtocol {
629618

630619
// MARK: - WithTrailingCommaSyntax
631620

632-
633621
public protocol WithTrailingCommaSyntax: SyntaxProtocol {
634622
/// ### Tokens
635623
///

0 commit comments

Comments
 (0)