@ogygia/compiler 0.0.0 → 0.14.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.
Files changed (160) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -1
  3. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  4. package/dist/app-spec.d.ts +18 -0
  5. package/dist/app-spec.js +22 -0
  6. package/dist/blank-styles.d.ts +1 -0
  7. package/dist/blank-styles.js +24 -0
  8. package/dist/bom.d.ts +2 -0
  9. package/dist/bom.js +7 -0
  10. package/dist/build-cache.d.ts +13 -0
  11. package/dist/build-cache.js +65 -0
  12. package/dist/ctx.d.ts +238 -0
  13. package/dist/ctx.js +221 -0
  14. package/dist/dev/css-scope.d.ts +16 -0
  15. package/dist/dev/css-scope.js +32 -0
  16. package/dist/dev/dev-hmr.d.ts +24 -0
  17. package/dist/dev/dev-hmr.js +51 -0
  18. package/dist/dev/hmr.d.ts +34 -0
  19. package/dist/dev/hmr.js +52 -0
  20. package/dist/driver.d.ts +351 -0
  21. package/dist/driver.js +1402 -0
  22. package/dist/exports/index.d.ts +77 -0
  23. package/dist/exports/index.js +349 -0
  24. package/dist/exports/parse.d.ts +65 -0
  25. package/dist/exports/parse.js +251 -0
  26. package/dist/fouc-css.d.ts +112 -0
  27. package/dist/fouc-css.js +314 -0
  28. package/dist/free-vars.d.ts +32 -0
  29. package/dist/free-vars.js +454 -0
  30. package/dist/fs.d.ts +31 -0
  31. package/dist/fs.js +82 -0
  32. package/dist/ids.d.ts +49 -0
  33. package/dist/ids.js +52 -0
  34. package/dist/index.d.ts +23 -0
  35. package/dist/index.js +23 -0
  36. package/dist/kit.d.ts +204 -0
  37. package/dist/kit.js +554 -0
  38. package/dist/leg.d.ts +34 -0
  39. package/dist/leg.js +26 -0
  40. package/dist/link/analyses.d.ts +26 -0
  41. package/dist/link/analyses.js +237 -0
  42. package/dist/link/build-output.d.ts +47 -0
  43. package/dist/link/build-output.js +168 -0
  44. package/dist/link/caps.d.ts +32 -0
  45. package/dist/link/caps.js +39 -0
  46. package/dist/link/context-detect.d.ts +28 -0
  47. package/dist/link/context-detect.js +143 -0
  48. package/dist/link/emit-gate.d.ts +43 -0
  49. package/dist/link/emit-gate.js +55 -0
  50. package/dist/link/file-token.d.ts +11 -0
  51. package/dist/link/file-token.js +15 -0
  52. package/dist/link/island-deps.d.ts +183 -0
  53. package/dist/link/island-deps.js +802 -0
  54. package/dist/link/location-map.d.ts +24 -0
  55. package/dist/link/location-map.js +51 -0
  56. package/dist/link/own-reach.d.ts +15 -0
  57. package/dist/link/own-reach.js +188 -0
  58. package/dist/link/registry-stub.d.ts +50 -0
  59. package/dist/link/registry-stub.js +163 -0
  60. package/dist/link/relocations.d.ts +79 -0
  61. package/dist/link/relocations.js +204 -0
  62. package/dist/link/router-css.d.ts +38 -0
  63. package/dist/link/router-css.js +124 -0
  64. package/dist/link/runtime-entry.d.ts +103 -0
  65. package/dist/link/runtime-entry.js +183 -0
  66. package/dist/link/server-manifest.d.ts +25 -0
  67. package/dist/link/server-manifest.js +102 -0
  68. package/dist/link/transport.d.ts +42 -0
  69. package/dist/link/transport.js +91 -0
  70. package/dist/macros/dollar.d.ts +24 -0
  71. package/dist/macros/dollar.js +397 -0
  72. package/dist/macros/pipeline.d.ts +50 -0
  73. package/dist/macros/pipeline.js +133 -0
  74. package/dist/macros/source.d.ts +9 -0
  75. package/dist/macros/source.js +115 -0
  76. package/dist/macros/store.d.ts +22 -0
  77. package/dist/macros/store.js +217 -0
  78. package/dist/macros/wire.d.ts +17 -0
  79. package/dist/macros/wire.js +116 -0
  80. package/dist/map-chain.d.ts +18 -0
  81. package/dist/map-chain.js +42 -0
  82. package/dist/messages.d.ts +673 -0
  83. package/dist/messages.js +938 -0
  84. package/dist/ownership-stamps.d.ts +11 -0
  85. package/dist/ownership-stamps.js +112 -0
  86. package/dist/parse/oxc.d.ts +36 -0
  87. package/dist/parse/oxc.js +35 -0
  88. package/dist/parse/roots.d.ts +4 -0
  89. package/dist/parse/roots.js +27 -0
  90. package/dist/parse/scan.d.ts +65 -0
  91. package/dist/parse/scan.js +297 -0
  92. package/dist/parse/svelte-ast.d.ts +7 -0
  93. package/dist/parse/svelte-ast.js +99 -0
  94. package/dist/program.d.ts +140 -0
  95. package/dist/program.js +304 -0
  96. package/dist/queries.d.ts +105 -0
  97. package/dist/queries.js +326 -0
  98. package/dist/region/emit.d.ts +168 -0
  99. package/dist/region/emit.js +292 -0
  100. package/dist/region/identity.d.ts +32 -0
  101. package/dist/region/identity.js +46 -0
  102. package/dist/region/ir.d.ts +56 -0
  103. package/dist/region/ir.js +0 -0
  104. package/dist/region/marked-imports.d.ts +27 -0
  105. package/dist/region/marked-imports.js +265 -0
  106. package/dist/region/transform.d.ts +189 -0
  107. package/dist/region/transform.js +1565 -0
  108. package/dist/scan-facts.d.ts +36 -0
  109. package/dist/scan-facts.js +54 -0
  110. package/dist/script-request.d.ts +1 -0
  111. package/dist/script-request.js +33 -0
  112. package/dist/splice.d.ts +11 -0
  113. package/dist/splice.js +33 -0
  114. package/dist/transportables.d.ts +23 -0
  115. package/dist/transportables.js +144 -0
  116. package/dist/virtual/core.d.ts +158 -0
  117. package/dist/virtual/core.js +588 -0
  118. package/dist/virtual/resolver.d.ts +62 -0
  119. package/dist/virtual/resolver.js +265 -0
  120. package/dist/virtual/specifier.d.ts +10 -0
  121. package/dist/virtual/specifier.js +6 -0
  122. package/dist/virtual/table.d.ts +120 -0
  123. package/dist/virtual/table.js +54 -0
  124. package/dist/virtual/types.d.ts +17 -0
  125. package/dist/virtual/types.js +100 -0
  126. package/dist/virtual/value-names.d.ts +2 -0
  127. package/dist/virtual/value-names.js +18 -0
  128. package/dist/vite/definers.d.ts +16 -0
  129. package/dist/vite/definers.js +69 -0
  130. package/dist/vite/dev-maps.d.ts +34 -0
  131. package/dist/vite/dev-maps.js +91 -0
  132. package/dist/vite/index.d.ts +21 -0
  133. package/dist/vite/index.js +890 -0
  134. package/dist/vite/kit-dirs.d.ts +6 -0
  135. package/dist/vite/kit-dirs.js +53 -0
  136. package/dist/vite/late-island.d.ts +8 -0
  137. package/dist/vite/late-island.js +18 -0
  138. package/dist/vite/lazy-files.d.ts +2 -0
  139. package/dist/vite/lazy-files.js +72 -0
  140. package/dist/vite/options.d.ts +195 -0
  141. package/dist/vite/options.js +72 -0
  142. package/dist/vite/package-files.d.ts +3 -0
  143. package/dist/vite/package-files.js +88 -0
  144. package/dist/vite/paths.d.ts +16 -0
  145. package/dist/vite/paths.js +39 -0
  146. package/dist/vite/plugin-api.d.ts +316 -0
  147. package/dist/vite/plugin-api.js +328 -0
  148. package/dist/vite/route-watch.d.ts +15 -0
  149. package/dist/vite/route-watch.js +32 -0
  150. package/dist/vite/session.d.ts +28 -0
  151. package/dist/vite/session.js +43 -0
  152. package/dist/vite/sourcemaps.d.ts +19 -0
  153. package/dist/vite/sourcemaps.js +75 -0
  154. package/dist/vite/style-preprocess.d.ts +2 -0
  155. package/dist/vite/style-preprocess.js +84 -0
  156. package/dist/vite/svelte-entries.d.ts +4 -0
  157. package/dist/vite/svelte-entries.js +10 -0
  158. package/dist/vite/svelte-options.d.ts +20 -0
  159. package/dist/vite/svelte-options.js +22 -0
  160. package/package.json +87 -12
@@ -0,0 +1,50 @@
1
+ import { StepMap } from "../map-chain.js";
2
+ import { Profiler } from "../driver.js";
3
+ import { AppAlias } from "../app-spec.js";
4
+ /** A resolved Vite alias (`resolve.alias`, normalized to its array form). */
5
+ export type AliasEntry = AppAlias;
6
+ /** What a macro knows about the module it rewrites. */
7
+ export interface MacroContext {
8
+ /** The module id, query stripped. */
9
+ readonly id: string;
10
+ /** The id relative to the app root, POSIX (a stable identity for stamps). */
11
+ readonly rel: string;
12
+ readonly root: string;
13
+ /** The app's resolved aliases (`$lib`, …), for a macro that resolves or bundles imports. */
14
+ readonly alias: readonly AliasEntry[];
15
+ /** Markup extensions whose `<script>` blocks hold the macro calls (`.svelte`). */
16
+ readonly markupExts: readonly string[];
17
+ /** Name a file the rewrite read: an edit to it transforms this module again (dev). */
18
+ watch: (file: string) => void;
19
+ }
20
+ /** A plugin's `import.meta.og.<name>` macro (registered with `b.macro`). */
21
+ export interface PluginMacro {
22
+ name: string;
23
+ handler: (code: string, m: MacroContext) => string | Promise<string>;
24
+ }
25
+ export interface MacroPipelineCtx {
26
+ root: string;
27
+ /** The app's resolved aliases, handed to every plugin macro (`bake` bundles with them). */
28
+ alias: readonly AliasEntry[];
29
+ /** ogygia's own package root — auto-brand skips ogygia source. */
30
+ pkgRoot: string;
31
+ /** Filled with every `import.meta.og.$` hoist (tag → factory source) for the fn-manifest emit. */
32
+ dollarHoists: Map<string, string>;
33
+ /** The plugins' macros, in plugin order (after core's; a `last` one after the others). */
34
+ macros?: readonly PluginMacro[];
35
+ /** Is `id` the app's own source, or a file a package declares in `ogygia.files`? Only those may
36
+ * use a macro: anywhere else one fails the build (`macro_outside_app`), and no handler runs. */
37
+ ownSource: (id: string) => boolean;
38
+ /** The bundler's watch for the module being rewritten (Vite's `addWatchFile`); none outside one. */
39
+ watch?: (file: string) => void;
40
+ }
41
+ /**
42
+ * Run the ordered macro passes over one module. `source` is the CURRENT text (a content-preset tag
43
+ * or an earlier pass may already have edited it upstream) — never the raw file — because each pass
44
+ * feeds the next. Returns the rewritten code and whether anything changed.
45
+ */
46
+ export declare function run_module_macros(source: string, id: string, ctx: MacroPipelineCtx, profiler: Profiler): Promise<{
47
+ code: string;
48
+ touched: boolean;
49
+ maps: StepMap[];
50
+ }>;
@@ -0,0 +1,133 @@
1
+ import { wire_edits } from "./wire.js";
2
+ import { dollar_edits } from "./dollar.js";
3
+ import { source_edits } from "./source.js";
4
+ import { auto_brand_edits, store_edits } from "./store.js";
5
+ import { splice_map, splice_text } from "../splice.js";
6
+ import { OgygiaError } from "../messages.js";
7
+ import { find_og_calls } from "../parse/scan.js";
8
+ import path from "node:path";
9
+ import { noop } from "@ogygia/machine";
10
+ // src/macros/pipeline.ts
11
+ const EXPORT_CONST_RE = /export\s+const/;
12
+ const MACRO_PACKAGES = {
13
+ code: {
14
+ package: "@ogygia/content",
15
+ plugin: "content()"
16
+ },
17
+ md: {
18
+ package: "@ogygia/content",
19
+ plugin: "content()"
20
+ },
21
+ loader: {
22
+ package: "@ogygia/content",
23
+ plugin: "content()"
24
+ },
25
+ regions: {
26
+ package: "@ogygia/content",
27
+ plugin: "content()"
28
+ },
29
+ bake: {
30
+ package: "@ogygia/bake",
31
+ plugin: "bake()"
32
+ }
33
+ };
34
+ function calls_macro(code, name) {
35
+ return find_og_calls(code, "import.meta.og.").some((c) => c.method === name || c.method.startsWith(`${name}.`));
36
+ }
37
+ const CONSTRUCT_MARKUP_EXTS = [".svelte"];
38
+ /**
39
+ * Run the ordered macro passes over one module. `source` is the CURRENT text (a content-preset tag
40
+ * or an earlier pass may already have edited it upstream) — never the raw file — because each pass
41
+ * feeds the next. Returns the rewritten code and whether anything changed.
42
+ */
43
+ async function run_module_macros(source, id, ctx, profiler) {
44
+ let out = source;
45
+ let touched = false;
46
+ const maps = [];
47
+ const apply = (edits) => {
48
+ const next = splice_text(out, edits);
49
+ if (next === out) return false;
50
+ const before = out;
51
+ maps.push(() => splice_map(before, edits, id));
52
+ out = next;
53
+ touched = true;
54
+ return true;
55
+ };
56
+ const own = ctx.ownSource(id.split("?")[0]);
57
+ const refuse = (macro) => {
58
+ throw new OgygiaError("macro_outside_app", {
59
+ file: id.split("?")[0],
60
+ macro
61
+ });
62
+ };
63
+ if (out.includes("import.meta.og.wire")) {
64
+ if (apply(wire_edits(out, id, CONSTRUCT_MARKUP_EXTS)) && !own) refuse("wire");
65
+ }
66
+ if (out.includes("import.meta.og.$")) {
67
+ const rel_dollar = path.relative(ctx.root, id.split("?")[0]).split(path.sep).join("/");
68
+ const res = dollar_edits(out, id, rel_dollar, CONSTRUCT_MARKUP_EXTS);
69
+ if (apply(res.edits)) {
70
+ if (!own) refuse("$");
71
+ for (const h of res.hoists) ctx.dollarHoists.set(h.tag, h.factory_src);
72
+ }
73
+ }
74
+ if (out.includes("import.meta.og.source")) {
75
+ const rel_source = path.relative(ctx.root, id.split("?")[0]).split(path.sep).join("/");
76
+ if (apply(source_edits(out, id, rel_source, CONSTRUCT_MARKUP_EXTS)) && !own) refuse("source");
77
+ }
78
+ if (out.includes("import.meta.og.store")) {
79
+ const rel_store = path.relative(ctx.root, id.split("?")[0]).split(path.sep).join("/");
80
+ if (apply(store_edits(out, id, rel_store, CONSTRUCT_MARKUP_EXTS)) && !own) refuse("store");
81
+ }
82
+ if (!id.includes("node_modules") && !id.startsWith(ctx.pkgRoot) && EXPORT_CONST_RE.test(out)) {
83
+ const rel_auto = path.relative(ctx.root, id.split("?")[0]).split(path.sep).join("/");
84
+ apply(auto_brand_edits(out, id, rel_auto, CONSTRUCT_MARKUP_EXTS));
85
+ }
86
+ if (ctx.macros?.length) {
87
+ let rel;
88
+ for (const macro of ctx.macros) {
89
+ if (!out.includes(`import.meta.og.${macro.name}`)) continue;
90
+ if (!own) {
91
+ if (calls_macro(out, macro.name)) refuse(macro.name);
92
+ continue;
93
+ }
94
+ rel ??= path.relative(ctx.root, id.split("?")[0]).split(path.sep).join("/");
95
+ const started = profiler.P ? performance.now() : 0;
96
+ const rewritten = await macro.handler(out, {
97
+ id: id.split("?")[0],
98
+ rel,
99
+ root: ctx.root,
100
+ alias: ctx.alias,
101
+ markupExts: CONSTRUCT_MARKUP_EXTS,
102
+ watch: ctx.watch ?? noop
103
+ });
104
+ if (profiler.P) {
105
+ profiler.prof.macroMs += performance.now() - started;
106
+ profiler.prof.macroN++;
107
+ }
108
+ if (rewritten !== out) {
109
+ out = rewritten;
110
+ touched = true;
111
+ maps.push(null);
112
+ }
113
+ }
114
+ }
115
+ if (out.includes("import.meta.og.")) for (const call of find_og_calls(out, "import.meta.og.")) {
116
+ const name = call.method.split(".")[0];
117
+ const pkg = MACRO_PACKAGES[name];
118
+ if (pkg === void 0 || ctx.macros?.some((m) => m.name === name)) continue;
119
+ if (!own) refuse(name);
120
+ throw new OgygiaError("macro_needs_plugin", {
121
+ file: id.split("?")[0],
122
+ macro: name,
123
+ package: pkg.package,
124
+ plugin: pkg.plugin
125
+ });
126
+ }
127
+ return {
128
+ code: out,
129
+ touched,
130
+ maps
131
+ };
132
+ }
133
+ export { run_module_macros };
@@ -0,0 +1,9 @@
1
+ import { Edit } from "../splice.js";
2
+ /**
3
+ * Rewrite every source mark in `src` (strict position rules), injecting the runtime-wrapper
4
+ * import once. `rel` is the module's root-relative posix path (the id rail). Returns the input
5
+ * unchanged (same reference) when there is nothing to do.
6
+ */
7
+ export declare function rewrite_source(src: string, id: string, rel: string, markup_exts: readonly string[]): string;
8
+ /** The edits `rewrite_source` makes to `src`: none when there is nothing to do. */
9
+ export declare function source_edits(src: string, id: string, rel: string, markup_exts: readonly string[]): Edit[];
@@ -0,0 +1,115 @@
1
+ import { find_og_calls, og_js_regions } from "../parse/scan.js";
2
+ import { parse_module } from "../parse/oxc.js";
3
+ import { og_member } from "./wire.js";
4
+ import { OgygiaError } from "../messages.js";
5
+ import { splice_text } from "../splice.js";
6
+ // src/macros/source.ts
7
+ const EXPORT_CONST_TAIL_RE = /\bexport\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*$/;
8
+ const MARKER = "import.meta.og.source";
9
+ const IMPORT = `import { __og_source } from 'ogygia/internal';`;
10
+ function line_of(src, offset) {
11
+ let line = 1;
12
+ for (let i = 0; i < offset && i < src.length; i++) if (src[i] === "\n") line++;
13
+ return line;
14
+ }
15
+ function misuse(id, src, offset, what) {
16
+ throw new OgygiaError("macro_source_misuse", {
17
+ file: id,
18
+ line: line_of(src, offset),
19
+ problem: what
20
+ });
21
+ }
22
+ function walk(node, visit) {
23
+ visit(node);
24
+ for (const key in node) {
25
+ if (key === "type" || key === "start" || key === "end") continue;
26
+ const child = node[key];
27
+ if (Array.isArray(child)) {
28
+ for (const c of child) if (c && typeof c === "object" && typeof c.type === "string") walk(c, visit);
29
+ } else if (child && typeof child === "object" && typeof child.type === "string") walk(child, visit);
30
+ }
31
+ }
32
+ function ast_edits_or_null_unparsed(src, region, id, rel) {
33
+ const { program, ok } = parse_module(region.code, id);
34
+ if (!ok || !program) return null;
35
+ const edits = [];
36
+ const claimed = /* @__PURE__ */ new Set();
37
+ for (const stmt of program.body ?? []) {
38
+ if (stmt.type !== "ExportNamedDeclaration") continue;
39
+ const decl = stmt.declaration;
40
+ if (!decl || decl.type !== "VariableDeclaration") continue;
41
+ for (const d of decl.declarations ?? []) {
42
+ const init = d.init;
43
+ if (init?.type !== "CallExpression" || og_member(init.callee) !== "source") continue;
44
+ const abs = region.offset + init.start;
45
+ if (decl.kind !== "const") misuse(id, src, abs, `a source must be \`export const\` (got \`${decl.kind}\`)`);
46
+ const name = d.id?.type === "Identifier" ? d.id.name : null;
47
+ if (!name) misuse(id, src, abs, `a source export must be a plain identifier (no destructuring)`);
48
+ const args = init.arguments ?? [];
49
+ if (args.length < 1 || args.length > 2) misuse(id, src, abs, `source() takes the function (+ optional { key }) — got ${args.length} args`);
50
+ claimed.add(init);
51
+ claimed.add(init.callee);
52
+ const callee = init.callee;
53
+ edits.push({
54
+ start: region.offset + callee.start,
55
+ end: region.offset + callee.end,
56
+ text: `__og_source`
57
+ });
58
+ edits.push({
59
+ start: region.offset + args[0].start,
60
+ end: region.offset + args[0].start,
61
+ text: `${JSON.stringify(`${rel}#${name}`)}, `
62
+ });
63
+ }
64
+ }
65
+ walk(program, (n) => {
66
+ if (n.type === "CallExpression" && og_member(n.callee) === "source" && !claimed.has(n)) misuse(id, src, region.offset + n.start, `source() outside an \`export const\` initializer`);
67
+ if (og_member(n) === "source" && !claimed.has(n)) misuse(id, src, region.offset + n.start, `bare import.meta.og.source used as a value`);
68
+ });
69
+ edits.sort((a, b) => a.start - b.start);
70
+ return edits;
71
+ }
72
+ function unparsed_scan_edits(src, region, rel) {
73
+ const edits = [];
74
+ const code = region.code;
75
+ for (const call of find_og_calls(code, "import.meta.og.")) {
76
+ if (call.method !== "source") continue;
77
+ const before = code.slice(0, call.start);
78
+ const m = EXPORT_CONST_TAIL_RE.exec(before);
79
+ if (!m) continue;
80
+ const inner = call.args.trim();
81
+ if (!inner) continue;
82
+ edits.push({
83
+ start: region.offset + call.start,
84
+ end: region.offset + call.end,
85
+ text: `__og_source(${JSON.stringify(`${rel}#${m[1]}`)}, ${inner})`
86
+ });
87
+ }
88
+ return edits;
89
+ }
90
+ /**
91
+ * Rewrite every source mark in `src` (strict position rules), injecting the runtime-wrapper
92
+ * import once. `rel` is the module's root-relative posix path (the id rail). Returns the input
93
+ * unchanged (same reference) when there is nothing to do.
94
+ */
95
+ function rewrite_source(src, id, rel, markup_exts) {
96
+ return splice_text(src, source_edits(src, id, rel, markup_exts));
97
+ }
98
+ /** The edits `rewrite_source` makes to `src`: none when there is nothing to do. */
99
+ function source_edits(src, id, rel, markup_exts) {
100
+ if (!src.includes(MARKER)) return [];
101
+ const regions = og_js_regions(src, id, markup_exts);
102
+ if (!regions) return [];
103
+ const edits = [];
104
+ for (const region of regions) {
105
+ const own = ast_edits_or_null_unparsed(src, region, id, rel) ?? unparsed_scan_edits(src, region, rel);
106
+ if (own.length && !edits.length) edits.push({
107
+ start: region.offset,
108
+ end: region.offset,
109
+ text: IMPORT
110
+ });
111
+ edits.push(...own);
112
+ }
113
+ return edits;
114
+ }
115
+ export { rewrite_source, source_edits };
@@ -0,0 +1,22 @@
1
+ import { Edit } from "../splice.js";
2
+ /** Rewrite every `import.meta.og.store` mark. Same-reference return when nothing to do. */
3
+ export declare function rewrite_store(src: string, id: string, rel_id: string, markup_exts: readonly string[]): string;
4
+ /** The edits `rewrite_store` makes to `src`: none when there is nothing to do. */
5
+ export declare function store_edits(src: string, id: string, rel_id: string, markup_exts: readonly string[]): Edit[];
6
+ /**
7
+ * AUTO-DETECT tier: brand exported factories whose body PROVABLY returns a store, with zero
8
+ * authoring. v1 scope (deliberately narrow — no guessing, under-branding is always safe since
9
+ * the generic writable tier still carries the value):
10
+ *
11
+ * export const createX = (seed) => ({ subscribe, … }) // object literal w/ subscribe
12
+ * export const createY = (seed) => writable(seed) // direct writable()/readable()
13
+ * export const createZ = (seed) => { … return { subscribe } } // every return provable
14
+ *
15
+ * Skipped (→ generic tier / the og.store assert): function declarations, re-exports, factories
16
+ * returning call results (`return make(x)`), mixed/ambiguous returns, non-exported consts.
17
+ * Requires the module to import from 'svelte/store' when the proof is a writable()/readable()
18
+ * call (a local function named `writable` must not count).
19
+ */
20
+ export declare function auto_brand_stores(src: string, id: string, rel_id: string, markup_exts: readonly string[]): string;
21
+ /** The edits `auto_brand_stores` makes to `src`: none when there is nothing to do. */
22
+ export declare function auto_brand_edits(src: string, id: string, rel_id: string, markup_exts: readonly string[]): Edit[];
@@ -0,0 +1,217 @@
1
+ import { og_js_regions } from "../parse/scan.js";
2
+ import { parse_module } from "../parse/oxc.js";
3
+ import { og_member } from "./wire.js";
4
+ import { OgygiaError, warn } from "../messages.js";
5
+ import { splice_text } from "../splice.js";
6
+ // src/macros/store.ts
7
+ const MARKER = "import.meta.og.store";
8
+ function walk(node, visit) {
9
+ visit(node);
10
+ for (const key in node) {
11
+ if (key === "type" || key === "start" || key === "end") continue;
12
+ const child = node[key];
13
+ if (Array.isArray(child)) {
14
+ for (const c of child) if (c && typeof c === "object" && typeof c.type === "string") walk(c, visit);
15
+ } else if (child && typeof child === "object" && typeof child.type === "string") walk(child, visit);
16
+ }
17
+ }
18
+ function line_of(src, offset) {
19
+ let line = 1;
20
+ for (let i = 0; i < offset && i < src.length; i++) if (src[i] === "\n") line++;
21
+ return line;
22
+ }
23
+ function misuse(id, src, offset, what) {
24
+ throw new OgygiaError("macro_store_misuse", {
25
+ file: id,
26
+ line: line_of(src, offset),
27
+ problem: what
28
+ });
29
+ }
30
+ function unparen(v) {
31
+ while (v && v.type === "ParenthesizedExpression") v = v.expression;
32
+ return v;
33
+ }
34
+ function literal_non_store(v) {
35
+ v = unparen(v);
36
+ if (!v) return true;
37
+ if (v.type === "Identifier") return v.name === "undefined";
38
+ if (v.type === "UnaryExpression") return v.operator === "void";
39
+ return v.type === "NullLiteral" || v.type === "BooleanLiteral" || v.type === "NumericLiteral" || v.type === "StringLiteral" || v.type === "BigIntLiteral" || v.type === "TemplateLiteral";
40
+ }
41
+ function first_non_store_return(fn) {
42
+ const body = fn.body;
43
+ if (body.type !== "BlockStatement") return literal_non_store(body) ? body.start : void 0;
44
+ let found;
45
+ (function collect(n) {
46
+ for (const key in n) {
47
+ if (found !== void 0) return;
48
+ if (key === "type" || key === "start" || key === "end") continue;
49
+ const child = n[key];
50
+ let kids = child;
51
+ if (!Array.isArray(child)) kids = child && typeof child === "object" && typeof child.type === "string" ? [child] : [];
52
+ for (const c of kids) {
53
+ if (c.type === "ArrowFunctionExpression" || c.type === "FunctionExpression" || c.type === "FunctionDeclaration") continue;
54
+ if (c.type === "ReturnStatement" && literal_non_store(c.argument)) {
55
+ found = c.start;
56
+ return;
57
+ }
58
+ collect(c);
59
+ }
60
+ }
61
+ })(body);
62
+ return found;
63
+ }
64
+ /** Rewrite every `import.meta.og.store` mark. Same-reference return when nothing to do. */
65
+ function rewrite_store(src, id, rel_id, markup_exts) {
66
+ return splice_text(src, store_edits(src, id, rel_id, markup_exts));
67
+ }
68
+ /** The edits `rewrite_store` makes to `src`: none when there is nothing to do. */
69
+ function store_edits(src, id, rel_id, markup_exts) {
70
+ if (!src.includes(MARKER)) return [];
71
+ const regions = og_js_regions(src, id, markup_exts);
72
+ if (!regions) return [];
73
+ const edits = [];
74
+ let seq = 0;
75
+ let needs_import = false;
76
+ for (const region of regions) {
77
+ const { program, ok } = parse_module(region.code, id);
78
+ if (!ok || !program) continue;
79
+ const claimed = /* @__PURE__ */ new Set();
80
+ walk(program, (n) => {
81
+ if (n.type === "CallExpression" && og_member(n.callee) === "store") {
82
+ const abs = region.offset + n.start;
83
+ const args = n.arguments;
84
+ if (!args || args.length !== 1) misuse(id, src, abs, `store() takes exactly one argument — the factory`);
85
+ const fn = args[0];
86
+ if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression" && fn.type !== "Identifier") misuse(id, src, abs, `store()'s argument must be a function expression or an identifier (got ${fn.type})`);
87
+ claimed.add(n);
88
+ claimed.add(n.callee);
89
+ if (fn.type !== "Identifier") {
90
+ const bad = first_non_store_return(fn);
91
+ if (bad !== void 0 && typeof console !== "undefined") warn("macro_store_returns_non_store", {
92
+ file: id,
93
+ line: line_of(src, region.offset + bad)
94
+ });
95
+ }
96
+ const fn_text = region.code.slice(fn.start, fn.end);
97
+ const tag = `${rel_id}#store${seq++}`;
98
+ needs_import = true;
99
+ edits.push({
100
+ start: abs,
101
+ end: region.offset + n.end,
102
+ text: `__og_store(${JSON.stringify(tag)}, ${fn_text})`
103
+ });
104
+ return;
105
+ }
106
+ if (og_member(n) === "store" && !claimed.has(n)) misuse(id, src, region.offset + n.start, `bare import.meta.og.store used as a value`);
107
+ });
108
+ if (needs_import && !edits.some((e) => e.text.startsWith("import"))) {
109
+ edits.push({
110
+ start: region.offset,
111
+ end: region.offset,
112
+ text: `import { __og_store } from 'ogygia/internal';`
113
+ });
114
+ needs_import = false;
115
+ }
116
+ }
117
+ return edits;
118
+ }
119
+ /**
120
+ * AUTO-DETECT tier: brand exported factories whose body PROVABLY returns a store, with zero
121
+ * authoring. v1 scope (deliberately narrow — no guessing, under-branding is always safe since
122
+ * the generic writable tier still carries the value):
123
+ *
124
+ * export const createX = (seed) => ({ subscribe, … }) // object literal w/ subscribe
125
+ * export const createY = (seed) => writable(seed) // direct writable()/readable()
126
+ * export const createZ = (seed) => { … return { subscribe } } // every return provable
127
+ *
128
+ * Skipped (→ generic tier / the og.store assert): function declarations, re-exports, factories
129
+ * returning call results (`return make(x)`), mixed/ambiguous returns, non-exported consts.
130
+ * Requires the module to import from 'svelte/store' when the proof is a writable()/readable()
131
+ * call (a local function named `writable` must not count).
132
+ */
133
+ function auto_brand_stores(src, id, rel_id, markup_exts) {
134
+ return splice_text(src, auto_brand_edits(src, id, rel_id, markup_exts));
135
+ }
136
+ /** The edits `auto_brand_stores` makes to `src`: none when there is nothing to do. */
137
+ function auto_brand_edits(src, id, rel_id, markup_exts) {
138
+ if (!src.includes("subscribe") && !src.includes("writable") && !src.includes("readable")) return [];
139
+ const regions = og_js_regions(src, id, markup_exts);
140
+ if (!regions) return [];
141
+ const edits = [];
142
+ let seq = 0;
143
+ let needs_import = false;
144
+ const has_subscribe_prop = (obj) => (obj.properties ?? []).some((p) => {
145
+ if (p.type !== "Property" && p.type !== "ObjectProperty") return false;
146
+ const k = p.key;
147
+ return !p.computed && k?.type === "Identifier" && k.name === "subscribe" || k?.type === "StringLiteral" && k.value === "subscribe" || p.shorthand === true && k?.type === "Identifier" && k.name === "subscribe";
148
+ });
149
+ for (const region of regions) {
150
+ const { program, ok } = parse_module(region.code, id);
151
+ if (!ok || !program) continue;
152
+ const store_fns = /* @__PURE__ */ new Set();
153
+ for (const node of program.body ?? []) {
154
+ if (node.type !== "ImportDeclaration" || node.source?.value !== "svelte/store") continue;
155
+ for (const sp of node.specifiers ?? []) if (sp.type === "ImportSpecifier" && (sp.imported?.name === "writable" || sp.imported?.name === "readable")) store_fns.add(sp.local?.name ?? sp.imported.name);
156
+ }
157
+ const provable_value = (v) => {
158
+ v = unparen(v);
159
+ if (!v) return false;
160
+ if (v.type === "ObjectExpression") return has_subscribe_prop(v);
161
+ if (v.type === "CallExpression" && v.callee?.type === "Identifier") return store_fns.has(v.callee.name);
162
+ return false;
163
+ };
164
+ const provable_factory = (fn) => {
165
+ if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return false;
166
+ const body = fn.body;
167
+ if (body.type !== "BlockStatement") return provable_value(body);
168
+ const returns = [];
169
+ (function collect(n) {
170
+ for (const key in n) {
171
+ if (key === "type" || key === "start" || key === "end") continue;
172
+ const child = n[key];
173
+ let kids = child;
174
+ if (!Array.isArray(child)) kids = child && typeof child === "object" && typeof child.type === "string" ? [child] : [];
175
+ for (const c of kids) {
176
+ if (c.type === "ArrowFunctionExpression" || c.type === "FunctionExpression" || c.type === "FunctionDeclaration") continue;
177
+ if (c.type === "ReturnStatement") returns.push(c);
178
+ collect(c);
179
+ }
180
+ }
181
+ })(body);
182
+ return returns.length > 0 && returns.every((r) => provable_value(r.argument));
183
+ };
184
+ for (const node of program.body ?? []) {
185
+ if (node.type !== "ExportNamedDeclaration") continue;
186
+ const decl = node.declaration;
187
+ if (decl?.type !== "VariableDeclaration") continue;
188
+ for (const d of decl.declarations ?? []) {
189
+ const init = d.init;
190
+ if (!init || !provable_factory(init)) continue;
191
+ const tag = `${rel_id}#auto:${d.id?.type === "Identifier" ? d.id.name : `auto`}${seq ? seq : ""}`;
192
+ seq++;
193
+ needs_import = true;
194
+ edits.push({
195
+ start: region.offset + init.start,
196
+ end: region.offset + init.start,
197
+ text: `__og_store(${JSON.stringify(tag)}, `
198
+ });
199
+ edits.push({
200
+ start: region.offset + init.end,
201
+ end: region.offset + init.end,
202
+ text: `)`
203
+ });
204
+ }
205
+ }
206
+ if (needs_import && !edits.some((e) => e.text.startsWith("import"))) {
207
+ edits.push({
208
+ start: region.offset,
209
+ end: region.offset,
210
+ text: `import { __og_store } from 'ogygia/internal';`
211
+ });
212
+ needs_import = false;
213
+ }
214
+ }
215
+ return edits;
216
+ }
217
+ export { auto_brand_edits, auto_brand_stores, rewrite_store, store_edits };
@@ -0,0 +1,17 @@
1
+ import { AstNode } from "../parse/oxc.js";
2
+ import { Edit } from "../splice.js";
3
+ /** The key expression a wire member rewrites to — the same symbol the runtime registry uses. */
4
+ export declare const WIRE_EXPR = "Symbol.for('ogygia.wire')";
5
+ type Node = AstNode;
6
+ /** If `node` is the member expression `import.meta.og.<name>`, return `<name>`; else null. */
7
+ export declare function og_member(node: Node | undefined): string | null;
8
+ /**
9
+ * Rewrite every wire mark in `src`, enforcing the strict position rules. Extension-aware:
10
+ * whole-file for JS/TS, `<script>` blocks for `.svelte`. Returns the input unchanged (same
11
+ * reference) when there is nothing to do. Throws (build-error voice, file:line) on any misuse.
12
+ * `markup_exts` is the resolved construct-host set (the plugin passes `['.svelte']`).
13
+ */
14
+ export declare function rewrite_wire(src: string, id: string, markup_exts: readonly string[]): string;
15
+ /** The edits `rewrite_wire` makes to `src`: none when there is nothing to do. */
16
+ export declare function wire_edits(src: string, id: string, markup_exts: readonly string[]): Edit[];
17
+ export {};