stencilproject/Stencil

Fatal error on parsing empty variable tag

jberkel opened this issue · 3 comments

In 0.12.x. and earlier, {{ }} simply yielded an empty string, now it produces "Fatal error: Index out of range" in Node.swift:87

let filter = try parser.compileResolvable(components[0], containedIn: token)

Testcase:

it("can parse an empty variable token") {
      let parser = TokenParser(tokens: [
        .variable(value: "", at: .unknown)
      ], environment: Environment())

      let nodes = try parser.parse()
      let node = nodes.first as? VariableNode
      try expect(nodes.count) == 0 // ?
}
djbe commented

Weird, because we explicitly have a test for empty tokens:
https://github.com/stencilproject/Stencil/blob/master/Tests/StencilTests/LexerSpec.swift#L79

Although you're right, we don't seem have a test for empty variables (at first glance). I do wonder though, should we even consider this correct syntax?

I think we shouldn't even if it would be an easy thing to do. Looking at the test we have it might be that extra space makes a difference in this case (IIRC it is used to break down content of the token into components). Throwing syntax error would be nicer than fatalError here though.

@jberkel what is your use case where you end up with such syntax?

@ilyapuchka I don't have a use case, but in my app the templates are user editable, so I have to deal with invalid input. I expect and handle TemplateSyntaxError, but this bug just crashes the app.