Repository: sveltejs/language-tools
Pull request #1433 (2022-04-07 16:14:54)

(fix) more robust handling of inner script tags
Remove scripts inside of `@html` tags
#1427


Patch:
diff --git a/packages/svelte2tsx/src/svelte2tsx/index.ts b/packages/svelte2tsx/src/svelte2tsx/index.ts
index c753d959..5073f1c4 100644
--- a/packages/svelte2tsx/src/svelte2tsx/index.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/index.ts
@@ -200,7 +200,10 @@ function processSvelteTemplate(
                 handleStyleTag(node);
                 break;
             case 'Element':
-                scripts.handleScriptTag(node, parent);
+                scripts.checkIfElementIsScriptTag(node, parent);
+                break;
+            case 'RawMustacheTag':
+                scripts.checkIfContainsScriptTag(node);
                 break;
             case 'BlockStatement':
                 scopeStack.push();
diff --git a/packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts b/packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts
index 151e5300..853a72fd 100644
--- a/packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/nodes/Scripts.ts
@@ -13,13 +13,19 @@ export class Scripts {
 
     constructor(private htmlxAst: Node) {}
 
-    handleScriptTag = (node: Node, parent: Node) => {
+    checkIfElementIsScriptTag(node: Node, parent: Node) {
         if (parent !== this.htmlxAst && node.name === 'script') {
             this.topLevelScripts = this.topLevelScripts.filter(
                 (tag) => tag.start !== node.start || tag.end !== node.end
             );
         }
-    };
+    }
+
+    checkIfContainsScriptTag(node: Node) {
+        this.topLevelScripts = this.topLevelScripts.filter(
+            (tag) => !(node.start <= tag.start && node.end >= tag.end)
+        );
+    }
 
     getTopLevelScriptTags(): { scriptTag: Node; moduleScriptTag: Node } {
         let scriptTag: Node = null;


Test patch:
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expected.tsx
new file mode 100644
index 00000000..275a38bc
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expected.tsx
@@ -0,0 +1,14 @@
+///<reference types="svelte" />
+<></>;function render() {
+
+    const schema = {
+        key: "value"
+    };
+;
+() => (<>
+
+{ ``}</>);
+return { props: {}, slots: {}, getters: {}, events: {} }}
+
+export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
+}
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expectedv2.ts
new file mode 100644
index 00000000..aecb30d9
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expectedv2.ts
@@ -0,0 +1,14 @@
+///<reference types="svelte" />
+;function render() {
+
+    const schema = {
+        key: "value"
+    };
+;
+async () => {
+
+ ``;};
+return { props: {}, slots: {}, getters: {}, events: {} }}
+
+export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
+}
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/input.svelte b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/input.svelte
new file mode 100644
index 00000000..571a3b20
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/input.svelte
@@ -0,0 +1,7 @@
+<script>
+    const schema = {
+        key: "value"
+    };
+</script>
+
+{@html `<script type="application/ld+json">${JSON.stringify(schema)}</script>`}