truecanvas 0.5.0 → 0.6.1

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.
@@ -0,0 +1,54 @@
1
+ // src/core/ast.ts
2
+ import { parse } from "@babel/parser";
3
+ function parseModule(source) {
4
+ return parse(source, {
5
+ sourceType: "module",
6
+ plugins: ["typescript", "jsx"],
7
+ errorRecovery: false
8
+ });
9
+ }
10
+ function idOf(node) {
11
+ const loc = node.loc.start;
12
+ return `${loc.line}:${loc.column}`;
13
+ }
14
+ function jsxName(name) {
15
+ if (name.type === "JSXIdentifier") return name.name;
16
+ if (name.type === "JSXNamespacedName") return `${name.namespace.name}:${name.name.name}`;
17
+ return `${jsxName(name.object)}.${name.property.name}`;
18
+ }
19
+ function walk(node, visit, parent = null) {
20
+ if (!node || typeof node !== "object") return;
21
+ if (Array.isArray(node)) {
22
+ for (const child of node) walk(child, visit, parent);
23
+ return;
24
+ }
25
+ const n = node;
26
+ if (typeof n.type !== "string") return;
27
+ if (visit(n, parent) === false) return;
28
+ for (const key of Object.keys(n)) {
29
+ if (key === "loc" || key === "leadingComments" || key === "trailingComments" || key === "innerComments" || key === "extra") continue;
30
+ const value = n[key];
31
+ if (value && typeof value === "object") walk(value, visit, n);
32
+ }
33
+ }
34
+ function isComponentName(name) {
35
+ return /^[A-Z]/.test(name) || name.includes(".");
36
+ }
37
+ function rewriteImportSource(statement, from, to) {
38
+ if (from === to) return statement;
39
+ for (const q of ['"', "'"]) {
40
+ const needle = `${q}${from}${q}`;
41
+ const i = statement.indexOf(needle);
42
+ if (i >= 0) return `${statement.slice(0, i)}${q}${to}${q}${statement.slice(i + needle.length)}`;
43
+ }
44
+ return statement;
45
+ }
46
+
47
+ export {
48
+ parseModule,
49
+ idOf,
50
+ jsxName,
51
+ walk,
52
+ isComponentName,
53
+ rewriteImportSource
54
+ };