From 6c85f6a6b5871d396f64d4ccdccdd41db3dc9e74 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Wed, 27 Feb 2019 08:17:55 -0500 Subject: [PATCH] change :elseif to :else if - fixes #2137 --- src/parse/state/mustache.ts | 97 +++++++++++-------- .../samples/if-block-elseif/input.svelte | 2 +- .../samples/if-block-elseif/output.json | 37 ++++--- .../dynamic-component-slot/main.svelte | 2 +- .../if-block-elseif-no-else/main.svelte | 2 +- .../samples/if-block-elseif-text/main.svelte | 2 +- .../samples/if-block-elseif/main.svelte | 2 +- .../if-block-outro-nested-else/main.svelte | 2 +- .../main.svelte | 2 +- 9 files changed, 80 insertions(+), 68 deletions(-) diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index 7a3b714d5c..980ec8555a 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -86,58 +86,73 @@ export default function mustache(parser: Parser) { block.end = parser.index; parser.stack.pop(); - } else if (parser.eat(':elseif')) { - const block = parser.current(); - if (block.type !== 'IfBlock') + } else if (parser.eat(':else')) { + if (parser.eat('if')) { parser.error({ - code: `invalid-elseif-placement`, - message: 'Cannot have an {:elseif ...} block outside an {#if ...} block' + code: 'invalid-elseif', + 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.eat('}', true); + parser.requireWhitespace(); - block.else = { - start: parser.index, - end: null, - type: 'ElseBlock', - children: [ - { - start: parser.index, - end: null, - type: 'IfBlock', - elseif: true, - expression, - children: [], - }, - ], - }; + const expression = readExpression(parser); - parser.stack.push(block.else.children[0]); - } else if (parser.eat(':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 = { + 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(); - parser.eat('}', true); + // :else + 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 = { - start: parser.index, - end: null, - type: 'ElseBlock', - children: [], - }; + block.else = { + start: parser.index, + end: null, + type: 'ElseBlock', + children: [], + }; - parser.stack.push(block.else); + parser.stack.push(block.else); + } } else if (parser.eat(':then')) { // TODO DRY out this and the next section const pendingBlock = parser.current(); diff --git a/test/parser/samples/if-block-elseif/input.svelte b/test/parser/samples/if-block-elseif/input.svelte index a3bd474af7..2566aef258 100644 --- a/test/parser/samples/if-block-elseif/input.svelte +++ b/test/parser/samples/if-block-elseif/input.svelte @@ -1,5 +1,5 @@ {#if x > 10}

x is greater than 10

-{:elseif x < 5} +{:else if x < 5}

x is less than 5

{/if} diff --git a/test/parser/samples/if-block-elseif/output.json b/test/parser/samples/if-block-elseif/output.json index 426e1f7fbc..188b57aaad 100644 --- a/test/parser/samples/if-block-elseif/output.json +++ b/test/parser/samples/if-block-elseif/output.json @@ -1,12 +1,12 @@ { "html": { "start": 0, - "end": 88, + "end": 89, "type": "Fragment", "children": [ { "start": 0, - "end": 88, + "end": 89, "type": "IfBlock", "expression": { "type": "BinaryExpression", @@ -45,45 +45,45 @@ } ], "else": { - "start": 57, - "end": 83, + "start": 58, + "end": 84, "type": "ElseBlock", "children": [ { - "start": 57, - "end": 88, + "start": 58, + "end": 89, "type": "IfBlock", "elseif": true, "expression": { "type": "BinaryExpression", - "start": 51, - "end": 56, + "start": 52, + "end": 57, "left": { "type": "Identifier", - "start": 51, - "end": 52, + "start": 52, + "end": 53, "name": "x" }, "operator": "<", "right": { "type": "Literal", - "start": 55, - "end": 56, + "start": 56, + "end": 57, "value": 5, "raw": "5" } }, "children": [ { - "start": 59, - "end": 82, + "start": 60, + "end": 83, "type": "Element", "name": "p", "attributes": [], "children": [ { - "start": 62, - "end": 78, + "start": 63, + "end": 79, "type": "Text", "data": "x is less than 5" } @@ -95,8 +95,5 @@ } } ] - }, - "css": null, - "instance": null, - "module": null + } } \ No newline at end of file diff --git a/test/runtime/samples/dynamic-component-slot/main.svelte b/test/runtime/samples/dynamic-component-slot/main.svelte index 99bcf9e64f..ed357f4302 100644 --- a/test/runtime/samples/dynamic-component-slot/main.svelte +++ b/test/runtime/samples/dynamic-component-slot/main.svelte @@ -17,7 +17,7 @@ {#if foo}

foo

- {:elseif bar} + {:else if bar}

bar

{:else}

neither foo nor bar

diff --git a/test/runtime/samples/if-block-elseif-no-else/main.svelte b/test/runtime/samples/if-block-elseif-no-else/main.svelte index a3bd474af7..2566aef258 100644 --- a/test/runtime/samples/if-block-elseif-no-else/main.svelte +++ b/test/runtime/samples/if-block-elseif-no-else/main.svelte @@ -1,5 +1,5 @@ {#if x > 10}

x is greater than 10

-{:elseif x < 5} +{:else if x < 5}

x is less than 5

{/if} diff --git a/test/runtime/samples/if-block-elseif-text/main.svelte b/test/runtime/samples/if-block-elseif-text/main.svelte index 43ac545677..ea1e1aac43 100644 --- a/test/runtime/samples/if-block-elseif-text/main.svelte +++ b/test/runtime/samples/if-block-elseif-text/main.svelte @@ -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 diff --git a/test/runtime/samples/if-block-elseif/main.svelte b/test/runtime/samples/if-block-elseif/main.svelte index ffa5879943..d4833c7aeb 100644 --- a/test/runtime/samples/if-block-elseif/main.svelte +++ b/test/runtime/samples/if-block-elseif/main.svelte @@ -1,6 +1,6 @@ {#if x > 10}

x is greater than 10

-{:elseif x < 5} +{:else if x < 5}

x is less than 5

{:else}

x is between 5 and 10

diff --git a/test/runtime/samples/if-block-outro-nested-else/main.svelte b/test/runtime/samples/if-block-outro-nested-else/main.svelte index 3937998c40..8b8f599e4f 100644 --- a/test/runtime/samples/if-block-outro-nested-else/main.svelte +++ b/test/runtime/samples/if-block-outro-nested-else/main.svelte @@ -7,7 +7,7 @@ {#if foo} {#if false} - {:elseif false} + {:else if false} {/if} {/if} diff --git a/test/runtime/samples/transition-js-if-elseif-block-outro/main.svelte b/test/runtime/samples/transition-js-if-elseif-block-outro/main.svelte index 462e073bd7..99c535dca2 100644 --- a/test/runtime/samples/transition-js-if-elseif-block-outro/main.svelte +++ b/test/runtime/samples/transition-js-if-elseif-block-outro/main.svelte @@ -17,6 +17,6 @@ {#if x}
yes
-{:elseif y} +{:else if y}
no
{/if} \ No newline at end of file