Skip to content

Commit 21e2acc

Browse files
committed
Make SyntaxStringInterpolationInvalidTokenTypeError public
This commit splits `SyntaxStringInterpolationError` into two error types: - `SyntaxStringInterpolationInvalidTokenTypeError` for situations when interpolation resulted in a wrong token type, and - `SyntaxStringInterpolationDiagnosticError` when another error occured, and the user provides additional diagnostics with the error. Additionally: - The `SyntaxStringInterpolationInvalidTokenTypeError` is now `public` so other clients can catch it. - Both error types are structs instead of enums. - All invocation spots are updated to match the API.
1 parent 0df36e7 commit 21e2acc

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

Sources/SwiftSyntaxBuilder/DeclSyntaxParseable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public extension DeclSyntaxParseable {
3131
if let castedDecl = node.as(Self.self) {
3232
self = castedDecl
3333
} else {
34-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: node)
34+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: node)
3535
}
3636
}
3737
}

Sources/SwiftSyntaxBuilder/Syntax+StringInterpolation.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,28 @@ where Self.StringInterpolation == SyntaxStringInterpolation {
161161
init(stringInterpolation: SyntaxStringInterpolation)
162162
}
163163

164-
// Used in `MacroExpansionContext` to make a nicer error message when a
165-
// macro expansion fails with SwiftSyntaxBuilder making a syntax node
166-
// with string interpolation.
167-
@_spi(Diagnostics) public enum SyntaxStringInterpolationError: Error, CustomStringConvertible {
168-
case producedInvalidNodeType(expectedType: SyntaxProtocol.Type, actualType: SyntaxProtocol.Type)
169-
case diagnostics([Diagnostic], tree: Syntax)
170-
171-
static func producedInvalidNodeType<S: SyntaxProtocol>(expectedType: SyntaxProtocol.Type, actualNode: S) -> Self {
172-
return .producedInvalidNodeType(expectedType: expectedType, actualType: type(of: actualNode))
164+
/// Describes an error when building a syntax node with string interpolation resulted in an unexpected node type.
165+
public struct SyntaxStringInterpolationInvalidNodeTypeError: Error, CustomStringConvertible {
166+
let expectedType: SyntaxProtocol.Type
167+
let actualType: SyntaxProtocol.Type
168+
169+
/// Initialize the invalid node type error providing an expected type, and the actual node that resulted.
170+
public init<S: SyntaxProtocol>(expectedType: SyntaxProtocol.Type, actualNode: S) {
171+
self.expectedType = expectedType
172+
self.actualType = type(of: actualNode)
173173
}
174174

175175
public var description: String {
176-
switch self {
177-
case .producedInvalidNodeType(expectedType: let expectedType, actualType: let actualType):
178-
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)"
179-
case .diagnostics(let diagnostics, let tree):
180-
// Start the diagnostic on a new line so it isn't prefixed with the file, which messes up the
181-
// column-aligned message from ``DiagnosticsFormatter``.
182-
return "\n" + DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics)
183-
}
176+
return "Parsing the code snippet was expected to produce a \(expectedType) but produced a \(actualType)"
177+
}
178+
}
179+
180+
struct SyntaxStringInterpolationDiagnosticError: Error, CustomStringConvertible {
181+
let diagnostics: [Diagnostic]
182+
let tree: Syntax
183+
184+
var description: String {
185+
return "\n" + DiagnosticsFormatter.annotatedSource(tree: tree, diags: diagnostics)
184186
}
185187
}
186188

Sources/SwiftSyntaxBuilder/SyntaxNodeWithBody.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public extension HasTrailingCodeBlock where Self: StmtSyntaxProtocol {
7171
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws {
7272
let stmt = StmtSyntax("\(header) {}")
7373
guard let castedStmt = stmt.as(Self.self) else {
74-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: stmt)
74+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: stmt)
7575
}
7676
self = castedStmt
7777
self.body = try CodeBlockSyntax(statements: bodyBuilder())
@@ -121,7 +121,7 @@ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
121121
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws {
122122
let decl = DeclSyntax("\(header) {}")
123123
guard let castedDecl = decl.as(Self.self) else {
124-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: decl)
124+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
125125
}
126126
self = castedDecl
127127
self.body = try CodeBlockSyntax(statements: bodyBuilder())
@@ -166,7 +166,7 @@ public extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
166166
init(_ header: SyntaxNodeString, @MemberBlockItemListBuilder membersBuilder: () throws -> MemberBlockItemListSyntax) throws {
167167
let decl = DeclSyntax("\(header) {}")
168168
guard let castedDecl = decl.as(Self.self) else {
169-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: decl)
169+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
170170
}
171171
self = castedDecl
172172
self.memberBlock = try MemberBlockSyntax(members: membersBuilder())
@@ -209,7 +209,7 @@ public extension IfExprSyntax {
209209
) throws {
210210
let expr = ExprSyntax("\(header) {}")
211211
guard let ifExpr = expr.as(Self.self) else {
212-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: expr)
212+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
213213
}
214214
self = ifExpr
215215
self.body = try CodeBlockSyntax(statements: bodyBuilder())
@@ -254,7 +254,7 @@ public extension IfExprSyntax {
254254
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax, elseIf: IfExprSyntax) throws {
255255
let expr = ExprSyntax("\(header) {}")
256256
guard let ifExpr = expr.as(Self.self) else {
257-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: expr)
257+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
258258
}
259259
self = ifExpr
260260
self.body = CodeBlockSyntax(statements: try bodyBuilder())
@@ -321,7 +321,7 @@ public extension SwitchExprSyntax {
321321
init(_ header: SyntaxNodeString, @SwitchCaseListBuilder casesBuilder: () throws -> SwitchCaseListSyntax = { SwitchCaseListSyntax([]) }) throws {
322322
let expr = ExprSyntax("\(header) {}")
323323
guard let switchExpr = expr.as(Self.self) else {
324-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: expr)
324+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: expr)
325325
}
326326
self = switchExpr
327327
self.cases = try casesBuilder()
@@ -355,7 +355,7 @@ public extension VariableDeclSyntax {
355355
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder accessor: () throws -> CodeBlockItemListSyntax) throws {
356356
let decl = DeclSyntax("\(header) {}")
357357
guard let castedDecl = decl.as(Self.self) else {
358-
throw SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: Self.self, actualNode: decl)
358+
throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
359359
}
360360
self = castedDecl
361361
precondition(self.bindings.count == 1)

Sources/SwiftSyntaxBuilder/ValidatingSyntaxNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension SyntaxProtocol {
2525
if node.hasError {
2626
let diagnostics = ParseDiagnosticsGenerator.diagnostics(for: node)
2727
precondition(!diagnostics.isEmpty)
28-
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(node))
28+
throw SyntaxStringInterpolationDiagnosticError(diagnostics: diagnostics, tree: Syntax(node))
2929
}
3030
self = node
3131
}
@@ -52,7 +52,7 @@ extension Trivia {
5252
}
5353
offset += piece.sourceLength.utf8Length
5454
}
55-
throw SyntaxStringInterpolationError.diagnostics(diagnostics, tree: Syntax(tree))
55+
throw SyntaxStringInterpolationDiagnosticError(diagnostics: diagnostics, tree: Syntax(tree))
5656
}
5757
}
5858
}

Sources/SwiftSyntaxMacros/MacroExpansionContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension MacroExpansionContext {
8686
diagnostics = diagnosticsError.diagnostics
8787
} else if let message = error as? DiagnosticMessage {
8888
diagnostics = [Diagnostic(node: Syntax(node), message: message)]
89-
} else if let error = error as? SyntaxStringInterpolationError {
89+
} else if let error = error as? SyntaxStringInterpolationInvalidNodeTypeError {
9090
let diagnostic = Diagnostic(node: Syntax(node), message: ThrownErrorDiagnostic(message: "Internal macro error: \(error.description)"))
9191
diagnostics = [diagnostic]
9292
} else {

Tests/SwiftSyntaxMacroExpansionTest/MacroExpansionContextTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import SwiftDiagnostics
1414
import SwiftSyntax
15-
@_spi(Diagnostics) import SwiftSyntaxBuilder
15+
import SwiftSyntaxBuilder
1616
import SwiftSyntaxMacroExpansion
1717
import SwiftSyntaxMacros
1818
import SwiftSyntaxMacrosTestSupport
@@ -44,10 +44,10 @@ final class MacroExpansionContextTests: XCTestCase {
4444

4545
func testMacroExpansionContextAddDiagnosticsAddsSwiftSyntaxInterpolationErrorsWithWrappingMessage() {
4646
let context = BasicMacroExpansionContext()
47-
let error = SyntaxStringInterpolationError.producedInvalidNodeType(expectedType: DeclSyntax.self, actualType: ExprSyntax.self)
48-
// Since we only care about the error switch inside of addDagnostics, we don't care about the particular node we're passing in.
49-
context.addDiagnostics(from: error, node: ExprSyntax("1"))
47+
let error = SyntaxStringInterpolationInvalidNodeTypeError(expectedType: DeclSyntax.self, actualNode: ExprSyntax("test"))
5048

49+
// Since we only care about the error switch inside of addDagnostics, we don't care about the particular node we're passing in
50+
context.addDiagnostics(from: error, node: ExprSyntax("1"))
5151
XCTAssertEqual(context.diagnostics.count, 1)
5252
XCTAssertTrue(context.diagnostics.first!.message.starts(with: "Internal macro error:"))
5353
}

0 commit comments

Comments
 (0)