mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixes #355 - status-var/separator not handled when looping over properties
This commit is contained in:
parent
fe0d1a749c
commit
40786f500e
@ -8,6 +8,8 @@ class ForEachProp extends Node {
|
|||||||
this.nameVarName = def.nameVarName;
|
this.nameVarName = def.nameVarName;
|
||||||
this.valueVarName = def.valueVarName;
|
this.valueVarName = def.valueVarName;
|
||||||
this.in = def.in;
|
this.in = def.in;
|
||||||
|
this.separator = def.separator;
|
||||||
|
this.statusVarName = def.statusVarName;
|
||||||
this.body = this.makeContainer(def.body);
|
this.body = this.makeContainer(def.body);
|
||||||
|
|
||||||
ok(this.nameVarName, '"nameVarName" is required');
|
ok(this.nameVarName, '"nameVarName" is required');
|
||||||
@ -20,16 +22,45 @@ class ForEachProp extends Node {
|
|||||||
var valueVarName = this.valueVarName;
|
var valueVarName = this.valueVarName;
|
||||||
var inExpression = this.in;
|
var inExpression = this.in;
|
||||||
var body = this.body;
|
var body = this.body;
|
||||||
|
var separator = this.separator;
|
||||||
|
var statusVarName = this.statusVarName;
|
||||||
|
|
||||||
|
if (separator && !statusVarName) {
|
||||||
|
statusVarName = '__loop';
|
||||||
|
}
|
||||||
|
|
||||||
var builder = codegen.builder;
|
var builder = codegen.builder;
|
||||||
|
|
||||||
let forEachVarName = codegen.addStaticVar('forEachProp', '__helpers.fp');
|
if (statusVarName) {
|
||||||
|
let helperVar = builder.require(builder.literal('marko/runtime/forEachPropStatusVar'));
|
||||||
|
let forEachVarName = codegen.addStaticVar('forEacPropStatusVar', helperVar);
|
||||||
|
let body = this.body;
|
||||||
|
|
||||||
return builder.functionCall(forEachVarName, [
|
if (separator) {
|
||||||
inExpression,
|
let isNotLastTest = builder.functionCall(
|
||||||
builder.functionDeclaration(null, [nameVarName, valueVarName], body)
|
builder.memberExpression(statusVarName, builder.identifier('isLast')),
|
||||||
]);
|
[]);
|
||||||
|
|
||||||
|
isNotLastTest = builder.negate(isNotLastTest);
|
||||||
|
|
||||||
|
body = body.items.concat([
|
||||||
|
builder.ifStatement(isNotLastTest, [
|
||||||
|
builder.text(separator)
|
||||||
|
])
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.functionCall(forEachVarName, [
|
||||||
|
inExpression,
|
||||||
|
builder.functionDeclaration(null, [nameVarName, valueVarName, statusVarName], body)
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
let forEachVarName = codegen.addStaticVar('forEachProp', '__helpers.fp');
|
||||||
|
return builder.functionCall(forEachVarName, [
|
||||||
|
inExpression,
|
||||||
|
builder.functionDeclaration(null, [nameVarName, valueVarName], body)
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
walk(walker) {
|
walk(walker) {
|
||||||
|
|||||||
32
runtime/forEachPropStatusVar.js
Normal file
32
runtime/forEachPropStatusVar.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
function LoopStatus(getLength, isLast, isFirst, getIndex) {
|
||||||
|
this.getLength = getLength;
|
||||||
|
this.isLast = isLast;
|
||||||
|
this.isFirst = isFirst;
|
||||||
|
this.getIndex = getIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = function forEachPropStatusVar(object, callback) {
|
||||||
|
var keys = Object.keys(object);
|
||||||
|
|
||||||
|
var i = 0;
|
||||||
|
var len = keys.length;
|
||||||
|
var loopStatus = new LoopStatus(
|
||||||
|
function getLength() {
|
||||||
|
return len;
|
||||||
|
},
|
||||||
|
function isLast() {
|
||||||
|
return i === len - 1;
|
||||||
|
},
|
||||||
|
function isFirst() {
|
||||||
|
return i === 0;
|
||||||
|
},
|
||||||
|
function getIndex() {
|
||||||
|
return i;
|
||||||
|
});
|
||||||
|
|
||||||
|
for (; i < len; i++) {
|
||||||
|
var key = keys[i];
|
||||||
|
var value = object[key];
|
||||||
|
callback(key, value, loopStatus);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -333,7 +333,9 @@ module.exports = function(str) {
|
|||||||
'loopType': loopType,
|
'loopType': loopType,
|
||||||
'nameVarName': nameVarName,
|
'nameVarName': nameVarName,
|
||||||
'valueVarName': valueVarName,
|
'valueVarName': valueVarName,
|
||||||
'in': inExpression
|
'in': inExpression,
|
||||||
|
'separator': separatorExpression,
|
||||||
|
'statusVarName': statusVarName
|
||||||
};
|
};
|
||||||
} else if (loopType === 'ForRange') {
|
} else if (loopType === 'ForRange') {
|
||||||
return {
|
return {
|
||||||
|
|||||||
1
test/autotests/render/for-props-separator/expected.html
Normal file
1
test/autotests/render/for-props-separator/expected.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
[foo=low],[bar=high]
|
||||||
1
test/autotests/render/for-props-separator/template.marko
Normal file
1
test/autotests/render/for-props-separator/template.marko
Normal file
@ -0,0 +1 @@
|
|||||||
|
<for(name,value in {'foo': 'low', 'bar': 'high'} | separator=',')>[${name}=${value}]</for>
|
||||||
1
test/autotests/render/for-props-separator/test.js
Normal file
1
test/autotests/render/for-props-separator/test.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
exports.templateData = {};
|
||||||
@ -0,0 +1 @@
|
|||||||
|
0) [foo=low],1) [bar=high]
|
||||||
@ -0,0 +1 @@
|
|||||||
|
<for(name,value in {'foo': 'low', 'bar': 'high'} | status-var=loop separator=',')>${loop.getIndex()}) [${name}=${value}]</for>
|
||||||
@ -0,0 +1 @@
|
|||||||
|
exports.templateData = {};
|
||||||
1
test/autotests/render/for-props-status-var/expected.html
Normal file
1
test/autotests/render/for-props-status-var/expected.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
0) [foo=low]1) [bar=high]
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
<for(name,value in {'foo': 'low', 'bar': 'high'} | status-var=loop)>
|
||||||
|
${loop.getIndex()}) [${name}=${value}]
|
||||||
|
</for>
|
||||||
1
test/autotests/render/for-props-status-var/test.js
Normal file
1
test/autotests/render/for-props-status-var/test.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
exports.templateData = {};
|
||||||
Loading…
x
Reference in New Issue
Block a user