@@ -69,36 +69,26 @@ extension HTMLKitUtilities {
69
69
return " \( raw: encodingResult ( context: context, node: context. expansion, string: string, for: encoding) ) "
70
70
}
71
71
private static func encodingResult( context: HTMLExpansionContext , node: MacroExpansionExprSyntax , string: String , for encoding: HTMLEncoding ) -> String {
72
- func hasNoInterpolation( ) -> Bool {
73
- let hasInterpolation : Bool = !string. ranges ( of: try ! Regex ( " \\ ((.*) \\ ) " ) ) . isEmpty
74
- guard !hasInterpolation else {
75
- if !context. ignoresCompilerWarnings {
76
- context. 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 expected result. " ) ) )
77
- }
78
- return false
79
- }
80
- return true
81
- }
82
72
func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
83
73
return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
84
74
}
85
75
switch encoding {
86
76
case . utf8Bytes:
87
- guard hasNoInterpolation ( ) else { return " " }
77
+ guard hasNoInterpolation ( context , node , string ) else { return " " }
88
78
return bytes ( [ UInt8] ( string. utf8) )
89
79
case . utf16Bytes:
90
- guard hasNoInterpolation ( ) else { return " " }
80
+ guard hasNoInterpolation ( context , node , string ) else { return " " }
91
81
return bytes ( [ UInt16] ( string. utf16) )
92
82
case . utf8CString:
93
- guard hasNoInterpolation ( ) else { return " " }
83
+ guard hasNoInterpolation ( context , node , string ) else { return " " }
94
84
return " \( string. utf8CString) "
95
85
96
86
case . foundationData:
97
- guard hasNoInterpolation ( ) else { return " " }
87
+ guard hasNoInterpolation ( context , node , string ) else { return " " }
98
88
return " Data( \( bytes ( [ UInt8] ( string. utf8) ) ) ) "
99
89
100
90
case . byteBuffer:
101
- guard hasNoInterpolation ( ) else { return " " }
91
+ guard hasNoInterpolation ( context , node , string ) else { return " " }
102
92
return " ByteBuffer(bytes: \( bytes ( [ UInt8] ( string. utf8) ) ) ) "
103
93
104
94
case . string:
@@ -107,14 +97,23 @@ extension HTMLKitUtilities {
107
97
return encoded. replacingOccurrences ( of: " $0 " , with: string)
108
98
}
109
99
}
100
+ private static func hasNoInterpolation( _ context: HTMLExpansionContext , _ node: MacroExpansionExprSyntax , _ string: String ) -> Bool {
101
+ guard string. firstRange ( of: try ! Regex ( " \\ ((.*) \\ ) " ) ) == nil else {
102
+ if !context. ignoresCompilerWarnings {
103
+ context. 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. " ) ) )
104
+ }
105
+ return false
106
+ }
107
+ return true
108
+ }
110
109
111
110
// MARK: Parse Arguments
112
111
public static func parseArguments(
113
112
context: HTMLExpansionContext ,
114
113
otherAttributes: [ String : String ] = [ : ]
115
114
) -> ElementData {
116
115
var context = context
117
- var global_attributes : [ HTMLAttribute ] = [ ]
116
+ var globalAttributes : [ HTMLAttribute ] = [ ]
118
117
var attributes : [ String : Sendable ] = [ : ]
119
118
var innerHTML : [ CustomStringConvertible & Sendable ] = [ ]
120
119
var trailingSlash : Bool = false
@@ -129,7 +128,7 @@ extension HTMLKitUtilities {
129
128
case " lookupFiles " :
130
129
context. lookupFiles = Set ( child. expression. array!. elements. compactMap ( { $0. expression. stringLiteral? . string ( encoding: context. encoding) } ) )
131
130
case " attributes " :
132
- ( global_attributes , trailingSlash) = parseGlobalAttributes ( context: context, array: child. expression. array!. elements)
131
+ ( globalAttributes , trailingSlash) = parseGlobalAttributes ( context: context, array: child. expression. array!. elements)
133
132
default :
134
133
context. key = otherAttributes [ key] ?? key
135
134
if let test = HTMLAttribute . Extra. parse ( context: context, expr: child. expression) {
@@ -141,8 +140,7 @@ extension HTMLKitUtilities {
141
140
case . int( let i) : attributes [ key] = i
142
141
case . float( let f) : attributes [ key] = f
143
142
case . array:
144
- let escaped : LiteralReturnType = literal. escapeArray ( )
145
- switch escaped {
143
+ switch literal. escapeArray ( ) {
146
144
case . array( let a) : attributes [ key] = a
147
145
default : break
148
146
}
@@ -169,27 +167,30 @@ extension HTMLKitUtilities {
169
167
}
170
168
}
171
169
}
172
- return ElementData ( context. encoding, global_attributes , attributes, innerHTML, trailingSlash)
170
+ return ElementData ( context. encoding, globalAttributes , attributes, innerHTML, trailingSlash)
173
171
}
174
172
175
173
// MARK: Parse Encoding
176
174
public static func parseEncoding( expression: ExprSyntax ) -> HTMLEncoding ? {
177
- if let key = expression. memberAccess? . declName. baseName. text {
178
- return HTMLEncoding ( rawValue: key)
179
- } else if let function = expression. functionCall {
175
+ switch expression. kind {
176
+ case . memberAccessExpr:
177
+ return HTMLEncoding ( rawValue: expression. memberAccess!. declName. baseName. text)
178
+ case . functionCallExpr:
179
+ let function = expression. functionCall!
180
180
switch function. calledExpression. as ( MemberAccessExprSyntax . self) ? . declName. baseName. text {
181
181
case " custom " :
182
- guard let logic = function. arguments. first? . expression. stringLiteral? . string ( encoding: . string) else { break }
182
+ guard let logic = function. arguments. first? . expression. stringLiteral? . string ( encoding: . string) else { return nil }
183
183
if function. arguments. count == 1 {
184
184
return . custom( logic)
185
185
} else {
186
186
return . custom( logic, stringDelimiter: function. arguments. last!. expression. stringLiteral!. string ( encoding: . string) )
187
187
}
188
188
default :
189
- break
189
+ return nil
190
190
}
191
+ default :
192
+ return nil
191
193
}
192
- return nil
193
194
}
194
195
195
196
// MARK: Parse Global Attributes
@@ -328,13 +329,13 @@ extension HTMLKitUtilities {
328
329
329
330
// MARK: Misc
330
331
extension ExprSyntax {
331
- package func string( context: HTMLExpansionContext ) -> String ? {
332
+ package func string( _ context: HTMLExpansionContext ) -> String ? {
332
333
return HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key)
333
334
}
334
- package func boolean( context: HTMLExpansionContext ) -> Bool ? {
335
+ package func boolean( _ context: HTMLExpansionContext ) -> Bool ? {
335
336
booleanLiteral? . literal. text == " true "
336
337
}
337
- package func enumeration< T : HTMLParsable > ( context: HTMLExpansionContext ) -> T ? {
338
+ package func enumeration< T : HTMLParsable > ( _ context: HTMLExpansionContext ) -> T ? {
338
339
if let function = functionCall, let member = function. calledExpression. memberAccess {
339
340
var c = context
340
341
c. key = member. declName. baseName. text
@@ -348,28 +349,28 @@ extension ExprSyntax {
348
349
}
349
350
return nil
350
351
}
351
- package func int( context: HTMLExpansionContext ) -> Int ? {
352
+ package func int( _ context: HTMLExpansionContext ) -> Int ? {
352
353
guard let s = HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key) else { return nil }
353
354
return Int ( s)
354
355
}
355
- package func arrayString( context: HTMLExpansionContext ) -> [ String ] ? {
356
- array? . elements. compactMap ( { $0. expression. string ( context: context ) } )
356
+ package func arrayString( _ context: HTMLExpansionContext ) -> [ String ] ? {
357
+ array? . elements. compactMap ( { $0. expression. string ( context) } )
357
358
}
358
- package func arrayEnumeration< T: HTMLParsable > ( context: HTMLExpansionContext ) -> [ T ] ? {
359
- array? . elements. compactMap ( { $0. expression. enumeration ( context: context ) } )
359
+ package func arrayEnumeration< T: HTMLParsable > ( _ context: HTMLExpansionContext ) -> [ T ] ? {
360
+ array? . elements. compactMap ( { $0. expression. enumeration ( context) } )
360
361
}
361
- package func dictionaryStringString( context: HTMLExpansionContext ) -> [ String : String ] {
362
+ package func dictionaryStringString( _ context: HTMLExpansionContext ) -> [ String : String ] {
362
363
var d : [ String : String ] = [ : ]
363
364
if let elements = dictionary? . content. as ( DictionaryElementListSyntax . self) {
364
365
for element in elements {
365
- if let key = element. key. string ( context: context ) , let value = element. value. string ( context : context) {
366
+ if let key = element. key. string ( context) , let value = element. value. string ( context) {
366
367
d [ key] = value
367
368
}
368
369
}
369
370
}
370
371
return d
371
372
}
372
- package func float( context: HTMLExpansionContext ) -> Float ? {
373
+ package func float( _ context: HTMLExpansionContext ) -> Float ? {
373
374
guard let s = HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key) else { return nil }
374
375
return Float ( s)
375
376
}
@@ -391,11 +392,11 @@ package struct DiagnosticMsg : DiagnosticMessage, FixItMessage {
391
392
392
393
// MARK: HTMLExpansionContext
393
394
extension HTMLExpansionContext {
394
- func string( ) -> String ? { expression? . string ( context : self ) }
395
- func boolean( ) -> Bool ? { expression? . boolean ( context : self ) }
396
- func enumeration< T : HTMLParsable > ( ) -> T ? { expression? . enumeration ( context : self ) }
397
- func int( ) -> Int ? { expression? . int ( context : self ) }
398
- func float( ) -> Float ? { expression? . float ( context : self ) }
399
- func arrayString( ) -> [ String ] ? { expression? . arrayString ( context : self ) }
400
- func arrayEnumeration< T: HTMLParsable > ( ) -> [ T ] ? { expression? . arrayEnumeration ( context : self ) }
395
+ func string( ) -> String ? { expression? . string ( self ) }
396
+ func boolean( ) -> Bool ? { expression? . boolean ( self ) }
397
+ func enumeration< T : HTMLParsable > ( ) -> T ? { expression? . enumeration ( self ) }
398
+ func int( ) -> Int ? { expression? . int ( self ) }
399
+ func float( ) -> Float ? { expression? . float ( self ) }
400
+ func arrayString( ) -> [ String ] ? { expression? . arrayString ( self ) }
401
+ func arrayEnumeration< T: HTMLParsable > ( ) -> [ T ] ? { expression? . arrayEnumeration ( self ) }
401
402
}
0 commit comments