Fixes #179 - invoke tag support in Marko v3

This commit is contained in:
Patrick Steele-Idem 2016-01-07 16:38:57 -07:00
parent 8c96302550
commit 1bcdc42b1f
8 changed files with 38 additions and 31 deletions

View File

@ -291,6 +291,10 @@ class Builder {
return new ObjectExpression({properties});
}
parseExpression(str) {
return parseExpression(str);
}
program(body) {
return new Program({body});
}

View File

@ -0,0 +1,20 @@
var createLoopNode = require('./util/createLoopNode');
module.exports = function codeGenerator(elNode, codegen) {
var functionAttr = elNode.attributes[0];
if (!functionAttr) {
codegen.addError('Invalid <invoke> tag. Missing function attribute. Expected: <invoke console.log("Hello World")');
return;
}
var arg = functionAttr.argument;
if (arg === undefined) {
codegen.addError('Invalid <invoke> tag. Missing function arguments. Expected: <invoke console.log("Hello World")');
return;
}
var functionName = functionAttr.name;
var functionCallExpression = functionName + '(' + arg + ')';
return codegen.builder.parseExpression(functionCallExpression);
};

View File

@ -1,4 +1,7 @@
{
"<assign>": {
"code-generator": "./assign-tag"
},
"<else>": {
"node-factory": "./else-tag"
},
@ -11,6 +14,9 @@
"<if>": {
"node-factory": "./if-tag"
},
"<invoke>": {
"code-generator": "./invoke-tag"
},
"<macro>": {
"code-generator": "./macro-tag"
},
@ -24,9 +30,6 @@
"<var>": {
"node-factory": "./var-tag"
},
"<assign>": {
"code-generator": "./assign-tag"
},
"<*>": {
"transformer": "./core-transformer"
}

View File

@ -1 +0,0 @@
A <p>Hello World!Hello World!</p> B <p>Hello Frank! You have 10 new messages.Hello John! You have 20 new messages.</p>

View File

@ -1,27 +0,0 @@
{%
function test(name) {
out.write("Hello " + name + "!");
}
%}
{%
function test2(name) {
return "Hello " + name + "!";
}
%}
A
<p>
<invoke function="test('World')"/>
<c-write value="test2('World')"/>
</p>
B
<p>
<def function="greeting(name, count)">
Hello ${name}! You have ${count} new messages.
</def>
<invoke function="greeting" name="Frank" count="${10}"/>
<invoke function="greeting('John', 20)"/>
</p>

View File

@ -0,0 +1 @@
Hello Frank! Hello John!

View File

@ -0,0 +1,7 @@
<template-init>
function greeting(name, out) {
out.write('Hello ' + name + '!');
}
</template-init>
<invoke greeting('Frank', out)/> <invoke greeting('John', out)/>