fixed highlighting of errors happening in {{ block.super }}
This commit is contained in:
@@ -146,13 +146,27 @@ class BlockNode : NodeType {
|
|||||||
|
|
||||||
func render(_ context: Context) throws -> String {
|
func render(_ context: Context) throws -> String {
|
||||||
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.popBlock(named: name) {
|
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.popBlock(named: name) {
|
||||||
// node is a block node from child template that extends this node (has the same name)
|
// child node is a block node from child template that extends this node (has the same name)
|
||||||
let newContext: [String: Any]
|
|
||||||
newContext = [
|
var newContext: [String: Any] = [BlockContext.contextKey: blockContext]
|
||||||
BlockContext.contextKey: blockContext,
|
|
||||||
// render current node so that it's content can be used as part of node that extends it
|
if let blockSuperNode = child.node.nodes.first(where: {
|
||||||
"block": ["super": try self.render(context)]
|
if case .variable(let variable, _)? = $0.token, variable == "block.super" { return true }
|
||||||
]
|
else { return false}
|
||||||
|
}) {
|
||||||
|
do {
|
||||||
|
// render current (base) node so that its content can be used as part of node that extends it
|
||||||
|
newContext["block"] = ["super": try self.render(context)]
|
||||||
|
} catch {
|
||||||
|
let baseError = context.errorReporter.reportError(error)
|
||||||
|
throw TemplateSyntaxError(
|
||||||
|
reason: (baseError as? TemplateSyntaxError)?.reason ?? "\(baseError)",
|
||||||
|
lexeme: blockSuperNode.token,
|
||||||
|
template: child.template,
|
||||||
|
parentError: baseError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// render extension node
|
// render extension node
|
||||||
do {
|
do {
|
||||||
return try context.push(dictionary: newContext) {
|
return try context.push(dictionary: newContext) {
|
||||||
@@ -165,7 +179,6 @@ class BlockNode : NodeType {
|
|||||||
if var error = error as? TemplateSyntaxError {
|
if var error = error as? TemplateSyntaxError {
|
||||||
error.template = error.template ?? child.template
|
error.template = error.template ?? child.template
|
||||||
error.lexeme = error.lexeme ?? child.node.token
|
error.lexeme = error.lexeme ?? child.node.token
|
||||||
|
|
||||||
throw error
|
throw error
|
||||||
} else {
|
} else {
|
||||||
throw error
|
throw error
|
||||||
|
|||||||
@@ -256,7 +256,7 @@ func testEnvironment() {
|
|||||||
let baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
|
let baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
|
||||||
|
|
||||||
let parentError = expectedSyntaxError(token: "target|unknown", template: baseTemplate, description: "filter error")
|
let parentError = expectedSyntaxError(token: "target|unknown", template: baseTemplate, description: "filter error")
|
||||||
var error = expectedSyntaxError(token: "extends \"invalid-base.html\"", template: template, description: "filter error")
|
var error = expectedSyntaxError(token: "block.super", template: template, description: "filter error")
|
||||||
error.parentError = parentError
|
error.parentError = parentError
|
||||||
|
|
||||||
try expect(environment.render(template: template, context: ["target": "World"])).toThrow(error)
|
try expect(environment.render(template: template, context: ["target": "World"])).toThrow(error)
|
||||||
|
|||||||
Reference in New Issue
Block a user