slm-lang / slm

Slim, Jade like template engine for node
MIT License
197 stars 16 forks source link

Getting an error thrown from a $ in the template #11

Open chrisnicola opened 9 years ago

chrisnicola commented 9 years ago

I'm trying to parse some of what were originally SLIM templates using webpack. I've created a pretty basic loader (code below) and it works for the most part but I'm getting some unusual errors being thrown. I'm also not getting line numbers from the errors, nor does the error look like something from the SLM library.

The Loader:

'use strict';

var loaderUtils = require("loader-utils");
var slm = require("slm");
var markdown = require("slm-markdown")
markdown.register(slm.template)

module.exports = function(source) {
  this.cacheable && this.cacheable(true);
  var query = loaderUtils.parseQuery(this.query);
  var req = loaderUtils.getRemainingRequest(this).replace(/^!/, "");
  slm.compile(source)()
}

The error

15:05:29 webpack.1 | Module build failed: SyntaxError: Unexpected token :
15:05:29 webpack.1 |   at VM.exports.runInThisContext [as runInContext] (vm.js:53:16)
15:05:29 webpack.1 |   at Template.p.exec (/Users/chrisnicola/src/wealthbar/node_modules/slm/lib/template.js:100:13)
15:05:29 webpack.1 |   at Template.p.compile (/Users/chrisnicola/src/wealthbar/node_modules/slm/lib/template.js:77:17)
15:05:29 webpack.1 |   at Object.module.exports (/Users/chrisnicola/src/wealthbar/webpack/loaders/slm.js:12:22)
15:05:29 webpack.1 |  @ ./webpack/wealthbar/views/index.coffee 43:23-63

The line that is causing it:

          p()
            | Net income ${{options.income - results.totalTax | number:2}}

If I remove the $ it works. This is an angular template so it is meant to return a currency value, hence the dollar sign.

chrisnicola commented 9 years ago

I'm guessing that it is interpreting this as a ES6 template string, so I will have to escape the $ as \$. This is probably not be a bug.

chrisnicola commented 9 years ago

Actually I would suggest this is a bug in that there is no line number reported. So the error is pretty confusing from that standpoint.