Skip to content

Commit 0ec0920

Browse files
axellewAxel Lewenhaupt
andauthored
[soy mode] Add support for index in for loops
- Fix variable scrope issue with list comprehensions. Co-authored-by: Axel Lewenhaupt <[email protected]>
1 parent 82d0f4a commit 0ec0920

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

mode/soy/soy.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
"switch": {},
3535
"case": { noEndTag: true, reduceIndent: true},
3636
"default": { noEndTag: true, reduceIndent: true},
37-
"foreach": { variableScope: true, soyState: "var-def" },
37+
"foreach": { variableScope: true, soyState: "for-loop" },
3838
"ifempty": { noEndTag: true, reduceIndent: true},
39-
"for": { variableScope: true, soyState: "var-def" },
39+
"for": { variableScope: true, soyState: "for-loop" },
4040
"call": { soyState: "templ-ref" },
4141
"param": { soyState: "param-ref"},
4242
"print": { noEndTag: true },
@@ -129,6 +129,7 @@
129129
var match;
130130
if (stream.match(/[[]/)) {
131131
state.soyState.push("list-literal");
132+
state.context = new Context(state.context, "list-literal", state.variables);
132133
state.lookupVariables = false;
133134
return null;
134135
} else if (stream.match(/map\b/)) {
@@ -364,6 +365,18 @@
364365
stream.next();
365366
return null;
366367

368+
case "for-loop":
369+
if (stream.match(/\bin\b/)) {
370+
state.soyState.pop();
371+
return "keyword";
372+
}
373+
if (stream.peek() == "$") {
374+
state.soyState.push('var-def');
375+
return null;
376+
}
377+
stream.next();
378+
return null;
379+
367380
case "record-literal":
368381
if (stream.match(/^[)]/)) {
369382
state.soyState.pop();
@@ -394,13 +407,12 @@
394407
if (stream.match(/\]/)) {
395408
state.soyState.pop();
396409
state.lookupVariables = true;
410+
popcontext(state);
397411
return null;
398412
}
399-
if (stream.match(/for\b/)) {
400-
state.soyState.push("var-def")
401-
return "keyword";
402-
} else if (stream.match(/in\b/)) {
413+
if (stream.match(/\bfor\b/)) {
403414
state.lookupVariables = true;
415+
state.soyState.push('for-loop');
404416
return "keyword";
405417
}
406418
return expression(stream, state);

mode/soy/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
'[keyword {/foreach}]',
139139
'');
140140

141+
MT('foreach-index',
142+
'[keyword {foreach] [def $foo],[def $index] [keyword in] [[]] [keyword }]',
143+
' [keyword {][variable-2 $foo][keyword }] [keyword {][variable-2 $index][keyword }]',
144+
'[keyword {/foreach}]');
145+
141146
MT('nested-kind-test',
142147
'[keyword {template] [def .foo] [attribute kind]=[string "html"][keyword }]',
143148
' [tag&bracket <][tag div][tag&bracket >]',
@@ -260,6 +265,20 @@
260265
'[keyword {let] [def $test]: [[[variable $a] [operator +] [atom 1] [keyword for] ' +
261266
'[def $a] [keyword in] [variable-2 $myList] [keyword if] [variable-2 $a] [operator >=] [atom 3] ] [keyword /}]');
262267

268+
MT('list-comprehension-index',
269+
'[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
270+
'[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]');
271+
272+
273+
MT('list-comprehension-variable-scope',
274+
'[keyword {let] [def $name]: [string "world"][keyword /}]',
275+
'[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
276+
'[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]',
277+
'[keyword {][variable-2&error $a][keyword }]',
278+
'[keyword {][variable-2&error $index][keyword }]',
279+
'[keyword {][variable-2 $test][keyword }]',
280+
'[keyword {][variable-2 $name][keyword }]');
281+
263282
MT('import',
264283
'[keyword import] {[def Name], [variable Person] [keyword as] [def P]} [keyword from] [string \'examples/proto/example.proto\'];');
265284
})();

0 commit comments

Comments
 (0)