From a7c6ef3d0aafc969284b9e96c5cd71bd54f22f70 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sat, 2 May 2020 18:55:56 +0300 Subject: [PATCH 1/5] test: update loop custom tag test --- test/expect/loop_customtag.html | 7 +++++++ test/fixtures/loop_customtag.html | 7 +++++-- test/test-loops.js | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/test/expect/loop_customtag.html b/test/expect/loop_customtag.html index bbdaa77..6a9143a 100644 --- a/test/expect/loop_customtag.html +++ b/test/expect/loop_customtag.html @@ -6,4 +6,11 @@

2: 3

+ +

0: 1

+ +

1: 2

+ +

2: 3

+

x

diff --git a/test/fixtures/loop_customtag.html b/test/fixtures/loop_customtag.html index 8683019..2f3641e 100644 --- a/test/fixtures/loop_customtag.html +++ b/test/fixtures/loop_customtag.html @@ -1,5 +1,8 @@

x

- +

{{index}}: {{item}}

-
+ + +

{{index}}: {{item}}

+

x

diff --git a/test/test-loops.js b/test/test-loops.js index 8adf727..3521808 100644 --- a/test/test-loops.js +++ b/test/test-loops.js @@ -86,7 +86,7 @@ test('Loops - conflicting locals', (t) => { test('Loops - custom tag', (t) => { return process(t, 'loop_customtag', { - loopTags: ['zeach'], + loopTags: ['for', 'each'], locals: { items: [1, 2, 3] } }) }) From 9874a8d84056c679a08ab6698b27a7e217502940 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sat, 2 May 2020 18:58:55 +0300 Subject: [PATCH 2/5] fix: support multiple custom loop tag names --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2e751ee..702fa2f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -204,7 +204,7 @@ function walk (opts, nodes) { } // if the node has content, recurse (unless it's a loop, handled later) - if (node.content && node.tag !== loops[0] && node.tag !== scopes[0]) { + if (node.content && !loops.includes(node.tag) && node.tag !== scopes[0]) { node.content = walk(opts, node.content) } @@ -318,7 +318,7 @@ function walk (opts, nodes) { } // parse loops - if (node.tag === loops[0]) { + if (loops.includes(node.tag)) { // handle syntax error if (!(node.attrs && node.attrs.loop)) { throw new Error(`the "${loops[0]}" tag must have a "loop" attribute`) From aac624b499e9e128f9d21a26db3a2d6b84bc39cb Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Sat, 2 May 2020 19:18:02 +0300 Subject: [PATCH 3/5] refactor: tag name in syntax error message --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 702fa2f..36e2969 100644 --- a/lib/index.js +++ b/lib/index.js @@ -321,7 +321,8 @@ function walk (opts, nodes) { if (loops.includes(node.tag)) { // handle syntax error if (!(node.attrs && node.attrs.loop)) { - throw new Error(`the "${loops[0]}" tag must have a "loop" attribute`) + const tagName = loops.find(() => node.tag) + throw new Error(`the "${tagName}" tag must have a "loop" attribute`) } // parse the "loop" param From 08baefef2a64130affc80aee36a3a7587f69f3bd Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 6 May 2020 11:11:49 +0300 Subject: [PATCH 4/5] refactor: use explicit comparison when recursing --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 36e2969..5a40e5d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -204,7 +204,7 @@ function walk (opts, nodes) { } // if the node has content, recurse (unless it's a loop, handled later) - if (node.content && !loops.includes(node.tag) && node.tag !== scopes[0]) { + if (node.content && loops.includes(node.tag) === false && node.tag !== scopes[0]) { node.content = walk(opts, node.content) } From 331f5e42f5e84f3d93971d0edf1f25a2a0237113 Mon Sep 17 00:00:00 2001 From: Cosmin Popovici Date: Wed, 6 May 2020 11:13:34 +0300 Subject: [PATCH 5/5] refactor: use known tag name in error message --- lib/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5a40e5d..6074742 100644 --- a/lib/index.js +++ b/lib/index.js @@ -321,8 +321,7 @@ function walk (opts, nodes) { if (loops.includes(node.tag)) { // handle syntax error if (!(node.attrs && node.attrs.loop)) { - const tagName = loops.find(() => node.tag) - throw new Error(`the "${tagName}" tag must have a "loop" attribute`) + throw new Error(`the "${node.tag}" tag must have a "loop" attribute`) } // parse the "loop" param