|
5 | 5 | // Created by Evan Anderson on 9/14/24. |
6 | 6 | // |
7 | 7 |
|
8 | | -import SwiftCompilerPlugin |
9 | 8 | import SwiftSyntax |
10 | 9 | import SwiftSyntaxMacros |
11 | | -import SwiftSyntaxBuilder |
12 | | -import SwiftDiagnostics |
13 | 10 |
|
14 | 11 | struct HTMLElement : ExpressionMacro { |
15 | 12 | static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax { |
16 | | - return "\(raw: parse_element(node: node, context: context))" |
| 13 | + return "\"\(raw: parse_element(node: node, context: context))\"" |
17 | 14 | } |
18 | 15 | } |
19 | 16 |
|
20 | 17 | extension HTMLElement { |
21 | 18 | static func parse_element(node: some FreestandingMacroExpansionSyntax, context: some MacroExpansionContext) -> String { |
22 | | - let macroName:String = node.macroName.text |
23 | | - let type:HTMLElementType = HTMLElementType(rawValue: macroName) ?? HTMLElementType.a |
| 19 | + let type:HTMLElementType = HTMLElementType(rawValue: node.macroName.text) ?? HTMLElementType.a |
24 | 20 | let data:ElementData = parse_arguments(elementType: type, arguments: node.arguments) |
25 | 21 | var string:String = (type == .html ? "<!DOCTYPE html>" : "") + "<" + type.rawValue + data.attributes + ">" + data.innerHTML |
26 | 22 | if !type.isVoid { |
27 | 23 | string += "</" + type.rawValue + ">" |
28 | 24 | } |
29 | | - return "\"" + string + "\"" |
| 25 | + return string |
30 | 26 | } |
31 | 27 | static func parse_arguments(elementType: HTMLElementType, arguments: LabeledExprListSyntax) -> ElementData { |
32 | | - var attributes:[String] = [] |
33 | | - var innerHTML:[String] = [] |
| 28 | + var attributes:[String] = [], innerHTML:[String] = [] |
34 | 29 | for element in arguments.children(viewMode: .all) { |
35 | 30 | if let child:LabeledExprSyntax = element.as(LabeledExprSyntax.self) { |
36 | 31 | if let key:String = child.label?.text { |
@@ -72,8 +67,7 @@ extension HTMLElement { |
72 | 67 | } |
73 | 68 |
|
74 | 69 | struct ElementData { |
75 | | - let attributes:String |
76 | | - let innerHTML:String |
| 70 | + let attributes:String, innerHTML:String |
77 | 71 |
|
78 | 72 | init(attributes: [String], innerHTML: [String]) { |
79 | 73 | self.attributes = attributes.isEmpty ? "" : " " + attributes.joined(separator: " ") |
@@ -212,7 +206,7 @@ private extension HTMLElement { |
212 | 206 | } |
213 | 207 |
|
214 | 208 | // MARK: HTMLElementType |
215 | | -public enum HTMLElementType : String, CaseIterable { |
| 209 | +enum HTMLElementType : String { |
216 | 210 | case html |
217 | 211 |
|
218 | 212 | case a |
@@ -346,7 +340,7 @@ public enum HTMLElementType : String, CaseIterable { |
346 | 340 |
|
347 | 341 | case wbr |
348 | 342 |
|
349 | | - public var isVoid : Bool { |
| 343 | + var isVoid : Bool { |
350 | 344 | switch self { |
351 | 345 | case .area, .base, .br, .col, .embed, .hr, .img, .input, .link, .meta, .source, .track, .wbr: |
352 | 346 | return true |
|
0 commit comments