From 67422c43343bc11d730b0a0fd6460a4fc36c7b18 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 24 Jun 2024 12:22:27 +0200 Subject: [PATCH] fix: allow whitespace before an after snippet parameters (#12159) fixes #12156 Also remove obsolete code --- .../src/compiler/phases/1-parse/read/context.js | 16 ++++------------ .../src/compiler/phases/1-parse/state/tag.js | 3 +++ .../samples/snippet-whitespace-ok/errors.json | 1 + .../samples/snippet-whitespace-ok/input.svelte | 4 ++++ 4 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 packages/svelte/tests/validator/samples/snippet-whitespace-ok/errors.json create mode 100644 packages/svelte/tests/validator/samples/snippet-whitespace-ok/input.svelte diff --git a/packages/svelte/src/compiler/phases/1-parse/read/context.js b/packages/svelte/src/compiler/phases/1-parse/read/context.js index 1188fb8eb4..0ae8eab512 100644 --- a/packages/svelte/src/compiler/phases/1-parse/read/context.js +++ b/packages/svelte/src/compiler/phases/1-parse/read/context.js @@ -14,17 +14,16 @@ import { locator } from '../../../state.js'; /** * @param {import('../index.js').Parser} parser - * @param {boolean} [optional_allowed] * @returns {import('estree').Pattern} */ -export default function read_pattern(parser, optional_allowed = false) { +export default function read_pattern(parser) { const start = parser.index; let i = parser.index; const code = full_char_code_at(parser.template, i); if (isIdentifierStart(code, true)) { const name = /** @type {string} */ (parser.read_identifier()); - const annotation = read_type_annotation(parser, optional_allowed); + const annotation = read_type_annotation(parser); return { type: 'Identifier', @@ -84,7 +83,7 @@ export default function read_pattern(parser, optional_allowed = false) { parse_expression_at(`${space_with_newline}(${pattern_string} = 1)`, parser.ts, start - 1) ).left; - expression.typeAnnotation = read_type_annotation(parser, optional_allowed); + expression.typeAnnotation = read_type_annotation(parser); if (expression.typeAnnotation) { expression.end = expression.typeAnnotation.end; } @@ -97,19 +96,12 @@ export default function read_pattern(parser, optional_allowed = false) { /** * @param {import('../index.js').Parser} parser - * @param {boolean} [optional_allowed] * @returns {any} */ -function read_type_annotation(parser, optional_allowed = false) { +function read_type_annotation(parser) { const start = parser.index; parser.allow_whitespace(); - if (optional_allowed && parser.eat('?')) { - // Acorn-TS puts the optional info as a property on the surrounding node. - // We spare the work here because it doesn't matter for us anywhere else. - parser.allow_whitespace(); - } - if (!parser.eat(':')) { parser.index = start; return undefined; diff --git a/packages/svelte/src/compiler/phases/1-parse/state/tag.js b/packages/svelte/src/compiler/phases/1-parse/state/tag.js index 762dab69bb..0dc7b9709e 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -269,6 +269,8 @@ function open(parser) { e.expected_identifier(parser.index); } + parser.allow_whitespace(); + const params_start = parser.index; parser.eat('(', true); @@ -289,6 +291,7 @@ function open(parser) { parse_expression_at(prelude + `${params} => {}`, parser.ts, params_start) ); + parser.allow_whitespace(); parser.eat('}', true); /** @type {ReturnType>} */ diff --git a/packages/svelte/tests/validator/samples/snippet-whitespace-ok/errors.json b/packages/svelte/tests/validator/samples/snippet-whitespace-ok/errors.json new file mode 100644 index 0000000000..fe51488c70 --- /dev/null +++ b/packages/svelte/tests/validator/samples/snippet-whitespace-ok/errors.json @@ -0,0 +1 @@ +[] diff --git a/packages/svelte/tests/validator/samples/snippet-whitespace-ok/input.svelte b/packages/svelte/tests/validator/samples/snippet-whitespace-ok/input.svelte new file mode 100644 index 0000000000..60174d3d56 --- /dev/null +++ b/packages/svelte/tests/validator/samples/snippet-whitespace-ok/input.svelte @@ -0,0 +1,4 @@ + +{#snippet ok () } + asd +{/snippet}