Skip to content

Commit 18e998c

Browse files
breaking changes; chunk result types are now formatted better when expanded
- instead of a single line array - minor documentation updates
1 parent 351cc73 commit 18e998c

File tree

4 files changed

+40
-37
lines changed

4 files changed

+40
-37
lines changed

Sources/HTMLKitParse/ExpandHTMLMacro.swift

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ extension HTMLKitUtilities {
77
public static func expandHTMLMacro(context: HTMLExpansionContext) throws -> ExprSyntax {
88
var context = context
99
let (string, encoding) = expandMacro(context: &context)
10-
let encodingResult = encodingResult(context: context, node: context.expansion, string: string, for: encoding)
11-
let expandedResult = representationResult(encoding: encoding, encodedResult: encodingResult, resultType: context.resultType)
10+
let encodingResult = encoding.result(context: context, node: context.expansion, string: string)
11+
let expandedResult = context.resultType.result(encoding: encoding, encodedResult: encodingResult)
1212
return "\(raw: expandedResult)"
1313
}
1414

@@ -24,14 +24,13 @@ extension HTMLKitUtilities {
2424
}
2525

2626
// MARK: Encoding result
27-
extension HTMLKitUtilities {
28-
static func encodingResult(
27+
extension HTMLEncoding {
28+
public func result(
2929
context: HTMLExpansionContext,
3030
node: MacroExpansionExprSyntax,
31-
string: String,
32-
for encoding: HTMLEncoding
31+
string: String
3332
) -> String {
34-
switch encoding {
33+
switch self {
3534
case .utf8Bytes:
3635
guard hasNoInterpolation(context, node, string) else { return "" }
3736
return bytes([UInt8](string.utf8))
@@ -56,15 +55,15 @@ extension HTMLKitUtilities {
5655
return encoded.replacingOccurrences(of: "$0", with: string)
5756
}
5857
}
59-
private static func bytes<T: FixedWidthInteger>(_ bytes: [T]) -> String {
58+
private func bytes<T: FixedWidthInteger>(_ bytes: [T]) -> String {
6059
var string = "["
6160
for b in bytes {
6261
string += "\(b),"
6362
}
6463
string.removeLast()
6564
return string.isEmpty ? "[]" : string + "]"
6665
}
67-
private static func hasNoInterpolation(_ context: HTMLExpansionContext, _ node: MacroExpansionExprSyntax, _ string: String) -> Bool {
66+
private func hasNoInterpolation(_ context: HTMLExpansionContext, _ node: MacroExpansionExprSyntax, _ string: String) -> Bool {
6867
guard string.firstRange(of: try! Regex("\\((.*)\\)")) == nil else {
6968
if !context.ignoresCompilerWarnings {
7069
context.diagnose(Diagnostic(node: node, message: DiagnosticMsg(id: "interpolationNotAllowedForDataType", message: "String Interpolation is not allowed for this data type. Runtime values get converted to raw text, which is not the intended result.")))
@@ -76,13 +75,12 @@ extension HTMLKitUtilities {
7675
}
7776

7877
// MARK: Representation results
79-
extension HTMLKitUtilities {
80-
static func representationResult(
78+
extension HTMLExpansionResultTypeAST {
79+
public func result(
8180
encoding: HTMLEncoding,
82-
encodedResult: String,
83-
resultType: HTMLExpansionResultTypeAST
81+
encodedResult: String
8482
) -> String {
85-
switch resultType {
83+
switch self {
8684
case .literal:
8785
if encoding == .string {
8886
return literal(encodedResult: encodedResult)
@@ -94,12 +92,13 @@ extension HTMLKitUtilities {
9492
// TODO: show compiler diagnostic
9593
}*/
9694
case .chunks(let optimized, let chunkSize):
97-
return "[" + chunks(encoding: encoding, encodedResult: encodedResult, async: false, optimized: optimized, chunkSize: chunkSize).joined(separator: ", ") + "]"
95+
let slices = chunks(encoding: encoding, encodedResult: encodedResult, async: false, optimized: optimized, chunkSize: chunkSize).joined(separator: ",\n")
96+
return "[" + (slices.isEmpty ? "" : "\n\(slices)\n") + "]"
9897
#if compiler(>=6.2)
9998
case .chunksInline(let optimized, let chunkSize):
10099
let typeAnnotation:String = "String" // TODO: fix
101-
let chunks = chunks(encoding: encoding, encodedResult: encodedResult, async: false, optimized: optimized, chunkSize: chunkSize).joined(separator: ", ")
102-
return "InlineArray<\(chunks.count), \(typeAnnotation)>([\(chunks)])"
100+
let slices = chunks(encoding: encoding, encodedResult: encodedResult, async: false, optimized: optimized, chunkSize: chunkSize).joined(separator: ",\n")
101+
return "InlineArray<\(chunks.count), \(typeAnnotation)>([" + (slices.isEmpty ? "" : "\n\(slices)\n") + "])"
103102
#endif
104103
case .stream(let optimized, let chunkSize):
105104
return streamed(
@@ -129,11 +128,11 @@ extension HTMLKitUtilities {
129128
}
130129

131130
// MARK: Literal
132-
extension HTMLKitUtilities {
133-
static var interpolationRegex: Regex<AnyRegexOutput> {
131+
extension HTMLExpansionResultTypeAST {
132+
public var interpolationRegex: Regex<AnyRegexOutput> {
134133
try! Regex.init(#"( \+ String\(describing: [\x00-\x2A\x2C-\xFF]+\) \+ )"#)
135134
}
136-
static func literal(encodedResult: String) -> String {
135+
public func literal(encodedResult: String) -> String {
137136
var interpolation = encodedResult.matches(of: interpolationRegex)
138137
guard !interpolation.isEmpty else {
139138
return encodedResult
@@ -170,8 +169,8 @@ extension HTMLKitUtilities {
170169
}
171170

172171
// MARK: Optimized literal
173-
extension HTMLKitUtilities {
174-
static func optimizedLiteral(encodedResult: String) -> String {
172+
extension HTMLExpansionResultTypeAST {
173+
public func optimizedLiteral(encodedResult: String) -> String {
175174
var interpolation = encodedResult.matches(of: interpolationRegex)
176175
guard !interpolation.isEmpty else {
177176
return encodedResult
@@ -195,7 +194,7 @@ extension HTMLKitUtilities {
195194
}
196195
return "HTMLOptimizedLiteral(reserveCapacity: \(reserveCapacity)).render((\n\(values.joined(separator: ",\n"))\n))"
197196
}
198-
static func normalizeInterpolation(_ value: Substring, withQuotationMarks: Bool) -> String {
197+
public func normalizeInterpolation(_ value: Substring, withQuotationMarks: Bool) -> String {
199198
var value = value
200199
value.removeFirst(22) // ` + String(describing: `.count
201200
value.removeLast(3) // ` + `.count
@@ -209,8 +208,8 @@ extension HTMLKitUtilities {
209208
}
210209

211210
// MARK: Chunks
212-
extension HTMLKitUtilities {
213-
static func chunks(
211+
extension HTMLExpansionResultTypeAST {
212+
public func chunks(
214213
encoding: HTMLEncoding,
215214
encodedResult: String,
216215
async: Bool,
@@ -279,8 +278,8 @@ extension HTMLKitUtilities {
279278
}
280279

281280
// MARK: Streamed
282-
extension HTMLKitUtilities {
283-
static func streamed(
281+
extension HTMLExpansionResultTypeAST {
282+
public func streamed(
284283
encoding: HTMLEncoding,
285284
encodedResult: String,
286285
async: Bool,

Sources/HTMLKitParse/ParseArguments.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ extension HTMLKitUtilities {
2626
context.key = key
2727
switch key {
2828
case "encoding":
29-
context.encoding = parseEncoding(expr: child.expression) ?? .string
29+
context.encoding = .parse(expr: child.expression) ?? .string
3030
case "resultType":
31-
context.resultType = parseRepresentation(expr: child.expression) ?? .literal
31+
context.resultType = .parse(expr: child.expression) ?? .literal
3232
case "lookupFiles":
3333
guard let array = child.expression.array?.elements else {
3434
context.diagnose(DiagnosticMsg.expectedArrayExpr(expr: child.expression))

Sources/HTMLKitParse/ParseData.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ extension HTMLKitUtilities {
3838
guard let child = e.labeled else { continue }
3939
if let key = child.label?.text {
4040
switch key {
41-
case "encoding": context.encoding = parseEncoding(expr: child.expression) ?? .string
42-
case "resultType": context.resultType = parseRepresentation(expr: child.expression) ?? .literal
41+
case "encoding": context.encoding = .parse(expr: child.expression) ?? .string
42+
case "resultType": context.resultType = .parse(expr: child.expression) ?? .literal
4343
case "minify": context.minify = child.expression.boolean(context) ?? false
4444
default: break
4545
}
@@ -57,9 +57,11 @@ extension HTMLKitUtilities {
5757
innerHTML.replace(HTMLKitUtilities.lineFeedPlaceholder, with: "\\n")
5858
return innerHTML
5959
}
60+
}
6061

61-
// MARK: Parse Encoding
62-
public static func parseEncoding(expr: some ExprSyntaxProtocol) -> HTMLEncoding? {
62+
// MARK: Parse encoding
63+
extension HTMLEncoding {
64+
public static func parse(expr: some ExprSyntaxProtocol) -> HTMLEncoding? {
6365
switch expr.kind {
6466
case .memberAccessExpr:
6567
return HTMLEncoding(rawValue: expr.memberAccess!.declName.baseName.text)
@@ -82,9 +84,11 @@ extension HTMLKitUtilities {
8284
return nil
8385
}
8486
}
87+
}
8588

86-
// MARK: Parse Representation
87-
public static func parseRepresentation(expr: ExprSyntax) -> HTMLExpansionResultTypeAST? {
89+
// MARK: Parse result type
90+
extension HTMLExpansionResultTypeAST {
91+
public static func parse(expr: some ExprSyntaxProtocol) -> Self? {
8892
switch expr.kind {
8993
case .memberAccessExpr:
9094
switch expr.memberAccess!.declName.baseName.text {

Sources/HTMLKitUtilities/HTMLExpansionResultType.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public enum HTMLExpansionResultType: Sendable {
4545
/// - optimized: Whether or not to use optimized literals. Default is `true`.
4646
/// - chunkSize: The maximum size of an individual literal. Default is `1024`.
4747
/// - Returns: An `AsyncStream` of encoded literals of length up-to `chunkSize`.
48-
/// - Warning: The values are yielded synchronously.
48+
/// - Warning: The values are yielded immediately.
4949
case stream(
5050
optimized: Bool = true,
5151
chunkSize: Int = 1024
@@ -58,7 +58,7 @@ public enum HTMLExpansionResultType: Sendable {
5858
/// - chunkSize: The maximum size of an individual literal. Default is `1024`.
5959
/// - afterYield: Work to execute after yielding a result. The `Int` closure parameter is the index of the yielded result.
6060
/// - Returns: An `AsyncStream` of encoded literals of length up-to `chunkSize`.
61-
/// - Warning: The values are yielded synchronously in a new `Task`. Populate `afterYield` with async work to make it completely asynchronous.
61+
/// - Warning: The values are yielded immediately in a new `Task`. Populate `afterYield` with async work to make it yield asynchronously.
6262
case streamAsync(
6363
optimized: Bool = true,
6464
chunkSize: Int = 1024,

0 commit comments

Comments
 (0)