Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Template syntax error? #8

Closed
robogeek opened this Issue · 13 comments

2 participants

@robogeek

A template that contains this snippet fails to compile

{skimlinks({start: 0, rows: 300, fq: "+groupId:45335650 +merchantId:12678 +id:86680570", searchFor: "Pogoplug" })}

A SyntaxError is thrown here (I have overidden resourceLoader)

    at length (/Users/davidherron/node_modules/kernel/kernel.js:175:18)
    at Function.K.resourceLoader (/Users/davidherron/boilerplate/gulp/lib/renderer.js:54:5)
    at compile (/Users/davidherron/node_modules/kernel/kernel.js:159:10)
@creationix

It seems to compile on it's own.

var source = 'before {skimlinks({start: 0, rows: 300, fq: "+groupId:45335650 +merchantId:12678 +id:86680570", searchFor: "Pogoplug" })} after';
var tokens = Kernel.tokenizer(source);
console.log(tokens);

Which outputs:

[ 'before ',                                                                                                                     
  { start: 7,                                                                                                                    
    end: 121,                                                                                                                    
    name: 'skimlinks',                                                                                                           
    args: '{start: 0, rows: 300, fq: "+groupId:45335650 +merchantId:12678 +id:86680570", searchFor: "Pogoplug" }' },             
  ' after' ]                                                                                                                     
@robogeek

BTW I updated to kernel 0.1.2 and the behavior is the same.

Here's a modification to your test that includes the whole template that I'm trying to compile. This also runs fine. Even does the first step of generating the javascript okay.

var Kernel = require('kernel');
var util   = require('util');

var source = "<p>Youtube URL https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec</p>\n\n"
   + "{oembed(\"https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec\")}\n\n"
   + "{skimlinks({start: 0, rows: 300, fq: \"+groupId:45335650 +merchantId:12678 +id:86680570\", searchFor: \"Pogoplug\" })}\n";

var blocks = [];
var tokens = Kernel.tokenizer(source);
console.log(tokens);
Kernel.generator(Kernel.parser(tokens, source, "yt.html.kernel"), blocks);
console.log(util.inspect(blocks));

var code = blocks.map(function (block, i) {
        return "function block_" + i + "(callback) {\n" +
          block + "\n}\n";
      }).join("\n");

console.log(code);

[ '<p>Youtube URL https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec</p>\n\n',
  { start: 79,
    end: 149,
    name: 'oembed',
    args: '"https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec"' },
  '\n\n',
  { start: 151,
    end: 265,
    name: 'skimlinks',
    args: '{start: 0, rows: 300, fq: "+groupId:45335650 +merchantId:12678 +id:86680570", searchFor: "Pogoplug" }' },
  '\n' ]
[ '  var state = new State(4, 2, callback);\n  state.parts[0] = "<p>Youtube URL https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec</p>\\n\\n";\n  state.parts[2] = "\\n";\n  run(state, 1, this.oembed, ["https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec"]);\n  run(state, 3, this.skimlinks, [{this.start: 0, this.rows: 300, this.fq: "+groupId:45335650 +merchantId:12678 +id:86680570", this.searchFor: "Pogoplug" }]);',
  needsLib: true ]
function block_0(callback) {
  var state = new State(4, 2, callback);
  state.parts[0] = "<p>Youtube URL https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec</p>\n\n";
  state.parts[2] = "\n";
  run(state, 1, this.oembed, ["https://www.youtube.com/watch?v=qrqv5wkHYmw&feature=g-vrec"]);
  run(state, 3, this.skimlinks, [{this.start: 0, this.rows: 300, this.fq: "+groupId:45335650 +merchantId:12678 +id:86680570", this.searchFor: "Pogoplug" }]);
}

@robogeek

The stack traceback points to this line:

      template = Function("locals", "callback", '"use strict"\n\n' +
        "block_0.call(locals, callback);\n\n" + code);

Meaning it's constructing a Function.. meaning, is the generated code something that can be constructed into a Function. Looking at that generated code I wonder if this line is legit.

  run(state, 3, this.skimlinks, [{this.start: 0, this.rows: 300, this.fq: "+groupId:45335650 +merchantId:12678 +id:86680570", this.searchFor: "Pogoplug" }]);

Specifically is this legit:

{
this.whosis: "gronk",
}
@robogeek

waaaaaait... is that even a good idea to modify the labels of the fields in my anonymous object to read "this.start" etc?

That anonymous object is getting passed to another API that is going to look for a field named "start" not "this.start".

@robogeek

And, changing my template to this results in a successful run. This means there is a workaround, but the bug is that you generate "{ this.whosis: value ... }" in this case rather than "{ whosis: value ... }".

<p>{skimlinks({"start": 0, "rows": 300, "fq": "+groupId:45335650 +merchantId:12678 +id:86680570", "searchFor": "Pogoplug" })}</p>
@creationix

Ahh, I see, it's a bug in withit. Yeah, it shouldn't be prepending "this" to object keys.

@creationix

Fixed by f7b0de1

@creationix creationix closed this
@creationix

Also published to npm and jamjs as version 0.1.3.

@robogeek

verified

@creationix

Thanks for the bug report! Also, I don't have many users. What do you think of kernel? What do you like about it? What drives you crazy?

@robogeek
@creationix

Good point. I just exposed "compile(source, filename)" as a standalone function where you just want to pass in some text and get back a template function. 7adf070

@creationix

I started development on Kernel several years ago long before dust existed. Though I did steal some of the syntax from them in this latest edition. Dust is way too complicated for me.

Regarding moving if, loop, and others into core, this is a non-feature. Kernel is meant to be embedded in a web-framework that provides those to the app user. I did provide examples of some of the common stuff so people who want to use it directly or put it in a framework have a starting point. Implementing if is very simple, but async loops are very complicated and everyone has a unique requirement on how it should work. I don't like baking in APIs that I'm unsure are a good idea. Better, I think, to leave it simple and extensible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.