Skip to content

Commit a774d89

Browse files
connected interpolation promotion logic; some fixes
1 parent cd092e7 commit a774d89

File tree

7 files changed

+103
-119
lines changed

7 files changed

+103
-119
lines changed

Sources/HTMLKitUtilities/HTMLElementAttribute.swift

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftSyntax
9+
import SwiftSyntaxMacros
910

1011
public enum HTMLElementAttribute : Hashable {
1112
case accesskey(String? = nil)
@@ -58,28 +59,13 @@ public enum HTMLElementAttribute : Hashable {
5859
case event(Extra.event, _ value: String? = nil)
5960

6061
// MARK: init rawValue
61-
public init?(key: String, _ function: FunctionCallExprSyntax) {
62+
public init?(context: some MacroExpansionContext, key: String, _ function: FunctionCallExprSyntax) {
6263
let expression:ExprSyntax = function.arguments.first!.expression
63-
func string() -> String? { expression.stringLiteral?.string }
64-
func boolean() -> Bool? { expression.booleanLiteral?.literal.text == "true" }
65-
func enumeration<T : HTMLInitializable>() -> T? {
66-
if let function:FunctionCallExprSyntax = expression.functionCall, let member:MemberAccessExprSyntax = function.calledExpression.memberAccess {
67-
return T(key: member.declName.baseName.text, arguments: function.arguments)
68-
}
69-
if let member:MemberAccessExprSyntax = expression.memberAccess {
70-
return T(key: member.declName.baseName.text, arguments: function.arguments)
71-
}
72-
return nil
73-
}
74-
func int() -> Int? {
75-
guard let s:String = expression.integerLiteral?.literal.text else { return nil }
76-
return Int(s)
77-
}
78-
func array_string() -> [String] { expression.array?.elements.compactMap({ $0.expression.stringLiteral?.string }) ?? [] }
79-
func float() -> Float? {
80-
guard let s:String = expression.integerLiteral?.literal.text ?? expression.floatLiteral?.literal.text else { return nil }
81-
return Float(s)
82-
}
64+
func string() -> String? { expression.string(context: context, key: key) }
65+
func boolean() -> Bool? { expression.boolean(context: context, key: key) }
66+
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: function.arguments) }
67+
func int() -> Int? { expression.int(context: context, key: key) }
68+
func array_string() -> [String] { expression.array_string(context: context, key: key) }
8369
switch key {
8470
case "accesskey": self = .accesskey(string())
8571
case "ariaattribute": self = .ariaattribute(enumeration())
@@ -89,7 +75,7 @@ public enum HTMLElementAttribute : Hashable {
8975
case "class": self = .class(array_string())
9076
case "contenteditable": self = .contenteditable(enumeration())
9177
case "data", "custom":
92-
guard let id:String = string(), let value:String = function.arguments.last?.expression.stringLiteral?.string else {
78+
guard let id:String = string(), let value:String = function.arguments.last?.expression.string(context: context, key: key) else {
9379
return nil
9480
}
9581
if key == "data" {
@@ -126,7 +112,7 @@ public enum HTMLElementAttribute : Hashable {
126112
case "trailingSlash": self = .trailingSlash
127113
case "htmx": self = .htmx(enumeration())
128114
case "event":
129-
guard let event:HTMLElementAttribute.Extra.event = enumeration(), let value:String = function.arguments.last?.expression.stringLiteral?.string else {
115+
guard let event:HTMLElementAttribute.Extra.event = enumeration(), let value:String = function.arguments.last?.expression.string(context: context, key: key) else {
130116
return nil
131117
}
132118
self = .event(event, value)

Sources/HTMLKitUtilities/HTMLElementAttributeExtra.swift

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
//
77

88
import SwiftSyntax
9+
import SwiftSyntaxMacros
910

1011
// MARK: HTMLInitializable
1112
public protocol HTMLInitializable : Hashable {
12-
init?(key: String, arguments: LabeledExprListSyntax)
13+
init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax)
1314

1415
var key : String { get }
1516
var htmlValue : String? { get }
@@ -18,7 +19,7 @@ public extension HTMLInitializable where Self: RawRepresentable, RawValue == Str
1819
var key : String { rawValue }
1920
var htmlValue : String? { rawValue }
2021

21-
init?(key: String, arguments: LabeledExprListSyntax) {
22+
init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
2223
guard let value:Self = .init(rawValue: key) else { return nil }
2324
self = value
2425
}
@@ -28,7 +29,7 @@ public extension HTMLInitializable where Self: RawRepresentable, RawValue == Str
2829
extension HTMLElementAttribute {
2930
public enum Extra {
3031

31-
public static func parse(key: String, expr: ExprSyntax) -> (any HTMLInitializable)? {
32+
public static func parse(context: some MacroExpansionContext, key: String, expr: ExprSyntax) -> (any HTMLInitializable)? {
3233
func get<T : HTMLInitializable>(_ type: T.Type) -> T? {
3334
let inner_key:String, arguments:LabeledExprListSyntax
3435
if let function:FunctionCallExprSyntax = expr.functionCall {
@@ -40,7 +41,7 @@ extension HTMLElementAttribute {
4041
} else {
4142
return nil
4243
}
43-
return T(key: inner_key, arguments: arguments)
44+
return T(context: context, key: inner_key, arguments: arguments)
4445
}
4546
switch key {
4647
case "as": return get(`as`.self)
@@ -175,28 +176,14 @@ public extension HTMLElementAttribute.Extra {
175176
case valuenow(Float?)
176177
case valuetext(String?)
177178

178-
public init?(key: String, arguments: LabeledExprListSyntax) {
179+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
179180
let expression:ExprSyntax = arguments.first!.expression
180-
func string() -> String? { expression.stringLiteral?.string }
181-
func boolean() -> Bool? { expression.booleanLiteral?.literal.text == "true" }
182-
func enumeration<T : HTMLInitializable>() -> T? {
183-
if let function:FunctionCallExprSyntax = expression.functionCall, let member:MemberAccessExprSyntax = function.calledExpression.memberAccess {
184-
return T(key: member.declName.baseName.text, arguments: function.arguments)
185-
}
186-
if let member:MemberAccessExprSyntax = expression.memberAccess {
187-
return T(key: member.declName.baseName.text, arguments: arguments)
188-
}
189-
return nil
190-
}
191-
func int() -> Int? {
192-
guard let s:String = expression.integerLiteral?.literal.text else { return nil }
193-
return Int(s)
194-
}
195-
func array_string() -> [String] { expression.array?.elements.compactMap({ $0.expression.stringLiteral?.string }) ?? [] }
196-
func float() -> Float? {
197-
guard let s:String = expression.integerLiteral?.literal.text ?? expression.floatLiteral?.literal.text else { return nil }
198-
return Float(s)
199-
}
181+
func string() -> String? { expression.string(context: context, key: key) }
182+
func boolean() -> Bool? { expression.boolean(context: context, key: key) }
183+
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: arguments) }
184+
func int() -> Int? { expression.int(context: context, key: key) }
185+
func array_string() -> [String] { expression.array_string(context: context, key: key) }
186+
func float() -> Float? { expression.float(context: context, key: key) }
200187
switch key {
201188
case "activedescendant": self = .activedescendant(string())
202189
case "atomic": self = .atomic(boolean())
@@ -582,7 +569,7 @@ public extension HTMLElementAttribute.Extra {
582569
case togglePopover
583570
case custom(String)
584571

585-
public init?(key: String, arguments: LabeledExprListSyntax) {
572+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
586573
switch key {
587574
case "showModal": self = .showModal
588575
case "close": self = .close
@@ -673,7 +660,7 @@ public extension HTMLElementAttribute.Extra {
673660
case empty
674661
case filename(String)
675662

676-
public init?(key: String, arguments: LabeledExprListSyntax) {
663+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
677664
switch key {
678665
case "empty": self = .empty
679666
case "filename": self = .filename(arguments.first!.expression.stringLiteral!.string)

Sources/HTMLKitUtilities/HTMLKitUtilities.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftSyntax
9+
import SwiftSyntaxMacros
910

1011
// MARK: HTMLKitUtilities
1112
public enum HTMLKitUtilities {
@@ -103,7 +104,7 @@ public extension HTMLElementAttribute {
103104
/// Relative to the parent element
104105
case percent(_ value: Float?)
105106

106-
public init?(key: String, arguments: LabeledExprListSyntax) {
107+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
107108
let expression:ExprSyntax = arguments.first!.expression
108109
func float() -> Float? {
109110
guard let s:String = expression.integerLiteral?.literal.text ?? expression.floatLiteral?.literal.text else { return nil }

Sources/HTMLKitUtilities/HTMX.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftSyntax
9+
import SwiftSyntaxMacros
910

1011
public extension HTMLElementAttribute {
1112
enum HTMX : HTMLInitializable {
@@ -50,28 +51,11 @@ public extension HTMLElementAttribute {
5051
case ws(WebSocket?)
5152

5253
// MARK: init
53-
public init?(key: String, arguments: LabeledExprListSyntax) {
54+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
5455
let expression:ExprSyntax = arguments.first!.expression
55-
func string() -> String? { expression.stringLiteral?.string }
56-
func boolean() -> Bool? { expression.booleanLiteral?.literal.text == "true" }
57-
func enumeration<T : HTMLInitializable>() -> T? {
58-
if let function:FunctionCallExprSyntax = expression.functionCall, let member:MemberAccessExprSyntax = function.calledExpression.memberAccess {
59-
return T(key: member.declName.baseName.text, arguments: function.arguments)
60-
}
61-
if let member:MemberAccessExprSyntax = expression.memberAccess {
62-
return T(key: member.declName.baseName.text, arguments: arguments)
63-
}
64-
return nil
65-
}
66-
func int() -> Int? {
67-
guard let s:String = expression.integerLiteral?.literal.text else { return nil }
68-
return Int(s)
69-
}
70-
func array_string() -> [String] { expression.array?.elements.compactMap({ $0.expression.stringLiteral?.string }) ?? [] }
71-
func float() -> Float? {
72-
guard let s:String = expression.integerLiteral?.literal.text ?? expression.floatLiteral?.literal.text else { return nil }
73-
return Float(s)
74-
}
56+
func string() -> String? { expression.string(context: context, key: key) }
57+
func boolean() -> Bool? { expression.boolean(context: context, key: key) }
58+
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: arguments) }
7559
switch key {
7660
case "boost": self = .boost(enumeration())
7761
case "confirm": self = .confirm(string())

Sources/HTMLKitUtilities/HTMXAttributes.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import SwiftSyntax
9+
import SwiftSyntaxMacros
910

1011
public extension HTMLElementAttribute.HTMX {
1112
// MARK: TrueOrFalse
@@ -121,9 +122,9 @@ public extension HTMLElementAttribute.HTMX {
121122
case not([String])
122123
case list([String])
123124

124-
public init?(key: String, arguments: LabeledExprListSyntax) {
125+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
125126
let expression:ExprSyntax = arguments.first!.expression
126-
func array_string() -> [String] { expression.array?.elements.compactMap({ $0.expression.stringLiteral?.string }) ?? [] }
127+
func array_string() -> [String] { expression.array_string(context: context, key: key) }
127128
switch key {
128129
case "all": self = .all
129130
case "none": self = .none
@@ -166,22 +167,14 @@ public extension HTMLElementAttribute.HTMX {
166167
case drop, abort, replace
167168
case queue(Queue?)
168169

169-
public init?(key: String, arguments: LabeledExprListSyntax) {
170+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
170171
switch key {
171172
case "drop": self = .drop
172173
case "abort": self = .abort
173174
case "replace": self = .replace
174175
case "queue":
175176
let expression:ExprSyntax = arguments.first!.expression
176-
func enumeration<T : HTMLInitializable>() -> T? {
177-
if let function:FunctionCallExprSyntax = expression.functionCall, let member:MemberAccessExprSyntax = function.calledExpression.memberAccess {
178-
return T(key: member.declName.baseName.text, arguments: function.arguments)
179-
}
180-
if let member:MemberAccessExprSyntax = expression.memberAccess {
181-
return T(key: member.declName.baseName.text, arguments: arguments)
182-
}
183-
return nil
184-
}
177+
func enumeration<T : HTMLInitializable>() -> T? { expression.enumeration(context: context, key: key, arguments: arguments) }
185178
self = .queue(enumeration())
186179
default: return nil
187180
}
@@ -215,7 +208,7 @@ public extension HTMLElementAttribute.HTMX {
215208
case `true`, `false`
216209
case url(String)
217210

218-
public init?(key: String, arguments: LabeledExprListSyntax) {
211+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
219212
switch key {
220213
case "true": self = .true
221214
case "false": self = .false
@@ -245,12 +238,12 @@ public extension HTMLElementAttribute.HTMX {
245238
// MARK: Server Sent Events
246239
public extension HTMLElementAttribute.HTMX {
247240
enum ServerSentEvents : HTMLInitializable {
248-
case connect(String)
249-
case swap(String)
250-
case close(String)
241+
case connect(String?)
242+
case swap(String?)
243+
case close(String?)
251244

252-
public init?(key: String, arguments: LabeledExprListSyntax) {
253-
func string() -> String { arguments.first!.expression.stringLiteral!.string }
245+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
246+
func string() -> String? { arguments.first!.expression.string(context: context, key: key) }
254247
switch key {
255248
case "connect": self = .connect(string())
256249
case "swap": self = .swap(string())
@@ -281,13 +274,13 @@ public extension HTMLElementAttribute.HTMX {
281274
// MARK: WebSocket
282275
public extension HTMLElementAttribute.HTMX {
283276
enum WebSocket : HTMLInitializable {
284-
case connect(String)
285-
case send(Bool)
277+
case connect(String?)
278+
case send(Bool?)
286279

287-
public init?(key: String, arguments: LabeledExprListSyntax) {
280+
public init?(context: some MacroExpansionContext, key: String, arguments: LabeledExprListSyntax) {
288281
let expression:ExprSyntax = arguments.first!.expression
289-
func string() -> String { expression.stringLiteral!.string }
290-
func boolean() -> Bool { expression.booleanLiteral!.literal.text == "true" }
282+
func string() -> String? { expression.string(context: context, key: key) }
283+
func boolean() -> Bool? { expression.boolean(context: context, key: key) }
291284
switch key {
292285
case "connect": self = .connect(string())
293286
case "send": self = .send(boolean())
@@ -305,7 +298,7 @@ public extension HTMLElementAttribute.HTMX {
305298
public var htmlValue : String? {
306299
switch self {
307300
case .connect(let value): return value
308-
case .send(_): return ""
301+
case .send(let value): return value ?? false ? "" : nil
309302
}
310303
}
311304

0 commit comments

Comments
 (0)