Skip to content
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
1 change: 1 addition & 0 deletions CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public let COMMON_NODES: [Node] = [
nameForDiagnostics: nil,
description: "A CodeBlockItem is any Syntax node that appears on its own line inside a CodeBlock.",
kind: "Syntax",
parserFunction: "parseNonOptionalCodeBlockItem",
children: [
Child(
name: "Item",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,22 @@ let parserEntryFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
"""
)

DeclSyntax(
"""
mutating func parseNonOptionalCodeBlockItem() -> RawCodeBlockItemSyntax {
guard let node = self.parseCodeBlockItem(isAtTopLevel: false, allowInitDecl: true) else {
// The missing item is not neccessary to be a declaration,
// which is just a placeholder here
return RawCodeBlockItemSyntax(
item: .decl(RawDeclSyntax(RawMissingDeclSyntax(attributes: nil, modifiers: nil, arena: self.arena))),
semicolon: nil,
arena: self.arena
)
}
return node
}
"""
)
}
}
21 changes: 21 additions & 0 deletions Sources/SwiftParser/generated/Parser+Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ extension ClosureParameterSyntax: SyntaxParseable {
}
}

extension CodeBlockItemSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
let node = parser.parseNonOptionalCodeBlockItem()
let raw = RawSyntax(parser.parseRemainder(into: node))
return Syntax(raw: raw).cast(Self.self)
}
}

extension DeclSyntax: SyntaxParseable {
public static func parse(from parser: inout Parser) -> Self {
let node = parser.parseDeclaration()
Expand Down Expand Up @@ -184,4 +192,17 @@ fileprivate extension Parser {
let withUnexpected = layout.replacingChild(at: layout.children.count - 1, with: unexpected.raw, arena: self.arena)
return R.init(withUnexpected)!
}

mutating func parseNonOptionalCodeBlockItem() -> RawCodeBlockItemSyntax {
guard let node = self.parseCodeBlockItem(isAtTopLevel: false, allowInitDecl: true) else {
// The missing item is not neccessary to be a declaration,
// which is just a placeholder here
return RawCodeBlockItemSyntax(
item: .decl(RawDeclSyntax(RawMissingDeclSyntax(attributes: nil, modifiers: nil, arena: self.arena))),
semicolon: nil,
arena: self.arena
)
}
return node
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extension CatchClauseSyntax: SyntaxExpressibleByStringInterpolation {}

extension ClosureParameterSyntax: SyntaxExpressibleByStringInterpolation {}

extension CodeBlockItemSyntax: SyntaxExpressibleByStringInterpolation {}

extension DeclSyntax: SyntaxExpressibleByStringInterpolation {}

extension EnumCaseParameterSyntax: SyntaxExpressibleByStringInterpolation {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,19 @@ final class CollectionNodeFlatteningTests: XCTestCase {
"""
)
}

func test_FlattenCodeBlockItemListWithCodeBlockItemStrings() {
let buildable = CodeBlockItemListSyntax {
"let one = object.methodOne()"
"let two = object.methodTwo()"
}

assertBuildResult(
buildable,
"""
let one = object.methodOne()
let two = object.methodTwo()
"""
)
}
}
37 changes: 37 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/StringInterpolationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,26 @@ final class StringInterpolationTests: XCTestCase {
)
}

func testCodeBlockItemInterpolation() {
let codeBlockItem: CodeBlockItemSyntax =
"""
func foo() {
return bar
}
"""

XCTAssertTrue(codeBlockItem.is(CodeBlockItemSyntax.self))
assertStringsEqualWithDiff(
codeBlockItem.description,
"""
func foo() {
return bar
}
"""
)

}

func testInvalidTrivia() {
let invalid = Trivia("/*comment*/ invalid /*comm*/")
XCTAssertEqual(invalid, [.blockComment("/*comment*/"), .spaces(1), .unexpectedText("invalid"), .spaces(1), .blockComment("/*comm*/")])
Expand Down Expand Up @@ -460,4 +480,21 @@ final class StringInterpolationTests: XCTestCase {
)
}
}

func testInvalidSyntax3() {
let invalid: CodeBlockItemSyntax = " "

XCTAssert(invalid.hasError)
XCTAssertThrowsError(try CodeBlockItemSyntax(validating: " ")) { error in
assertStringsEqualWithDiff(
String(describing: error),
"""

1 │
│ ╰─ error: expected declaration

"""
)
}
}
}