Repository: sveltejs/language-tools
Pull request #1636 (2022-09-15 08:19:52)

(fix) use let for #each block
it's allowed to reassign them inside the loop
#1352


Patch:
diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts
index b3bc07d0..0af1981d 100644
--- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts
+++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts
@@ -34,13 +34,13 @@ export function handleEach(str: MagicString, eachBlock: BaseNode): void {
         transforms = [
             `{ const $$_each = __sveltets_2_ensureArray(${containsComma ? '(' : ''}`,
             [eachBlock.expression.start, eachBlock.expression.end],
-            `${containsComma ? ')' : ''}); for(const `,
+            `${containsComma ? ')' : ''}); for(let `,
             [eachBlock.context.start, eachBlock.context.end],
             ' of $$_each){'
         ];
     } else {
         transforms = [
-            'for(const ',
+            'for(let ',
             [eachBlock.context.start, eachBlock.context.end],
             ` of __sveltets_2_ensureArray(${containsComma ? '(' : ''}`,
             [eachBlock.expression.start, eachBlock.expression.end],


Test patch:
diff --git a/packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts b/packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts
index c7ad1ba3..ddd984fc 100644
--- a/packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts
+++ b/packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts
@@ -203,7 +203,7 @@ function test(useNewTransformation: boolean) {
                     },
                     {
                         name: 'foo',
-                        kind: 14,
+                        kind: 13,
                         location: {
                             uri: getUri('documentsymbols.svelte'),
                             range: {
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/blocks-without-whitespace-inbetween/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/blocks-without-whitespace-inbetween/expectedv2.js
index 13efb020..fcdd24ae 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/blocks-without-whitespace-inbetween/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/blocks-without-whitespace-inbetween/expectedv2.js
@@ -1,1 +1,1 @@
-if(name == "world"){ }  for(const y of __sveltets_2_ensureArray(x)){ }   { const $$_value = await (x);{ const y = $$_value;  }}if(bla){ }
\ No newline at end of file
+if(name == "world"){ }  for(let y of __sveltets_2_ensureArray(x)){ }   { const $$_value = await (x);{ const y = $$_value;  }}if(bla){ }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic-sequence/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic-sequence/expectedv2.js
index 4dc15acf..915d7522 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic-sequence/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic-sequence/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const item of __sveltets_2_ensureArray((true, items))){
+  for(let item of __sveltets_2_ensureArray((true, items))){
      { svelteHTML.createElement("div", {});item; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic/expectedv2.js
index cfc79a8f..fae8223c 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-basic/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const item of __sveltets_2_ensureArray(items)){
+  for(let item of __sveltets_2_ensureArray(items)){
      { svelteHTML.createElement("div", {});item; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-index/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-index/expectedv2.js
index b58cfffb..bbfd903f 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-index/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-index/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const item of __sveltets_2_ensureArray(items)){let i = 1;
+  for(let item of __sveltets_2_ensureArray(items)){let i = 1;
      { svelteHTML.createElement("div", {});item;i; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key-else/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key-else/expectedv2.js
index 75e69611..0a2b43cd 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key-else/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key-else/expectedv2.js
@@ -1,4 +1,4 @@
-   for(const item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
+   for(let item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
      { svelteHTML.createElement("div", {});item;i; }
 }
      { svelteHTML.createElement("div", {});  }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key/expectedv2.js
index 04cc9851..851462a4 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-key/expectedv2.js
@@ -1,3 +1,3 @@
-   for(const item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
+   for(let item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
      { svelteHTML.createElement("div", {});item;i; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-nullish-coalescing/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-nullish-coalescing/expectedv2.js
index 85fbdc6e..b910227b 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-nullish-coalescing/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-nullish-coalescing/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const item of __sveltets_2_ensureArray(items ?? [])){
+  for(let item of __sveltets_2_ensureArray(items ?? [])){
      { svelteHTML.createElement("div", {});item; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-optional-chaining/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-optional-chaining/expectedv2.js
index 4d5eed95..2627ebde 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-optional-chaining/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/each-block-optional-chaining/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const item of __sveltets_2_ensureArray(someObject?.items)){
+  for(let item of __sveltets_2_ensureArray(someObject?.items)){
      { svelteHTML.createElement("div", {});item; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-animation/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-animation/expectedv2.js
index 76b19847..b5f63b64 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-animation/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-animation/expectedv2.js
@@ -1,3 +1,3 @@
-   for(const item of __sveltets_2_ensureArray(list)){item;
+   for(let item of __sveltets_2_ensureArray(list)){item;
 	 { svelteHTML.createElement("li", {  });__sveltets_2_ensureAnimation(flip(svelteHTML.mapElementTag('li'),__sveltets_2_AnimationMove,(opt?.)));item; }
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-const/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-const/expectedv2.js
index d2b8c0c5..06115798 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-const/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-const/expectedv2.js
@@ -1,3 +1,3 @@
-  for(const str of __sveltets_2_ensureArray([''])){
+  for(let str of __sveltets_2_ensureArray([''])){
     const lower = str.;
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block-shadowed/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block-shadowed/expectedv2.js
index 58188d75..d051b7db 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block-shadowed/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block-shadowed/expectedv2.js
@@ -1,13 +1,13 @@
 if(hello){
-       for(const hello of __sveltets_2_ensureArray(items)){let i = 1;hello.id;
+       for(let hello of __sveltets_2_ensureArray(items)){let i = 1;hello.id;
          { svelteHTML.createElement("div", {});hello;i; }
         if(hello){
-              for(const hello of __sveltets_2_ensureArray(items)){
+              for(let hello of __sveltets_2_ensureArray(items)){
                 if(hello){
                     hello;
                 }
             }
-              for(const foo of __sveltets_2_ensureArray(items)){
+              for(let foo of __sveltets_2_ensureArray(items)){
                 const hello = foo;
                 if(hello){
                     hello;
@@ -20,7 +20,7 @@ if(hello){
         }
     
     if(hi && bye){
-          for(const bye of __sveltets_2_ensureArray(items)){
+          for(let bye of __sveltets_2_ensureArray(items)){
              { svelteHTML.createElement("div", {});bye; }
         }
             if(bye){
@@ -28,17 +28,17 @@ if(hello){
             }
         
     } else if (cool){
-          for(const item of __sveltets_2_ensureArray(items)){let cool = 1;
+          for(let item of __sveltets_2_ensureArray(items)){let cool = 1;
              { svelteHTML.createElement("div", {});item;cool; }
         }
     }else{
-          for(const hello of __sveltets_2_ensureArray(items)){
+          for(let hello of __sveltets_2_ensureArray(items)){
              { svelteHTML.createElement("div", {});hello; }
         }
     }
 }
 
-  for(const hello of __sveltets_2_ensureArray(items)){let i = 1;
+  for(let hello of __sveltets_2_ensureArray(items)){let i = 1;
     if(hello && i && bye){
         hello; i; bye;
     } else if (hello && i && bye){
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block/expectedv2.js
index fbea6081..fb12d6d4 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block/expectedv2.js
@@ -1,19 +1,19 @@
 if(hello){
-       for(const item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
+       for(let item of __sveltets_2_ensureArray(items)){let i = 1;item.id;
          { svelteHTML.createElement("div", {});item;i; }
     }
     if(hi && bye){
-          for(const item of __sveltets_2_ensureArray(items)){
+          for(let item of __sveltets_2_ensureArray(items)){
              { svelteHTML.createElement("div", {});item; }
         }
              { svelteHTML.createElement("p", {});  }
         
     } else if (cool){
-          for(const item of __sveltets_2_ensureArray(items)){let i = 1;
+          for(let item of __sveltets_2_ensureArray(items)){let i = 1;
              { svelteHTML.createElement("div", {});item;i; }
         }
     }else{
-          for(const item of __sveltets_2_ensureArray(items)){
+          for(let item of __sveltets_2_ensureArray(items)){
              { svelteHTML.createElement("div", {});item; }
         }
     }
diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/nested-if-else-if-and-each-block/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/nested-if-else-if-and-each-block/expectedv2.js
index cd383cd1..45ffe209 100644
--- a/packages/svelte2tsx/test/htmlx2jsx/samples/nested-if-else-if-and-each-block/expectedv2.js
+++ b/packages/svelte2tsx/test/htmlx2jsx/samples/nested-if-else-if-and-each-block/expectedv2.js
@@ -1,5 +1,5 @@
 if(true){
   if(true){} else if (true){}
 }else{
-    for(const _ of __sveltets_2_ensureArray([])){}
+    for(let _ of __sveltets_2_ensureArray([])){}
 }
\ No newline at end of file
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/circle-drawer-example/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/circle-drawer-example/expectedv2.ts
index 37f1ef04..f680890f 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/circle-drawer-example/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/circle-drawer-example/expectedv2.ts
@@ -70,7 +70,7 @@ async () => {
  }
 
   { svelteHTML.createElement("svg", {  "on:click":handleClick,});
-	  for(const circle of __sveltets_2_ensureArray(circles)){
+	  for(let circle of __sveltets_2_ensureArray(circles)){
 		 { svelteHTML.createElement("circle", {            "cx":circle.cx,"cy":circle.cy,"r":circle.r,"on:click":event => select(circle, event),"on:contextmenu":() => {
 				adjusting = !adjusting;
 				if (adjusting) selected = circle;
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-inside-each/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-inside-each/expectedv2.ts
index 83939d49..db173a02 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-inside-each/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-inside-each/expectedv2.ts
@@ -1,10 +1,10 @@
 ///<reference types="svelte" />
 ;function render() {
 /*Ωignore_startΩ*/;const __sveltets_createSlot = __sveltets_2_createCreateSlot();/*Ωignore_endΩ*/
-async () => {  for(const item of __sveltets_2_ensureArray(items)){
+async () => {  for(let item of __sveltets_2_ensureArray(items)){
      { __sveltets_createSlot("default", { "a":item,});  }
 }
-  for(const { a } of __sveltets_2_ensureArray(items2)){
+  for(let { a } of __sveltets_2_ensureArray(items2)){
      { __sveltets_createSlot("second", {  a,});  }
 }};
 return { props: {}, slots: {'default': {a:__sveltets_1_unwrapArr(items)}, 'second': {a:(({ a }) => a)(__sveltets_1_unwrapArr(items2))}}, getters: {}, events: {} }}
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-nest-scope/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-nest-scope/expectedv2.ts
index d949f8f6..51c0c7c6 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-nest-scope/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-nest-scope/expectedv2.ts
@@ -1,8 +1,8 @@
 ///<reference types="svelte" />
 ;function render() {
 /*Ωignore_startΩ*/;const __sveltets_createSlot = __sveltets_2_createCreateSlot();/*Ωignore_endΩ*/
-async () => {  for(const item of __sveltets_2_ensureArray(items)){
-      for(const { a } of __sveltets_2_ensureArray(item)){
+async () => {  for(let item of __sveltets_2_ensureArray(items)){
+      for(let { a } of __sveltets_2_ensureArray(item)){
          { __sveltets_createSlot("default", {a,});  }
     }
       { __sveltets_createSlot("second", {  a,}); }
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-object-key/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-object-key/expectedv2.ts
index 85174473..083c92d9 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-object-key/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-object-key/expectedv2.ts
@@ -1,7 +1,7 @@
 ///<reference types="svelte" />
 ;function render() {
 /*Ωignore_startΩ*/;const __sveltets_createSlot = __sveltets_2_createCreateSlot();/*Ωignore_endΩ*/
-async () => {  for(const item of __sveltets_2_ensureArray(items)){
+async () => {  for(let item of __sveltets_2_ensureArray(items)){
      { __sveltets_createSlot("default", {             "a":item,"b":{ item },"c":{ item: 'abc' }.item,"d":{ item: item },"e":$item,"f":$item,...g,...item,});  }
 }};
 return { props: {}, slots: {'default': {a:__sveltets_1_unwrapArr(items), b:{ item:__sveltets_1_unwrapArr(items) }, c:{ item: 'abc' }.item, d:{ item: __sveltets_1_unwrapArr(items) }, e:$item, f:$item, ...g, ...__sveltets_1_unwrapArr(items)}}, getters: {}, events: {} }}
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-var-shadowing/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-var-shadowing/expectedv2.ts
index 23e60a6f..3e2a87ff 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-var-shadowing/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/component-slot-var-shadowing/expectedv2.ts
@@ -1,7 +1,7 @@
 ///<reference types="svelte" />
 ;function render() {
 /*Ωignore_startΩ*/;const __sveltets_createSlot = __sveltets_2_createCreateSlot();/*Ωignore_endΩ*/
-async () => {  { const $$_each = __sveltets_2_ensureArray(items); for(const items of $$_each){
+async () => {  { const $$_each = __sveltets_2_ensureArray(items); for(let items of $$_each){
      { __sveltets_createSlot("default", { "a":items,});  }
 }}};
 return { props: {}, slots: {'default': {a:__sveltets_1_unwrapArr(items)}}, getters: {}, events: {} }}
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each-destructure/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each-destructure/expectedv2.ts
index 6c22dbf7..683c3bcc 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each-destructure/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each-destructure/expectedv2.ts
@@ -14,7 +14,7 @@
 ;
 async () => {
 
-  for(const { width, height } of __sveltets_2_ensureArray(boxes)){
+  for(let { width, height } of __sveltets_2_ensureArray(boxes)){
 	const {area, volume} = calculate(width, height, constant);
 	const perimeter = (width + height) * constant;
 	const [_width, _height, sum] = [width * constant, height, width * constant + height];
diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each/expectedv2.ts b/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each/expectedv2.ts
index 0878fcef..ef3faa97 100644
--- a/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each/expectedv2.ts
+++ b/packages/svelte2tsx/test/svelte2tsx/samples/const-tag-each/expectedv2.ts
@@ -14,7 +14,7 @@
 ;
 async () => {
 
-  for(const box of __sveltets_2_ensureArray(boxes)){
+  for(let box of __sveltets_2_ensureArray(boxes)){
 	const {area, volume} = calculate(box.width, box.height, constant);
 	const perimeter = (box.width + box.height) * constant;
 	const [width, height, sum] = [box.width * constant, box.height, box.width * constant + box.height];