From 9a97d5c547b4c041d9931b95c650f08168054aa2 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:31:07 +0100 Subject: [PATCH] fix: ignore expressions in top level script/style tag attributes (#9498) Related to sveltejs/language-tools#2204 / sveltejs/language-tools#2039 --- .changeset/ten-ducks-jump.md | 5 + .../svelte/src/compiler/parse/state/tag.js | 44 ++++- .../input.svelte | 5 + .../output.json | 150 ++++++++++++++++++ 4 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 .changeset/ten-ducks-jump.md create mode 100644 packages/svelte/test/parser/samples/script-attribute-with-curly-braces/input.svelte create mode 100644 packages/svelte/test/parser/samples/script-attribute-with-curly-braces/output.json diff --git a/.changeset/ten-ducks-jump.md b/.changeset/ten-ducks-jump.md new file mode 100644 index 0000000000..0bd70fbe17 --- /dev/null +++ b/.changeset/ten-ducks-jump.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ignore expressions in top level script/style tag attributes diff --git a/packages/svelte/src/compiler/parse/state/tag.js b/packages/svelte/src/compiler/parse/state/tag.js index c0df00df92..37281c5156 100644 --- a/packages/svelte/src/compiler/parse/state/tag.js +++ b/packages/svelte/src/compiler/parse/state/tag.js @@ -161,8 +161,9 @@ export default function tag(parser) { * @type {Set} */ const unique_names = new Set(); + const is_top_level_script_or_style = specials.has(name) && parser.stack.length === 1; let attribute; - while ((attribute = read_attribute(parser, unique_names))) { + while ((attribute = read_attribute(parser, unique_names, is_top_level_script_or_style))) { element.attributes.push(attribute); parser.allow_whitespace(); } @@ -196,8 +197,7 @@ export default function tag(parser) { } element.tag = definition.value[0].data || definition.value[0].expression; } - // special cases – top-level + +

Hello {name}!

\ No newline at end of file diff --git a/packages/svelte/test/parser/samples/script-attribute-with-curly-braces/output.json b/packages/svelte/test/parser/samples/script-attribute-with-curly-braces/output.json new file mode 100644 index 0000000000..af4ec56a68 --- /dev/null +++ b/packages/svelte/test/parser/samples/script-attribute-with-curly-braces/output.json @@ -0,0 +1,150 @@ +{ + "html": { + "start": 79, + "end": 101, + "type": "Fragment", + "children": [ + { + "start": 77, + "end": 79, + "type": "Text", + "raw": "\n\n", + "data": "\n\n" + }, + { + "start": 79, + "end": 101, + "type": "Element", + "name": "h1", + "attributes": [], + "children": [ + { + "start": 83, + "end": 89, + "type": "Text", + "raw": "Hello ", + "data": "Hello " + }, + { + "start": 89, + "end": 95, + "type": "MustacheTag", + "expression": { + "type": "Identifier", + "start": 90, + "end": 94, + "loc": { + "start": { + "line": 5, + "column": 11 + }, + "end": { + "line": 5, + "column": 15 + } + }, + "name": "name" + } + }, + { + "start": 95, + "end": 96, + "type": "Text", + "raw": "!", + "data": "!" + } + ] + } + ] + }, + "instance": { + "type": "Script", + "start": 0, + "end": 77, + "context": "default", + "content": { + "type": "Program", + "start": 46, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 0 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 48, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 52, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": "name" + }, + "init": { + "type": "Literal", + "start": 59, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "value": "world", + "raw": "'world'" + } + } + ], + "kind": "let" + } + ], + "sourceType": "module" + } + } +}