From 7ec1bdb7126b185ef2dcf2a7105a8f5f57d6e44c Mon Sep 17 00:00:00 2001 From: Emil Tholin Date: Sat, 27 Apr 2019 21:35:32 +0200 Subject: [PATCH] Don't show 'Empty block' warnings for non-existent await branches --- src/compile/nodes/CatchBlock.ts | 6 +++-- src/compile/nodes/PendingBlock.ts | 6 +++-- src/compile/nodes/ThenBlock.ts | 6 +++-- src/parse/state/mustache.ts | 25 ++++++++++++++----- .../samples/await-then-catch/output.json | 5 +++- .../samples/await-no-catch/input.svelte | 9 +++++++ .../samples/await-no-catch/warnings.json | 1 + .../await-shorthand-no-catch/input.svelte | 7 ++++++ .../await-shorthand-no-catch/warnings.json | 1 + 9 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 test/validator/samples/await-no-catch/input.svelte create mode 100644 test/validator/samples/await-no-catch/warnings.json create mode 100644 test/validator/samples/await-shorthand-no-catch/input.svelte create mode 100644 test/validator/samples/await-shorthand-no-catch/warnings.json diff --git a/src/compile/nodes/CatchBlock.ts b/src/compile/nodes/CatchBlock.ts index f728c1b850..0941e68d5b 100644 --- a/src/compile/nodes/CatchBlock.ts +++ b/src/compile/nodes/CatchBlock.ts @@ -15,6 +15,8 @@ export default class CatchBlock extends Node { this.scope.add(parent.error, parent.expression.dependencies, this); this.children = map_children(component, parent, this.scope, info.children); - this.warn_if_empty_block(); + if (!info.skip) { + this.warn_if_empty_block(); + } } -} \ No newline at end of file +} diff --git a/src/compile/nodes/PendingBlock.ts b/src/compile/nodes/PendingBlock.ts index 720442fab6..61e315481e 100644 --- a/src/compile/nodes/PendingBlock.ts +++ b/src/compile/nodes/PendingBlock.ts @@ -10,6 +10,8 @@ export default class PendingBlock extends Node { super(component, parent, scope, info); this.children = map_children(component, parent, scope, info.children); - this.warn_if_empty_block(); + if (!info.skip) { + this.warn_if_empty_block(); + } } -} \ No newline at end of file +} diff --git a/src/compile/nodes/ThenBlock.ts b/src/compile/nodes/ThenBlock.ts index 54319a7ae5..ace5cfb5c1 100644 --- a/src/compile/nodes/ThenBlock.ts +++ b/src/compile/nodes/ThenBlock.ts @@ -15,6 +15,8 @@ export default class ThenBlock extends Node { this.scope.add(parent.value, parent.expression.dependencies, this); this.children = map_children(component, parent, this.scope, info.children); - this.warn_if_empty_block(); + if (!info.skip) { + this.warn_if_empty_block(); + } } -} \ No newline at end of file +} diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index 1acae36c9c..c24b5995a2 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -172,7 +172,8 @@ export default function mustache(parser: Parser) { start, end: null, type: 'ThenBlock', - children: [] + children: [], + skip: false }; await_block.then = then_block; @@ -196,7 +197,8 @@ export default function mustache(parser: Parser) { start, end: null, type: 'CatchBlock', - children: [] + children: [], + skip: false }; await_block.catch = catch_block; @@ -235,19 +237,22 @@ export default function mustache(parser: Parser) { start: null, end: null, type: 'PendingBlock', - children: [] + children: [], + skip: true }, then: { start: null, end: null, type: 'ThenBlock', - children: [] + children: [], + skip: true }, catch: { start: null, end: null, type: 'CatchBlock', - children: [] + children: [], + skip: true }, } : { @@ -310,7 +315,15 @@ export default function mustache(parser: Parser) { parser.stack.push(block); if (type === 'AwaitBlock') { - const child_block = await_block_shorthand ? block.then : block.pending; + let child_block; + if (await_block_shorthand) { + block.then.skip = false; + child_block = block.then; + } else { + block.pending.skip = false; + child_block = block.pending; + } + child_block.start = parser.index; parser.stack.push(child_block); } diff --git a/test/parser/samples/await-then-catch/output.json b/test/parser/samples/await-then-catch/output.json index 21fc13eff9..f62ad16574 100644 --- a/test/parser/samples/await-then-catch/output.json +++ b/test/parser/samples/await-then-catch/output.json @@ -19,6 +19,7 @@ "pending": { "start": 19, "end": 39, + "skip": false, "type": "PendingBlock", "children": [ { @@ -53,6 +54,7 @@ "then": { "start": 39, "end": 88, + "skip": false, "type": "ThenBlock", "children": [ { @@ -98,6 +100,7 @@ "catch": { "start": 88, "end": 140, + "skip": false, "type": "CatchBlock", "children": [ { @@ -158,4 +161,4 @@ "css": null, "instance": null, "module": null -} \ No newline at end of file +} diff --git a/test/validator/samples/await-no-catch/input.svelte b/test/validator/samples/await-no-catch/input.svelte new file mode 100644 index 0000000000..1d332f0e32 --- /dev/null +++ b/test/validator/samples/await-no-catch/input.svelte @@ -0,0 +1,9 @@ + + +{#await promise} +

Loading

+{:then data} +

Data: {data}

+{/await} diff --git a/test/validator/samples/await-no-catch/warnings.json b/test/validator/samples/await-no-catch/warnings.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/test/validator/samples/await-no-catch/warnings.json @@ -0,0 +1 @@ +[] diff --git a/test/validator/samples/await-shorthand-no-catch/input.svelte b/test/validator/samples/await-shorthand-no-catch/input.svelte new file mode 100644 index 0000000000..e106f8d842 --- /dev/null +++ b/test/validator/samples/await-shorthand-no-catch/input.svelte @@ -0,0 +1,7 @@ + + +{#await promise then data} +

Data: {data}

+{/await} diff --git a/test/validator/samples/await-shorthand-no-catch/warnings.json b/test/validator/samples/await-shorthand-no-catch/warnings.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/test/validator/samples/await-shorthand-no-catch/warnings.json @@ -0,0 +1 @@ +[]