From 7cb71ac572495d5ebcff043201d57e5444476057 Mon Sep 17 00:00:00 2001 From: navorite Date: Mon, 1 Jan 2024 06:45:18 +0200 Subject: [PATCH 1/7] fix: improve matching of script tag --- .changeset/hip-balloons-begin.md | 5 + .../src/compiler/phases/1-parse/index.js | 13 +- .../comment-before-script/input.svelte | 4 + .../samples/comment-before-script/output.json | 132 ++++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 .changeset/hip-balloons-begin.md create mode 100644 packages/svelte/tests/parser-modern/samples/comment-before-script/input.svelte create mode 100644 packages/svelte/tests/parser-modern/samples/comment-before-script/output.json diff --git a/.changeset/hip-balloons-begin.md b/.changeset/hip-balloons-begin.md new file mode 100644 index 000000000000..f5bbf77cf07c --- /dev/null +++ b/.changeset/hip-balloons-begin.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve matching of script tag diff --git a/packages/svelte/src/compiler/phases/1-parse/index.js b/packages/svelte/src/compiler/phases/1-parse/index.js index 6b7642c251bd..41c0dd2757b5 100644 --- a/packages/svelte/src/compiler/phases/1-parse/index.js +++ b/packages/svelte/src/compiler/phases/1-parse/index.js @@ -11,7 +11,7 @@ import read_options from './read/options.js'; const regex_position_indicator = / \(\d+:\d+\)$/; const regex_lang_attribute = - /|]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/; + /|]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/g; export class Parser { /** @@ -49,7 +49,16 @@ export class Parser { this.template = template.trimRight(); - this.ts = regex_lang_attribute.exec(template)?.[2] === 'ts'; + const lang_matches = this.template.matchAll(regex_lang_attribute); + + for(const match of lang_matches) + { + if(!match[0]?.startsWith(' + diff --git a/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json b/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json new file mode 100644 index 000000000000..d1505bfedebe --- /dev/null +++ b/packages/svelte/tests/parser-modern/samples/comment-before-script/output.json @@ -0,0 +1,132 @@ +{ + "css": null, + "js": [], + "start": 0, + "end": 27, + "type": "Root", + "fragment": { + "type": "Fragment", + "nodes": [ + { + "type": "Comment", + "start": 0, + "end": 27, + "data": "should not error out", + "ignores": [] + }, + { + "type": "Text", + "start": 27, + "end": 28, + "raw": "\n", + "data": "\n" + } + ], + "transparent": false + }, + "options": null, + "instance": { + "type": "Script", + "start": 28, + "end": 76, + "context": "default", + "content": { + "type": "Program", + "start": 46, + "end": 67, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 0 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 48, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 52, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 18, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "name": "count", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 59, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 18 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "sourceType": "module" + } + } +} \ No newline at end of file From 650bc3e54f75aac5fc2074df5ff568927b35068c Mon Sep 17 00:00:00 2001 From: navorite Date: Mon, 1 Jan 2024 06:57:40 +0200 Subject: [PATCH 2/7] better --- packages/svelte/src/compiler/phases/1-parse/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/svelte/src/compiler/phases/1-parse/index.js b/packages/svelte/src/compiler/phases/1-parse/index.js index 41c0dd2757b5..87e616d0a6f6 100644 --- a/packages/svelte/src/compiler/phases/1-parse/index.js +++ b/packages/svelte/src/compiler/phases/1-parse/index.js @@ -53,9 +53,9 @@ export class Parser { for(const match of lang_matches) { - if(!match[0]?.startsWith('