Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 16, 2024
1 parent a733432 commit 1b86e4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
7 changes: 5 additions & 2 deletions Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension EnumeratorMacroType: MemberMacro {
if exprList.isEmpty {
throw MacroError.expectedAtLeastOneArgument
}
let templates = exprList.compactMap {
let templates = exprList.compactMap {
element -> (template: String, syntax: StringLiteralExprSyntax)? in
guard let stringLiteral = element.expression.as(StringLiteralExprSyntax.self) else {
context.diagnose(
Expand All @@ -45,15 +45,18 @@ extension EnumeratorMacroType: MemberMacro {
)
return nil
}
var hadBadSegment = false
for segment in stringLiteral.segments where !segment.is(StringSegmentSyntax.self) {
context.diagnose(
Diagnostic(
node: segment,
message: MacroError.allArgumentsMustBeNonInterpolatedStringLiterals
)
)
return nil
hadBadSegment = true
}
if hadBadSegment { return nil }

let template = stringLiteral.segments.description
return (template, stringLiteral)
}
Expand Down
37 changes: 26 additions & 11 deletions Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ final class EnumeratorMacroTests: XCTestCase {
{{#cases}}
case \(name)
{{/cases}}
\(comments)
}
""")
enum TestEnum {
Expand All @@ -440,18 +441,32 @@ final class EnumeratorMacroTests: XCTestCase {
case testCase(testValue: String)
}
"""#,
diagnostics: [.init(
id: .init(
domain: "EnumeratorMacro.MacroError",
id: "allArgumentsMustBeNonInterpolatedStringLiterals"
diagnostics: [
.init(
id: .init(
domain: "EnumeratorMacro.MacroError",
id: "allArgumentsMustBeNonInterpolatedStringLiterals"
),
message: """
All arguments must be non-interpolated string literals.
""",
line: 4,
column: 10,
severity: .error
),
message: """
All arguments must be non-interpolated string literals.
""",
line: 4,
column: 10,
severity: .error
)],
.init(
id: .init(
domain: "EnumeratorMacro.MacroError",
id: "allArgumentsMustBeNonInterpolatedStringLiterals"
),
message: """
All arguments must be non-interpolated string literals.
""",
line: 6,
column: 5,
severity: .error
)
],
macros: EnumeratorMacroEntryPoint.macros
)
}
Expand Down

0 comments on commit 1b86e4b

Please sign in to comment.