From 666286f62a69000b20607a8e42b3bc83267482df Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 11 Feb 2022 17:08:05 +0100 Subject: [PATCH] (fix) add key after index definition in new each transformation #1352 --- .../features/diagnostics/fixtures/each/expected.json | 8 ++++---- .../features/diagnostics/fixtures/each/expectedv2.json | 8 ++++---- .../features/diagnostics/fixtures/each/input.svelte | 3 +++ packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts | 6 +++--- .../htmlx2jsx/samples/each-block-key-else/expectedv2.js | 2 +- .../test/htmlx2jsx/samples/each-block-key/expectedv2.js | 2 +- .../samples/if-nested-each-block-shadowed/expectedv2.js | 2 +- .../htmlx2jsx/samples/if-nested-each-block/expectedv2.js | 2 +- 8 files changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expected.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expected.json index 1dec57797..5766147f5 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expected.json +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expected.json @@ -1,8 +1,8 @@ [ { "range": { - "start": { "line": 26, "character": 7 }, - "end": { "line": 26, "character": 24 } + "start": { "line": 29, "character": 7 }, + "end": { "line": 29, "character": 24 } }, "severity": 1, "source": "ts", @@ -12,8 +12,8 @@ }, { "range": { - "start": { "line": 30, "character": 7 }, - "end": { "line": 30, "character": 24 } + "start": { "line": 33, "character": 7 }, + "end": { "line": 33, "character": 24 } }, "severity": 1, "source": "ts", diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expectedv2.json b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expectedv2.json index 1dec57797..5766147f5 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expectedv2.json +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/expectedv2.json @@ -1,8 +1,8 @@ [ { "range": { - "start": { "line": 26, "character": 7 }, - "end": { "line": 26, "character": 24 } + "start": { "line": 29, "character": 7 }, + "end": { "line": 29, "character": 24 } }, "severity": 1, "source": "ts", @@ -12,8 +12,8 @@ }, { "range": { - "start": { "line": 30, "character": 7 }, - "end": { "line": 30, "character": 24 } + "start": { "line": 33, "character": 7 }, + "end": { "line": 33, "character": 24 } }, "severity": 1, "source": "ts", diff --git a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/input.svelte b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/input.svelte index 07452f033..56899d006 100644 --- a/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/input.svelte +++ b/packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/each/input.svelte @@ -17,6 +17,9 @@ const badOptions = { {#each simpleOptions as option, i}
{option}, {i}
{/each} +{#each simpleOptions as option, i (i)} +
{option}, {i}
+{/each} {#each complexOptions as option, i (typeof option === "string" ? option : option.value)} diff --git a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts index 719566b46..b3bc07d0f 100644 --- a/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts +++ b/packages/svelte2tsx/src/htmlxtojsx_v2/nodes/EachBlock.ts @@ -47,14 +47,14 @@ export function handleEach(str: MagicString, eachBlock: BaseNode): void { `${containsComma ? ')' : ''})){` ]; } - if (eachBlock.key) { - transforms.push([eachBlock.key.start, eachBlock.key.end], ';'); - } if (eachBlock.index) { const indexStart = str.original.indexOf(eachBlock.index, eachBlock.context.end); const indexEnd = indexStart + eachBlock.index.length; transforms.push('let ', [indexStart, indexEnd], ' = 1;'); } + if (eachBlock.key) { + transforms.push([eachBlock.key.start, eachBlock.key.end], ';'); + } transform(str, eachBlock.start, startEnd, startEnd, transforms); const endEach = str.original.lastIndexOf('{', eachBlock.end - 1); 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 959d01b9d..465f21c2e 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)){item.id;let i = 1; + for(const 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 d0e2f3304..81a20c827 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)){item.id;let i = 1; + for(const 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/if-nested-each-block-shadowed/expectedv2.js b/packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-each-block-shadowed/expectedv2.js index 2457eab02..c7119e16f 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,5 +1,5 @@ if(hello){ - for(const hello of __sveltets_2_ensureArray(items)){hello.id;let i = 1; + for(const 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)){ 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 cc64ceb71..7484e2f0f 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,5 +1,5 @@ if(hello){ - for(const item of __sveltets_2_ensureArray(items)){item.id;let i = 1; + for(const item of __sveltets_2_ensureArray(items)){let i = 1;item.id; { svelteHTML.createElement("div", {});item;i; } } if(hi && bye){