Skip to content

Commit 64bd071

Browse files
no longer get compiler warning about initializer being unused when initializing an element's innerHTML via a closure
1 parent cc196ed commit 64bd071

File tree

2 files changed

+10
-39
lines changed

2 files changed

+10
-39
lines changed

Sources/HTMLKitUtilityMacros/HTMLElements.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ enum HTMLElements : DeclarationMacro {
105105
initializers += "self.trailingSlash = data.trailingSlash\n"
106106
}
107107
initializers += "self.attributes = data.globalAttributes\n"
108+
var builders = ""
108109
for (key, valueType, _) in attributes {
109110
var keyLiteral = key
110111
if keyLiteral.first == "`" {
@@ -119,6 +120,13 @@ enum HTMLElements : DeclarationMacro {
119120
break
120121
}
121122
initializers += "self.\(key) = data.attributes[\"\(keyLiteral)\"] " + value + "\n"
123+
builders += """
124+
@inlinable
125+
public mutating func \(key)(_ value: \(valueType)\(valueType == "Bool" ? "" : "?")) -> Self {
126+
self.\(key) = value
127+
return self
128+
}
129+
"""
122130
}
123131
initializers += "self.innerHTML = data.innerHTML\n"
124132
initializers += "}"
@@ -186,6 +194,7 @@ enum HTMLElements : DeclarationMacro {
186194
render += "}"
187195

188196
string += render
197+
//string += "\n" + builders
189198
string += "\n}"
190199
items.append("\(raw: string)")
191200
}
@@ -208,7 +217,7 @@ enum HTMLElements : DeclarationMacro {
208217
innerHTMLValueType: String,
209218
assignInnerHTML: String
210219
) -> String {
211-
var initializers = "public init(\n"
220+
var initializers = "@discardableResult public init(\n"
212221
initializers += "attributes: [HTMLAttribute] = [],\n"
213222
for (key, valueType, defaultValue) in attributes {
214223
initializers += key + ": " + valueType + defaultValue + ",\n"

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,6 @@ extension StringProtocol {
2929

3030
// MARK: Representations
3131
struct HTMLKitTests {
32-
@Test
33-
func memoryLayout() {
34-
//print("before=\((MemoryLayout<a>.alignment, MemoryLayout<a>.size, MemoryLayout<a>.stride))")
35-
//print("after=\((MemoryLayout<NewA>.alignment, MemoryLayout<NewA>.size, MemoryLayout<NewA>.stride))")
36-
}
37-
38-
public struct NewA : HTMLElement {
39-
public init(_ encoding: HTMLEncoding, _ data: HTMLKitUtilities.ElementData) {
40-
}
41-
42-
public private(set) var encoding:HTMLEncoding = .string
43-
44-
/// Causes the browser to treat the linked URL as a download. Can be used with or without a `filename` value.
45-
///
46-
/// Without a value, the browser will suggest a filename/extension, generated from various sources:
47-
/// - The [`Content-Disposition`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) HTTP header
48-
/// - The final segment in the URL [path](https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname)
49-
/// - The [media type](https://developer.mozilla.org/en-US/docs/Glossary/MIME_type) (from the [`Content-Type`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) header, the start of a [`data:` URL](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data), or [`Blob.type`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/type) for a [`blob:` URL](https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL_static))
50-
public var download:HTMLAttribute.Extra.download? = nil
51-
public var href:String? = nil
52-
public var hrefLang:String? = nil
53-
public let tag:String = "a"
54-
public var type:String? = nil
55-
public var attributes:[HTMLAttribute] = []
56-
public var attributionsrc:[String] = []
57-
public var innerHTML:[CustomStringConvertible & Sendable] = []
58-
public var ping:[String] = []
59-
public var rel:[HTMLAttribute.Extra.rel] = []
60-
public var escaped:Bool = false
61-
public private(set) var fromMacro:Bool = false
62-
public let isVoid:Bool = false
63-
public var referrerPolicy:HTMLAttribute.Extra.referrerpolicy? = nil
64-
public var target:HTMLAttribute.Extra.target? = nil
65-
public var trailingSlash:Bool = false
66-
67-
public var description : String { "" }
68-
}
69-
7032
@Test
7133
func representations() {
7234
let _ = #anyHTML(p())

0 commit comments

Comments
 (0)