-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) support for svelte:fragment (#848)
- Loading branch information
1 parent
55c2e91
commit eac5c74
Showing
17 changed files
with
167 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ Input.svelte | |
</script> | ||
<h1>hello {world}</h1> | ||
``` | ||
|
||
will produce this ugly but type checkable TSX | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import MagicString from 'magic-string'; | ||
import { Node } from 'estree-walker'; | ||
import { beforeStart } from '../utils/node-utils'; | ||
import { getSingleSlotDef } from '../../svelte2tsx/nodes/slot'; | ||
|
||
export function handleSlot( | ||
htmlx: string, | ||
str: MagicString, | ||
slotEl: Node, | ||
component: Node, | ||
slotName: string | ||
): void { | ||
//collect "let" definitions | ||
const slotElIsComponent = slotEl === component; | ||
let hasMoved = false; | ||
let slotDefInsertionPoint: number; | ||
for (const attr of slotEl.attributes) { | ||
if (attr.type != 'Let') { | ||
continue; | ||
} | ||
|
||
if (slotElIsComponent && slotEl.children.length == 0) { | ||
//no children anyway, just wipe out the attribute | ||
str.remove(attr.start, attr.end); | ||
continue; | ||
} | ||
|
||
slotDefInsertionPoint = | ||
slotDefInsertionPoint || | ||
(slotElIsComponent | ||
? htmlx.lastIndexOf('>', slotEl.children[0].start) + 1 | ||
: slotEl.start); | ||
|
||
str.move(attr.start, attr.end, slotDefInsertionPoint); | ||
|
||
//remove let: | ||
if (hasMoved) { | ||
str.overwrite(attr.start, attr.start + 'let:'.length, ', '); | ||
} else { | ||
str.remove(attr.start, attr.start + 'let:'.length); | ||
} | ||
hasMoved = true; | ||
if (attr.expression) { | ||
//overwrite the = as a : | ||
const equalSign = htmlx.lastIndexOf('=', attr.expression.start); | ||
const curly = htmlx.lastIndexOf('{', beforeStart(attr.expression.start)); | ||
str.overwrite(equalSign, curly + 1, ':'); | ||
str.remove(attr.expression.end, attr.end); | ||
} | ||
} | ||
if (!hasMoved) { | ||
return; | ||
} | ||
str.appendLeft(slotDefInsertionPoint, '{() => { let {'); | ||
str.appendRight(slotDefInsertionPoint, `} = ${getSingleSlotDef(component, slotName)}` + ';<>'); | ||
|
||
const closeSlotDefInsertionPoint = slotElIsComponent | ||
? htmlx.lastIndexOf('<', slotEl.end - 1) | ||
: slotEl.end; | ||
str.appendLeft(closeSlotDefInsertionPoint, '</>}}'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/svelte2tsx/test/htmlx2jsx/samples/component-named-slot/expected.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<><Parent >{() => { let {foo, bar:baz} = __sveltets_instanceOf(Parent).$$slot_def['default'];<> | ||
{() => { let {bla} = __sveltets_instanceOf(Parent).$$slot_def['named'];<><Component > | ||
{foo} {baz} {bla} | ||
</Component></>}} | ||
<Component >{() => { let {blubb} = __sveltets_instanceOf(Component).$$slot_def['default'];<> | ||
{blubb} | ||
</>}}</Component> | ||
</>}}</Parent></> |
8 changes: 8 additions & 0 deletions
8
packages/svelte2tsx/test/htmlx2jsx/samples/component-named-slot/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Parent let:foo let:bar={baz}> | ||
<Component slot="named" let:bla> | ||
{foo} {baz} {bla} | ||
</Component> | ||
<Component let:blubb> | ||
{blubb} | ||
</Component> | ||
</Parent> |
19 changes: 19 additions & 0 deletions
19
packages/svelte2tsx/test/htmlx2jsx/samples/svelte-fragment/expected.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<><Component> | ||
<sveltefragment> | ||
<p>hi</p> | ||
</sveltefragment> | ||
|
||
<sveltefragment > | ||
<p>hi</p> | ||
</sveltefragment> | ||
</Component> | ||
|
||
<Component> | ||
{() => { let {foo, bar:baz} = __sveltets_instanceOf(Component).$$slot_def['default'];<><sveltefragment > | ||
<p>{foo} {baz}</p> | ||
</sveltefragment></>}} | ||
|
||
{() => { let {foo, bar:baz} = __sveltets_instanceOf(Component).$$slot_def['named'];<><sveltefragment > | ||
<p>{foo} {baz}</p> | ||
</sveltefragment></>}} | ||
</Component></> |
19 changes: 19 additions & 0 deletions
19
packages/svelte2tsx/test/htmlx2jsx/samples/svelte-fragment/input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Component> | ||
<svelte:fragment> | ||
<p>hi</p> | ||
</svelte:fragment> | ||
|
||
<svelte:fragment slot="named"> | ||
<p>hi</p> | ||
</svelte:fragment> | ||
</Component> | ||
|
||
<Component> | ||
<svelte:fragment let:foo let:bar={baz}> | ||
<p>{foo} {baz}</p> | ||
</svelte:fragment> | ||
|
||
<svelte:fragment slot="named" let:foo let:bar={baz}> | ||
<p>{foo} {baz}</p> | ||
</svelte:fragment> | ||
</Component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
<svelte:head> | ||
<h1>Hi</h1> | ||
</svelte:head> | ||
<svelte:options /> | ||
<svelte:options /> | ||
<svelte:fragment /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters