svelte2tsx 0.5.16 → 0.5.17

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 +32 -22
  2. package/index.mjs +32 -22
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -4469,8 +4469,8 @@ function addSlotLet(node, element) {
4469
4469
  * {a} --> a;
4470
4470
  */
4471
4471
  function handleMustacheTag(str, node, parent) {
4472
- if (parent.type === 'Attribute') {
4473
- // handles inside Attribute.ts
4472
+ if (parent.type === 'Attribute' || parent.type === 'StyleDirective') {
4473
+ // handled inside Attribute.ts / StyleDirective.ts
4474
4474
  return;
4475
4475
  }
4476
4476
  str.overwrite(node.start, node.start + 1, '', { contentOnly: true });
@@ -4517,15 +4517,6 @@ function handleStyleDirective(str, style, element) {
4517
4517
  ]);
4518
4518
  return;
4519
4519
  }
4520
- let start = style.value[0].start;
4521
- if (style.value[0].type === 'MustacheTag') {
4522
- start++;
4523
- }
4524
- const last = style.value[style.value.length - 1];
4525
- let end = last.end;
4526
- if (last.type === 'MustacheTag') {
4527
- end--;
4528
- }
4529
4520
  if (style.value.length > 1) {
4530
4521
  // We have multiple attribute values, so we build a template string out of them.
4531
4522
  for (const n of style.value) {
@@ -4533,7 +4524,11 @@ function handleStyleDirective(str, style, element) {
4533
4524
  str.appendRight(n.start, '$');
4534
4525
  }
4535
4526
  }
4536
- element.appendToStartEnd([ensureType + '`', [start, end], '`);']);
4527
+ element.appendToStartEnd([
4528
+ ensureType + '`',
4529
+ [style.value[0].start, style.value[style.value.length - 1].end],
4530
+ '`);'
4531
+ ]);
4537
4532
  return;
4538
4533
  }
4539
4534
  const styleVal = style.value[0];
@@ -4541,11 +4536,15 @@ function handleStyleDirective(str, style, element) {
4541
4536
  const quote = ['"', "'"].includes(str.original[styleVal.start - 1])
4542
4537
  ? str.original[styleVal.start - 1]
4543
4538
  : '"';
4544
- element.appendToStartEnd([`${ensureType}${quote}`, [start, end], `${quote});`]);
4539
+ element.appendToStartEnd([
4540
+ `${ensureType}${quote}`,
4541
+ [styleVal.start, styleVal.end],
4542
+ `${quote});`
4543
+ ]);
4545
4544
  }
4546
4545
  else {
4547
4546
  // MustacheTag
4548
- element.appendToStartEnd([ensureType, [start, end], ');']);
4547
+ element.appendToStartEnd([ensureType, [styleVal.start + 1, styleVal.end - 1], ');']);
4549
4548
  }
4550
4549
  }
4551
4550
 
@@ -6409,6 +6408,24 @@ function isNewGroup(sourceFile, topLevelImportDecl, scanner) {
6409
6408
  }
6410
6409
  return false;
6411
6410
  }
6411
+ /**
6412
+ * ensure it's in a newline.
6413
+ * if file has module script ensure an empty line to separate imports
6414
+ */
6415
+ function handleFirstInstanceImport(tsAst, astOffset, hasModuleScript, str) {
6416
+ var _a;
6417
+ const firstImport = tsAst.statements
6418
+ .filter(ts__default['default'].isImportDeclaration)
6419
+ .sort((a, b) => a.end - b.end)[0];
6420
+ if (!firstImport) {
6421
+ return;
6422
+ }
6423
+ const firstComment = Array.from((_a = ts__default['default'].getLeadingCommentRanges(firstImport.getFullText(), 0)) !== null && _a !== void 0 ? _a : []).sort((a, b) => a.pos - b.pos)[0];
6424
+ const start = firstComment && firstComment.kind === ts__default['default'].SyntaxKind.MultiLineCommentTrivia
6425
+ ? firstComment.pos + firstImport.getFullStart()
6426
+ : firstImport.getStart();
6427
+ str.appendRight(start + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
6428
+ }
6412
6429
 
6413
6430
  function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript) {
6414
6431
  const htmlx = str.original;
@@ -6586,14 +6603,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6586
6603
  // declare implicit reactive variables we found in the script
6587
6604
  implicitTopLevelNames.modifyCode(rootScope.declared);
6588
6605
  implicitStoreValues.modifyCode(astOffset, str);
6589
- const firstImport = tsAst.statements
6590
- .filter(ts__namespace.isImportDeclaration)
6591
- .sort((a, b) => a.end - b.end)[0];
6592
- if (firstImport) {
6593
- // ensure it's in a newline.
6594
- // if file has module script ensure an empty line to separate imports
6595
- str.appendRight(firstImport.getStart() + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
6596
- }
6606
+ handleFirstInstanceImport(tsAst, astOffset, hasModuleScript, str);
6597
6607
  if (mode === 'dts') {
6598
6608
  // Transform interface declarations to type declarations because indirectly
6599
6609
  // using interfaces inside the return type of a function is forbidden.
package/index.mjs CHANGED
@@ -4439,8 +4439,8 @@ function addSlotLet(node, element) {
4439
4439
  * {a} --> a;
4440
4440
  */
4441
4441
  function handleMustacheTag(str, node, parent) {
4442
- if (parent.type === 'Attribute') {
4443
- // handles inside Attribute.ts
4442
+ if (parent.type === 'Attribute' || parent.type === 'StyleDirective') {
4443
+ // handled inside Attribute.ts / StyleDirective.ts
4444
4444
  return;
4445
4445
  }
4446
4446
  str.overwrite(node.start, node.start + 1, '', { contentOnly: true });
@@ -4487,15 +4487,6 @@ function handleStyleDirective(str, style, element) {
4487
4487
  ]);
4488
4488
  return;
4489
4489
  }
4490
- let start = style.value[0].start;
4491
- if (style.value[0].type === 'MustacheTag') {
4492
- start++;
4493
- }
4494
- const last = style.value[style.value.length - 1];
4495
- let end = last.end;
4496
- if (last.type === 'MustacheTag') {
4497
- end--;
4498
- }
4499
4490
  if (style.value.length > 1) {
4500
4491
  // We have multiple attribute values, so we build a template string out of them.
4501
4492
  for (const n of style.value) {
@@ -4503,7 +4494,11 @@ function handleStyleDirective(str, style, element) {
4503
4494
  str.appendRight(n.start, '$');
4504
4495
  }
4505
4496
  }
4506
- element.appendToStartEnd([ensureType + '`', [start, end], '`);']);
4497
+ element.appendToStartEnd([
4498
+ ensureType + '`',
4499
+ [style.value[0].start, style.value[style.value.length - 1].end],
4500
+ '`);'
4501
+ ]);
4507
4502
  return;
4508
4503
  }
4509
4504
  const styleVal = style.value[0];
@@ -4511,11 +4506,15 @@ function handleStyleDirective(str, style, element) {
4511
4506
  const quote = ['"', "'"].includes(str.original[styleVal.start - 1])
4512
4507
  ? str.original[styleVal.start - 1]
4513
4508
  : '"';
4514
- element.appendToStartEnd([`${ensureType}${quote}`, [start, end], `${quote});`]);
4509
+ element.appendToStartEnd([
4510
+ `${ensureType}${quote}`,
4511
+ [styleVal.start, styleVal.end],
4512
+ `${quote});`
4513
+ ]);
4515
4514
  }
4516
4515
  else {
4517
4516
  // MustacheTag
4518
- element.appendToStartEnd([ensureType, [start, end], ');']);
4517
+ element.appendToStartEnd([ensureType, [styleVal.start + 1, styleVal.end - 1], ');']);
4519
4518
  }
4520
4519
  }
4521
4520
 
@@ -6379,6 +6378,24 @@ function isNewGroup(sourceFile, topLevelImportDecl, scanner) {
6379
6378
  }
6380
6379
  return false;
6381
6380
  }
6381
+ /**
6382
+ * ensure it's in a newline.
6383
+ * if file has module script ensure an empty line to separate imports
6384
+ */
6385
+ function handleFirstInstanceImport(tsAst, astOffset, hasModuleScript, str) {
6386
+ var _a;
6387
+ const firstImport = tsAst.statements
6388
+ .filter(ts__default.isImportDeclaration)
6389
+ .sort((a, b) => a.end - b.end)[0];
6390
+ if (!firstImport) {
6391
+ return;
6392
+ }
6393
+ const firstComment = Array.from((_a = ts__default.getLeadingCommentRanges(firstImport.getFullText(), 0)) !== null && _a !== void 0 ? _a : []).sort((a, b) => a.pos - b.pos)[0];
6394
+ const start = firstComment && firstComment.kind === ts__default.SyntaxKind.MultiLineCommentTrivia
6395
+ ? firstComment.pos + firstImport.getFullStart()
6396
+ : firstImport.getStart();
6397
+ str.appendRight(start + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
6398
+ }
6382
6399
 
6383
6400
  function processInstanceScriptContent(str, script, events, implicitStoreValues, mode, hasModuleScript) {
6384
6401
  const htmlx = str.original;
@@ -6556,14 +6573,7 @@ function processInstanceScriptContent(str, script, events, implicitStoreValues,
6556
6573
  // declare implicit reactive variables we found in the script
6557
6574
  implicitTopLevelNames.modifyCode(rootScope.declared);
6558
6575
  implicitStoreValues.modifyCode(astOffset, str);
6559
- const firstImport = tsAst.statements
6560
- .filter(ts.isImportDeclaration)
6561
- .sort((a, b) => a.end - b.end)[0];
6562
- if (firstImport) {
6563
- // ensure it's in a newline.
6564
- // if file has module script ensure an empty line to separate imports
6565
- str.appendRight(firstImport.getStart() + astOffset, '\n' + (hasModuleScript ? '\n' : ''));
6566
- }
6576
+ handleFirstInstanceImport(tsAst, astOffset, hasModuleScript, str);
6567
6577
  if (mode === 'dts') {
6568
6578
  // Transform interface declarations to type declarations because indirectly
6569
6579
  // using interfaces inside the return type of a function is forbidden.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte2tsx",
3
- "version": "0.5.16",
3
+ "version": "0.5.17",
4
4
  "description": "Convert Svelte components to TSX for type checking",
5
5
  "author": "David Pershouse",
6
6
  "license": "MIT",