Repository: sveltejs/language-tools
Pull request #1380 (2022-02-10 07:07:42)

(fix) svelte2tsx function expression scope
#1379 


Patch:
diff --git a/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts b/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts
index 1b8e326a..b65afe56 100644
--- a/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts
+++ b/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts
@@ -274,12 +274,7 @@ export function processInstanceScriptContent(
             exportedNames.handleExportFunctionOrClass(node);
         }
 
-        if (ts.isBlock(node)) {
-            pushScope();
-            onLeaveCallbacks.push(() => popScope());
-        }
-
-        if (ts.isArrowFunction(node)) {
+        if (ts.isBlock(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
             pushScope();
             onLeaveCallbacks.push(() => popScope());
         }


Test patch:
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/expected.tsx b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/expected.tsx
new file mode 100644
index 00000000..5abfd09e
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/expected.tsx
@@ -0,0 +1,18 @@
+///<reference types="svelte" />
+<></>;function render() {
+
+const a = function (shadowed1) {}
+const b = (shadowed2) => {}
+let  c = __sveltets_1_invalidate(() => function (shadowed3) {})
+let  d = __sveltets_1_invalidate(() => (shadowed4) => {})
+
+let  shadowed1 = __sveltets_1_invalidate(() => 1)
+let  shadowed2 = __sveltets_1_invalidate(() => 1)
+let  shadowed3 = __sveltets_1_invalidate(() => 1)
+let  shadowed4 = __sveltets_1_invalidate(() => 1)
+;
+() => (<></>);
+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/reactive-declare-same-name-as-function-parameter/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/expectedv2.ts
new file mode 100644
index 00000000..7a2dde8f
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/expectedv2.ts
@@ -0,0 +1,18 @@
+///<reference types="svelte" />
+;function render() {
+
+const a = function (shadowed1) {}
+const b = (shadowed2) => {}
+let  c = __sveltets_1_invalidate(() => function (shadowed3) {})
+let  d = __sveltets_1_invalidate(() => (shadowed4) => {})
+
+let  shadowed1 = __sveltets_1_invalidate(() => 1)
+let  shadowed2 = __sveltets_1_invalidate(() => 1)
+let  shadowed3 = __sveltets_1_invalidate(() => 1)
+let  shadowed4 = __sveltets_1_invalidate(() => 1)
+;
+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/reactive-declare-same-name-as-function-parameter/input.svelte b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/input.svelte
new file mode 100644
index 00000000..ab090f15
--- /dev/null
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/reactive-declare-same-name-as-function-parameter/input.svelte
@@ -0,0 +1,11 @@
+<script>
+const a = function (shadowed1) {}
+const b = (shadowed2) => {}
+$: c = function (shadowed3) {}
+$: d = (shadowed4) => {}
+
+$: shadowed1 = 1
+$: shadowed2 = 1
+$: shadowed3 = 1
+$: shadowed4 = 1
+</script>
\ No newline at end of file