mustache / spec

The Mustache spec.
MIT License
372 stars 71 forks source link

Comments: Behavior on variable name collision #136

Closed s9105947 closed 2 years ago

s9105947 commented 2 years ago

Dear Maintainers,

I've noticed that some implementations treat {{! comments}} just as normal variable replacements, just hoping that the user does not define a variable ! comments.

Example: Data: {"! blabla": "never shown"} Template: >{{! blabla}}< Rendered A: >never shown< Rendered B: ><

Handling comments as (hopefully) undefined variables yields Rendered A, but I think the spec suggests Rendered B.

Which is correct?

There is no test case ensuring this behavior, can one get added? (I can hack together a PR, but I'm not certain that I'll get it to run.)

jgonggrijp commented 2 years ago

Good call @s9105947. Intuitively, I agree with you that B is correct. I also agree that the top-level overview of the comments spec seems to indicate the same:

https://github.com/mustache/spec/blob/b2aeb3c283de931a7004b5f7a2cb394b89382369/specs/comments.yml#L2-L6

Especially the combination of the phrases "should never appear in the resulting output" and "may contain any substring" (emphasis mine in both cases) seems to indicate that comments are very absolute, so a collision with a variable name should not result in that variable being interpolated.

Thirdly, I agree that this intention isn't enforced in the tests yet. None of the tests have data.

If you like a challenge, please feel welcome to hack together a PR. Reviews are there to help smooth out rough edges. I will happily review your changes and test them against my own implementation, and others might join in, too.