Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] better error message for invalid logic block placement #7862

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/parse/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
19 changes: 16 additions & 3 deletions src/compiler/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <textarea>'
);
parser.read(/^<\/textarea(\s[^>]*)?>/i);
element.end = parser.index;
Expand Down Expand Up @@ -473,7 +474,7 @@ function read_attribute_value(parser: Parser) {

let value;
try {
value = read_sequence(parser, () => !!parser.match_regex(regex));
value = read_sequence(parser, () => !!parser.match_regex(regex), 'in attribute value');
} catch (error) {
if (error.code === 'parse-error') {
// if the attribute value didn't close + self-closing tag
Expand All @@ -495,7 +496,7 @@ function read_attribute_value(parser: Parser) {
return value;
}

function read_sequence(parser: Parser, done: () => boolean): TemplateNode[] {
function read_sequence(parser: Parser, done: () => boolean, location: string): TemplateNode[] {
let current_chunk: Text = {
start: parser.index,
end: null,
Expand All @@ -521,6 +522,18 @@ function read_sequence(parser: Parser, done: () => boolean): TemplateNode[] {
flush(parser.index);
return chunks;
} else if (parser.eat('{')) {
if (parser.match('#')) {
const index = parser.index - 1;
parser.eat('#');
const name = parser.read_until(/[^a-z]/);
parser.error(parser_errors.invalid_logic_block_placement(location, name), index);
} else if (parser.match('@')) {
const index = parser.index - 1;
parser.eat('@');
const name = parser.read_until(/[^a-z]/);
parser.error(parser_errors.invalid_tag_placement(location, name), index);
}

flush(parser.index - 1);

parser.allow_whitespace();
Expand Down
9 changes: 9 additions & 0 deletions test/validator/samples/html-block-in-attribute/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-tag-placement",
"message": "{@html} tag cannot be in attribute value",
"start": { "line": 1, "column": 12, "character": 12 },
"end": { "line": 1, "column": 12, "character": 12 },
"pos": 12
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div style="{@html text}" />
9 changes: 9 additions & 0 deletions test/validator/samples/html-block-in-textarea/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-tag-placement",
"message": "{@html} tag cannot be inside <textarea>",
"start": { "line": 2, "column": 1, "character": 12 },
"end": { "line": 2, "column": 1, "character": 12 },
"pos": 12
}
]
3 changes: 3 additions & 0 deletions test/validator/samples/html-block-in-textarea/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<textarea>
{@html fruit}
</textarea>
9 changes: 9 additions & 0 deletions test/validator/samples/logic-block-in-attribute/errors.json
Original file line number Diff line number Diff line change
@@ -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
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div style="{#if condition}a{/if}" />
9 changes: 9 additions & 0 deletions test/validator/samples/logic-block-in-textarea/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-logic-block-placement",
"message": "{#each} logic block cannot be inside <textarea>",
"start": { "line": 2, "column": 1, "character": 12 },
"end": { "line": 2, "column": 1, "character": 12 },
"pos": 12
}
]
5 changes: 5 additions & 0 deletions test/validator/samples/logic-block-in-textarea/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<textarea>
{#each fruits as fruit}
{fruit}
{/each}
</textarea>