mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: parse init/test/update parts of for loop
This commit is contained in:
parent
0c87b4a91a
commit
a574a2be82
@ -82,6 +82,14 @@ function parseExpression(str, errorMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
function parseStatement(str, errorMessage) {
|
||||
try {
|
||||
return compiler.builder.parseStatement(str);
|
||||
} catch(e) {
|
||||
throwError(errorMessage + ': ' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
function createNumberExpression(str, errorMessage) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
@ -294,6 +302,18 @@ module.exports = function(str) {
|
||||
statusVarName = buildIdentifier(statusVarName, 'Invalid status-var option');
|
||||
}
|
||||
|
||||
if (forInit) {
|
||||
forInit = parseStatement(forInit, 'Invalid for loop init');
|
||||
}
|
||||
|
||||
if (forTest) {
|
||||
forTest = parseExpression(forTest, 'Invalid for loop test');
|
||||
}
|
||||
|
||||
if (forUpdate) {
|
||||
forUpdate = parseExpression(forUpdate, 'Invalid for loop update');
|
||||
}
|
||||
|
||||
// No more tokens... now we need to sort out what happened
|
||||
if (loopType === 'ForEach') {
|
||||
return {
|
||||
|
||||
@ -1,6 +1,33 @@
|
||||
{
|
||||
"loopType": "For",
|
||||
"init": "",
|
||||
"test": " i<foo.length",
|
||||
"update": " i++"
|
||||
"test": {
|
||||
"type": "BinaryExpression",
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"name": "i"
|
||||
},
|
||||
"operator": "<",
|
||||
"right": {
|
||||
"type": "MemberExpression",
|
||||
"object": {
|
||||
"type": "Identifier",
|
||||
"name": "foo"
|
||||
},
|
||||
"property": {
|
||||
"type": "Identifier",
|
||||
"name": "length"
|
||||
},
|
||||
"computed": false
|
||||
}
|
||||
},
|
||||
"update": {
|
||||
"type": "UpdateExpression",
|
||||
"argument": {
|
||||
"type": "Identifier",
|
||||
"name": "i"
|
||||
},
|
||||
"operator": "++",
|
||||
"prefix": false
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,36 @@
|
||||
{
|
||||
"loopType": "For",
|
||||
"init": "var i=0",
|
||||
"test": " i<foo.length",
|
||||
"update": " i++"
|
||||
"init": {
|
||||
"type": "Expression",
|
||||
"value": "var i=0"
|
||||
},
|
||||
"test": {
|
||||
"type": "BinaryExpression",
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"name": "i"
|
||||
},
|
||||
"operator": "<",
|
||||
"right": {
|
||||
"type": "MemberExpression",
|
||||
"object": {
|
||||
"type": "Identifier",
|
||||
"name": "foo"
|
||||
},
|
||||
"property": {
|
||||
"type": "Identifier",
|
||||
"name": "length"
|
||||
},
|
||||
"computed": false
|
||||
}
|
||||
},
|
||||
"update": {
|
||||
"type": "UpdateExpression",
|
||||
"argument": {
|
||||
"type": "Identifier",
|
||||
"name": "i"
|
||||
},
|
||||
"operator": "++",
|
||||
"prefix": false
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user