Skip to content

Commit 2863414

Browse files
authored
(feat) support {#key} block (#571)
Also bump Svelte to 2.28.0 #570
1 parent 080a4f0 commit 2863414

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

packages/language-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"prettier": "2.1.1",
5454
"prettier-plugin-svelte": "~1.2.1",
5555
"source-map": "^0.7.3",
56-
"svelte": "3.25.1",
56+
"svelte": "3.28.0",
5757
"svelte-preprocess": "~4.3.0",
5858
"svelte2tsx": "*",
5959
"typescript": "*",

packages/svelte2tsx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"rollup-plugin-typescript": "^1.0.1",
3333
"source-map": "^0.6.1",
3434
"source-map-support": "^0.5.16",
35-
"svelte": "3.25.1",
35+
"svelte": "3.28.0",
3636
"tiny-glob": "^0.2.6",
3737
"tslib": "^1.10.0",
3838
"typescript": "^4.0.2"

packages/svelte2tsx/src/htmlxtojsx/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { handleActionDirective } from './nodes/action-directive';
66
import { handleAnimateDirective } from './nodes/animation-directive';
77
import { handleAttribute } from './nodes/attribute';
88
import { handleAwait } from './nodes/await';
9+
import { handleKey } from './nodes/key';
910
import { handleBinding } from './nodes/binding';
1011
import { handleClassDirective } from './nodes/class-directive';
1112
import { handleComment } from './nodes/comment';
@@ -60,6 +61,9 @@ export function convertHtmlxToJsx(
6061
case 'AwaitBlock':
6162
handleAwait(htmlx, str, node);
6263
break;
64+
case 'KeyBlock':
65+
handleKey(htmlx, str, node);
66+
break;
6367
case 'RawMustacheTag':
6468
handleRawHtml(htmlx, str, node);
6569
break;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import MagicString from 'magic-string';
2+
import { Node } from 'estree-walker';
3+
4+
/**
5+
* {#key expr}content{/key} ---> {expr || <>...</>}
6+
*/
7+
export function handleKey(htmlx: string, str: MagicString, keyBlock: Node): void {
8+
// {#key expr} -> {expr || <>
9+
str.overwrite(keyBlock.start, keyBlock.expression.start, '{');
10+
const end = htmlx.indexOf('}', keyBlock.expression.end);
11+
str.overwrite(keyBlock.expression.end, end + 1, ' || <>');
12+
13+
// {/key} -> </>}
14+
const endKey = htmlx.lastIndexOf('{', keyBlock.end - 1);
15+
str.overwrite(endKey, keyBlock.end, '</>}');
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<>{value || <>
2+
<p>hello</p>
3+
</>}
4+
{$store || <>
5+
<p>hello</p>
6+
</>}
7+
{expr.obj || <>
8+
<p>hello</p>
9+
</>}</>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{#key value}
2+
<p>hello</p>
3+
{/key}
4+
{#key $store}
5+
<p>hello</p>
6+
{/key}
7+
{#key expr.obj}
8+
<p>hello</p>
9+
{/key}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,10 +1975,10 @@ svelte-preprocess@~4.3.0:
19751975
detect-indent "^6.0.0"
19761976
strip-indent "^3.0.0"
19771977

1978-
svelte@3.25.1:
1979-
version "3.25.1"
1980-
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.25.1.tgz#218def1243fea5a97af6eb60f5e232315bb57ac4"
1981-
integrity sha512-IbrVKTmuR0BvDw4ii8/gBNy8REu7nWTRy9uhUz+Yuae5lIjWgSGwKlWtJGC2Vg95s+UnXPqDu0Kk/sUwe0t2GQ==
1978+
svelte@3.28.0:
1979+
version "3.28.0"
1980+
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.28.0.tgz#e257fab5666701cf230bea583ceb470bdda1344c"
1981+
integrity sha512-WJW8wD+aTmU5GUnTUjdhVF35mve2MjylubLgB6fGWoXHpYENdwcwWsWvjMQLayzMynqNH733h1Ck8wJzNR7gdQ==
19821982

19831983
table@^5.2.3:
19841984
version "5.4.6"

0 commit comments

Comments
 (0)