Skip to content

Commit 804a0d9

Browse files
fixes #5
1 parent 32d152b commit 804a0d9

File tree

2 files changed

+111
-17
lines changed

2 files changed

+111
-17
lines changed

Sources/HTMLKitUtilities/interpolation/ParseLiteral.swift

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ extension HTMLKitUtilities {
132132
break
133133
}
134134
}
135-
return .interpolation(expr_to_single_line(function))
135+
return .interpolation(to_single_line(function))
136136
}
137137
if let member:MemberAccessExprSyntax = expression.memberAccess {
138-
return .interpolation(expr_to_single_line(member))
138+
return .interpolation(to_single_line(member))
139139
}
140140
if let array:ArrayExprSyntax = expression.array {
141141
let separator:String
@@ -181,33 +181,35 @@ extension HTMLKitUtilities {
181181
}
182182
}
183183
if let unwrap:ForceUnwrapExprSyntax = expression.as(ForceUnwrapExprSyntax.self) {
184-
let merged:String = expr_to_single_line(unwrap)
184+
let merged:String = to_single_line(unwrap)
185185
return .interpolation("\\(" + merged + ")")
186186
}
187187
return nil
188188
}
189189

190-
// MARK: Expr to Single Line
191-
static func expr_to_single_line(_ expression: ExprSyntax) -> String {
190+
// MARK: To Single Line
191+
static func to_single_line(_ expression: ExprSyntax) -> String {
192192
if let function:FunctionCallExprSyntax = expression.functionCall {
193-
return expr_to_single_line(function)
193+
return to_single_line(function)
194194
} else if let member:MemberAccessExprSyntax = expression.memberAccess {
195-
return expr_to_single_line(member)
195+
return to_single_line(member)
196196
} else if let force_unwrap:ForceUnwrapExprSyntax = expression.as(ForceUnwrapExprSyntax.self) {
197-
return expr_to_single_line(force_unwrap) + "!"
197+
return to_single_line(force_unwrap) + "!"
198+
} else if let closure:ClosureExprSyntax = expression.as(ClosureExprSyntax.self) {
199+
return to_single_line(closure)
198200
} else {
199201
return "\(expression)"
200202
}
201203
}
202-
static func expr_to_single_line(_ force_unwrap: ForceUnwrapExprSyntax) -> String {
203-
return expr_to_single_line(force_unwrap.expression) + "!"
204+
static func to_single_line(_ force_unwrap: ForceUnwrapExprSyntax) -> String {
205+
return to_single_line(force_unwrap.expression) + "!"
204206
}
205-
static func expr_to_single_line(_ member: MemberAccessExprSyntax) -> String {
207+
static func to_single_line(_ member: MemberAccessExprSyntax) -> String {
206208
var string:String = "\(member)"
207209
string.removeAll { $0.isWhitespace }
208210
return string
209211
}
210-
static func expr_to_single_line(_ function: FunctionCallExprSyntax) -> String {
212+
static func to_single_line(_ function: FunctionCallExprSyntax) -> String {
211213
var string:String = "\(function.calledExpression)"
212214
string.removeAll { $0.isWhitespace }
213215
var args:String = ""
@@ -223,7 +225,7 @@ extension HTMLKitUtilities {
223225
arg.insert(",", at: arg.startIndex)
224226
}
225227
arg += ": "
226-
var expr:String = expr_to_single_line(argument.expression)
228+
var expr:String = to_single_line(argument.expression)
227229
while expr.first?.isWhitespace ?? false {
228230
expr.removeFirst()
229231
}
@@ -237,9 +239,57 @@ extension HTMLKitUtilities {
237239
args += arg
238240
is_first = false
239241
}
242+
if let closure:ClosureExprSyntax = function.trailingClosure {
243+
args += to_single_line(closure)
244+
}
240245
args = "(" + args + ")"
241246
return string + args
242247
}
248+
static func to_single_line(_ decl: DeclSyntax) -> String {
249+
return "\(decl)" // TODO: fix?
250+
}
251+
static func to_single_line(_ statement: StmtSyntax) -> String {
252+
return "\(statement)" // TODO: fix?
253+
}
254+
static func to_single_line(_ closure: ClosureExprSyntax) -> String {
255+
var signature:String = "", body:String = ""
256+
if let sig:ClosureSignatureSyntax = closure.signature {
257+
signature = to_single_line(sig)
258+
}
259+
var is_first:Bool = true
260+
for statement in closure.statements {
261+
if !is_first {
262+
body += "; "
263+
}
264+
switch statement.item {
265+
case .decl(let decl): body += to_single_line(decl)
266+
case .expr(let expression): body += to_single_line(expression)
267+
case .stmt(let stmt): body += to_single_line(stmt)
268+
}
269+
is_first = false
270+
}
271+
return "{ " + signature + body + " }"
272+
}
273+
static func to_single_line(_ signature: ClosureSignatureSyntax) -> String {
274+
var string:String = ""
275+
switch signature.parameterClause {
276+
case nil:
277+
break
278+
case .simpleInput(let list):
279+
for i in list {
280+
string += i.name.text
281+
}
282+
break
283+
case .parameterClause(let clause):
284+
string += "("
285+
for i in clause.parameters {
286+
string += "\(i)"
287+
}
288+
string += ")"
289+
break
290+
}
291+
return string + " in "
292+
}
243293
}
244294

245295
// MARK: LiteralReturnType

Tests/HTMLKitTests/InterpolationTests.swift

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct InterpolationTests {
6969

7070
// MARK: multi-line func
7171
@Test func multiline_func_interpolation() {
72+
var expected_result:String = "<div>Bikini Bottom: Spongebob Squarepants, Patrick Star, Squidward Tentacles, Mr. Krabs, Sandy Cheeks, Pearl Krabs</div>"
7273
var string:String = #html(
7374
div(
7475
"Bikini Bottom: ",
@@ -95,17 +96,19 @@ struct InterpolationTests {
9596
)
9697
)
9798
)
98-
#expect(string == "<div>Bikini Bottom: Spongebob Squarepants, Patrick Star, Squidward Tentacles, Mr. Krabs, Sandy Cheeks, Pearl Krabs</div>")
99-
99+
#expect(string == expected_result)
100+
101+
expected_result = "<div>Don&#39t forget Gary!</div>"
100102
string = #html(
101103
div(
102104
"Don't forget ",
103105
InterpolationTests.BikiniBottom.gary(),
104106
"!"
105107
)
106108
)
107-
#expect(string == "<div>Don&#39t forget Gary!</div>")
109+
#expect(string == expected_result)
108110

111+
expected_result = "<div>Spongeboob</div>"
109112
string = #html(
110113
div(
111114
InterpolationTests
@@ -121,7 +124,16 @@ struct InterpolationTests {
121124
)
122125
)
123126
)
124-
#expect(string == "<div>Spongeboob</div>")
127+
#expect(string == expected_result)
128+
}
129+
130+
// MARK: multi-line closure
131+
@Test func multiline_closure_interpolation() {
132+
/*var expected_result:String = "<div>Mrs. Puff</div>"
133+
string = #html(div(InterpolationTests.character2 {
134+
let test:String = "Mrs. Puff"
135+
} ))
136+
#expect(string == expected_result)*/
125137
}
126138

127139
// MARK: multi-line member
@@ -148,6 +160,34 @@ struct InterpolationTests {
148160
#expect(string == "<div>Shrek isLove, Shrek isLife</div>")
149161
}
150162

163+
// MARK: closure
164+
@Test func closure_interpolation() {
165+
let expected_result:String = "<div>Mrs. Puff</div>"
166+
var string:String = #html(div(InterpolationTests.character1(body: { "Mrs. Puff" })))
167+
#expect(string == expected_result)
168+
169+
string = #html(div(InterpolationTests.character2({ "Mrs. Puff" })))
170+
#expect(string == expected_result)
171+
172+
string = #html(div(InterpolationTests.character2 { "Mrs. Puff" } ))
173+
#expect(string == expected_result)
174+
175+
string = #html(div(InterpolationTests.character2 { let test:String = "Mrs. Puff"; return test } ))
176+
#expect(string == expected_result)
177+
178+
string = #html(div(InterpolationTests.character3 { _ in let test:String = "Mrs. Puff"; return test } ))
179+
#expect(string == expected_result)
180+
181+
string = #html(div(InterpolationTests.character3 { isTrue in let test:String = "Mrs. Puff"; return isTrue ? test : "" } ))
182+
#expect(string == expected_result)
183+
184+
string = #html(div(InterpolationTests.character3 { (isTrue:Bool) in let test:String = "Mrs. Puff"; return isTrue ? test : "" } ))
185+
#expect(string == expected_result)
186+
187+
string = #html(div(InterpolationTests.character4 { (string, integer, isTrue) in let test:String = "Mrs. Puff"; return (isTrue.first ?? false) ? test : "" } ))
188+
#expect(string == expected_result)
189+
}
190+
151191
// MARK: inferred type
152192
@Test func inferred_type_interpolation() {
153193
var array:[String] = ["toothless", "hiccup"]
@@ -248,6 +288,10 @@ extension InterpolationTests {
248288
default: return "Plankton"
249289
}
250290
}
291+
static func character1(body: () -> String) -> String { body() }
292+
static func character2(_ body: () -> String) -> String { body() }
293+
static func character3(_ body: (Bool) -> String) -> String { body(true) }
294+
static func character4(_ body: (String, Int, Bool...) -> String) -> String { body("", 0, true) }
251295
static func sandyCheeks() -> String {
252296
return "Sandy Cheeks"
253297
}

0 commit comments

Comments
 (0)