Control flow and looping migration fixes

This commit is contained in:
Dylan Piercey 2019-07-02 14:03:47 -07:00
parent 535daf9451
commit e1a1d401ae
No known key found for this signature in database
GPG Key ID: DA54E25D5BF13DBE
6 changed files with 30 additions and 16 deletions

View File

@ -10,15 +10,12 @@ const CONTROL_FLOW_ATTRIBUTES = [
module.exports = function migrate(el, context) {
const builder = context.builder;
if (CONTROL_FLOW_ATTRIBUTES.includes(el.tagName)) {
return;
}
el.forEachAttribute(attr => {
const name = attr.name;
if (
CONTROL_FLOW_ATTRIBUTES.includes(name) &&
(name === "else" || attr.argument)
(name === "else" || attr.argument) &&
(el.tagName !== "else" || name !== "if") // <else if(x)> gets passed through
) {
context.deprecate(
`The "${name}" attribute is deprecated. Please use the <${name}> tag instead. See: https://github.com/marko-js/marko/wiki/Deprecation:-control-flow-directive`

View File

@ -258,20 +258,23 @@ function normalizeParts(parsed, builder) {
const update = parsed.update;
const test = parsed.test;
if (
!init ||
!update ||
!test ||
init.type !== "Vars" ||
init.declarations.length !== 1 ||
test.type !== "BinaryExpression"
) {
if (!init || !update || !test || test.type !== "BinaryExpression") {
return;
}
const declarator = init.declarations[0];
const varName = declarator.id;
let from = declarator.init;
let varName;
let from;
if (init.type === "Vars" && init.declarations.length === 1) {
const declarator = init.declarations[0];
varName = declarator.id;
from = declarator.init;
} else if (init.type === "Assignment" && init.operator === "=") {
varName = init.left;
from = init.right;
} else {
return;
}
if (!from) {
return;

View File

@ -18,3 +18,8 @@
<if(!x)>
<div/>
</if>
<if(y)>
<if(x)>A</if>
</if>
<!-- passed through -->
<else if(x)>A</else>

View File

@ -5,3 +5,7 @@
<span else(z)/>
<div unless(x)/>
<if(x) if(y)>A</if>
<!-- passed through -->
<else if(x)>A</else>

View File

@ -94,6 +94,7 @@ $ input.iterator(colors, function(item) {
<!-- Regular -->
<for|i| from=0 to=(list.length - 1)>${i}</for>
<for|i| from=0 to=listSize step=2>${i}</for>
<for|i| from=0 to=listSize step=2>${i}</for>
<!-- Stange: backwards -->
$ var i = 0;
<while(list.length >= i)>

View File

@ -92,6 +92,10 @@
${i}
</for>
<for(i = 0; i <= listSize; i += 2)>
${i}
</for>
<!-- Stange: backwards -->
<for(var i = 0; list.length >= i; i++)>
${i}