Skip to content

Commit 17bfd6e

Browse files
authored
fix svelte2tsx parentheses overwrite issues (#302)
* fix parentheses overwrite for await block * fix class directive parentheses overwrite
1 parent bfb4133 commit 17bfd6e

File tree

7 files changed

+36
-9
lines changed

7 files changed

+36
-9
lines changed

packages/svelte2tsx/src/htmlxtojsx.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ export function convertHtmlxToJsx(
8383
};
8484

8585
const handleClassDirective = (attr: Node) => {
86-
const needCurly = attr.expression.start == attr.start + 'class:'.length;
8786
str.overwrite(attr.start, attr.expression.start, `{...__sveltets_ensureType(Boolean, !!(`);
88-
str.appendLeft(attr.expression.end, `))${needCurly ? '}' : ''}`);
89-
if (htmlx[attr.end - 1] == '"') {
90-
str.remove(attr.end - 1, attr.end);
87+
const endBrackets = `))}`;
88+
if (attr.end !== attr.expression.end) {
89+
str.overwrite(attr.expression.end, attr.end, endBrackets);
90+
} else {
91+
str.appendLeft(attr.end, endBrackets);
9192
}
9293
};
9394

@@ -501,7 +502,6 @@ export function convertHtmlxToJsx(
501502
// {() => {let _$$p = (somePromise);
502503
const handleAwait = (awaitBlock: Node) => {
503504
str.overwrite(awaitBlock.start, awaitBlock.expression.start, '{() => {let _$$p = (');
504-
str.prependLeft(awaitBlock.expression.end, ');');
505505
// then value } | {:then value} ->
506506
// _$$p.then((value) => {<>
507507
let thenStart: number;
@@ -517,11 +517,16 @@ export function convertHtmlxToJsx(
517517
str.prependLeft(thenStart, '</>; ');
518518
// add the start tag too
519519
const awaitEnd = htmlx.indexOf('}', awaitBlock.expression.end);
520-
str.remove(awaitEnd, awaitEnd + 1);
521-
str.appendRight(awaitEnd, ' <>');
520+
521+
// somePromise} -> somePromise);
522+
str.overwrite(awaitBlock.expression.end, awaitEnd + 1, ');');
523+
str.appendRight(awaitEnd + 1, ' <>');
522524
} else {
523525
thenEnd = htmlx.lastIndexOf('}', awaitBlock.then.start) + 1;
524526
thenStart = htmlx.indexOf('then', awaitBlock.expression.end);
527+
528+
// somePromise then -> somePromise); then
529+
str.overwrite(awaitBlock.expression.end, thenStart, '); ');
525530
}
526531
if (awaitBlock.value) {
527532
str.overwrite(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<>{() => {let _$$p = (somePromise); _$$p.then((value) => {<>
2+
<h1>Promise Resolved</h1>
3+
</>})}}
4+
5+
{() => {let _$$p = (somePromise); <>
6+
<h1>Loading...</h1>
7+
</>; _$$p.then(() => {<>
8+
<h1>Promise Resolved</h1>
9+
</>})}}</>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{#await (somePromise) then value}
2+
<h1>Promise Resolved</h1>
3+
{/await}
4+
5+
{#await (somePromise)}
6+
<h1>Loading...</h1>
7+
{:then}
8+
<h1>Promise Resolved</h1>
9+
{/await}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1 class:active={("test"=="test")}>Hello</h1>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
1+
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1>
2+
<h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<h1 class:active={"test"=="test"}>Hello</h1>
1+
<h1 class:active={"test"=="test"}>Hello</h1>
2+
<h1 class:active="{"test"=="test"}">Hello</h1>

0 commit comments

Comments
 (0)