From 8de7931c64b46d22fc303adce14eb3f680c8ca66 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Fri, 7 Oct 2022 08:23:12 +0800 Subject: [PATCH] [feat] better error message for invalid logic block placement (#7862) * better error message for invalid logic block placement * include checking for {@html} tags in invalid location --- src/compiler/parse/errors.ts | 8 ++++++++ src/compiler/parse/state/tag.ts | 19 ++++++++++++++++--- .../html-block-in-attribute/errors.json | 9 +++++++++ .../html-block-in-attribute/input.svelte | 1 + .../html-block-in-textarea/errors.json | 9 +++++++++ .../html-block-in-textarea/input.svelte | 3 +++ .../logic-block-in-attribute/errors.json | 9 +++++++++ .../logic-block-in-attribute/input.svelte | 1 + .../logic-block-in-textarea/errors.json | 9 +++++++++ .../logic-block-in-textarea/input.svelte | 5 +++++ 10 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 test/validator/samples/html-block-in-attribute/errors.json create mode 100644 test/validator/samples/html-block-in-attribute/input.svelte create mode 100644 test/validator/samples/html-block-in-textarea/errors.json create mode 100644 test/validator/samples/html-block-in-textarea/input.svelte create mode 100644 test/validator/samples/logic-block-in-attribute/errors.json create mode 100644 test/validator/samples/logic-block-in-attribute/input.svelte create mode 100644 test/validator/samples/logic-block-in-textarea/errors.json create mode 100644 test/validator/samples/logic-block-in-textarea/input.svelte diff --git a/src/compiler/parse/errors.ts b/src/compiler/parse/errors.ts index 63bd5b0919..26f03c0cb1 100644 --- a/src/compiler/parse/errors.ts +++ b/src/compiler/parse/errors.ts @@ -107,6 +107,14 @@ export default { code: `invalid-${slug}-placement`, message: `<${name}> tags cannot be inside elements or blocks` }), + invalid_logic_block_placement: (location: string, name: string) => ({ + code: 'invalid-logic-block-placement', + message: `{#${name}} logic block cannot be ${location}` + }), + invalid_tag_placement: (location: string, name: string) => ({ + code: 'invalid-tag-placement', + message: `{@${name}} tag cannot be ${location}` + }), invalid_ref_directive: (name: string) => ({ code: 'invalid-ref-directive', message: `The ref directive is no longer supported — use \`bind:this={${name}}\` instead` diff --git a/src/compiler/parse/state/tag.ts b/src/compiler/parse/state/tag.ts index efce375b7e..4be47f25b0 100644 --- a/src/compiler/parse/state/tag.ts +++ b/src/compiler/parse/state/tag.ts @@ -219,7 +219,8 @@ export default function tag(parser: Parser) { element.children = read_sequence( parser, () => - /^<\/textarea(\s[^>]*)?>/i.test(parser.template.slice(parser.index)) + /^<\/textarea(\s[^>]*)?>/i.test(parser.template.slice(parser.index)), + 'inside \ No newline at end of file diff --git a/test/validator/samples/logic-block-in-attribute/errors.json b/test/validator/samples/logic-block-in-attribute/errors.json new file mode 100644 index 0000000000..ebf68938e7 --- /dev/null +++ b/test/validator/samples/logic-block-in-attribute/errors.json @@ -0,0 +1,9 @@ +[ + { + "code": "invalid-logic-block-placement", + "message": "{#if} logic block cannot be in attribute value", + "start": { "line": 1, "column": 12, "character": 12 }, + "end": { "line": 1, "column": 12, "character": 12 }, + "pos": 12 + } +] diff --git a/test/validator/samples/logic-block-in-attribute/input.svelte b/test/validator/samples/logic-block-in-attribute/input.svelte new file mode 100644 index 0000000000..241529527c --- /dev/null +++ b/test/validator/samples/logic-block-in-attribute/input.svelte @@ -0,0 +1 @@ +
\ No newline at end of file diff --git a/test/validator/samples/logic-block-in-textarea/errors.json b/test/validator/samples/logic-block-in-textarea/errors.json new file mode 100644 index 0000000000..b1d73d4903 --- /dev/null +++ b/test/validator/samples/logic-block-in-textarea/errors.json @@ -0,0 +1,9 @@ +[ + { + "code": "invalid-logic-block-placement", + "message": "{#each} logic block cannot be inside \ No newline at end of file