fix: improve script `lang` attribute detection (#10046)

closes #10038
pull/10054/head
navorite 1 year ago committed by GitHub
parent 346041f454
commit d56223b162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: improve script `lang` attribute detection

@ -11,7 +11,7 @@ import read_options from './read/options.js';
const regex_position_indicator = / \(\d+:\d+\)$/;
const regex_lang_attribute =
/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/;
/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\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 '<s' to match script tags
regex_lang_attribute.lastIndex = 0; // reset matched index to pass tests - otherwise declare the regex inside the constructor
this.ts = match_lang?.[2] === 'ts';
this.root = {
css: null,

@ -0,0 +1,4 @@
<!--should not error out-->
<script lang="ts">
let count: number;
</script>

@ -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"
}
}
}
Loading…
Cancel
Save