Skip to content

Commit

Permalink
HTMLElementAttribute now conforms to HTMLInitializable
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomHashTags committed Dec 5, 2024
1 parent 52092eb commit 72bd29f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let package = Package(
.product(name: "SwiftSyntaxMacros", package: "swift-syntax")
]
),

.target(
name: "HTMLKitUtilities",
dependencies: [
Expand All @@ -37,6 +38,7 @@ let package = Package(
.product(name: "SwiftSyntaxMacros", package: "swift-syntax")
]
),

.macro(
name: "HTMLKitMacros",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTMLKitUtilities/ParseData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public extension HTMLKitUtilities {
context.diagnose(Diagnostic(node: first_expression, message: DiagnosticMsg(id: "spacesNotAllowedInAttributeDeclaration", message: "Spaces are not allowed in attribute declaration.")))
} else if keys.contains(key) {
global_attribute_already_defined(context: context, attribute: key, node: first_expression)
} else if let attr:HTMLElementAttribute = HTMLElementAttribute(context: context, key: key, function) {
} else if let attr:HTMLElementAttribute = HTMLElementAttribute(context: context, key: key, arguments: function.arguments) {
attributes.append(attr)
key = attr.key
keys.insert(key)
Expand Down
12 changes: 6 additions & 6 deletions Sources/HTMLKitUtilities/attributes/HTMLElementAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftSyntax
import SwiftSyntaxMacros

// MARK: HTMLElementAttribute
public enum HTMLElementAttribute : Hashable {
public enum HTMLElementAttribute : HTMLInitializable {
case accesskey(String? = nil)

case ariaattribute(Extra.ariaattribute? = nil)
Expand Down Expand Up @@ -60,11 +60,11 @@ public enum HTMLElementAttribute : Hashable {
case event(Extra.event, _ value: String? = nil)

// MARK: init rawValue
public init?(context: some MacroExpansionContext, key: String, _ function: FunctionCallExprSyntax) {
let expression:ExprSyntax = function.arguments.first!.expression
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
let expression:ExprSyntax = arguments.first!.expression
func string() -> String? { expression.string(context: context, key: key) }
func boolean() -> Bool? { expression.boolean(context: context, key: key) }
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: function.arguments) }
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: arguments) }
func int() -> Int? { expression.int(context: context, key: key) }
func array_string() -> [String]? { expression.array_string(context: context, key: key) }
switch key {
Expand All @@ -76,7 +76,7 @@ public enum HTMLElementAttribute : Hashable {
case "class": self = .class(array_string())
case "contenteditable": self = .contenteditable(enumeration())
case "data", "custom":
guard let id:String = string(), let value:String = function.arguments.last?.expression.string(context: context, key: key) else {
guard let id:String = string(), let value:String = arguments.last?.expression.string(context: context, key: key) else {
return nil
}
if key == "data" {
Expand Down Expand Up @@ -113,7 +113,7 @@ public enum HTMLElementAttribute : Hashable {
case "trailingSlash": self = .trailingSlash
case "htmx": self = .htmx(enumeration())
case "event":
guard let event:HTMLElementAttribute.Extra.event = enumeration(), let value:String = function.arguments.last?.expression.string(context: context, key: key) else {
guard let event:HTMLElementAttribute.Extra.event = enumeration(), let value:String = arguments.last?.expression.string(context: context, key: key) else {
return nil
}
self = .event(event, value)
Expand Down
File renamed without changes.

0 comments on commit 72bd29f

Please sign in to comment.