From 980c7e2321b5e9041e868c7d32de7029631a3650 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 29 May 2026 21:51:33 +0200 Subject: [PATCH] fix: don't error on `{type}` in declaration tags (#18321) The `\b` in the regex matched on `}`, too, so you would get a very confusing "invalid declaration tag" error on `{type}`. Match on whitespace instead (because that's what has to come afterwards) --------- Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Vercel Co-authored-by: Rich-Harris --- .../src/compiler/phases/1-parse/state/tag.js | 5 ++++- .../declaration-tag-invalid-type-2/errors.json | 14 ++++++++++++++ .../declaration-tag-invalid-type-2/input.svelte | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/errors.json create mode 100644 packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/input.svelte 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 7428c8660c..45d0081707 100644 --- a/packages/svelte/src/compiler/phases/1-parse/state/tag.js +++ b/packages/svelte/src/compiler/phases/1-parse/state/tag.js @@ -12,7 +12,10 @@ import { find_matching_bracket, match_bracket } from '../utils/bracket.js'; const regex_whitespace_with_closing_curly_brace = /\s*}/y; const regex_supported_declaration = /(?:let|const)\b/y; -const regex_unsupported_declaration = /(?:var|function|class|type|interface|enum)\b/y; +// All except `type` are reserved keywords and cannot be used as variable names. +// For type we check if it's not something like `type .x` / `type ()` / `type % 2` / ... +const regex_unsupported_declaration = + /(?:(?:var|function|class|interface|enum)\b)|(?:type\s+[^?.(`<[&|%^}])/y; const pointy_bois = { '<': '>' }; diff --git a/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/errors.json b/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/errors.json new file mode 100644 index 0000000000..052636cddb --- /dev/null +++ b/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/errors.json @@ -0,0 +1,14 @@ +[ + { + "code": "declaration_tag_invalid_type", + "message": "Declaration tags must be `let` or `const` declarations", + "start": { + "line": 14, + "column": 2 + }, + "end": { + "line": 14, + "column": 8 + } + } +] diff --git a/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/input.svelte b/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/input.svelte new file mode 100644 index 0000000000..129a8ac7ba --- /dev/null +++ b/packages/svelte/tests/validator/samples/declaration-tag-invalid-type-2/input.svelte @@ -0,0 +1,15 @@ +{#if true} + + {type} + {type } + {type && foo} + {type || bar} + {type % 2} + {type .x} + {type ?.x} + {type ()} + {type [1]} + {type `tag`} + + {type foo = boolean} +{/if}