@@ -69,36 +69,26 @@ extension HTMLKitUtilities {
6969 return " \( raw: encodingResult ( context: context, node: context. expansion, string: string, for: encoding) ) "
7070 }
7171 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- }
8272 func bytes< T: FixedWidthInteger > ( _ bytes: [ T ] ) -> String {
8373 return " [ " + bytes. map ( { " \( $0) " } ) . joined ( separator: " , " ) + " ] "
8474 }
8575 switch encoding {
8676 case . utf8Bytes:
87- guard hasNoInterpolation ( ) else { return " " }
77+ guard hasNoInterpolation ( context , node , string ) else { return " " }
8878 return bytes ( [ UInt8] ( string. utf8) )
8979 case . utf16Bytes:
90- guard hasNoInterpolation ( ) else { return " " }
80+ guard hasNoInterpolation ( context , node , string ) else { return " " }
9181 return bytes ( [ UInt16] ( string. utf16) )
9282 case . utf8CString:
93- guard hasNoInterpolation ( ) else { return " " }
83+ guard hasNoInterpolation ( context , node , string ) else { return " " }
9484 return " \( string. utf8CString) "
9585
9686 case . foundationData:
97- guard hasNoInterpolation ( ) else { return " " }
87+ guard hasNoInterpolation ( context , node , string ) else { return " " }
9888 return " Data( \( bytes ( [ UInt8] ( string. utf8) ) ) ) "
9989
10090 case . byteBuffer:
101- guard hasNoInterpolation ( ) else { return " " }
91+ guard hasNoInterpolation ( context , node , string ) else { return " " }
10292 return " ByteBuffer(bytes: \( bytes ( [ UInt8] ( string. utf8) ) ) ) "
10393
10494 case . string:
@@ -107,14 +97,23 @@ extension HTMLKitUtilities {
10797 return encoded. replacingOccurrences ( of: " $0 " , with: string)
10898 }
10999 }
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+ }
110109
111110 // MARK: Parse Arguments
112111 public static func parseArguments(
113112 context: HTMLExpansionContext ,
114113 otherAttributes: [ String : String ] = [ : ]
115114 ) -> ElementData {
116115 var context = context
117- var global_attributes : [ HTMLAttribute ] = [ ]
116+ var globalAttributes : [ HTMLAttribute ] = [ ]
118117 var attributes : [ String : Sendable ] = [ : ]
119118 var innerHTML : [ CustomStringConvertible & Sendable ] = [ ]
120119 var trailingSlash : Bool = false
@@ -129,7 +128,7 @@ extension HTMLKitUtilities {
129128 case " lookupFiles " :
130129 context. lookupFiles = Set ( child. expression. array!. elements. compactMap ( { $0. expression. stringLiteral? . string ( encoding: context. encoding) } ) )
131130 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)
133132 default :
134133 context. key = otherAttributes [ key] ?? key
135134 if let test = HTMLAttribute . Extra. parse ( context: context, expr: child. expression) {
@@ -141,8 +140,7 @@ extension HTMLKitUtilities {
141140 case . int( let i) : attributes [ key] = i
142141 case . float( let f) : attributes [ key] = f
143142 case . array:
144- let escaped : LiteralReturnType = literal. escapeArray ( )
145- switch escaped {
143+ switch literal. escapeArray ( ) {
146144 case . array( let a) : attributes [ key] = a
147145 default : break
148146 }
@@ -169,27 +167,30 @@ extension HTMLKitUtilities {
169167 }
170168 }
171169 }
172- return ElementData ( context. encoding, global_attributes , attributes, innerHTML, trailingSlash)
170+ return ElementData ( context. encoding, globalAttributes , attributes, innerHTML, trailingSlash)
173171 }
174172
175173 // MARK: Parse Encoding
176174 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!
180180 switch function. calledExpression. as ( MemberAccessExprSyntax . self) ? . declName. baseName. text {
181181 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 }
183183 if function. arguments. count == 1 {
184184 return . custom( logic)
185185 } else {
186186 return . custom( logic, stringDelimiter: function. arguments. last!. expression. stringLiteral!. string ( encoding: . string) )
187187 }
188188 default :
189- break
189+ return nil
190190 }
191+ default :
192+ return nil
191193 }
192- return nil
193194 }
194195
195196 // MARK: Parse Global Attributes
@@ -328,13 +329,13 @@ extension HTMLKitUtilities {
328329
329330// MARK: Misc
330331extension ExprSyntax {
331- package func string( context: HTMLExpansionContext ) -> String ? {
332+ package func string( _ context: HTMLExpansionContext ) -> String ? {
332333 return HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key)
333334 }
334- package func boolean( context: HTMLExpansionContext ) -> Bool ? {
335+ package func boolean( _ context: HTMLExpansionContext ) -> Bool ? {
335336 booleanLiteral? . literal. text == " true "
336337 }
337- package func enumeration< T : HTMLParsable > ( context: HTMLExpansionContext ) -> T ? {
338+ package func enumeration< T : HTMLParsable > ( _ context: HTMLExpansionContext ) -> T ? {
338339 if let function = functionCall, let member = function. calledExpression. memberAccess {
339340 var c = context
340341 c. key = member. declName. baseName. text
@@ -348,28 +349,28 @@ extension ExprSyntax {
348349 }
349350 return nil
350351 }
351- package func int( context: HTMLExpansionContext ) -> Int ? {
352+ package func int( _ context: HTMLExpansionContext ) -> Int ? {
352353 guard let s = HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key) else { return nil }
353354 return Int ( s)
354355 }
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) } )
357358 }
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) } )
360361 }
361- package func dictionaryStringString( context: HTMLExpansionContext ) -> [ String : String ] {
362+ package func dictionaryStringString( _ context: HTMLExpansionContext ) -> [ String : String ] {
362363 var d : [ String : String ] = [ : ]
363364 if let elements = dictionary? . content. as ( DictionaryElementListSyntax . self) {
364365 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) {
366367 d [ key] = value
367368 }
368369 }
369370 }
370371 return d
371372 }
372- package func float( context: HTMLExpansionContext ) -> Float ? {
373+ package func float( _ context: HTMLExpansionContext ) -> Float ? {
373374 guard let s = HTMLKitUtilities . parseLiteralValue ( context: context, expression: self ) ? . value ( key: context. key) else { return nil }
374375 return Float ( s)
375376 }
@@ -391,11 +392,11 @@ package struct DiagnosticMsg : DiagnosticMessage, FixItMessage {
391392
392393// MARK: HTMLExpansionContext
393394extension 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 ) }
401402}
0 commit comments