Skip to content

Commit

Permalink
[soy mode] Add support for index in for loops
Browse files Browse the repository at this point in the history
- Fix variable scrope issue with list comprehensions.

Co-authored-by: Axel Lewenhaupt <lewenhaupt@google.com>
  • Loading branch information
axellew and Axel Lewenhaupt committed Jul 9, 2020
1 parent 82d0f4a commit 0ec0920
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
24 changes: 18 additions & 6 deletions mode/soy/soy.js
Expand Up @@ -34,9 +34,9 @@
"switch": {},
"case": { noEndTag: true, reduceIndent: true},
"default": { noEndTag: true, reduceIndent: true},
"foreach": { variableScope: true, soyState: "var-def" },
"foreach": { variableScope: true, soyState: "for-loop" },
"ifempty": { noEndTag: true, reduceIndent: true},
"for": { variableScope: true, soyState: "var-def" },
"for": { variableScope: true, soyState: "for-loop" },
"call": { soyState: "templ-ref" },
"param": { soyState: "param-ref"},
"print": { noEndTag: true },
Expand Down Expand Up @@ -129,6 +129,7 @@
var match;
if (stream.match(/[[]/)) {
state.soyState.push("list-literal");
state.context = new Context(state.context, "list-literal", state.variables);
state.lookupVariables = false;
return null;
} else if (stream.match(/map\b/)) {
Expand Down Expand Up @@ -364,6 +365,18 @@
stream.next();
return null;

case "for-loop":
if (stream.match(/\bin\b/)) {
state.soyState.pop();
return "keyword";
}
if (stream.peek() == "$") {
state.soyState.push('var-def');
return null;
}
stream.next();
return null;

case "record-literal":
if (stream.match(/^[)]/)) {
state.soyState.pop();
Expand Down Expand Up @@ -394,13 +407,12 @@
if (stream.match(/\]/)) {
state.soyState.pop();
state.lookupVariables = true;
popcontext(state);
return null;
}
if (stream.match(/for\b/)) {
state.soyState.push("var-def")
return "keyword";
} else if (stream.match(/in\b/)) {
if (stream.match(/\bfor\b/)) {
state.lookupVariables = true;
state.soyState.push('for-loop');
return "keyword";
}
return expression(stream, state);
Expand Down
19 changes: 19 additions & 0 deletions mode/soy/test.js
Expand Up @@ -138,6 +138,11 @@
'[keyword {/foreach}]',
'');

MT('foreach-index',
'[keyword {foreach] [def $foo],[def $index] [keyword in] [[]] [keyword }]',
' [keyword {][variable-2 $foo][keyword }] [keyword {][variable-2 $index][keyword }]',
'[keyword {/foreach}]');

MT('nested-kind-test',
'[keyword {template] [def .foo] [attribute kind]=[string "html"][keyword }]',
' [tag&bracket <][tag div][tag&bracket >]',
Expand Down Expand Up @@ -260,6 +265,20 @@
'[keyword {let] [def $test]: [[[variable $a] [operator +] [atom 1] [keyword for] ' +
'[def $a] [keyword in] [variable-2 $myList] [keyword if] [variable-2 $a] [operator >=] [atom 3] ] [keyword /}]');

MT('list-comprehension-index',
'[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
'[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]');


MT('list-comprehension-variable-scope',
'[keyword {let] [def $name]: [string "world"][keyword /}]',
'[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
'[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]',
'[keyword {][variable-2&error $a][keyword }]',
'[keyword {][variable-2&error $index][keyword }]',
'[keyword {][variable-2 $test][keyword }]',
'[keyword {][variable-2 $name][keyword }]');

MT('import',
'[keyword import] {[def Name], [variable Person] [keyword as] [def P]} [keyword from] [string \'examples/proto/example.proto\'];');
})();

0 comments on commit 0ec0920

Please sign in to comment.