"Comments" in Multi-Language Files

January 07, 2014

I’ve lost the exact context for this issue, but here’s an example (I’ve also captured relevant details in this issue in my githooks repo.

In a Slim file, embed some JavaScript:

javascript:
    alert('Slim supports embedded javascript, #{user.name(}')

… and it doesn’t work correctly. The template failed to compile. (We know why - there’s that open paren w/out a matching close. But make believe that you don’t know the exact cause for now.)

So, in order to debug what’s going wrong, I commented out that line of JavaScript:

javascript:
    // alert('Slim supports embedded javascript, #{user.name(}')

And compilation still fails. What? That’s the only line I’ve changed since the working copy. How is it failing?

As you probably guessed, The Slim transformation engine doesn’t care that that line is commented out. When it eventually gets passed to the JavaScript interpreter, the JS interpreter will ignore the commented-out line. But Slim doesn’t know that; it’s still going to invoke the Ruby interpreter on that string interpolation, to try to create the string to pass to JS. If that Ruby expression is bad, well, boom.

So remember; when working in multi-language files, always consider which transformation engine or interpreter is honoring each style of comment.

Tags: javascript, slim
comments powered by Disqus