From 5fb7089624d2b0eac36cc9dd60a2cb632b7111f0 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Mon, 28 Dec 2015 11:45:40 -0700 Subject: [PATCH] marko v3: custom iterator support --- compiler/ast/ForEach.js | 15 ++++++++++++++- .../looping-iterator/expected.html | 1 - .../looping-iterator/template.marko | 17 ----------------- .../autotest/for-iterator-attr/expected.html | 1 + .../autotest/for-iterator-attr/template.marko | 3 +++ .../for-iterator-attr}/test.js | 0 .../for-iterator-status-var-attr/expected.html | 1 + .../for-iterator-status-var-attr/template.marko | 3 +++ .../for-iterator-status-var-attr/test.js | 9 +++++++++ .../for-iterator-status-var/expected.html | 1 + .../for-iterator-status-var/template.marko | 3 +++ .../autotest/for-iterator-status-var/test.js | 9 +++++++++ .../render/autotest/for-iterator/expected.html | 1 + .../render/autotest/for-iterator/template.marko | 3 +++ .../render/autotest/for-iterator/test.js | 9 +++++++++ 15 files changed, 57 insertions(+), 19 deletions(-) delete mode 100644 test/fixtures/render/autotest-pending/looping-iterator/expected.html delete mode 100644 test/fixtures/render/autotest-pending/looping-iterator/template.marko create mode 100644 test/fixtures/render/autotest/for-iterator-attr/expected.html create mode 100644 test/fixtures/render/autotest/for-iterator-attr/template.marko rename test/fixtures/render/{autotest-pending/looping-iterator => autotest/for-iterator-attr}/test.js (100%) create mode 100644 test/fixtures/render/autotest/for-iterator-status-var-attr/expected.html create mode 100644 test/fixtures/render/autotest/for-iterator-status-var-attr/template.marko create mode 100644 test/fixtures/render/autotest/for-iterator-status-var-attr/test.js create mode 100644 test/fixtures/render/autotest/for-iterator-status-var/expected.html create mode 100644 test/fixtures/render/autotest/for-iterator-status-var/template.marko create mode 100644 test/fixtures/render/autotest/for-iterator-status-var/test.js create mode 100644 test/fixtures/render/autotest/for-iterator/expected.html create mode 100644 test/fixtures/render/autotest/for-iterator/template.marko create mode 100644 test/fixtures/render/autotest/for-iterator/test.js diff --git a/compiler/ast/ForEach.js b/compiler/ast/ForEach.js index 080cbcde0..a987c15f9 100644 --- a/compiler/ast/ForEach.js +++ b/compiler/ast/ForEach.js @@ -10,6 +10,7 @@ class ForEach extends Node { this.body = this.makeContainer(def.body); this.separator = def.separator; this.statusVarName = def.statusVarName; + this.iterator = def.iterator; ok(this.varName, '"varName" is required'); ok(this.in != null, '"in" is required'); @@ -20,6 +21,7 @@ class ForEach extends Node { var inExpression = this.in; var separator = this.separator; var statusVarName = this.statusVarName; + var iterator = this.iterator; var builder = generator.builder; @@ -27,7 +29,18 @@ class ForEach extends Node { statusVarName = '__loop'; } - if (statusVarName) { + if (iterator) { + let params = [varName]; + + if (statusVarName) { + params.push(statusVarName); + } + + return builder.functionCall(iterator, [ + inExpression, + builder.functionDeclaration(null, params, this.body) + ]); + } else if (statusVarName) { let forEachVarName = generator.addStaticVar('forEachWithStatusVar', '__helpers.fv'); let body = this.body; diff --git a/test/fixtures/render/autotest-pending/looping-iterator/expected.html b/test/fixtures/render/autotest-pending/looping-iterator/expected.html deleted file mode 100644 index ed6cd2adb..000000000 --- a/test/fixtures/render/autotest-pending/looping-iterator/expected.html +++ /dev/null @@ -1 +0,0 @@ -cba2-c1-b0-a
c
b
a
2-c
1-b
0-a
\ No newline at end of file diff --git a/test/fixtures/render/autotest-pending/looping-iterator/template.marko b/test/fixtures/render/autotest-pending/looping-iterator/template.marko deleted file mode 100644 index 61ae971e5..000000000 --- a/test/fixtures/render/autotest-pending/looping-iterator/template.marko +++ /dev/null @@ -1,17 +0,0 @@ - - ${item} - - - - - ${status.index}-${item} - - -
- ${item} -
- - -
- ${status.index}-${item} -
\ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-attr/expected.html b/test/fixtures/render/autotest/for-iterator-attr/expected.html new file mode 100644 index 000000000..853d53157 --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-attr/expected.html @@ -0,0 +1 @@ +
c
b
a
\ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-attr/template.marko b/test/fixtures/render/autotest/for-iterator-attr/template.marko new file mode 100644 index 000000000..2ead1c6a8 --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-attr/template.marko @@ -0,0 +1,3 @@ +
+ ${item} +
\ No newline at end of file diff --git a/test/fixtures/render/autotest-pending/looping-iterator/test.js b/test/fixtures/render/autotest/for-iterator-attr/test.js similarity index 100% rename from test/fixtures/render/autotest-pending/looping-iterator/test.js rename to test/fixtures/render/autotest/for-iterator-attr/test.js diff --git a/test/fixtures/render/autotest/for-iterator-status-var-attr/expected.html b/test/fixtures/render/autotest/for-iterator-status-var-attr/expected.html new file mode 100644 index 000000000..52e051fce --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var-attr/expected.html @@ -0,0 +1 @@ +
2-c
1-b
0-a
\ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-status-var-attr/template.marko b/test/fixtures/render/autotest/for-iterator-status-var-attr/template.marko new file mode 100644 index 000000000..669e2ee46 --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var-attr/template.marko @@ -0,0 +1,3 @@ +
+ ${status.index}-${item} +
\ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-status-var-attr/test.js b/test/fixtures/render/autotest/for-iterator-status-var-attr/test.js new file mode 100644 index 000000000..73303673b --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var-attr/test.js @@ -0,0 +1,9 @@ +exports.templateData = { + reverseIterator: function(arrayList, callback){ + var statusVar = {first: 0, last: arrayList.length-1}; + for(var i=arrayList.length-1; i>=0; i--){ + statusVar.index = i; + callback(arrayList[i], statusVar); + } + } +}; diff --git a/test/fixtures/render/autotest/for-iterator-status-var/expected.html b/test/fixtures/render/autotest/for-iterator-status-var/expected.html new file mode 100644 index 000000000..0bbeda048 --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var/expected.html @@ -0,0 +1 @@ +2-c1-b0-a \ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-status-var/template.marko b/test/fixtures/render/autotest/for-iterator-status-var/template.marko new file mode 100644 index 000000000..4185c475c --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var/template.marko @@ -0,0 +1,3 @@ + + ${status.index}-${item} + \ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator-status-var/test.js b/test/fixtures/render/autotest/for-iterator-status-var/test.js new file mode 100644 index 000000000..73303673b --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator-status-var/test.js @@ -0,0 +1,9 @@ +exports.templateData = { + reverseIterator: function(arrayList, callback){ + var statusVar = {first: 0, last: arrayList.length-1}; + for(var i=arrayList.length-1; i>=0; i--){ + statusVar.index = i; + callback(arrayList[i], statusVar); + } + } +}; diff --git a/test/fixtures/render/autotest/for-iterator/expected.html b/test/fixtures/render/autotest/for-iterator/expected.html new file mode 100644 index 000000000..051e235a5 --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator/expected.html @@ -0,0 +1 @@ +cba \ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator/template.marko b/test/fixtures/render/autotest/for-iterator/template.marko new file mode 100644 index 000000000..2f5bf609a --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator/template.marko @@ -0,0 +1,3 @@ + + ${item} + \ No newline at end of file diff --git a/test/fixtures/render/autotest/for-iterator/test.js b/test/fixtures/render/autotest/for-iterator/test.js new file mode 100644 index 000000000..73303673b --- /dev/null +++ b/test/fixtures/render/autotest/for-iterator/test.js @@ -0,0 +1,9 @@ +exports.templateData = { + reverseIterator: function(arrayList, callback){ + var statusVar = {first: 0, last: arrayList.length-1}; + for(var i=arrayList.length-1; i>=0; i--){ + statusVar.index = i; + callback(arrayList[i], statusVar); + } + } +};