Skip to content

Commit f3d74de

Browse files
only the function call is now warned for unsafe interpolation instead of the whole function (which was visually noisy)
1 parent e9842d0 commit f3d74de

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

Sources/GenerateElements/main.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
// generate the html element files using the following command:
99
/*
10-
swiftc main.swift ../HTMLKitUtilities/HTMLEncoding.swift \
10+
swiftc main.swift \
11+
../HTMLKitUtilities/HTMLElementType.swift \
12+
../HTMLKitUtilities/HTMLEncoding.swift \
1113
../HTMLKitUtilities/attributes/HTMLElementAttribute.swift \
1214
../HTMLKitUtilities/attributes/HTMLElementAttributeExtra.swift \
1315
../HTMLKitUtilities/attributes/HTMX.swift \
@@ -18,7 +20,7 @@
1820
#if canImport(Foundation) && GENERATE_ELEMENTS
1921

2022
// Why do we do it this way?
21-
// - The documentation doesn't link correctly if we generate from a macro
23+
// - The documentation doesn't link correctly (or at all) if we generate from a macro
2224

2325
import Foundation
2426

Sources/HTMLKitUtilities/HTMLEncoding.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// The value type the data should be encoded to when returned from the macro.
99
///
1010
/// ### Interpolation Promotion
11+
///
1112
/// Swift HTMLKit tries to [promote](https://github.com/RandomHashTags/swift-htmlkit/blob/94793984763308ef5275dd9f71ea0b5e83fea417/Sources/HTMLKitMacros/HTMLElement.swift#L423) known interpolation at compile time with an equivalent string literal for the best performance, regardless of encoding.
1213
/// It is currently limited due to macro expansions being sandboxed and lexical contexts/AST not being available for the macro argument types.
1314
/// This means referencing content in an html macro won't get promoted to its expected value.
@@ -26,7 +27,7 @@
2627
/// ```swift
2728
/// let string:StaticString = "Test"
2829
/// let _:StaticString = #html(div(string)) // ❌ promotion cannot be applied; StaticString not allowed
29-
/// let _:String = #html(div(string)) // ⚠️ promotion cannot be applied; compiles to "<div>\(string)</div>"
30+
/// let _:String = #html(div(string)) // ⚠️ promotion cannot be applied; compiles to "<div>" + String(describing: string) + "</div>"`
3031
/// ```
3132
///
3233
public enum HTMLEncoding {

Sources/HTMLKitUtilities/HTMLKitUtilities.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import SwiftSyntaxMacros
1111
// MARK: HTMLKitUtilities
1212
public enum HTMLKitUtilities {
1313
public struct ElementData {
14-
public let trailingSlash:Bool
1514
public let encoding:HTMLEncoding
1615
public let globalAttributes:[HTMLElementAttribute]
1716
public let attributes:[String:Any]
1817
public let innerHTML:[CustomStringConvertible]
18+
public let trailingSlash:Bool
1919

2020
init(
2121
_ encoding: HTMLEncoding,
@@ -35,18 +35,21 @@ public enum HTMLKitUtilities {
3535

3636
// MARK: Escape HTML
3737
public extension String {
38-
/// Escapes all occurrences of source-breaking HTML characters
38+
/// Escapes all occurrences of source-breaking HTML characters.
39+
///
3940
/// - Parameters:
40-
/// - escapeAttributes: Whether or not to escape source-breaking HTML attribute characters
41-
/// - Returns: A new `String` escaping source-breaking HTML
41+
/// - escapeAttributes: Whether or not to escape source-breaking HTML attribute characters.
42+
/// - Returns: A new `String` escaping source-breaking HTML.
4243
func escapingHTML(escapeAttributes: Bool) -> String {
4344
var string:String = self
4445
string.escapeHTML(escapeAttributes: escapeAttributes)
4546
return string
4647
}
47-
/// Escapes all occurrences of source-breaking HTML characters
48+
49+
/// Escapes all occurrences of source-breaking HTML characters.
50+
///
4851
/// - Parameters:
49-
/// - escapeAttributes: Whether or not to escape source-breaking HTML attribute characters
52+
/// - escapeAttributes: Whether or not to escape source-breaking HTML attribute characters.
5053
mutating func escapeHTML(escapeAttributes: Bool) {
5154
self.replace("&", with: "&amp;")
5255
self.replace("<", with: "&lt;")
@@ -55,14 +58,17 @@ public extension String {
5558
self.escapeHTMLAttributes()
5659
}
5760
}
58-
/// Escapes all occurrences of source-breaking HTML attribute characters
59-
/// - Returns: A new `String` escaping source-breaking HTML attribute characters
61+
62+
/// Escapes all occurrences of source-breaking HTML attribute characters.
63+
///
64+
/// - Returns: A new `String` escaping source-breaking HTML attribute characters.
6065
func escapingHTMLAttributes() -> String {
6166
var string:String = self
6267
string.escapeHTMLAttributes()
6368
return string
6469
}
65-
/// Escapes all occurrences of source-breaking HTML attribute characters
70+
71+
/// Escapes all occurrences of source-breaking HTML attribute characters.
6672
mutating func escapeHTMLAttributes() {
6773
self.replace("\\\"", with: "&quot;")
6874
self.replace("\"", with: "&quot;")

Sources/HTMLKitUtilities/interpolation/ParseLiteral.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ extension HTMLKitUtilities {
5757
}
5858
string = segments.map({ "\($0)" }).joined()
5959
} else {
60-
warn_interpolation(context: context, node: expression)
60+
if let function:FunctionCallExprSyntax = expression.functionCall {
61+
warn_interpolation(context: context, node: function.calledExpression)
62+
} else {
63+
warn_interpolation(context: context, node: expression)
64+
}
6165
if let member:MemberAccessExprSyntax = expression.memberAccess {
6266
string = "\\(" + member.singleLineDescription + ")"
6367
} else {

Tests/HTMLKitTests/HTMLKitTests.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ import struct Foundation.Data
1818
struct HTMLKitTests {
1919
@Test
2020
func memoryLayout() {
21-
//print("before=\((MemoryLayout<a>.alignment, MemoryLayout<a>.size, MemoryLayout<a>.stride))")
21+
//print("before=\((MemoryLayout<HTMLKitUtilities.ElementData>.alignment, MemoryLayout<HTMLKitUtilities.ElementData>.size, MemoryLayout<HTMLKitUtilities.ElementData>.stride))")
2222
//print("after=\((MemoryLayout<Brother>.alignment, MemoryLayout<Brother>.size, MemoryLayout<Brother>.stride))")
2323
}
2424

2525
struct Brother {
26-
public let tag:String
27-
public var attributes:[HTMLElementAttribute]
28-
public var innerHTML:[CustomStringConvertible]
29-
private var encoding:HTMLEncoding = .string
30-
public var isVoid:Bool
31-
public var trailingSlash:Bool
32-
public var escaped:Bool = false
33-
private var fromMacro:Bool = false
26+
public let encoding:HTMLEncoding
27+
public let globalAttributes:[HTMLElementAttribute]
28+
public let attributes:[String:Any]
29+
public let innerHTML:[CustomStringConvertible]
30+
public let trailingSlash:Bool
3431
}
3532

3633
@Test

0 commit comments

Comments
 (0)