diff --git a/.changeset/hip-balloons-begin.md b/.changeset/hip-balloons-begin.md new file mode 100644 index 0000000000..413b254065 --- /dev/null +++ b/.changeset/hip-balloons-begin.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve script `lang` attribute detection diff --git a/packages/svelte/src/compiler/phases/1-parse/index.js b/packages/svelte/src/compiler/phases/1-parse/index.js index 6b7642c251..e99e91d052 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,14 @@ export class Parser { this.template = template.trimRight(); - this.ts = regex_lang_attribute.exec(template)?.[2] === 'ts'; + let match_lang; + + do match_lang = regex_lang_attribute.exec(template); + while (match_lang && match_lang[0][1] !== 's'); // ensure it starts with ' + 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 0000000000..d52a3752b4 --- /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" + } + } +}