Skip to content

Fix "escaped newline symbol" bug in CodeGeneration module #2468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions CodeGeneration/Sources/SyntaxSupport/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,19 @@ public func lowercaseFirstWord(name: String) -> String {
extension SwiftSyntax.Trivia {
/// Make a new trivia from a (possibly multi-line) string, prepending `///`
/// to each line and creating a `.docLineComment` trivia piece for each line.
public static func docCommentTrivia(from string: String?) -> SwiftSyntax.Trivia {
guard let string else {
return []
}

let lines = string.split(separator: "\n", omittingEmptySubsequences: false)
let pieces = lines.enumerated().map { (index, line) in
var line = line
if index != lines.count - 1 {
line = "\(line)\n"
}
return SwiftSyntax.TriviaPiece.docLineComment("/// \(line)")
}
return SwiftSyntax.Trivia(pieces: pieces)
public static func docCommentTrivia(from string: String?) -> Self {
guard let string else { return [] }
let lines = string.split(separator: "\n", omittingEmptySubsequences: false).map { "/// \($0)" }
return .init(pieces: lines.flatMap { [.docLineComment($0), .newlines(1)] })
}

/// Make a new trivia by joining together ``SwiftSyntax/TriviaPiece``s from `joining`,
/// and gluing them together with pieces from `separator`.
public init(
joining items: [SwiftSyntax.Trivia],
separator: SwiftSyntax.Trivia = SwiftSyntax.Trivia(pieces: [TriviaPiece.newlines(1), TriviaPiece.docLineComment("///"), TriviaPiece.newlines(1)])
separator: SwiftSyntax.Trivia = .init(pieces: [.docLineComment("///"), .newlines(1)])
) {

self.init(
pieces:
items
.filter { !$0.isEmpty }
.joined(separator: separator)
)
self.init(pieces: items.filter { !$0.isEmpty }.joined(separator: separator))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

try! StructDeclSyntax(
"""
\(documentation)
\(documentation)\
\(node.apiAttributes())\
public struct \(node.kind.syntaxType): \(node.kind.protocolType), SyntaxHashable
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

try! StructDeclSyntax(
"""
\(documentation)
\(documentation)\
\(node.node.apiAttributes())\
public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
"""
// MARK: - \(node.kind.syntaxType)

\(SwiftSyntax.Trivia(joining: [node.documentation, node.experimentalDocNote, node.grammar, node.containedIn]))
\(SwiftSyntax.Trivia(joining: [node.documentation, node.experimentalDocNote, node.grammar, node.containedIn]))\
\(node.node.apiAttributes())\
public struct \(node.kind.syntaxType): \(node.baseType.syntaxBaseName)Protocol, SyntaxHashable, \(node.base.leafProtocolType)
"""
Expand Down Expand Up @@ -55,7 +55,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {

try! InitializerDeclSyntax(
"""
\(node.generateInitializerDocComment())
\(node.generateInitializerDocComment())\
\(node.generateInitializerDeclHeader())
"""
) {
Expand Down Expand Up @@ -137,7 +137,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {

try! VariableDeclSyntax(
"""
\(child.documentation)
\(child.documentation)\
\(child.apiAttributes)public var \(child.varOrCaseName.backtickedIfNeeded): \(type)
"""
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
"""
// MARK: - \(trait.protocolName)

\(trait.documentation)
\(trait.documentation)\
public protocol \(trait.protocolName): SyntaxProtocol\(raw:
trait.baseKind != nil
? ", \(trait.baseKind!.protocolType)"
: ""
trait.baseKind == nil ? "" : ", \(trait.baseKind!.protocolType)"
)
"""
) {
Expand All @@ -34,7 +32,7 @@ let syntaxTraitsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

DeclSyntax(
"""
\(child.documentation)
\(child.documentation)\
\(child.apiAttributes)var \(child.varOrCaseName): \(child.syntaxNodeKind.syntaxType)\(raw: questionMark) { get set }
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {

try! StructDeclSyntax(
"""

// MARK: - \(type.resultBuilderType)

@resultBuilder
Expand Down
12 changes: 0 additions & 12 deletions Sources/SwiftSyntax/generated/SyntaxTraits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

// MARK: - BracedSyntax


public protocol BracedSyntax: SyntaxProtocol {
/// ### Tokens
///
Expand Down Expand Up @@ -62,7 +61,6 @@ public extension SyntaxProtocol {

// MARK: - DeclGroupSyntax


public protocol DeclGroupSyntax: SyntaxProtocol, DeclSyntaxProtocol {
var attributes: AttributeListSyntax {
get
Expand Down Expand Up @@ -120,7 +118,6 @@ public extension SyntaxProtocol {

// MARK: - EffectSpecifiersSyntax


public protocol EffectSpecifiersSyntax: SyntaxProtocol {
var unexpectedBeforeAsyncSpecifier: UnexpectedNodesSyntax? {
get
Expand Down Expand Up @@ -182,7 +179,6 @@ public extension SyntaxProtocol {

// MARK: - FreestandingMacroExpansionSyntax


public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
/// ### Tokens
///
Expand Down Expand Up @@ -266,7 +262,6 @@ public extension SyntaxProtocol {

// MARK: - NamedDeclSyntax


public protocol NamedDeclSyntax: SyntaxProtocol {
/// ### Tokens
///
Expand Down Expand Up @@ -350,7 +345,6 @@ public extension SyntaxProtocol {

// MARK: - ParenthesizedSyntax


public protocol ParenthesizedSyntax: SyntaxProtocol {
/// ### Tokens
///
Expand Down Expand Up @@ -398,7 +392,6 @@ public extension SyntaxProtocol {

// MARK: - WithAttributesSyntax


public protocol WithAttributesSyntax: SyntaxProtocol {
var attributes: AttributeListSyntax {
get
Expand Down Expand Up @@ -435,7 +428,6 @@ public extension SyntaxProtocol {

// MARK: - WithCodeBlockSyntax


public protocol WithCodeBlockSyntax: SyntaxProtocol {
var body: CodeBlockSyntax {
get
Expand Down Expand Up @@ -518,7 +510,6 @@ public extension SyntaxProtocol {

// MARK: - WithModifiersSyntax


public protocol WithModifiersSyntax: SyntaxProtocol {
var modifiers: DeclModifierListSyntax {
get
Expand Down Expand Up @@ -555,7 +546,6 @@ public extension SyntaxProtocol {

// MARK: - WithOptionalCodeBlockSyntax


public protocol WithOptionalCodeBlockSyntax: SyntaxProtocol {
var body: CodeBlockSyntax? {
get
Expand Down Expand Up @@ -592,7 +582,6 @@ public extension SyntaxProtocol {

// MARK: - WithStatementsSyntax


public protocol WithStatementsSyntax: SyntaxProtocol {
var statements: CodeBlockItemListSyntax {
get
Expand Down Expand Up @@ -629,7 +618,6 @@ public extension SyntaxProtocol {

// MARK: - WithTrailingCommaSyntax


public protocol WithTrailingCommaSyntax: SyntaxProtocol {
/// ### Tokens
///
Expand Down