Skip to content

Commit

Permalink
fix svelte2tsx parentheses overwrite issues (#302)
Browse files Browse the repository at this point in the history
* fix parentheses overwrite for await block

* fix class directive parentheses overwrite
  • Loading branch information
jasonlyu123 committed Jul 12, 2020
1 parent bfb4133 commit 17bfd6e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
19 changes: 12 additions & 7 deletions packages/svelte2tsx/src/htmlxtojsx.ts
Expand Up @@ -83,11 +83,12 @@ export function convertHtmlxToJsx(
};

const handleClassDirective = (attr: Node) => {
const needCurly = attr.expression.start == attr.start + 'class:'.length;
str.overwrite(attr.start, attr.expression.start, `{...__sveltets_ensureType(Boolean, !!(`);
str.appendLeft(attr.expression.end, `))${needCurly ? '}' : ''}`);
if (htmlx[attr.end - 1] == '"') {
str.remove(attr.end - 1, attr.end);
const endBrackets = `))}`;
if (attr.end !== attr.expression.end) {
str.overwrite(attr.expression.end, attr.end, endBrackets);
} else {
str.appendLeft(attr.end, endBrackets);
}
};

Expand Down Expand Up @@ -501,7 +502,6 @@ export function convertHtmlxToJsx(
// {() => {let _$$p = (somePromise);
const handleAwait = (awaitBlock: Node) => {
str.overwrite(awaitBlock.start, awaitBlock.expression.start, '{() => {let _$$p = (');
str.prependLeft(awaitBlock.expression.end, ');');
// then value } | {:then value} ->
// _$$p.then((value) => {<>
let thenStart: number;
Expand All @@ -517,11 +517,16 @@ export function convertHtmlxToJsx(
str.prependLeft(thenStart, '</>; ');
// add the start tag too
const awaitEnd = htmlx.indexOf('}', awaitBlock.expression.end);
str.remove(awaitEnd, awaitEnd + 1);
str.appendRight(awaitEnd, ' <>');

// somePromise} -> somePromise);
str.overwrite(awaitBlock.expression.end, awaitEnd + 1, ');');
str.appendRight(awaitEnd + 1, ' <>');
} else {
thenEnd = htmlx.lastIndexOf('}', awaitBlock.then.start) + 1;
thenStart = htmlx.indexOf('then', awaitBlock.expression.end);

// somePromise then -> somePromise); then
str.overwrite(awaitBlock.expression.end, thenStart, '); ');
}
if (awaitBlock.value) {
str.overwrite(
Expand Down
@@ -0,0 +1,9 @@
<>{() => {let _$$p = (somePromise); _$$p.then((value) => {<>
<h1>Promise Resolved</h1>
</>})}}

{() => {let _$$p = (somePromise); <>
<h1>Loading...</h1>
</>; _$$p.then(() => {<>
<h1>Promise Resolved</h1>
</>})}}</>
@@ -0,0 +1,9 @@
{#await (somePromise) then value}
<h1>Promise Resolved</h1>
{/await}

{#await (somePromise)}
<h1>Loading...</h1>
{:then}
<h1>Promise Resolved</h1>
{/await}
@@ -0,0 +1 @@
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
@@ -0,0 +1 @@
<h1 class:active={("test"=="test")}>Hello</h1>
@@ -1 +1,2 @@
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
<><h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1>
<h1 {...__sveltets_ensureType(Boolean, !!("test"=="test"))}>Hello</h1></>
@@ -1 +1,2 @@
<h1 class:active={"test"=="test"}>Hello</h1>
<h1 class:active={"test"=="test"}>Hello</h1>
<h1 class:active="{"test"=="test"}">Hello</h1>

0 comments on commit 17bfd6e

Please sign in to comment.