@@ -11,7 +11,7 @@ import SwiftSyntaxMacros
1111
1212public extension HTMLKitUtilities {
1313 // MARK: Parse Arguments
14- static func parse_arguments (
14+ static func parseArguments (
1515 context: some MacroExpansionContext ,
1616 children: SyntaxChildren ,
1717 otherAttributes: [ String : String ] = [ : ]
@@ -24,7 +24,7 @@ public extension HTMLKitUtilities {
2424 var lookupFiles : Set < String > = [ ]
2525 for element in children {
2626 if let child: LabeledExprSyntax = element. labeled {
27- if var key: String = child. label? . text {
27+ if let key: String = child. label? . text {
2828 if key == " encoding " {
2929 if let key: String = child. expression. memberAccess? . declName. baseName. text {
3030 encoding = HTMLEncoding ( rawValue: key) ?? . string
@@ -34,12 +34,10 @@ public extension HTMLKitUtilities {
3434 } else if key == " lookupFiles " {
3535 lookupFiles = Set ( child. expression. array!. elements. compactMap ( { $0. expression. stringLiteral? . string } ) )
3636 } else if key == " attributes " {
37- ( global_attributes, trailingSlash) = parse_global_attributes ( context: context, array: child. expression. array!. elements, lookupFiles: lookupFiles)
37+ ( global_attributes, trailingSlash) = parseGlobalAttributes ( context: context, array: child. expression. array!. elements, lookupFiles: lookupFiles)
3838 } else {
3939 var target_key : String = key
40- if key == " acceptCharset " {
41- key = " accept-charset "
42- } else if let target: String = otherAttributes [ key] {
40+ if let target: String = otherAttributes [ key] {
4341 target_key = target
4442 }
4543 if let test: any HTMLInitializable = HTMLElementAttribute . Extra. parse ( context: context, key: target_key, expr: child. expression) {
@@ -55,15 +53,15 @@ public extension HTMLKitUtilities {
5553 }
5654 }
5755 // inner html
58- } else if let inner_html: CustomStringConvertible = parse_inner_html ( context: context, child: child, lookupFiles: lookupFiles) {
56+ } else if let inner_html: CustomStringConvertible = parseInnerHTML ( context: context, child: child, lookupFiles: lookupFiles) {
5957 innerHTML. append ( inner_html)
6058 }
6159 }
6260 }
6361 return ElementData ( encoding, global_attributes, attributes, innerHTML, trailingSlash)
6462 }
6563 // MARK: Parse Global Attributes
66- static func parse_global_attributes (
64+ static func parseGlobalAttributes (
6765 context: some MacroExpansionContext ,
6866 array: ArrayElementListSyntax ,
6967 lookupFiles: Set < String >
@@ -95,11 +93,9 @@ public extension HTMLKitUtilities {
9593 }
9694 return ( attributes, trailingSlash)
9795 }
98- static func global_attribute_already_defined( context: some MacroExpansionContext , attribute: String , node: some SyntaxProtocol ) {
99- context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " globalAttributeAlreadyDefined " , message: " Global attribute \" " + attribute + " \" is already defined. " ) ) )
100- }
96+
10197 // MARK: Parse innerHTML
102- static func parse_inner_html (
98+ static func parseInnerHTML (
10399 context: some MacroExpansionContext ,
104100 child: LabeledExprSyntax ,
105101 lookupFiles: Set < String >
@@ -115,16 +111,6 @@ public extension HTMLKitUtilities {
115111 return nil
116112 }
117113 }
118- static func unallowed_expression( context: some MacroExpansionContext , node: LabeledExprSyntax ) {
119- context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " unallowedExpression " , message: " String Interpolation is required when encoding runtime values. " ) , fixIts: [
120- FixIt ( message: DiagnosticMsg ( id: " useStringInterpolation " , message: " Use String Interpolation. " ) , changes: [
121- FixIt . Change. replace (
122- oldNode: Syntax ( node) ,
123- newNode: Syntax ( StringLiteralExprSyntax ( content: " \\ ( \( node) ) " ) )
124- )
125- ] )
126- ] ) )
127- }
128114
129115 // MARK: Parse element
130116 static func parse_element( context: some MacroExpansionContext , expr: ExprSyntax ) -> HTMLElement ? {
@@ -163,7 +149,7 @@ public extension HTMLKitUtilities {
163149 interpolation = stringLiteral. segments. compactMap ( { $0. as ( ExpressionSegmentSyntax . self) } )
164150 }
165151 for expr in interpolation {
166- string. replace ( " \( expr) " , with: promote_interpolation ( context: context, remaining_interpolation: & remaining_interpolation, expr: expr, lookupFiles: lookupFiles) )
152+ string. replace ( " \( expr) " , with: promoteInterpolation ( context: context, remaining_interpolation: & remaining_interpolation, expr: expr, lookupFiles: lookupFiles) )
167153 }
168154 if remaining_interpolation > 0 {
169155 warn_interpolation ( context: context, node: expression, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
@@ -178,8 +164,55 @@ public extension HTMLKitUtilities {
178164 }
179165 return returnType
180166 }
167+ // MARK: Promote Interpolation
168+ static func promoteInterpolation(
169+ context: some MacroExpansionContext ,
170+ remaining_interpolation: inout Int ,
171+ expr: ExpressionSegmentSyntax ,
172+ lookupFiles: Set < String >
173+ ) -> String {
174+ var string : String = " \( expr) "
175+ guard let expression: ExprSyntax = expr. expressions. first? . expression else { return string }
176+ if let stringLiteral: StringLiteralExprSyntax = expression. stringLiteral {
177+ let segments : StringLiteralSegmentListSyntax = stringLiteral. segments
178+ if segments. count ( where: { $0. is ( StringSegmentSyntax . self) } ) == segments. count {
179+ remaining_interpolation = 0
180+ string = segments. map ( { $0. as ( StringSegmentSyntax . self) !. content. text } ) . joined ( )
181+ } else {
182+ string = " "
183+ for segment in segments {
184+ if let literal: String = segment. as ( StringSegmentSyntax . self) ? . content. text {
185+ string += literal
186+ } else if let interpolation: ExpressionSegmentSyntax = segment. as ( ExpressionSegmentSyntax . self) {
187+ let promoted : String = promoteInterpolation ( context: context, remaining_interpolation: & remaining_interpolation, expr: interpolation, lookupFiles: lookupFiles)
188+ if " \( interpolation) " == promoted {
189+ //string += "\\(\"\(promoted)\".escapingHTML(escapeAttributes: true))"
190+ string += " \( promoted) "
191+ warn_interpolation ( context: context, node: interpolation, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
192+ } else {
193+ string += promoted
194+ }
195+ } else {
196+ //string += "\\(\"\(segment)\".escapingHTML(escapeAttributes: true))"
197+ warn_interpolation ( context: context, node: segment, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
198+ string += " \( segment) "
199+ }
200+ }
201+ }
202+ } else if let fix: String = expression. integerLiteral? . literal. text ?? expression. floatLiteral? . literal. text {
203+ let target : String = " \( expr) "
204+ remaining_interpolation -= string. ranges ( of: target) . count
205+ string. replace ( target, with: fix)
206+ } else {
207+ //string = "\\(\"\(string)\".escapingHTML(escapeAttributes: true))"
208+ warn_interpolation ( context: context, node: expr, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
209+ }
210+ return string
211+ }
212+ }
213+ extension HTMLKitUtilities {
181214 // MARK: Extract literal
182- private static func extract_literal(
215+ static func extract_literal(
183216 context: some MacroExpansionContext ,
184217 key: String ,
185218 expression: ExprSyntax ,
@@ -253,52 +286,25 @@ public extension HTMLKitUtilities {
253286 }
254287 return nil
255288 }
256- // MARK: Promote Interpolation
257- static func promote_interpolation(
258- context: some MacroExpansionContext ,
259- remaining_interpolation: inout Int ,
260- expr: ExpressionSegmentSyntax ,
261- lookupFiles: Set < String >
262- ) -> String {
263- var string : String = " \( expr) "
264- guard let expression: ExprSyntax = expr. expressions. first? . expression else { return string }
265- if let stringLiteral: StringLiteralExprSyntax = expression. stringLiteral {
266- let segments : StringLiteralSegmentListSyntax = stringLiteral. segments
267- if segments. count ( where: { $0. is ( StringSegmentSyntax . self) } ) == segments. count {
268- remaining_interpolation = 0
269- string = segments. map ( { $0. as ( StringSegmentSyntax . self) !. content. text } ) . joined ( )
270- } else {
271- string = " "
272- for segment in segments {
273- if let literal: String = segment. as ( StringSegmentSyntax . self) ? . content. text {
274- string += literal
275- } else if let interpolation: ExpressionSegmentSyntax = segment. as ( ExpressionSegmentSyntax . self) {
276- let promoted : String = promote_interpolation ( context: context, remaining_interpolation: & remaining_interpolation, expr: interpolation, lookupFiles: lookupFiles)
277- if " \( interpolation) " == promoted {
278- //string += "\\(\"\(promoted)\".escapingHTML(escapeAttributes: true))"
279- string += " \( promoted) "
280- warn_interpolation ( context: context, node: interpolation, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
281- } else {
282- string += promoted
283- }
284- } else {
285- //string += "\\(\"\(segment)\".escapingHTML(escapeAttributes: true))"
286- warn_interpolation ( context: context, node: segment, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
287- string += " \( segment) "
288- }
289- }
290- }
291- } else if let fix: String = expression. integerLiteral? . literal. text ?? expression. floatLiteral? . literal. text {
292- let target : String = " \( expr) "
293- remaining_interpolation -= string. ranges ( of: target) . count
294- string. replace ( target, with: fix)
295- } else {
296- //string = "\\(\"\(string)\".escapingHTML(escapeAttributes: true))"
297- warn_interpolation ( context: context, node: expr, string: & string, remaining_interpolation: & remaining_interpolation, lookupFiles: lookupFiles)
298- }
299- return string
289+
290+ // MARK: GA Already Defined
291+ static func global_attribute_already_defined( context: some MacroExpansionContext , attribute: String , node: some SyntaxProtocol ) {
292+ context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " globalAttributeAlreadyDefined " , message: " Global attribute \" " + attribute + " \" is already defined. " ) ) )
293+ }
294+
295+ // MARK: Unallowed Expression
296+ static func unallowed_expression( context: some MacroExpansionContext , node: LabeledExprSyntax ) {
297+ context. diagnose ( Diagnostic ( node: node, message: DiagnosticMsg ( id: " unallowedExpression " , message: " String Interpolation is required when encoding runtime values. " ) , fixIts: [
298+ FixIt ( message: DiagnosticMsg ( id: " useStringInterpolation " , message: " Use String Interpolation. " ) , changes: [
299+ FixIt . Change. replace (
300+ oldNode: Syntax ( node) ,
301+ newNode: Syntax ( StringLiteralExprSyntax ( content: " \\ ( \( node) ) " ) )
302+ )
303+ ] )
304+ ] ) )
300305 }
301- // MARK: warn interpolation
306+
307+ // MARK: Warn Interpolation
302308 static func warn_interpolation(
303309 context: some MacroExpansionContext ,
304310 node: some SyntaxProtocol ,
0 commit comments