tempo-sdk 0.0.16 → 0.0.18

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 (59) hide show
  1. package/dist/canvas/editor.d.ts +44 -9
  2. package/dist/canvas/editor.js +116 -416
  3. package/dist/canvas/editor.js.map +1 -1
  4. package/dist/canvas/index.d.ts +1 -0
  5. package/dist/canvas/index.js +152 -44
  6. package/dist/canvas/index.js.map +1 -1
  7. package/dist/canvas/parser.d.ts +1 -2
  8. package/dist/canvas/parser.js +1 -3
  9. package/dist/{chunk-ICP7LCPJ.js → chunk-6Q4I3RFN.js} +37 -9
  10. package/dist/chunk-6Q4I3RFN.js.map +1 -0
  11. package/dist/{chunk-77BNMPBS.js → chunk-CB5NJJJT.js} +31 -5
  12. package/dist/chunk-CB5NJJJT.js.map +1 -0
  13. package/dist/expo/plugin.cjs +30 -4
  14. package/dist/expo/plugin.cjs.map +1 -1
  15. package/dist/expo/plugin.js +1 -1
  16. package/dist/expo-babel-plugin.cjs +30 -4
  17. package/dist/expo-babel-plugin.cjs.map +1 -1
  18. package/dist/expo-babel-plugin.d.cts +1 -1
  19. package/dist/expo-babel-plugin.d.ts +1 -1
  20. package/dist/expo-babel-plugin.js +1 -1
  21. package/dist/index.js +26 -6
  22. package/dist/index.js.map +1 -1
  23. package/dist/nextjs-loader.cjs +29 -4
  24. package/dist/nextjs-loader.cjs.map +1 -1
  25. package/package.json +4 -3
  26. package/dist/annotation-strategy-DCJHc--2.d.cts +0 -13
  27. package/dist/annotation-strategy-DCJHc--2.d.ts +0 -13
  28. package/dist/chunk-2LP5G6CD.js +0 -748
  29. package/dist/chunk-2LP5G6CD.js.map +0 -1
  30. package/dist/chunk-77BNMPBS.js.map +0 -1
  31. package/dist/chunk-B3PHU7XX.js +0 -571
  32. package/dist/chunk-B3PHU7XX.js.map +0 -1
  33. package/dist/chunk-EJBWZCEE.js +0 -434
  34. package/dist/chunk-EJBWZCEE.js.map +0 -1
  35. package/dist/chunk-F64WRL4H.js +0 -884
  36. package/dist/chunk-F64WRL4H.js.map +0 -1
  37. package/dist/chunk-G7M57ARC.js +0 -902
  38. package/dist/chunk-G7M57ARC.js.map +0 -1
  39. package/dist/chunk-GHPL4LGE.js +0 -205
  40. package/dist/chunk-GHPL4LGE.js.map +0 -1
  41. package/dist/chunk-ICP7LCPJ.js.map +0 -1
  42. package/dist/chunk-IG3JZVZI.js +0 -846
  43. package/dist/chunk-IG3JZVZI.js.map +0 -1
  44. package/dist/chunk-MBTLXFNP.js +0 -605
  45. package/dist/chunk-MBTLXFNP.js.map +0 -1
  46. package/dist/chunk-MD3HJ7L3.js +0 -457
  47. package/dist/chunk-MD3HJ7L3.js.map +0 -1
  48. package/dist/chunk-O3HYJ6BU.js +0 -433
  49. package/dist/chunk-O3HYJ6BU.js.map +0 -1
  50. package/dist/chunk-WGGLHHC5.js +0 -337
  51. package/dist/chunk-WGGLHHC5.js.map +0 -1
  52. package/dist/chunk-X3K7Z2BH.js +0 -900
  53. package/dist/chunk-X3K7Z2BH.js.map +0 -1
  54. package/dist/chunk-ZHORRCQG.js +0 -756
  55. package/dist/chunk-ZHORRCQG.js.map +0 -1
  56. package/dist/index.cjs +0 -1170
  57. package/dist/index.cjs.map +0 -1
  58. package/dist/index.d.cts +0 -99
  59. package/dist/page-BokkL6FZ.d.ts +0 -35
@@ -1,8 +1,6 @@
1
1
  import {
2
- fallbackPageName,
3
- parseCanvasFile,
4
- unwrapTsExpression
5
- } from "../chunk-ICP7LCPJ.js";
2
+ parseCanvasFile
3
+ } from "../chunk-6Q4I3RFN.js";
6
4
 
7
5
  // canvas/editor.ts
8
6
  import { parseSync } from "oxc-parser";
@@ -12,51 +10,15 @@ function applyCanvasSourceEdits(source, edits) {
12
10
  let nextSource = source;
13
11
  for (const edit of sorted) {
14
12
  if (edit.start < 0 || edit.end < edit.start || edit.end > source.length || edit.end > previousStart) {
15
- throw new Error(`Invalid overlapping canvas source edit ${edit.start}:${edit.end}`);
13
+ throw new Error(
14
+ `Invalid overlapping canvas source edit ${edit.start}:${edit.end}`
15
+ );
16
16
  }
17
17
  nextSource = nextSource.slice(0, edit.start) + edit.value + nextSource.slice(edit.end);
18
18
  previousStart = edit.start;
19
19
  }
20
20
  return nextSource;
21
21
  }
22
- function migrateLegacyCanvasToReact(options) {
23
- const existing = parseCanvasFile(options.filePath, options.source);
24
- if (existing.canvasElementSpan) {
25
- return {
26
- source: options.source,
27
- edits: [],
28
- parsed: existing,
29
- migrated: false
30
- };
31
- }
32
- const legacy = readLegacyCanvasMigration(options.filePath, options.source);
33
- if (legacy.unsupported.length > 0) {
34
- throw new Error(legacy.unsupported.join("\n"));
35
- }
36
- const newline = detectNewline(options.source);
37
- const edits = [...legacy.exportEdits];
38
- const importPrefix = legacy.importInsertionOffset === 0 || options.source.slice(0, legacy.importInsertionOffset).endsWith(newline) ? "" : newline;
39
- edits.push({
40
- start: legacy.importInsertionOffset,
41
- end: legacy.importInsertionOffset,
42
- value: `${importPrefix}${legacy.importSource}${newline}`
43
- });
44
- edits.push({
45
- start: options.source.length,
46
- end: options.source.length,
47
- value: `${newline}${buildMigratedCanvasComponent(legacy, newline)}`
48
- });
49
- const source = collapseBlankLines(
50
- applyCanvasSourceEdits(options.source, edits),
51
- newline
52
- );
53
- return {
54
- source,
55
- edits,
56
- parsed: parseCanvasFile(options.filePath, source),
57
- migrated: true
58
- };
59
- }
60
22
  function updateCanvasName(options) {
61
23
  return editCanvasStringAttribute(
62
24
  options,
@@ -161,7 +123,9 @@ function updateStoryboardLayout(options) {
161
123
  const storyboard = findStoryboard(parsed, options.storyboardId);
162
124
  const spans = storyboard.layoutSpans;
163
125
  if (!spans) {
164
- throw new Error(`Storyboard "${options.storyboardId}" has no editable layout`);
126
+ throw new Error(
127
+ `Storyboard "${options.storyboardId}" has no editable layout`
128
+ );
165
129
  }
166
130
  const edits = [];
167
131
  for (const key of ["x", "y", "width", "height"]) {
@@ -204,7 +168,9 @@ function updateStoryboardComponent(options) {
204
168
  const parsed = parseCanvasFile(options.filePath, options.source);
205
169
  const storyboard = findStoryboard(parsed, options.storyboardId);
206
170
  if (storyboard.kind !== "component") {
207
- throw new Error(`Storyboard "${options.storyboardId}" is not a component storyboard`);
171
+ throw new Error(
172
+ `Storyboard "${options.storyboardId}" is not a component storyboard`
173
+ );
208
174
  }
209
175
  const imports = [];
210
176
  if (options.componentImport) {
@@ -255,7 +221,9 @@ function updateRouteStoryboardRoute(options) {
255
221
  const parsed = parseCanvasFile(options.filePath, options.source);
256
222
  const storyboard = findStoryboard(parsed, options.storyboardId);
257
223
  if (storyboard.kind !== "route") {
258
- throw new Error(`Storyboard "${options.storyboardId}" is not a route storyboard`);
224
+ throw new Error(
225
+ `Storyboard "${options.storyboardId}" is not a route storyboard`
226
+ );
259
227
  }
260
228
  const edit = upsertJsxStringAttribute({
261
229
  source: options.source,
@@ -274,7 +242,9 @@ function updateHtmlStoryboardSource(options) {
274
242
  const parsed = parseCanvasFile(options.filePath, options.source);
275
243
  const storyboard = findStoryboard(parsed, options.storyboardId);
276
244
  if (storyboard.kind !== "html") {
277
- throw new Error(`Storyboard "${options.storyboardId}" is not an html storyboard`);
245
+ throw new Error(
246
+ `Storyboard "${options.storyboardId}" is not an html storyboard`
247
+ );
278
248
  }
279
249
  const filepath = options.filepath && options.filepath.trim() !== "" ? options.filepath.trim() : void 0;
280
250
  const src = options.src && options.src.trim() !== "" ? options.src : void 0;
@@ -305,7 +275,9 @@ function updateHtmlStoryboardSource(options) {
305
275
  }
306
276
  function addCanvasStoryboard(options) {
307
277
  const parsed = parseCanvasFile(options.filePath, options.source);
308
- if (parsed.storyboards.some((item) => item.storyboardKey === options.storyboard.id)) {
278
+ if (parsed.storyboards.some(
279
+ (item) => item.storyboardKey === options.storyboard.id
280
+ )) {
309
281
  throw new Error(`Storyboard id "${options.storyboard.id}" already exists`);
310
282
  }
311
283
  const imports = [];
@@ -320,7 +292,9 @@ function addCanvasStoryboard(options) {
320
292
  if (options.storyboard.kind === "component") {
321
293
  componentExpression = options.storyboard.componentExpression ?? options.storyboard.componentImport?.localName;
322
294
  if (!componentExpression) {
323
- throw new Error("Component storyboard requires componentExpression or componentImport");
295
+ throw new Error(
296
+ "Component storyboard requires componentExpression or componentImport"
297
+ );
324
298
  }
325
299
  if (options.storyboard.componentImport) {
326
300
  const importLine = buildMissingComponentImportLine(
@@ -339,10 +313,7 @@ function addCanvasStoryboard(options) {
339
313
  storyboard: options.storyboard,
340
314
  componentExpression
341
315
  });
342
- const edits = [
343
- ...importEdit ? [importEdit] : [],
344
- insertionEdit
345
- ];
316
+ const edits = [...importEdit ? [importEdit] : [], insertionEdit];
346
317
  return applyResult(options.source, parsed, edits);
347
318
  }
348
319
  function removeCanvasStoryboard(options) {
@@ -405,7 +376,11 @@ function upsertJsxStringAttribute(options) {
405
376
  }
406
377
  const value = JSON.stringify(options.value);
407
378
  if (options.valueSpan) {
408
- return { start: options.valueSpan.start, end: options.valueSpan.end, value };
379
+ return {
380
+ start: options.valueSpan.start,
381
+ end: options.valueSpan.end,
382
+ value
383
+ };
409
384
  }
410
385
  const offset = findOpeningElementAttributeInsertionOffset(
411
386
  options.source,
@@ -427,7 +402,11 @@ function upsertObjectStringProperty(options) {
427
402
  }
428
403
  const value = JSON.stringify(options.value);
429
404
  if (options.valueSpan) {
430
- return { start: options.valueSpan.start, end: options.valueSpan.end, value };
405
+ return {
406
+ start: options.valueSpan.start,
407
+ end: options.valueSpan.end,
408
+ value
409
+ };
431
410
  }
432
411
  if (typeof options.objectClose !== "number") {
433
412
  throw new Error(`Object close not found for "${options.name}" property`);
@@ -509,9 +488,14 @@ function buildCanvasChildInsertionEdit(options) {
509
488
  if (!openingSpan) {
510
489
  throw new Error("Canvas closing element not found");
511
490
  }
512
- const openingSource = options.source.slice(openingSpan.start, openingSpan.end);
491
+ const openingSource = options.source.slice(
492
+ openingSpan.start,
493
+ openingSpan.end
494
+ );
513
495
  const selfClosingMatch = openingSource.match(/\s*\/\s*>$/);
514
- const canvasName = openingSource.match(/^<\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)/)?.[1];
496
+ const canvasName = openingSource.match(
497
+ /^<\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)/
498
+ )?.[1];
515
499
  if (!selfClosingMatch || !canvasName) {
516
500
  throw new Error("Canvas closing element not found");
517
501
  }
@@ -527,27 +511,44 @@ function buildCanvasChildInsertionEdit(options) {
527
511
  return {
528
512
  start: replacementStart,
529
513
  end: openingSpan.end,
530
- value: `>${newline}${childIndent}<${options.tag}${newline}${props.map((prop) => `${propIndent}${prop}`).join(newline)}${newline}${childIndent}/>${newline}${closingIndent}</${canvasName}>`
514
+ value: `>${newline}${childIndent}<${options.tag}${newline}${props.map((prop) => `${propIndent}${prop}`).join(
515
+ newline
516
+ )}${newline}${childIndent}/>${newline}${closingIndent}</${canvasName}>`
531
517
  };
532
518
  }
533
519
  function storyboardElementProps(storyboard, componentExpression) {
520
+ const idProp = formatJsxStringAttribute(
521
+ "id",
522
+ requireNonEmpty(storyboard.id, "Storyboard id")
523
+ );
524
+ if (storyboard.kind === "legacy-v1") {
525
+ return [idProp, `storyboard={${storyboard.storyboardExpression}}`];
526
+ }
534
527
  return [
535
- `id={${JSON.stringify(requireNonEmpty(storyboard.id, "Storyboard id"))}}`,
536
- ...storyboard.name && storyboard.name.trim() !== "" ? [`name={${JSON.stringify(storyboard.name)}}`] : [],
528
+ idProp,
529
+ ...storyboard.name && storyboard.name.trim() !== "" ? [formatJsxStringAttribute("name", storyboard.name)] : [],
537
530
  ...storyboardKindProps(storyboard, componentExpression),
538
531
  `layout={{ ${formatLayout(storyboard.layout)} }}`
539
532
  ];
540
533
  }
534
+ function formatJsxStringAttribute(name, value) {
535
+ const encoded = JSON.stringify(value);
536
+ const plainSafe = encoded === `"${value}"` && !value.includes("&");
537
+ return plainSafe ? `${name}=${encoded}` : `${name}={${encoded}}`;
538
+ }
541
539
  function storyboardKindProps(storyboard, componentExpression) {
542
540
  if (storyboard.kind === "component") {
543
- return [`component={${componentExpression}}`];
541
+ return [
542
+ `component={${componentExpression}}`,
543
+ ...storyboard.props && storyboard.props.trim() !== "" ? [`props={${storyboard.props.trim()}}`] : []
544
+ ];
544
545
  }
545
546
  if (storyboard.kind === "route") {
546
- return [`route={${JSON.stringify(storyboard.route)}}`];
547
+ return [formatJsxStringAttribute("route", storyboard.route)];
547
548
  }
548
549
  return [
549
- ...storyboard.filepath ? [`filepath={${JSON.stringify(storyboard.filepath)}}`] : [],
550
- ...storyboard.src ? [`src={${JSON.stringify(storyboard.src)}}`] : []
550
+ ...storyboard.filepath ? [formatJsxStringAttribute("filepath", storyboard.filepath)] : [],
551
+ ...storyboard.src ? [formatJsxStringAttribute("src", storyboard.src)] : []
551
552
  ];
552
553
  }
553
554
  function formatLayout(layout) {
@@ -563,18 +564,17 @@ function formatLayout(layout) {
563
564
  function sdkComponentForStoryboard(kind) {
564
565
  if (kind === "route") return "RouteStoryboard";
565
566
  if (kind === "html") return "HtmlStoryboard";
567
+ if (kind === "legacy-v1") return "LegacyStoryboardV1";
566
568
  return "Storyboard";
567
569
  }
568
570
  function resolveSdkTag(parsed, component) {
569
571
  for (const item of parsed.imports) {
570
572
  if (item.source !== "tempo-sdk/canvas") continue;
571
- if (item.kind === "namespace") return { tag: `${item.localName}.${component}` };
573
+ if (item.kind === "namespace")
574
+ return { tag: `${item.localName}.${component}` };
572
575
  if (item.importedName === component) return { tag: item.localName };
573
576
  }
574
- const localName = pickAvailableIdentifier(
575
- parsed,
576
- `TempoCanvas${component}`
577
- );
577
+ const localName = pickAvailableIdentifier(parsed, `TempoCanvas${component}`);
578
578
  return {
579
579
  tag: localName,
580
580
  importLine: `import { ${component} as ${localName} } from "tempo-sdk/canvas";`
@@ -586,11 +586,17 @@ function buildMissingComponentImportLine(parsed, source, componentImport) {
586
586
  );
587
587
  if (existing) {
588
588
  if (existing.source !== componentImport.source) {
589
- throw new Error(`Import binding "${componentImport.localName}" already exists`);
589
+ throw new Error(
590
+ `Import binding "${componentImport.localName}" already exists`
591
+ );
590
592
  }
591
593
  return null;
592
594
  }
593
- if (hasIdentifier(source, componentImport.localName)) {
595
+ if (sourceContainsIdentifierNode(
596
+ parsed.filePath,
597
+ source,
598
+ componentImport.localName
599
+ )) {
594
600
  throw new Error(`Identifier "${componentImport.localName}" already exists`);
595
601
  }
596
602
  if (!componentImport.importedName || componentImport.importedName === "default") {
@@ -638,7 +644,8 @@ function buildUnusedComponentImportRemoval(options) {
638
644
  sourceAfterStoryboardDelete,
639
645
  componentImport.declarationSpan
640
646
  );
641
- if (hasIdentifier(sourceWithoutImport2, componentImport.localName)) return null;
647
+ if (hasIdentifier(sourceWithoutImport2, componentImport.localName))
648
+ return null;
642
649
  return {
643
650
  ...expandToFullLines(options.source, componentImport.declarationSpan),
644
651
  value: ""
@@ -655,7 +662,8 @@ function buildUnusedComponentImportRemoval(options) {
655
662
  sourceAfterStoryboardDelete,
656
663
  specifierRange
657
664
  );
658
- if (hasIdentifier(sourceWithoutImport, componentImport.localName)) return null;
665
+ if (hasIdentifier(sourceWithoutImport, componentImport.localName))
666
+ return null;
659
667
  return {
660
668
  ...specifierRange,
661
669
  value: ""
@@ -763,347 +771,6 @@ function expandStatementForward(source, span) {
763
771
  }
764
772
  return { start: span.start, end };
765
773
  }
766
- function readLegacyCanvasMigration(filePath, source) {
767
- const result = parseSync(filePath, source);
768
- const program = result.program;
769
- const programBody = Array.isArray(program?.body) ? program.body : [];
770
- const bindings = collectLegacyBindings(programBody);
771
- const usedIdentifiers = collectSourceIdentifiers(source);
772
- const canvasTag = pickAvailableSourceIdentifier(usedIdentifiers, "TempoCanvas");
773
- const legacyTag = pickAvailableSourceIdentifier(
774
- usedIdentifiers,
775
- "TempoLegacyStoryboardV1"
776
- );
777
- const routeTag = pickAvailableSourceIdentifier(
778
- usedIdentifiers,
779
- "TempoRouteStoryboard"
780
- );
781
- const htmlTag = pickAvailableSourceIdentifier(
782
- usedIdentifiers,
783
- "TempoHtmlStoryboard"
784
- );
785
- usedIdentifiers.add(canvasTag);
786
- usedIdentifiers.add(legacyTag);
787
- usedIdentifiers.add(routeTag);
788
- usedIdentifiers.add(htmlTag);
789
- const componentName = pickAvailableSourceIdentifier(
790
- usedIdentifiers,
791
- `${toPascalIdentifier(fallbackPageName(filePath))}Canvas`
792
- );
793
- const pageBindingName = pickAvailableSourceIdentifier(usedIdentifiers, "page");
794
- const migration = {
795
- storyboards: [],
796
- exportEdits: [],
797
- unsupported: [],
798
- importInsertionOffset: lastImportEnd(programBody) ?? topLevelInsertionOffset(source),
799
- importSource: `import { Canvas as ${canvasTag}, LegacyStoryboardV1 as ${legacyTag}, RouteStoryboard as ${routeTag}, HtmlStoryboard as ${htmlTag} } from "tempo-sdk/canvas";`,
800
- componentName,
801
- canvasTag,
802
- legacyTag,
803
- routeTag,
804
- htmlTag,
805
- canvasName: fallbackPageName(filePath),
806
- hasPageBackgroundColor: false
807
- };
808
- let defaultExportCount = 0;
809
- const seenStoryboardIds = /* @__PURE__ */ new Set();
810
- const addStoryboard = (id, localName, typeName) => {
811
- if (seenStoryboardIds.has(id)) return;
812
- if (typeName !== "TempoStoryboard" && typeName !== "TempoRouteStoryboard" && typeName !== "TempoHtmlStoryboard") {
813
- return;
814
- }
815
- seenStoryboardIds.add(id);
816
- const binding = bindings.get(localName);
817
- const pageName = readObjectStringProperty(binding?.init, "name");
818
- if (!migration.pageExpression && pageName) {
819
- migration.canvasName = pageName;
820
- }
821
- if (typeName === "TempoStoryboard") {
822
- migration.storyboards.push({ kind: "component", id, localName });
823
- return;
824
- }
825
- const layout = readObjectLayoutProperty(binding?.init);
826
- if (!layout) {
827
- migration.unsupported.push(
828
- `Legacy ${typeName} export "${id}" requires a literal layout object`
829
- );
830
- return;
831
- }
832
- if (typeName === "TempoRouteStoryboard") {
833
- const route = readObjectStringProperty(binding?.init, "route");
834
- if (!route) {
835
- migration.unsupported.push(
836
- `Legacy TempoRouteStoryboard export "${id}" requires a literal route string`
837
- );
838
- return;
839
- }
840
- migration.storyboards.push({
841
- kind: "route",
842
- id,
843
- route,
844
- ...pageName ? { name: pageName } : {},
845
- layout
846
- });
847
- return;
848
- }
849
- const filepath = readObjectStringProperty(binding?.init, "filepath");
850
- if (!filepath) {
851
- migration.unsupported.push(
852
- `Legacy TempoHtmlStoryboard export "${id}" requires a literal filepath string`
853
- );
854
- return;
855
- }
856
- migration.storyboards.push({
857
- kind: "html",
858
- id,
859
- filepath,
860
- ...pageName ? { name: pageName } : {},
861
- layout
862
- });
863
- };
864
- for (const statement of programBody) {
865
- const span = nodeSpan(statement);
866
- if (!span) continue;
867
- if (statement?.type === "ExportAllDeclaration") {
868
- migration.unsupported.push("export * declarations require manual migration");
869
- continue;
870
- }
871
- if (statement?.type === "ExportDefaultDeclaration") {
872
- defaultExportCount += 1;
873
- if (defaultExportCount > 1) {
874
- migration.unsupported.push("Multiple default exports require manual migration");
875
- continue;
876
- }
877
- const declaration = unwrapTsExpression(statement.declaration);
878
- if (declaration?.type === "Identifier") {
879
- migration.pageExpression = declaration.name;
880
- const binding = bindings.get(declaration.name);
881
- const pageName = readObjectStringProperty(binding?.init, "name");
882
- if (pageName) migration.canvasName = pageName;
883
- migration.hasPageBackgroundColor = hasObjectProperty(
884
- binding?.init,
885
- "backgroundColor"
886
- );
887
- migration.exportEdits.push({
888
- ...expandStatementForward(source, span),
889
- value: ""
890
- });
891
- continue;
892
- }
893
- if (declaration?.type === "ObjectExpression") {
894
- migration.pageExpression = pageBindingName;
895
- const pageName = readObjectStringProperty(declaration, "name");
896
- if (pageName) migration.canvasName = pageName;
897
- migration.hasPageBackgroundColor = hasObjectProperty(
898
- declaration,
899
- "backgroundColor"
900
- );
901
- migration.exportEdits.push({
902
- start: statement.start,
903
- end: declaration.start,
904
- value: `const ${pageBindingName} = `
905
- });
906
- continue;
907
- }
908
- migration.unsupported.push("Default export must be a page object or identifier");
909
- continue;
910
- }
911
- if (statement?.type !== "ExportNamedDeclaration") {
912
- continue;
913
- }
914
- if (statement.source) {
915
- migration.unsupported.push(
916
- "Re-export declarations with a source require manual migration"
917
- );
918
- continue;
919
- }
920
- if (statement.declaration) {
921
- const declarationSpan = nodeSpan(statement.declaration);
922
- if (declarationSpan) {
923
- migration.exportEdits.push({
924
- start: statement.start,
925
- end: declarationSpan.start,
926
- value: ""
927
- });
928
- }
929
- if (statement.declaration.type === "VariableDeclaration") {
930
- for (const declaration of statement.declaration.declarations ?? []) {
931
- if (declaration?.id?.type !== "Identifier") continue;
932
- const localName = declaration.id.name;
933
- const typeName = legacyTypeName(
934
- declaration.id.typeAnnotation?.typeAnnotation
935
- );
936
- addStoryboard(localName, localName, typeName);
937
- }
938
- }
939
- continue;
940
- }
941
- migration.exportEdits.push({
942
- ...expandStatementForward(source, span),
943
- value: ""
944
- });
945
- for (const specifier of statement.specifiers ?? []) {
946
- const localName = legacyIdentifierName(specifier.local);
947
- const exportedName = legacyIdentifierName(
948
- specifier.exported ?? specifier.local
949
- );
950
- if (!localName || !exportedName) continue;
951
- addStoryboard(
952
- exportedName,
953
- localName,
954
- bindings.get(localName)?.typeName ?? null
955
- );
956
- }
957
- }
958
- return migration;
959
- }
960
- function buildMigratedCanvasComponent(migration, newline) {
961
- const canvasProps = [
962
- migration.pageExpression ? `name={${migration.pageExpression}.name}` : `name={${JSON.stringify(migration.canvasName)}}`,
963
- ...migration.pageExpression && migration.hasPageBackgroundColor ? [`backgroundColor={${migration.pageExpression}.backgroundColor}`] : []
964
- ].join(" ");
965
- if (migration.storyboards.length === 0) {
966
- return `export default function ${migration.componentName}() {
967
- return <${migration.canvasTag} ${canvasProps} />;
968
- }
969
- `;
970
- }
971
- const storyboards = migration.storyboards.map((storyboard) => ` ${formatMigratedStoryboard(migration, storyboard)}`).join(newline);
972
- return `export default function ${migration.componentName}() {
973
- return (
974
- <${migration.canvasTag} ${canvasProps}>
975
- ${storyboards}
976
- </${migration.canvasTag}>
977
- );
978
- }
979
- `;
980
- }
981
- function formatMigratedStoryboard(migration, storyboard) {
982
- if (storyboard.kind === "component") {
983
- return `<${migration.legacyTag} id=${JSON.stringify(storyboard.id)} storyboard={${storyboard.localName}} />`;
984
- }
985
- if (storyboard.kind === "route") {
986
- return `<${migration.routeTag} id=${JSON.stringify(storyboard.id)}${formatOptionalJsxStringAttribute("name", storyboard.name)} route=${JSON.stringify(storyboard.route)} layout={{ ${formatLayout(storyboard.layout)} }} />`;
987
- }
988
- return `<${migration.htmlTag} id=${JSON.stringify(storyboard.id)}${formatOptionalJsxStringAttribute("name", storyboard.name)} filepath=${JSON.stringify(storyboard.filepath)} layout={{ ${formatLayout(storyboard.layout)} }} />`;
989
- }
990
- function formatOptionalJsxStringAttribute(name, value) {
991
- return value ? ` ${name}=${JSON.stringify(value)}` : "";
992
- }
993
- function collectLegacyBindings(programBody) {
994
- const bindings = /* @__PURE__ */ new Map();
995
- const collect = (node) => {
996
- if (node?.type !== "VariableDeclaration") return;
997
- for (const declaration of node.declarations ?? []) {
998
- if (declaration?.id?.type !== "Identifier") continue;
999
- bindings.set(declaration.id.name, {
1000
- typeName: legacyTypeName(declaration.id.typeAnnotation?.typeAnnotation),
1001
- init: declaration.init
1002
- });
1003
- }
1004
- };
1005
- for (const statement of programBody) {
1006
- collect(statement);
1007
- if (statement?.type === "ExportNamedDeclaration") {
1008
- collect(statement.declaration);
1009
- }
1010
- }
1011
- return bindings;
1012
- }
1013
- function legacyTypeName(typeAnnotation) {
1014
- if (!typeAnnotation || typeAnnotation.type !== "TSTypeReference") return null;
1015
- const typeName = typeAnnotation.typeName;
1016
- if (typeName?.type === "Identifier") return typeName.name;
1017
- if (typeName?.type === "TSQualifiedName" && typeName.right?.type === "Identifier") {
1018
- return typeName.right.name;
1019
- }
1020
- return null;
1021
- }
1022
- function legacyIdentifierName(node) {
1023
- if (node?.type === "Identifier") return node.name;
1024
- if (node?.type === "Literal" && typeof node.value === "string") {
1025
- return node.value;
1026
- }
1027
- return null;
1028
- }
1029
- function readObjectStringProperty(node, propertyName) {
1030
- const property = getObjectPropertyFromNode(node, propertyName);
1031
- return property?.value?.type === "Literal" && typeof property.value.value === "string" ? property.value.value : null;
1032
- }
1033
- function hasObjectProperty(node, propertyName) {
1034
- return getObjectPropertyFromNode(node, propertyName) !== null;
1035
- }
1036
- function readObjectLayoutProperty(node) {
1037
- const property = getObjectPropertyFromNode(node, "layout");
1038
- const layout = unwrapTsExpression(property?.value);
1039
- if (!layout || layout.type !== "ObjectExpression") return null;
1040
- const x = readObjectNumberProperty(layout, "x");
1041
- const y = readObjectNumberProperty(layout, "y");
1042
- const width = readObjectNumberProperty(layout, "width");
1043
- const height = readObjectNumberProperty(layout, "height");
1044
- if (x == null || y == null || width == null || height == null) {
1045
- return null;
1046
- }
1047
- const zIndex = readObjectNumberProperty(layout, "zIndex");
1048
- return {
1049
- x,
1050
- y,
1051
- width,
1052
- height,
1053
- ...zIndex == null ? {} : { zIndex }
1054
- };
1055
- }
1056
- function readObjectNumberProperty(node, propertyName) {
1057
- const property = getObjectPropertyFromNode(node, propertyName);
1058
- const value = unwrapTsExpression(property?.value);
1059
- if (value?.type === "Literal" && typeof value.value === "number") {
1060
- return value.value;
1061
- }
1062
- if (value?.type === "UnaryExpression" && value.operator === "-" && value.argument?.type === "Literal" && typeof value.argument.value === "number") {
1063
- return -value.argument.value;
1064
- }
1065
- return null;
1066
- }
1067
- function getObjectPropertyFromNode(node, propertyName) {
1068
- const objectNode = unwrapTsExpression(node);
1069
- if (!objectNode || objectNode.type !== "ObjectExpression") return null;
1070
- for (const property of objectNode.properties ?? []) {
1071
- if (property?.type !== "Property" || property.computed) continue;
1072
- const key = property.key?.type === "Identifier" ? property.key.name : property.key?.type === "Literal" && typeof property.key.value === "string" ? property.key.value : null;
1073
- if (key === propertyName) return property;
1074
- }
1075
- return null;
1076
- }
1077
- function lastImportEnd(programBody) {
1078
- let end = null;
1079
- for (const statement of programBody) {
1080
- if (statement?.type !== "ImportDeclaration") continue;
1081
- const span = nodeSpan(statement);
1082
- if (span) end = Math.max(end ?? 0, span.end);
1083
- }
1084
- return end;
1085
- }
1086
- function nodeSpan(node) {
1087
- return typeof node?.start === "number" && typeof node?.end === "number" && Number.isFinite(node.start) && Number.isFinite(node.end) ? { start: node.start, end: node.end } : null;
1088
- }
1089
- function collectSourceIdentifiers(source) {
1090
- return new Set(
1091
- Array.from(source.matchAll(/\b[A-Za-z_$][\w$]*\b/g), (match) => match[0])
1092
- );
1093
- }
1094
- function pickAvailableSourceIdentifier(used, base) {
1095
- const normalizedBase = /^[A-Za-z_$]/.test(base) ? base : `Canvas${base}`;
1096
- if (!used.has(normalizedBase)) return normalizedBase;
1097
- for (let index = 2; index < 1e4; index += 1) {
1098
- const candidate = `${normalizedBase}${index}`;
1099
- if (!used.has(candidate)) return candidate;
1100
- }
1101
- throw new Error(`Unable to find available identifier for ${base}`);
1102
- }
1103
- function toPascalIdentifier(value) {
1104
- const identifier = value.split(/[^A-Za-z0-9_$]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
1105
- return identifier || "Canvas";
1106
- }
1107
774
  function lineIndentAt(source, offset) {
1108
775
  const lineStart = Math.max(source.lastIndexOf("\n", offset - 1) + 1, 0);
1109
776
  const linePrefix = source.slice(lineStart, offset);
@@ -1157,6 +824,31 @@ function pickAvailableIdentifier(parsed, base) {
1157
824
  function hasIdentifier(source, identifier) {
1158
825
  return new RegExp(`\\b${escapeRegExp(identifier)}\\b`).test(source);
1159
826
  }
827
+ function sourceContainsIdentifierNode(filePath, source, name) {
828
+ const program = parseSync(filePath, source).program;
829
+ let found = false;
830
+ walkWithParent(program, (node) => {
831
+ if (found) return;
832
+ if ((node.type === "Identifier" || node.type === "JSXIdentifier") && node.name === name) {
833
+ found = true;
834
+ }
835
+ });
836
+ return found;
837
+ }
838
+ function walkWithParent(node, visit, parent = null, key = null, seen = /* @__PURE__ */ new Set()) {
839
+ if (!node || typeof node !== "object" || seen.has(node)) return;
840
+ seen.add(node);
841
+ if (typeof node.type === "string") visit(node, parent, key);
842
+ for (const [childKey, value] of Object.entries(node)) {
843
+ if (!value || typeof value !== "object") continue;
844
+ if (Array.isArray(value)) {
845
+ for (const item of value)
846
+ walkWithParent(item, visit, node, childKey, seen);
847
+ } else {
848
+ walkWithParent(value, visit, node, childKey, seen);
849
+ }
850
+ }
851
+ }
1160
852
  function escapeRegExp(value) {
1161
853
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1162
854
  }
@@ -1182,8 +874,15 @@ function requiredSpan(value, message) {
1182
874
  export {
1183
875
  addCanvasStoryboard,
1184
876
  applyCanvasSourceEdits,
1185
- migrateLegacyCanvasToReact,
877
+ collapseBlankLines,
878
+ detectNewline,
879
+ expandStatementForward,
880
+ expandToFullLines,
881
+ formatJsxStringAttribute,
882
+ hasIdentifier,
1186
883
  removeCanvasStoryboard,
884
+ sourceContainsIdentifierNode,
885
+ topLevelInsertionOffset,
1187
886
  updateCanvasBackgroundColor,
1188
887
  updateCanvasName,
1189
888
  updateCanvasPath,
@@ -1193,6 +892,7 @@ export {
1193
892
  updateStoryboardId,
1194
893
  updateStoryboardLayout,
1195
894
  updateStoryboardName,
1196
- updateStoryboardProps
895
+ updateStoryboardProps,
896
+ walkWithParent
1197
897
  };
1198
898
  //# sourceMappingURL=editor.js.map