svelte2tsx 0.5.9 → 0.5.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +54 -9
  2. package/index.mjs +54 -9
  3. package/package.json +3 -3
package/index.js CHANGED
@@ -2440,22 +2440,42 @@ function handleEventHandler$1(htmlx, str, attr, parent) {
2440
2440
  */
2441
2441
  function handleIf$1(htmlx, str, ifBlock, ifScope) {
2442
2442
  const endIf = htmlx.lastIndexOf('{', ifBlock.end - 1);
2443
+ const constTags = extractConstTags(ifBlock.children);
2444
+ const ifConditionEnd = htmlx.indexOf('}', ifBlock.expression.end) + 1;
2445
+ const hasConstTags = !!constTags.length;
2446
+ const endIIFE = createEndIIFE(hasConstTags);
2447
+ const startIIFE = createStartIIFE(hasConstTags);
2448
+ if (hasConstTags) {
2449
+ // {@const hi = exp} <div>{hi}> -> {(() => { const hi = exp; return <> <div>{hi}<div></> })}
2450
+ constTags.forEach((constTag) => {
2451
+ constTag(ifConditionEnd, str);
2452
+ });
2453
+ str.appendRight(ifConditionEnd, 'return <>');
2454
+ if (ifBlock.else) {
2455
+ // {:else} -> </>})()}</> : <>
2456
+ const elseWord = htmlx.lastIndexOf(':else', ifBlock.else.start);
2457
+ const elseStart = htmlx.lastIndexOf('{', elseWord);
2458
+ str.appendLeft(elseStart, endIIFE);
2459
+ }
2460
+ }
2443
2461
  if (ifBlock.elseif) {
2444
2462
  // {:else if expr} -> : (expr) ? <>
2463
+ // {:else if expr}{@const ...} -> : (expr) ? <>{(() => {const ...; return <>
2445
2464
  const elseIfStart = htmlx.lastIndexOf('{', ifBlock.expression.start);
2446
- const elseIfConditionEnd = htmlx.indexOf('}', ifBlock.expression.end) + 1;
2447
- str.overwrite(elseIfStart, ifBlock.expression.start, '</> : (', { contentOnly: true });
2448
- str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), elseIfConditionEnd, ') ? <>');
2465
+ str.overwrite(elseIfStart, ifBlock.expression.start, '</> : (', {
2466
+ contentOnly: true
2467
+ });
2468
+ str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), ifConditionEnd, ') ? <>' + startIIFE);
2449
2469
  ifScope.addElseIf(ifBlock.expression, str);
2450
2470
  if (!ifBlock.else) {
2451
- str.appendLeft(endIf, '</> : <>');
2471
+ str.appendLeft(endIf, endIIFE + '</> : <>');
2452
2472
  }
2453
2473
  return;
2454
2474
  }
2455
2475
  // {#if expr} -> {(expr) ? <>
2476
+ // {#if expr}{@const ...} -> {(expr) ? <>{(() => {const ...; return <>
2456
2477
  str.overwrite(ifBlock.start, ifBlock.expression.start, '{(', { contentOnly: true });
2457
- const end = htmlx.indexOf('}', ifBlock.expression.end);
2458
- str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), end + 1, ') ? <>', { contentOnly: true });
2478
+ str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), ifConditionEnd, ') ? <>' + startIIFE, { contentOnly: true });
2459
2479
  ifScope.addNestedIf(ifBlock.expression, str);
2460
2480
  if (ifBlock.else) {
2461
2481
  // {/if} -> </> }
@@ -2463,11 +2483,21 @@ function handleIf$1(htmlx, str, ifBlock, ifScope) {
2463
2483
  }
2464
2484
  else {
2465
2485
  // {/if} -> </> : <></>}
2466
- str.overwrite(endIf, ifBlock.end, '</> : <></>}', { contentOnly: true });
2486
+ // {@const ...} -> </>})()}</> : <></>}
2487
+ str.overwrite(endIf, ifBlock.end, endIIFE + '</> : <></>}', {
2488
+ contentOnly: true
2489
+ });
2467
2490
  }
2468
2491
  }
2492
+ function createStartIIFE(hasConstTags) {
2493
+ return hasConstTags ? '{(() => {' : '';
2494
+ }
2495
+ function createEndIIFE(hasConstTags) {
2496
+ return hasConstTags ? '</>})()}' : '';
2497
+ }
2469
2498
  /**
2470
2499
  * {:else} ---> </> : <>
2500
+ * {:else} {@const ...} -> </> : <>{(() => { const ...; return<>
2471
2501
  */
2472
2502
  function handleElse$1(htmlx, str, elseBlock, parent, ifScope) {
2473
2503
  var _a, _b;
@@ -2478,8 +2508,18 @@ function handleElse$1(htmlx, str, elseBlock, parent, ifScope) {
2478
2508
  const elseEnd = htmlx.lastIndexOf('}', elseBlock.start);
2479
2509
  const elseword = htmlx.lastIndexOf(':else', elseEnd);
2480
2510
  const elseStart = htmlx.lastIndexOf('{', elseword);
2481
- str.overwrite(elseStart, elseEnd + 1, '</> : <>');
2511
+ const constTags = extractConstTags(elseBlock.children);
2512
+ const hasConstTags = !!constTags.length;
2513
+ str.overwrite(elseStart, elseEnd + 1, '</> : <>' + createStartIIFE(hasConstTags));
2482
2514
  ifScope.addElse();
2515
+ if (!hasConstTags) {
2516
+ return;
2517
+ }
2518
+ constTags.forEach((constTag) => {
2519
+ constTag(elseEnd + 1, str);
2520
+ });
2521
+ str.appendRight(elseEnd + 1, 'return <>');
2522
+ str.appendLeft(elseBlock.end, createEndIIFE(true));
2483
2523
  }
2484
2524
 
2485
2525
  var IfType;
@@ -3925,7 +3965,7 @@ function handleAttribute(str, attr, parent, preserveCase, element) {
3925
3965
  return;
3926
3966
  }
3927
3967
  if (attr.value.length == 0) {
3928
- // attr=""
3968
+ // shouldn't happen
3929
3969
  addAttribute(attributeName, ['""']);
3930
3970
  return;
3931
3971
  }
@@ -3933,6 +3973,11 @@ function handleAttribute(str, attr, parent, preserveCase, element) {
3933
3973
  if (attr.value.length == 1) {
3934
3974
  const attrVal = attr.value[0];
3935
3975
  if (attrVal.type == 'Text') {
3976
+ // Handle the attr="" special case with a transformation that allows mapping of the position
3977
+ if (attrVal.start === attrVal.end) {
3978
+ addAttribute(attributeName, [[attrVal.start - 1, attrVal.end + 1]]);
3979
+ return;
3980
+ }
3936
3981
  const hasBrackets = str.original.lastIndexOf('}', attrVal.end) === attrVal.end - 1 ||
3937
3982
  str.original.lastIndexOf('}"', attrVal.end) === attrVal.end - 1 ||
3938
3983
  str.original.lastIndexOf("}'", attrVal.end) === attrVal.end - 1;
package/index.mjs CHANGED
@@ -2410,22 +2410,42 @@ function handleEventHandler$1(htmlx, str, attr, parent) {
2410
2410
  */
2411
2411
  function handleIf$1(htmlx, str, ifBlock, ifScope) {
2412
2412
  const endIf = htmlx.lastIndexOf('{', ifBlock.end - 1);
2413
+ const constTags = extractConstTags(ifBlock.children);
2414
+ const ifConditionEnd = htmlx.indexOf('}', ifBlock.expression.end) + 1;
2415
+ const hasConstTags = !!constTags.length;
2416
+ const endIIFE = createEndIIFE(hasConstTags);
2417
+ const startIIFE = createStartIIFE(hasConstTags);
2418
+ if (hasConstTags) {
2419
+ // {@const hi = exp} <div>{hi}> -> {(() => { const hi = exp; return <> <div>{hi}<div></> })}
2420
+ constTags.forEach((constTag) => {
2421
+ constTag(ifConditionEnd, str);
2422
+ });
2423
+ str.appendRight(ifConditionEnd, 'return <>');
2424
+ if (ifBlock.else) {
2425
+ // {:else} -> </>})()}</> : <>
2426
+ const elseWord = htmlx.lastIndexOf(':else', ifBlock.else.start);
2427
+ const elseStart = htmlx.lastIndexOf('{', elseWord);
2428
+ str.appendLeft(elseStart, endIIFE);
2429
+ }
2430
+ }
2413
2431
  if (ifBlock.elseif) {
2414
2432
  // {:else if expr} -> : (expr) ? <>
2433
+ // {:else if expr}{@const ...} -> : (expr) ? <>{(() => {const ...; return <>
2415
2434
  const elseIfStart = htmlx.lastIndexOf('{', ifBlock.expression.start);
2416
- const elseIfConditionEnd = htmlx.indexOf('}', ifBlock.expression.end) + 1;
2417
- str.overwrite(elseIfStart, ifBlock.expression.start, '</> : (', { contentOnly: true });
2418
- str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), elseIfConditionEnd, ') ? <>');
2435
+ str.overwrite(elseIfStart, ifBlock.expression.start, '</> : (', {
2436
+ contentOnly: true
2437
+ });
2438
+ str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), ifConditionEnd, ') ? <>' + startIIFE);
2419
2439
  ifScope.addElseIf(ifBlock.expression, str);
2420
2440
  if (!ifBlock.else) {
2421
- str.appendLeft(endIf, '</> : <>');
2441
+ str.appendLeft(endIf, endIIFE + '</> : <>');
2422
2442
  }
2423
2443
  return;
2424
2444
  }
2425
2445
  // {#if expr} -> {(expr) ? <>
2446
+ // {#if expr}{@const ...} -> {(expr) ? <>{(() => {const ...; return <>
2426
2447
  str.overwrite(ifBlock.start, ifBlock.expression.start, '{(', { contentOnly: true });
2427
- const end = htmlx.indexOf('}', ifBlock.expression.end);
2428
- str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), end + 1, ') ? <>', { contentOnly: true });
2448
+ str.overwrite(withTrailingPropertyAccess$1(str.original, ifBlock.expression.end), ifConditionEnd, ') ? <>' + startIIFE, { contentOnly: true });
2429
2449
  ifScope.addNestedIf(ifBlock.expression, str);
2430
2450
  if (ifBlock.else) {
2431
2451
  // {/if} -> </> }
@@ -2433,11 +2453,21 @@ function handleIf$1(htmlx, str, ifBlock, ifScope) {
2433
2453
  }
2434
2454
  else {
2435
2455
  // {/if} -> </> : <></>}
2436
- str.overwrite(endIf, ifBlock.end, '</> : <></>}', { contentOnly: true });
2456
+ // {@const ...} -> </>})()}</> : <></>}
2457
+ str.overwrite(endIf, ifBlock.end, endIIFE + '</> : <></>}', {
2458
+ contentOnly: true
2459
+ });
2437
2460
  }
2438
2461
  }
2462
+ function createStartIIFE(hasConstTags) {
2463
+ return hasConstTags ? '{(() => {' : '';
2464
+ }
2465
+ function createEndIIFE(hasConstTags) {
2466
+ return hasConstTags ? '</>})()}' : '';
2467
+ }
2439
2468
  /**
2440
2469
  * {:else} ---> </> : <>
2470
+ * {:else} {@const ...} -> </> : <>{(() => { const ...; return<>
2441
2471
  */
2442
2472
  function handleElse$1(htmlx, str, elseBlock, parent, ifScope) {
2443
2473
  var _a, _b;
@@ -2448,8 +2478,18 @@ function handleElse$1(htmlx, str, elseBlock, parent, ifScope) {
2448
2478
  const elseEnd = htmlx.lastIndexOf('}', elseBlock.start);
2449
2479
  const elseword = htmlx.lastIndexOf(':else', elseEnd);
2450
2480
  const elseStart = htmlx.lastIndexOf('{', elseword);
2451
- str.overwrite(elseStart, elseEnd + 1, '</> : <>');
2481
+ const constTags = extractConstTags(elseBlock.children);
2482
+ const hasConstTags = !!constTags.length;
2483
+ str.overwrite(elseStart, elseEnd + 1, '</> : <>' + createStartIIFE(hasConstTags));
2452
2484
  ifScope.addElse();
2485
+ if (!hasConstTags) {
2486
+ return;
2487
+ }
2488
+ constTags.forEach((constTag) => {
2489
+ constTag(elseEnd + 1, str);
2490
+ });
2491
+ str.appendRight(elseEnd + 1, 'return <>');
2492
+ str.appendLeft(elseBlock.end, createEndIIFE(true));
2453
2493
  }
2454
2494
 
2455
2495
  var IfType;
@@ -3895,7 +3935,7 @@ function handleAttribute(str, attr, parent, preserveCase, element) {
3895
3935
  return;
3896
3936
  }
3897
3937
  if (attr.value.length == 0) {
3898
- // attr=""
3938
+ // shouldn't happen
3899
3939
  addAttribute(attributeName, ['""']);
3900
3940
  return;
3901
3941
  }
@@ -3903,6 +3943,11 @@ function handleAttribute(str, attr, parent, preserveCase, element) {
3903
3943
  if (attr.value.length == 1) {
3904
3944
  const attrVal = attr.value[0];
3905
3945
  if (attrVal.type == 'Text') {
3946
+ // Handle the attr="" special case with a transformation that allows mapping of the position
3947
+ if (attrVal.start === attrVal.end) {
3948
+ addAttribute(attributeName, [[attrVal.start - 1, attrVal.end + 1]]);
3949
+ return;
3950
+ }
3906
3951
  const hasBrackets = str.original.lastIndexOf('}', attrVal.end) === attrVal.end - 1 ||
3907
3952
  str.original.lastIndexOf('}"', attrVal.end) === attrVal.end - 1 ||
3908
3953
  str.original.lastIndexOf("}'", attrVal.end) === attrVal.end - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.5.9",
3
+ "version": "0.5.10",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",
@@ -18,6 +18,7 @@
18
18
  "module": "index.mjs",
19
19
  "types": "index.d.ts",
20
20
  "devDependencies": {
21
+ "@jridgewell/trace-mapping": "^0.3.9",
21
22
  "@rollup/plugin-commonjs": "^15.0.0",
22
23
  "@rollup/plugin-json": "^4.0.0",
23
24
  "@rollup/plugin-node-resolve": "^9.0.0",
@@ -31,10 +32,9 @@
31
32
  "periscopic": "^2.0.2",
32
33
  "rollup": "2.52.7",
33
34
  "rollup-plugin-delete": "^2.0.0",
34
- "source-map": "^0.6.1",
35
35
  "source-map-support": "^0.5.16",
36
36
  "sourcemap-codec": "^1.4.8",
37
- "svelte": "~3.47.0",
37
+ "svelte": "~3.48.0",
38
38
  "tiny-glob": "^0.2.6",
39
39
  "tslib": "^1.10.0",
40
40
  "typescript": "^4.6.2"