From 3146e93bbb34fff06dad9c87c9fd53b3865896f7 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:53:33 +0100 Subject: [PATCH] chore: loose parser improvements (#14733) * handle invalid expression inside each key * handle invalid expression inside each expression * handle invalid expression inside await block * handle "in the middle of typing" components with starting lowercase and dot at the end * changeset --- .changeset/tiny-kings-serve.md | 5 + .../phases/1-parse/read/expression.js | 5 +- .../compiler/phases/1-parse/state/element.js | 9 +- .../src/compiler/phases/1-parse/state/tag.js | 55 +++- .../compiler/phases/1-parse/utils/bracket.js | 5 + .../loose-invalid-expression/input.svelte | 12 + .../loose-invalid-expression/output.json | 268 +++++++++++++++++- .../samples/loose-unclosed-tag/input.svelte | 8 + .../samples/loose-unclosed-tag/output.json | 146 +++++++--- .../loose-invalid-expression/input.svelte | 12 + .../loose-invalid-expression/output.json | 234 ++++++++++++++- .../samples/loose-unclosed-tag/input.svelte | 8 + .../samples/loose-unclosed-tag/output.json | 168 ++++++++--- 13 files changed, 836 insertions(+), 99 deletions(-) create mode 100644 .changeset/tiny-kings-serve.md diff --git a/.changeset/tiny-kings-serve.md b/.changeset/tiny-kings-serve.md new file mode 100644 index 0000000000..b883767132 --- /dev/null +++ b/.changeset/tiny-kings-serve.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +chore: more loose parser improvements diff --git a/packages/svelte/src/compiler/phases/1-parse/read/expression.js b/packages/svelte/src/compiler/phases/1-parse/read/expression.js index 021bd95a6e..907608b11b 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/expression.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/expression.js @@ -7,9 +7,10 @@ import { find_matching_bracket } from '../utils/bracket.js'; /** * @param {Parser} parser + * @param {string} [opening_token] * @returns {Expression} */ -export default function read_expression(parser) { +export default function read_expression(parser, opening_token) { try { const node = parse_expression_at(parser.template, parser.ts, parser.index); @@ -42,7 +43,7 @@ export default function read_expression(parser) { } catch (err) { if (parser.loose) { // Find the next } and treat it as the end of the expression - const end = find_matching_bracket(parser.template, parser.index, '{'); + const end = find_matching_bracket(parser.template, parser.index, opening_token ?? '{'); if (end) { const start = parser.index; parser.index = end; diff --git a/packages/svelte/src/compiler/phases/1-parse/state/element.js b/packages/svelte/src/compiler/phases/1-parse/state/element.js index c112cf6789..66946a8f8d 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/element.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/element.js @@ -123,8 +123,11 @@ export default function element(parser) { } if (!regex_valid_element_name.test(name) && !regex_valid_component_name.test(name)) { - const bounds = { start: start + 1, end: start + 1 + name.length }; - e.tag_invalid_name(bounds); + //