Skip to content

Commit

Permalink
more diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiBM committed Jul 20, 2024
1 parent b0afa45 commit 8bdb100
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "5fb3f3f652f6046ecafacd4cf94fd7cbc6347977cdfde61d767ba9728638b23b",
"originHash" : "471e59eb3ce1f697200ce8e807d75c97a2b2065d8033fd5c430fc887e3c33f05",
"pins" : [
{
"identity" : "swift-mustache",
"kind" : "remoteSourceControl",
"location" : "https://github.com/hummingbird-project/swift-mustache",
"state" : {
"revision" : "5bb66ac425ec25fd45e0cd7d3829d73e45096352",
"version" : "2.0.0-beta.2"
"revision" : "cde358e364ab26f2b72a5f70613cfb0a574de6c9",
"version" : "2.0.0-beta.3"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
),
.package(
url: "https://github.com/hummingbird-project/swift-mustache",
from: "2.0.0-beta.2"
from: "2.0.0-beta.3"
),
],
targets: [
Expand Down
5 changes: 5 additions & 0 deletions Sources/EnumeratorMacroImpl/MacroError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum MacroError: Error, CustomStringConvertible {
case invalidTransform(transform: String, normalizedTypeName: String)
case commentKeyNotAllowed(key: String)
case declaredHere(name: String)
case redundantKeyValuesFunctionCall

var caseName: String {
switch self {
Expand Down Expand Up @@ -44,6 +45,8 @@ enum MacroError: Error, CustomStringConvertible {
"commentKeyNotAllowed"
case .declaredHere:
"declaredHere"
case .redundantKeyValuesFunctionCall:
"redundantKeyValuesFunctionCall"
}
}

Expand Down Expand Up @@ -75,6 +78,8 @@ enum MacroError: Error, CustomStringConvertible {
"Comment key '\(key)' is not allowed by the 'allowedComments' of the macro declaration"
case let .declaredHere(name):
"\(name) declared here:"
case .redundantKeyValuesFunctionCall:
"Redundant 'keyValues' function used. The array is already of type '\(EArray<EKeyValue>.self)'"
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions Sources/EnumeratorMacroImpl/Types/EArray.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import SwiftDiagnostics
import Mustache
import Foundation

struct EArray<Element> {
let underlying: [Element]

init(underlying: [Element]) {
self.underlying = underlying
}

@available(*, unavailable, message: "Unwrap the array first")
init(underlying: EArray<Element>) {
fatalError()
}
}

extension EArray: Sequence, MustacheSequence {
Expand Down Expand Up @@ -58,6 +53,15 @@ extension EArray: EMustacheTransformable {
let string = EString(joined)
return string
case "keyValues":
if Self.self is EArray<EKeyValue>.Type {
RenderingContext.current.context.diagnose(
Diagnostic(
node: RenderingContext.current.node,
message: MacroError.redundantKeyValuesFunctionCall
)
)
return self
}
let split: [EKeyValue] = self.underlying
.map { String(describing: $0) }
.compactMap(EKeyValue.init(from:))
Expand Down
11 changes: 7 additions & 4 deletions Sources/EnumeratorMacroImpl/Types/EComments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ extension EComments: EMustacheTransformable {
case "sorted":
return EComments(underlying: self.underlying.underlying.sorted())
case "keyValues":
let split: [EKeyValue] = self.underlying.underlying
.map { String(describing: $0) }
.compactMap(EKeyValue.init(from:))
return EComments(underlying: split)
RenderingContext.current.context.diagnose(
Diagnostic(
node: RenderingContext.current.node,
message: MacroError.redundantKeyValuesFunctionCall
)
)
return self
default:
if let context = RenderingContext.current,
let allowedComments = context.allowedComments,
Expand Down
15 changes: 10 additions & 5 deletions Sources/EnumeratorMacroImpl/Types/EOptionalsArray.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftDiagnostics
import Mustache

struct EOptionalsArray<Element> {
Expand All @@ -10,11 +11,6 @@ struct EOptionalsArray<Element> {
init(underlying: [EOptional<Element>]) {
self.underlying = underlying
}

@available(*, unavailable, message: "Unwrap the optionals-array first")
init(underlying: EOptionalsArray<Element>) {
fatalError()
}
}

extension EOptionalsArray: Sequence, MustacheSequence {
Expand Down Expand Up @@ -62,6 +58,15 @@ extension EOptionalsArray: EMustacheTransformable {
let string = EString(joined)
return string
case "keyValues":
if Self.self is EOptionalsArray<EKeyValue>.Type {
RenderingContext.current.context.diagnose(
Diagnostic(
node: RenderingContext.current.node,
message: MacroError.redundantKeyValuesFunctionCall
)
)
return self
}
let split: [EKeyValue] = self.underlying
.compactMap { $0.toOptional().map { String(describing: $0) } }
.compactMap(EKeyValue.init(from:))
Expand Down
35 changes: 35 additions & 0 deletions Tests/EnumeratorMacroTests/EnumeratorMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,41 @@ final class EnumeratorMacroTests: XCTestCase {
)
}

func testRejectsDoubleKeyValueTransform() {
let diagnostic = DiagnosticSpec(
id: .init(
domain: "EnumeratorMacro.MacroError",
id: "redundantKeyValuesFunctionCall"
),
message: "Redundant 'keyValues' function used. The array is already of type 'EArray<EKeyValue>'",
line: 1,
column: 13,
severity: .error
)
assertMacroExpansion(
#"""
@Enumerator("""
/// {{#cases}} {{keyValues(comments)}} {{/cases}}
""")
public enum ErrorMessage {
case case1 // business_error
case case2 // business_error: true
}
"""#,
expandedSource: #"""
public enum ErrorMessage {
case case1 // business_error
case case2 // business_error: true
}
"""#,
diagnostics: [
diagnostic,
diagnostic
],
macros: EnumeratorMacroEntryPoint.macros
)
}

/// Test name is referenced in the README.
func testRemovesExcessiveTrivia() {
assertMacroExpansion(
Expand Down

0 comments on commit 8bdb100

Please sign in to comment.