tempo-sdk 0.0.15 → 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.
- package/dist/canvas/editor.d.ts +164 -0
- package/dist/canvas/editor.js +898 -0
- package/dist/canvas/editor.js.map +1 -0
- package/dist/canvas/index.d.ts +174 -0
- package/dist/canvas/index.js +2301 -0
- package/dist/canvas/index.js.map +1 -0
- package/dist/canvas/parser.d.ts +108 -0
- package/dist/canvas/parser.js +11 -0
- package/dist/canvas/parser.js.map +1 -0
- package/dist/chunk-6Q4I3RFN.js +643 -0
- package/dist/chunk-6Q4I3RFN.js.map +1 -0
- package/dist/{chunk-77BNMPBS.js → chunk-CB5NJJJT.js} +31 -5
- package/dist/chunk-CB5NJJJT.js.map +1 -0
- package/dist/expo/plugin.cjs +30 -4
- package/dist/expo/plugin.cjs.map +1 -1
- package/dist/expo/plugin.js +1 -1
- package/dist/expo-babel-plugin.cjs +30 -4
- package/dist/expo-babel-plugin.cjs.map +1 -1
- package/dist/expo-babel-plugin.d.cts +1 -1
- package/dist/expo-babel-plugin.d.ts +1 -1
- package/dist/expo-babel-plugin.js +1 -1
- package/dist/index.d.ts +3 -34
- package/dist/index.js +26 -6
- package/dist/index.js.map +1 -1
- package/dist/nextjs-loader.cjs +29 -4
- package/dist/nextjs-loader.cjs.map +1 -1
- package/dist/page-xi1aI5a7.d.ts +36 -0
- package/package.json +18 -3
- package/dist/annotation-strategy-DCJHc--2.d.cts +0 -13
- package/dist/annotation-strategy-DCJHc--2.d.ts +0 -13
- package/dist/chunk-2LP5G6CD.js +0 -748
- package/dist/chunk-2LP5G6CD.js.map +0 -1
- package/dist/chunk-77BNMPBS.js.map +0 -1
- package/dist/chunk-F64WRL4H.js +0 -884
- package/dist/chunk-F64WRL4H.js.map +0 -1
- package/dist/chunk-G7M57ARC.js +0 -902
- package/dist/chunk-G7M57ARC.js.map +0 -1
- package/dist/chunk-GHPL4LGE.js +0 -205
- package/dist/chunk-GHPL4LGE.js.map +0 -1
- package/dist/chunk-IG3JZVZI.js +0 -846
- package/dist/chunk-IG3JZVZI.js.map +0 -1
- package/dist/chunk-WGGLHHC5.js +0 -337
- package/dist/chunk-WGGLHHC5.js.map +0 -1
- package/dist/chunk-X3K7Z2BH.js +0 -900
- package/dist/chunk-X3K7Z2BH.js.map +0 -1
- package/dist/chunk-ZHORRCQG.js +0 -756
- package/dist/chunk-ZHORRCQG.js.map +0 -1
- package/dist/index.cjs +0 -1170
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -99
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
// canvas/parser.ts
|
|
2
|
+
import { parseSync } from "oxc-parser";
|
|
3
|
+
var SDK_IMPORT_SOURCES = /* @__PURE__ */ new Set(["tempo-sdk/canvas"]);
|
|
4
|
+
var SDK_COMPONENTS = /* @__PURE__ */ new Set([
|
|
5
|
+
"Canvas",
|
|
6
|
+
"Storyboard",
|
|
7
|
+
"LegacyStoryboardV1",
|
|
8
|
+
"RouteStoryboard",
|
|
9
|
+
"HtmlStoryboard"
|
|
10
|
+
]);
|
|
11
|
+
var DEFAULT_LAYOUT = {
|
|
12
|
+
x: 0,
|
|
13
|
+
y: 0,
|
|
14
|
+
width: 600,
|
|
15
|
+
height: 400
|
|
16
|
+
};
|
|
17
|
+
function parseCanvasFile(filePath, source) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
let program;
|
|
20
|
+
try {
|
|
21
|
+
const result = parseSync(filePath, source);
|
|
22
|
+
program = result.program;
|
|
23
|
+
for (const error of result.errors ?? []) {
|
|
24
|
+
errors.push({
|
|
25
|
+
message: error.message ?? "Unable to parse canvas source",
|
|
26
|
+
span: toSpan(error) ?? toSpan(error?.labels?.[0])
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
return {
|
|
31
|
+
filePath,
|
|
32
|
+
pageName: fallbackPageName(filePath),
|
|
33
|
+
imports: [],
|
|
34
|
+
storyboards: [],
|
|
35
|
+
errors: [
|
|
36
|
+
{
|
|
37
|
+
message: error instanceof Error ? error.message : "Unable to parse canvas source"
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const imports = readImports(program);
|
|
43
|
+
const { legacyObjects, stringBindings: objectStringBindings } = readObjectExpressionBindings(program, imports, source);
|
|
44
|
+
const storyboards = [];
|
|
45
|
+
let canvasElement = null;
|
|
46
|
+
walk(program, (node) => {
|
|
47
|
+
if (node.type !== "JSXElement") return;
|
|
48
|
+
const component = resolveSdkComponent(node.openingElement?.name, imports);
|
|
49
|
+
if (component === "Canvas" && !canvasElement) {
|
|
50
|
+
canvasElement = node;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (component === "Storyboard" || component === "LegacyStoryboardV1" || component === "RouteStoryboard" || component === "HtmlStoryboard") {
|
|
54
|
+
storyboards.push(
|
|
55
|
+
readStoryboard(node, component, imports, legacyObjects, source, errors)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (!canvasElement) {
|
|
60
|
+
errors.push({ message: "No <Canvas> declaration found in canvas file" });
|
|
61
|
+
} else {
|
|
62
|
+
errors.push(...validateCanvasChildren(canvasElement, imports));
|
|
63
|
+
}
|
|
64
|
+
const openingElement = canvasElement?.openingElement;
|
|
65
|
+
const name = readResolvableStringAttribute(
|
|
66
|
+
openingElement,
|
|
67
|
+
"name",
|
|
68
|
+
objectStringBindings
|
|
69
|
+
);
|
|
70
|
+
const path = readStringAttribute(openingElement, "path");
|
|
71
|
+
const backgroundColor = readResolvableStringAttribute(
|
|
72
|
+
openingElement,
|
|
73
|
+
"backgroundColor",
|
|
74
|
+
objectStringBindings
|
|
75
|
+
) ?? readResolvableStringAttribute(
|
|
76
|
+
openingElement,
|
|
77
|
+
"background",
|
|
78
|
+
objectStringBindings
|
|
79
|
+
);
|
|
80
|
+
if (openingElement && !name) {
|
|
81
|
+
errors.push({
|
|
82
|
+
message: '<Canvas> must include a literal "name" prop',
|
|
83
|
+
span: toSpan(openingElement)
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
filePath,
|
|
88
|
+
canvasPath: path?.value,
|
|
89
|
+
pageName: name?.value ?? fallbackPageName(filePath),
|
|
90
|
+
backgroundColor: backgroundColor?.value,
|
|
91
|
+
canvasElementSpan: toSpan(canvasElement),
|
|
92
|
+
canvasOpeningElementSpan: toSpan(openingElement),
|
|
93
|
+
canvasChildrenInsertionOffset: typeof canvasElement?.closingElement?.start === "number" ? canvasElement.closingElement.start : void 0,
|
|
94
|
+
nameSpan: name?.valueSpan,
|
|
95
|
+
nameAttributeSpan: name?.attributeSpan,
|
|
96
|
+
pathSpan: path?.valueSpan,
|
|
97
|
+
pathAttributeSpan: path?.attributeSpan,
|
|
98
|
+
backgroundColorSpan: backgroundColor?.valueSpan,
|
|
99
|
+
backgroundColorAttributeSpan: backgroundColor?.attributeSpan,
|
|
100
|
+
imports: imports.all,
|
|
101
|
+
storyboards,
|
|
102
|
+
errors
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function readImports(program) {
|
|
106
|
+
const all = [];
|
|
107
|
+
const byLocalName = /* @__PURE__ */ new Map();
|
|
108
|
+
const sdkLocalNames = /* @__PURE__ */ new Map();
|
|
109
|
+
const sdkNamespaces = /* @__PURE__ */ new Set();
|
|
110
|
+
for (const statement of program?.body ?? []) {
|
|
111
|
+
if (statement?.type !== "ImportDeclaration" || typeof statement.source?.value !== "string") {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const source = statement.source.value;
|
|
115
|
+
const isSdkImport = isSdkImportSource(source);
|
|
116
|
+
for (const specifier of statement.specifiers ?? []) {
|
|
117
|
+
const localName = specifier.local?.name;
|
|
118
|
+
if (typeof localName !== "string") continue;
|
|
119
|
+
const importedName = readImportedName(specifier);
|
|
120
|
+
const imported = {
|
|
121
|
+
localName,
|
|
122
|
+
source,
|
|
123
|
+
importedName,
|
|
124
|
+
kind: readImportKind(specifier),
|
|
125
|
+
importKind: specifier.importKind === "type" ? "type" : "value",
|
|
126
|
+
span: toSpan(specifier) ?? toSpan(statement) ?? { start: 0, end: 0 },
|
|
127
|
+
declarationSpan: toSpan(statement) ?? { start: 0, end: 0 },
|
|
128
|
+
specifierCount: statement.specifiers?.length ?? 0,
|
|
129
|
+
namedSpecifierCount: (statement.specifiers ?? []).filter(
|
|
130
|
+
(item) => item?.type === "ImportSpecifier"
|
|
131
|
+
).length,
|
|
132
|
+
hasDefaultSpecifier: (statement.specifiers ?? []).some(
|
|
133
|
+
(item) => item?.type === "ImportDefaultSpecifier"
|
|
134
|
+
),
|
|
135
|
+
hasNamespaceSpecifier: (statement.specifiers ?? []).some(
|
|
136
|
+
(item) => item?.type === "ImportNamespaceSpecifier"
|
|
137
|
+
)
|
|
138
|
+
};
|
|
139
|
+
all.push(imported);
|
|
140
|
+
byLocalName.set(localName, imported);
|
|
141
|
+
if (!isSdkImport) continue;
|
|
142
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
143
|
+
sdkNamespaces.add(localName);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (SDK_COMPONENTS.has(importedName)) {
|
|
147
|
+
sdkLocalNames.set(localName, importedName);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return { all, byLocalName, sdkLocalNames, sdkNamespaces };
|
|
152
|
+
}
|
|
153
|
+
function readObjectExpressionBindings(program, imports, source) {
|
|
154
|
+
const legacyObjects = /* @__PURE__ */ new Map();
|
|
155
|
+
const stringBindings = /* @__PURE__ */ new Map();
|
|
156
|
+
walk(program, (node) => {
|
|
157
|
+
if (node?.type !== "VariableDeclarator" || node.id?.type !== "Identifier") {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const objectExpression = unwrapTsExpression(node.init);
|
|
161
|
+
if (objectExpression?.type !== "ObjectExpression") return;
|
|
162
|
+
const bindingName = node.id.name;
|
|
163
|
+
legacyObjects.set(
|
|
164
|
+
bindingName,
|
|
165
|
+
readLegacyStoryboardObject(bindingName, objectExpression, imports, source)
|
|
166
|
+
);
|
|
167
|
+
const stringProperties = readObjectStringProperties(objectExpression);
|
|
168
|
+
if (stringProperties.size > 0) {
|
|
169
|
+
stringBindings.set(bindingName, stringProperties);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return { legacyObjects, stringBindings };
|
|
173
|
+
}
|
|
174
|
+
function readLegacyStoryboardObject(bindingName, objectExpression, imports, source) {
|
|
175
|
+
const errors = [];
|
|
176
|
+
const nameProperty = getObjectProperty(objectExpression, "name");
|
|
177
|
+
const name = nameProperty ? readStringLiteralValue(nameProperty.value) : null;
|
|
178
|
+
const layoutProperty = getObjectProperty(objectExpression, "layout");
|
|
179
|
+
const layout = layoutProperty ? readLayoutExpression(layoutProperty.value, "LegacyStoryboardV1") : {
|
|
180
|
+
value: DEFAULT_LAYOUT,
|
|
181
|
+
errors: [
|
|
182
|
+
{
|
|
183
|
+
message: '<LegacyStoryboardV1> storyboard object must include a literal "layout" object',
|
|
184
|
+
span: toSpan(objectExpression)
|
|
185
|
+
}
|
|
186
|
+
]
|
|
187
|
+
};
|
|
188
|
+
const renderProperty = getObjectProperty(objectExpression, "render");
|
|
189
|
+
const component = readRenderComponentReference(
|
|
190
|
+
renderProperty?.value,
|
|
191
|
+
imports,
|
|
192
|
+
source
|
|
193
|
+
);
|
|
194
|
+
if (nameProperty && !name) {
|
|
195
|
+
errors.push({
|
|
196
|
+
message: "<LegacyStoryboardV1> storyboard.name must be a string literal when set",
|
|
197
|
+
span: toSpan(nameProperty)
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
errors.push(...layout.errors);
|
|
201
|
+
return {
|
|
202
|
+
bindingName,
|
|
203
|
+
displayName: name?.value,
|
|
204
|
+
component: component ?? void 0,
|
|
205
|
+
objectSpan: toSpan(objectExpression),
|
|
206
|
+
objectClose: readLayoutClose(objectExpression),
|
|
207
|
+
nameSpan: name?.span,
|
|
208
|
+
namePropertySpan: toSpan(nameProperty),
|
|
209
|
+
layoutPropertySpan: toSpan(layoutProperty),
|
|
210
|
+
layout: layout.value,
|
|
211
|
+
layoutSpans: layout.spans,
|
|
212
|
+
errors
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function readObjectStringProperties(objectExpression) {
|
|
216
|
+
const properties = /* @__PURE__ */ new Map();
|
|
217
|
+
for (const property of objectExpression.properties ?? []) {
|
|
218
|
+
if (property?.type !== "Property" || property.computed) continue;
|
|
219
|
+
const propertyName = readPropertyKeyName(property.key);
|
|
220
|
+
const value = readStringLiteralValue(property.value);
|
|
221
|
+
if (!propertyName || !value) continue;
|
|
222
|
+
properties.set(propertyName, value);
|
|
223
|
+
}
|
|
224
|
+
return properties;
|
|
225
|
+
}
|
|
226
|
+
function readStoryboard(element, component, imports, legacyObjects, source, globalErrors) {
|
|
227
|
+
const openingElement = element.openingElement;
|
|
228
|
+
const errors = [];
|
|
229
|
+
const id = readStringAttribute(openingElement, "id");
|
|
230
|
+
const name = readStringAttribute(openingElement, "name");
|
|
231
|
+
const legacyReference = component === "LegacyStoryboardV1" ? readExpressionReference(openingElement, "storyboard", source) : void 0;
|
|
232
|
+
const legacyObject = legacyReference?.expression && isIdentifierExpression(legacyReference.expression) ? legacyObjects.get(legacyReference.expression) : void 0;
|
|
233
|
+
const layout = component === "LegacyStoryboardV1" ? {
|
|
234
|
+
value: legacyObject?.layout ?? DEFAULT_LAYOUT,
|
|
235
|
+
spans: legacyObject?.layoutSpans,
|
|
236
|
+
attributeSpan: void 0,
|
|
237
|
+
errors: legacyObject?.errors ?? (legacyReference ? [
|
|
238
|
+
{
|
|
239
|
+
message: "<LegacyStoryboardV1> storyboard prop must reference a same-file object literal",
|
|
240
|
+
span: legacyReference.span
|
|
241
|
+
}
|
|
242
|
+
] : [
|
|
243
|
+
{
|
|
244
|
+
message: '<LegacyStoryboardV1> must include a "storyboard" expression prop',
|
|
245
|
+
span: toSpan(openingElement)
|
|
246
|
+
}
|
|
247
|
+
])
|
|
248
|
+
} : readLayout(openingElement, component);
|
|
249
|
+
const kind = component === "Storyboard" || component === "LegacyStoryboardV1" ? "component" : component === "RouteStoryboard" ? "route" : "html";
|
|
250
|
+
if (!id) {
|
|
251
|
+
errors.push({
|
|
252
|
+
message: `<${component}> must include a literal "id" prop`,
|
|
253
|
+
span: toSpan(openingElement)
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (layout.errors.length > 0) {
|
|
257
|
+
errors.push(...layout.errors);
|
|
258
|
+
}
|
|
259
|
+
const storyboard = {
|
|
260
|
+
storyboardKey: id?.value ?? "",
|
|
261
|
+
displayName: name?.value ?? legacyObject?.displayName,
|
|
262
|
+
kind,
|
|
263
|
+
layout: layout.value,
|
|
264
|
+
editable: Boolean(id) && layout.errors.length === 0,
|
|
265
|
+
jsxElementSpan: toSpan(element) ?? { start: 0, end: 0 },
|
|
266
|
+
openingElementSpan: toSpan(openingElement),
|
|
267
|
+
idSpan: id?.valueSpan,
|
|
268
|
+
idAttributeSpan: id?.attributeSpan,
|
|
269
|
+
nameSpan: name?.valueSpan ?? legacyObject?.nameSpan,
|
|
270
|
+
nameAttributeSpan: name?.attributeSpan ?? legacyObject?.namePropertySpan,
|
|
271
|
+
layoutAttributeSpan: layout.attributeSpan,
|
|
272
|
+
layoutSpans: layout.spans,
|
|
273
|
+
props: readExpressionReference(openingElement, "props", source),
|
|
274
|
+
component: legacyObject?.component,
|
|
275
|
+
legacyV1: legacyReference ? {
|
|
276
|
+
storyboard: legacyReference,
|
|
277
|
+
objectName: legacyObject?.bindingName,
|
|
278
|
+
objectSpan: legacyObject?.objectSpan,
|
|
279
|
+
objectClose: legacyObject?.objectClose,
|
|
280
|
+
namePropertySpan: legacyObject?.namePropertySpan,
|
|
281
|
+
layoutPropertySpan: legacyObject?.layoutPropertySpan
|
|
282
|
+
} : void 0,
|
|
283
|
+
errors
|
|
284
|
+
};
|
|
285
|
+
if (component === "Storyboard") {
|
|
286
|
+
const componentReference = readComponentReference(
|
|
287
|
+
openingElement,
|
|
288
|
+
imports,
|
|
289
|
+
source
|
|
290
|
+
);
|
|
291
|
+
if (componentReference) {
|
|
292
|
+
storyboard.component = componentReference;
|
|
293
|
+
} else {
|
|
294
|
+
errors.push({
|
|
295
|
+
message: '<Storyboard> must include a "component" expression prop',
|
|
296
|
+
span: toSpan(openingElement)
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (component === "RouteStoryboard") {
|
|
301
|
+
const route = readStringAttribute(openingElement, "route");
|
|
302
|
+
if (route) {
|
|
303
|
+
storyboard.route = route.value;
|
|
304
|
+
storyboard.routeSpan = route.valueSpan;
|
|
305
|
+
storyboard.routeAttributeSpan = route.attributeSpan;
|
|
306
|
+
} else {
|
|
307
|
+
errors.push({
|
|
308
|
+
message: '<RouteStoryboard> must include a literal "route" prop',
|
|
309
|
+
span: toSpan(openingElement)
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (component === "HtmlStoryboard") {
|
|
314
|
+
const filepath = readStringAttribute(openingElement, "filepath");
|
|
315
|
+
const src = readStringAttribute(openingElement, "src");
|
|
316
|
+
storyboard.filepath = filepath?.value;
|
|
317
|
+
storyboard.filepathSpan = filepath?.valueSpan;
|
|
318
|
+
storyboard.filepathAttributeSpan = filepath?.attributeSpan;
|
|
319
|
+
storyboard.src = src?.value;
|
|
320
|
+
storyboard.srcSpan = src?.valueSpan;
|
|
321
|
+
storyboard.srcAttributeSpan = src?.attributeSpan;
|
|
322
|
+
if (!filepath && !src) {
|
|
323
|
+
errors.push({
|
|
324
|
+
message: '<HtmlStoryboard> must include a literal "filepath" or "src" prop',
|
|
325
|
+
span: toSpan(openingElement)
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
globalErrors.push(...errors);
|
|
330
|
+
return storyboard;
|
|
331
|
+
}
|
|
332
|
+
function validateCanvasChildren(canvasElement, imports) {
|
|
333
|
+
const errors = [];
|
|
334
|
+
for (const child of canvasElement.children ?? []) {
|
|
335
|
+
if (!child) continue;
|
|
336
|
+
if (child.type === "JSXText") {
|
|
337
|
+
if (typeof child.value === "string" && child.value.trim() === "") {
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (child.type === "JSXExpressionContainer") {
|
|
342
|
+
if (child.expression?.type === "JSXEmptyExpression") {
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
errors.push({
|
|
346
|
+
message: "<Canvas> children must be static storyboard declarations, not expressions",
|
|
347
|
+
span: toSpan(child)
|
|
348
|
+
});
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
if (child.type === "JSXElement") {
|
|
352
|
+
const component = resolveSdkComponent(
|
|
353
|
+
child.openingElement?.name,
|
|
354
|
+
imports
|
|
355
|
+
);
|
|
356
|
+
if (component === "Storyboard" || component === "LegacyStoryboardV1" || component === "RouteStoryboard" || component === "HtmlStoryboard") {
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
errors.push({
|
|
361
|
+
message: "<Canvas> children must be <Storyboard>, <RouteStoryboard>, <HtmlStoryboard>, or <LegacyStoryboardV1> declarations",
|
|
362
|
+
span: toSpan(child)
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return errors;
|
|
366
|
+
}
|
|
367
|
+
function readComponentReference(openingElement, imports, source) {
|
|
368
|
+
const attribute = getAttribute(openingElement, "component");
|
|
369
|
+
const expression = unwrapAttributeExpression(attribute?.value);
|
|
370
|
+
const span = toSpan(expression);
|
|
371
|
+
if (!expression || !span) return null;
|
|
372
|
+
const localName = expression.type === "Identifier" ? expression.name : expression.type === "MemberExpression" && expression.object?.type === "Identifier" ? expression.object.name : void 0;
|
|
373
|
+
return {
|
|
374
|
+
expression: source.slice(span.start, span.end),
|
|
375
|
+
span,
|
|
376
|
+
attributeSpan: toSpan(attribute),
|
|
377
|
+
localName,
|
|
378
|
+
import: localName ? imports.byLocalName.get(localName) : void 0
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
function readRenderComponentReference(renderValue, imports, source) {
|
|
382
|
+
if (renderValue?.type !== "ArrowFunctionExpression") return null;
|
|
383
|
+
const body = unwrapTsExpression(renderValue.body);
|
|
384
|
+
if (body?.type !== "JSXElement") return null;
|
|
385
|
+
const name = body.openingElement?.name;
|
|
386
|
+
if (name?.type !== "JSXIdentifier") return null;
|
|
387
|
+
const span = toSpan(name);
|
|
388
|
+
if (!span || typeof name.name !== "string") return null;
|
|
389
|
+
return {
|
|
390
|
+
expression: source.slice(span.start, span.end),
|
|
391
|
+
span,
|
|
392
|
+
localName: name.name,
|
|
393
|
+
import: imports.byLocalName.get(name.name)
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function readExpressionReference(openingElement, name, source) {
|
|
397
|
+
const attribute = getAttribute(openingElement, name);
|
|
398
|
+
const expression = unwrapAttributeExpression(attribute?.value);
|
|
399
|
+
const span = toSpan(expression);
|
|
400
|
+
if (!expression || !span) return void 0;
|
|
401
|
+
return {
|
|
402
|
+
expression: source.slice(span.start, span.end),
|
|
403
|
+
span,
|
|
404
|
+
attributeSpan: toSpan(attribute)
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function isIdentifierExpression(expression) {
|
|
408
|
+
return /^[A-Za-z_$][\w$]*$/.test(expression);
|
|
409
|
+
}
|
|
410
|
+
function readLayout(openingElement, componentName) {
|
|
411
|
+
const errors = [];
|
|
412
|
+
const attribute = getAttribute(openingElement, "layout");
|
|
413
|
+
const expression = unwrapAttributeExpression(attribute?.value);
|
|
414
|
+
if (!expression) {
|
|
415
|
+
return {
|
|
416
|
+
value: DEFAULT_LAYOUT,
|
|
417
|
+
attributeSpan: toSpan(attribute),
|
|
418
|
+
errors: [
|
|
419
|
+
{
|
|
420
|
+
message: `<${componentName}> must include a literal "layout" object prop`,
|
|
421
|
+
span: toSpan(openingElement)
|
|
422
|
+
}
|
|
423
|
+
]
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
const layout = readLayoutExpression(expression, componentName);
|
|
427
|
+
return {
|
|
428
|
+
...layout,
|
|
429
|
+
attributeSpan: toSpan(attribute)
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function readLayoutExpression(expression, componentName) {
|
|
433
|
+
const errors = [];
|
|
434
|
+
if (expression.type !== "ObjectExpression") {
|
|
435
|
+
return {
|
|
436
|
+
value: DEFAULT_LAYOUT,
|
|
437
|
+
spans: { layoutClose: readLayoutClose(expression) },
|
|
438
|
+
errors: [
|
|
439
|
+
{
|
|
440
|
+
message: `<${componentName}> layout must be an object literal`,
|
|
441
|
+
span: toSpan(expression)
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
const spans = {
|
|
447
|
+
layoutClose: readLayoutClose(expression)
|
|
448
|
+
};
|
|
449
|
+
const layout = { ...DEFAULT_LAYOUT };
|
|
450
|
+
for (const key of ["x", "y", "width", "height"]) {
|
|
451
|
+
const property = getObjectProperty(expression, key);
|
|
452
|
+
const value = property ? readNumberLiteral(property.value) : null;
|
|
453
|
+
if (value) {
|
|
454
|
+
layout[key] = value.value;
|
|
455
|
+
spans[key] = value.span;
|
|
456
|
+
} else {
|
|
457
|
+
errors.push({
|
|
458
|
+
message: `<${componentName}> layout.${key} must be a number literal`,
|
|
459
|
+
span: toSpan(property ?? expression)
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const zIndex = getObjectProperty(expression, "zIndex");
|
|
464
|
+
if (zIndex) {
|
|
465
|
+
const value = readNumberLiteral(zIndex.value);
|
|
466
|
+
if (value) {
|
|
467
|
+
layout.zIndex = value.value;
|
|
468
|
+
spans.zIndex = value.span;
|
|
469
|
+
} else {
|
|
470
|
+
errors.push({
|
|
471
|
+
message: `<${componentName}> layout.zIndex must be a number literal`,
|
|
472
|
+
span: toSpan(zIndex)
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
const intrinsicSizing = getObjectProperty(expression, "intrinsicSizing");
|
|
477
|
+
if (intrinsicSizing) {
|
|
478
|
+
if (intrinsicSizing.value?.type === "Literal" && intrinsicSizing.value.value === "root-element") {
|
|
479
|
+
layout.intrinsicSizing = "root-element";
|
|
480
|
+
spans.intrinsicSizing = toSpan(intrinsicSizing.value);
|
|
481
|
+
} else {
|
|
482
|
+
errors.push({
|
|
483
|
+
message: `<${componentName}> layout.intrinsicSizing must be "root-element" when set`,
|
|
484
|
+
span: toSpan(intrinsicSizing)
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return { value: layout, spans, errors };
|
|
489
|
+
}
|
|
490
|
+
function readStringLiteralValue(node) {
|
|
491
|
+
return node?.type === "Literal" && typeof node.value === "string" ? { value: node.value, span: toSpan(node) ?? { start: 0, end: 0 } } : null;
|
|
492
|
+
}
|
|
493
|
+
function unwrapTsExpression(node) {
|
|
494
|
+
let current = node;
|
|
495
|
+
while (current?.type === "TSSatisfiesExpression" || current?.type === "TSAsExpression" || current?.type === "TSNonNullExpression" || current?.type === "ParenthesizedExpression") {
|
|
496
|
+
current = current.expression;
|
|
497
|
+
}
|
|
498
|
+
return current;
|
|
499
|
+
}
|
|
500
|
+
function readResolvableStringAttribute(openingElement, name, bindings) {
|
|
501
|
+
const literal = readStringAttribute(openingElement, name);
|
|
502
|
+
if (literal) return literal;
|
|
503
|
+
const attribute = getAttribute(openingElement, name);
|
|
504
|
+
const expression = unwrapAttributeExpression(attribute?.value);
|
|
505
|
+
const expressionSpan = toSpan(expression);
|
|
506
|
+
const attributeSpan = toSpan(attribute);
|
|
507
|
+
if (!expression || !expressionSpan || !attributeSpan) return null;
|
|
508
|
+
const resolved = resolveStringMemberExpression(expression, bindings);
|
|
509
|
+
return resolved ? {
|
|
510
|
+
value: resolved.value,
|
|
511
|
+
valueSpan: expressionSpan,
|
|
512
|
+
attributeSpan
|
|
513
|
+
} : null;
|
|
514
|
+
}
|
|
515
|
+
function resolveStringMemberExpression(expression, bindings) {
|
|
516
|
+
if (expression?.type !== "MemberExpression") return null;
|
|
517
|
+
if (expression.object?.type !== "Identifier") return null;
|
|
518
|
+
const propertyName = readPropertyKeyName(expression.property);
|
|
519
|
+
if (!propertyName) return null;
|
|
520
|
+
return bindings.get(expression.object.name)?.get(propertyName) ?? null;
|
|
521
|
+
}
|
|
522
|
+
function readStringAttribute(openingElement, name) {
|
|
523
|
+
const attribute = getAttribute(openingElement, name);
|
|
524
|
+
if (!attribute) return null;
|
|
525
|
+
const value = attribute.value;
|
|
526
|
+
if (value?.type === "Literal" && typeof value.value === "string") {
|
|
527
|
+
return {
|
|
528
|
+
value: value.value,
|
|
529
|
+
valueSpan: toSpan(value) ?? toSpan(attribute) ?? { start: 0, end: 0 },
|
|
530
|
+
attributeSpan: toSpan(attribute) ?? { start: 0, end: 0 }
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
const expression = unwrapAttributeExpression(value);
|
|
534
|
+
if (expression?.type === "Literal" && typeof expression.value === "string") {
|
|
535
|
+
return {
|
|
536
|
+
value: expression.value,
|
|
537
|
+
valueSpan: toSpan(expression) ?? toSpan(attribute) ?? { start: 0, end: 0 },
|
|
538
|
+
attributeSpan: toSpan(attribute) ?? { start: 0, end: 0 }
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
function getAttribute(openingElement, name) {
|
|
544
|
+
for (const attribute of openingElement?.attributes ?? []) {
|
|
545
|
+
if (attribute?.type === "JSXAttribute" && attribute.name?.type === "JSXIdentifier" && attribute.name.name === name) {
|
|
546
|
+
return attribute;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
function unwrapAttributeExpression(value) {
|
|
552
|
+
if (!value) return null;
|
|
553
|
+
if (value.type === "JSXExpressionContainer") {
|
|
554
|
+
return value.expression;
|
|
555
|
+
}
|
|
556
|
+
return value;
|
|
557
|
+
}
|
|
558
|
+
function readPropertyKeyName(key) {
|
|
559
|
+
if (key?.type === "Identifier") return key.name;
|
|
560
|
+
if (key?.type === "Literal" && typeof key.value === "string") {
|
|
561
|
+
return key.value;
|
|
562
|
+
}
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
function getObjectProperty(objectExpression, name) {
|
|
566
|
+
if (objectExpression?.type !== "ObjectExpression") return null;
|
|
567
|
+
for (const property of objectExpression.properties ?? []) {
|
|
568
|
+
if (property?.type !== "Property" || property.computed) continue;
|
|
569
|
+
const propertyName = readPropertyKeyName(property.key);
|
|
570
|
+
if (propertyName === name) return property;
|
|
571
|
+
}
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
function readNumberLiteral(node) {
|
|
575
|
+
if (node?.type === "Literal" && typeof node.value === "number" && Number.isFinite(node.value)) {
|
|
576
|
+
return { value: node.value, span: toSpan(node) ?? { start: 0, end: 0 } };
|
|
577
|
+
}
|
|
578
|
+
if (node?.type === "UnaryExpression" && node.operator === "-" && node.argument?.type === "Literal" && typeof node.argument.value === "number" && Number.isFinite(node.argument.value)) {
|
|
579
|
+
return {
|
|
580
|
+
value: -node.argument.value,
|
|
581
|
+
span: toSpan(node) ?? { start: 0, end: 0 }
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
return null;
|
|
585
|
+
}
|
|
586
|
+
function resolveSdkComponent(nameNode, imports) {
|
|
587
|
+
if (nameNode?.type === "JSXIdentifier") {
|
|
588
|
+
return imports.sdkLocalNames.get(nameNode.name) ?? null;
|
|
589
|
+
}
|
|
590
|
+
if (nameNode?.type === "JSXMemberExpression" && nameNode.object?.type === "JSXIdentifier" && nameNode.property?.type === "JSXIdentifier" && imports.sdkNamespaces.has(nameNode.object.name) && SDK_COMPONENTS.has(nameNode.property.name)) {
|
|
591
|
+
return nameNode.property.name;
|
|
592
|
+
}
|
|
593
|
+
return null;
|
|
594
|
+
}
|
|
595
|
+
function readImportedName(specifier) {
|
|
596
|
+
if (specifier.type === "ImportDefaultSpecifier") return "default";
|
|
597
|
+
if (specifier.type === "ImportNamespaceSpecifier") return "*";
|
|
598
|
+
return specifier.imported?.name ?? specifier.local?.name ?? "unknown";
|
|
599
|
+
}
|
|
600
|
+
function readImportKind(specifier) {
|
|
601
|
+
if (specifier.type === "ImportDefaultSpecifier") return "default";
|
|
602
|
+
if (specifier.type === "ImportNamespaceSpecifier") return "namespace";
|
|
603
|
+
return "named";
|
|
604
|
+
}
|
|
605
|
+
function isSdkImportSource(source) {
|
|
606
|
+
return SDK_IMPORT_SOURCES.has(source) || source.endsWith("/tempo-sdk/canvas");
|
|
607
|
+
}
|
|
608
|
+
function readLayoutClose(node) {
|
|
609
|
+
return typeof node?.end === "number" && Number.isFinite(node.end) ? node.end - 1 : void 0;
|
|
610
|
+
}
|
|
611
|
+
function fallbackPageName(filePath) {
|
|
612
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
613
|
+
const segments = normalized.split("/").filter(Boolean);
|
|
614
|
+
const fileName = segments.length > 0 ? segments[segments.length - 1] : filePath;
|
|
615
|
+
const parentName = segments.length > 1 ? segments[segments.length - 2] : fileName;
|
|
616
|
+
const baseName = /^index\.canvas\.[tj]sx?$/.test(fileName) ? parentName : fileName.replace(/\.canvas\.[tj]sx?$/, "");
|
|
617
|
+
return baseName.split(/[-_\s]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join(" ");
|
|
618
|
+
}
|
|
619
|
+
function toSpan(node) {
|
|
620
|
+
return typeof node?.start === "number" && Number.isFinite(node.start) && typeof node?.end === "number" && Number.isFinite(node.end) ? { start: node.start, end: node.end } : void 0;
|
|
621
|
+
}
|
|
622
|
+
function walk(node, visitor, seen = /* @__PURE__ */ new Set()) {
|
|
623
|
+
if (!node || typeof node !== "object" || seen.has(node)) return;
|
|
624
|
+
seen.add(node);
|
|
625
|
+
visitor(node);
|
|
626
|
+
for (const value of Object.values(node)) {
|
|
627
|
+
if (!value) continue;
|
|
628
|
+
if (Array.isArray(value)) {
|
|
629
|
+
for (const item of value) {
|
|
630
|
+
walk(item, visitor, seen);
|
|
631
|
+
}
|
|
632
|
+
} else if (typeof value === "object") {
|
|
633
|
+
walk(value, visitor, seen);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
export {
|
|
639
|
+
parseCanvasFile,
|
|
640
|
+
unwrapTsExpression,
|
|
641
|
+
fallbackPageName
|
|
642
|
+
};
|
|
643
|
+
//# sourceMappingURL=chunk-6Q4I3RFN.js.map
|