Skip to content

Commit

Permalink
Fix nested extend with sugar (#127)
Browse files Browse the repository at this point in the history
When nesting an `#extend` with two parameters, the second parameter was ignored instead of being used as context.

Fixes #126
  • Loading branch information
benblakely authored Dec 8, 2023
1 parent 728dd3d commit 547a134
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
8 changes: 1 addition & 7 deletions Sources/LeafKit/LeafAST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,7 @@ public struct LeafAST: Hashable {
}

// replace the original Syntax with the results of inlining, potentially 1...n
let replacementSyntax: [Syntax]
if case .extend(let extend) = ast[pos], let context = extend.context {
let inner = ast[pos].inlineRefs(providedExts, [:])
replacementSyntax = [.with(.init(context: context, body: inner))]
} else {
replacementSyntax = ast[pos].inlineRefs(providedExts, [:])
}
let replacementSyntax = ast[pos].inlineRefs(providedExts, [:])
ast.replaceSubrange(pos...pos, with: replacementSyntax)
// any returned new inlined syntaxes can't be further resolved at this point
// but we need to add their unresolvable references to the global set
Expand Down
4 changes: 4 additions & 0 deletions Sources/LeafKit/LeafSyntax/LeafSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ extension Syntax: BodiedSyntax {
}

internal func inlineRefs(_ externals: [String: LeafAST], _ imports: [String: Export]) -> [Syntax] {
if case .extend(let extend) = self, let context = extend.context {
let inner = extend.inlineRefs(externals, imports)
return [.with(.init(context: context, body: inner))]
}
var result = [Syntax]()
switch self {
case .import(let im):
Expand Down
33 changes: 33 additions & 0 deletions Tests/LeafKitTests/LeafTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,39 @@ final class LeafTests: XCTestCase {
XCTAssertEqual(str, expected)
}

func testNestedExtendWithSugar() throws {
let layout = """
<body>#import("content")</body>
"""

let header = """
<h1>#(child)</h1>
"""

let base = """
#extend("layout"):#export("content"):<main>#extend("header", parent)</main>#endexport#endextend
"""

let expected = """
<body><main><h1>Elizabeth</h1></main></body>
"""

let layoutAST = try LeafAST(name: "layout", ast: parse(layout))
let headerAST = try LeafAST(name: "header", ast: parse(header))
let baseAST = try LeafAST(name: "base", ast: parse(base))

let baseResolved = LeafAST(from: baseAST, referencing: ["layout": layoutAST, "header": headerAST])

var serializer = LeafSerializer(
ast: baseResolved.ast,
ignoreUnfoundImports: false
)
let view = try serializer.serialize(context: ["parent": ["child": "Elizabeth"]])
let str = view.getString(at: view.readerIndex, length: view.readableBytes) ?? ""

XCTAssertEqual(str, expected)
}

func testEmptyForLoop() throws {
let template = """
#for(category in categories):
Expand Down

0 comments on commit 547a134

Please sign in to comment.