From 29b04990171d71598ebef3e27ff43f70bf3fbf47 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 7 Apr 2022 18:14:07 +0200 Subject: [PATCH] (fix) more robust handling of inner script tags Remove scripts inside of @html tags #1427 --- packages/svelte2tsx/src/svelte2tsx/index.ts | 5 ++++- .../svelte2tsx/src/svelte2tsx/nodes/Scripts.ts | 10 ++++++++-- .../samples/script-in-rawhtml/expected.tsx | 14 ++++++++++++++ .../samples/script-in-rawhtml/expectedv2.ts | 14 ++++++++++++++ .../samples/script-in-rawhtml/input.svelte | 7 +++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expected.tsx create mode 100644 packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expectedv2.ts create mode 100644 packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/input.svelte diff --git a/packages/svelte2tsx/src/svelte2tsx/index.ts b/packages/svelte2tsx/src/svelte2tsx/index.ts index c753d9594..5073f1c4e 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 151e5300a..853a72fd1 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; 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 000000000..275a38bce --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expected.tsx @@ -0,0 +1,14 @@ +/// +<>;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 000000000..aecb30d94 --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/expectedv2.ts @@ -0,0 +1,14 @@ +/// +;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 000000000..571a3b20c --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/script-in-rawhtml/input.svelte @@ -0,0 +1,7 @@ + + +{@html ``}