change :elseif to :else if - fixes #2137

pull/2138/head
Richard Harris 6 years ago
parent c672ad8df7
commit 6c85f6a6b5

@ -86,58 +86,73 @@ export default function mustache(parser: Parser) {
block.end = parser.index; block.end = parser.index;
parser.stack.pop(); parser.stack.pop();
} else if (parser.eat(':elseif')) { } else if (parser.eat(':else')) {
const block = parser.current(); if (parser.eat('if')) {
if (block.type !== 'IfBlock')
parser.error({ parser.error({
code: `invalid-elseif-placement`, code: 'invalid-elseif',
message: 'Cannot have an {:elseif ...} block outside an {#if ...} block' message: `'elseif' should be 'else if'`
}); });
}
parser.requireWhitespace(); parser.allowWhitespace();
const expression = readExpression(parser); // :else if
if (parser.eat('if')) {
const block = parser.current();
if (block.type !== 'IfBlock')
parser.error({
code: `invalid-elseif-placement`,
message: 'Cannot have an {:else if ...} block outside an {#if ...} block'
});
parser.allowWhitespace(); parser.requireWhitespace();
parser.eat('}', true);
block.else = { const expression = readExpression(parser);
start: parser.index,
end: null,
type: 'ElseBlock',
children: [
{
start: parser.index,
end: null,
type: 'IfBlock',
elseif: true,
expression,
children: [],
},
],
};
parser.stack.push(block.else.children[0]); parser.allowWhitespace();
} else if (parser.eat(':else')) { parser.eat('}', true);
const block = parser.current();
if (block.type !== 'IfBlock' && block.type !== 'EachBlock') { block.else = {
parser.error({ start: parser.index,
code: `invalid-else-placement`, end: null,
message: 'Cannot have an {:else} block outside an {#if ...} or {#each ...} block' type: 'ElseBlock',
}); children: [
{
start: parser.index,
end: null,
type: 'IfBlock',
elseif: true,
expression,
children: [],
},
],
};
parser.stack.push(block.else.children[0]);
} }
parser.allowWhitespace(); // :else
parser.eat('}', true); else {
const block = parser.current();
if (block.type !== 'IfBlock' && block.type !== 'EachBlock') {
parser.error({
code: `invalid-else-placement`,
message: 'Cannot have an {:else} block outside an {#if ...} or {#each ...} block'
});
}
parser.allowWhitespace();
parser.eat('}', true);
block.else = { block.else = {
start: parser.index, start: parser.index,
end: null, end: null,
type: 'ElseBlock', type: 'ElseBlock',
children: [], children: [],
}; };
parser.stack.push(block.else); parser.stack.push(block.else);
}
} else if (parser.eat(':then')) { } else if (parser.eat(':then')) {
// TODO DRY out this and the next section // TODO DRY out this and the next section
const pendingBlock = parser.current(); const pendingBlock = parser.current();

@ -1,5 +1,5 @@
{#if x > 10} {#if x > 10}
<p>x is greater than 10</p> <p>x is greater than 10</p>
{:elseif x < 5} {:else if x < 5}
<p>x is less than 5</p> <p>x is less than 5</p>
{/if} {/if}

@ -1,12 +1,12 @@
{ {
"html": { "html": {
"start": 0, "start": 0,
"end": 88, "end": 89,
"type": "Fragment", "type": "Fragment",
"children": [ "children": [
{ {
"start": 0, "start": 0,
"end": 88, "end": 89,
"type": "IfBlock", "type": "IfBlock",
"expression": { "expression": {
"type": "BinaryExpression", "type": "BinaryExpression",
@ -45,45 +45,45 @@
} }
], ],
"else": { "else": {
"start": 57, "start": 58,
"end": 83, "end": 84,
"type": "ElseBlock", "type": "ElseBlock",
"children": [ "children": [
{ {
"start": 57, "start": 58,
"end": 88, "end": 89,
"type": "IfBlock", "type": "IfBlock",
"elseif": true, "elseif": true,
"expression": { "expression": {
"type": "BinaryExpression", "type": "BinaryExpression",
"start": 51, "start": 52,
"end": 56, "end": 57,
"left": { "left": {
"type": "Identifier", "type": "Identifier",
"start": 51, "start": 52,
"end": 52, "end": 53,
"name": "x" "name": "x"
}, },
"operator": "<", "operator": "<",
"right": { "right": {
"type": "Literal", "type": "Literal",
"start": 55, "start": 56,
"end": 56, "end": 57,
"value": 5, "value": 5,
"raw": "5" "raw": "5"
} }
}, },
"children": [ "children": [
{ {
"start": 59, "start": 60,
"end": 82, "end": 83,
"type": "Element", "type": "Element",
"name": "p", "name": "p",
"attributes": [], "attributes": [],
"children": [ "children": [
{ {
"start": 62, "start": 63,
"end": 78, "end": 79,
"type": "Text", "type": "Text",
"data": "x is less than 5" "data": "x is less than 5"
} }
@ -95,8 +95,5 @@
} }
} }
] ]
}, }
"css": null,
"instance": null,
"module": null
} }

@ -17,7 +17,7 @@
{#if foo} {#if foo}
<p>foo</p> <p>foo</p>
{:elseif bar} {:else if bar}
<p>bar</p> <p>bar</p>
{:else} {:else}
<p>neither foo nor bar</p> <p>neither foo nor bar</p>

@ -1,5 +1,5 @@
{#if x > 10} {#if x > 10}
<p>x is greater than 10</p> <p>x is greater than 10</p>
{:elseif x < 5} {:else if x < 5}
<p>x is less than 5</p> <p>x is less than 5</p>
{/if} {/if}

@ -1 +1 @@
before-{#if x > 10}if{:elseif x < 5}elseif{:else}else{/if}-after before-{#if x > 10}if{:else if x < 5}elseif{:else}else{/if}-after

@ -1,6 +1,6 @@
{#if x > 10} {#if x > 10}
<p>x is greater than 10</p> <p>x is greater than 10</p>
{:elseif x < 5} {:else if x < 5}
<p>x is less than 5</p> <p>x is less than 5</p>
{:else} {:else}
<p>x is between 5 and 10</p> <p>x is between 5 and 10</p>

@ -7,7 +7,7 @@
{#if foo} {#if foo}
{#if false} {#if false}
<Component/> <Component/>
{:elseif false} {:else if false}
<Component/> <Component/>
{/if} {/if}
{/if} {/if}

@ -17,6 +17,6 @@
{#if x} {#if x}
<div bind:this={yes} out:foo>yes</div> <div bind:this={yes} out:foo>yes</div>
{:elseif y} {:else if y}
<div bind:this={no} out:foo>no</div> <div bind:this={no} out:foo>no</div>
{/if} {/if}
Loading…
Cancel
Save