ferrovue 0.3.0 → 0.5.0

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 (106) hide show
  1. package/README.md +225 -42
  2. package/dist/attrs.d.ts +3 -53
  3. package/dist/attrs.js +41 -469
  4. package/dist/calls.d.ts +4 -0
  5. package/dist/calls.js +221 -0
  6. package/dist/children.d.ts +16 -0
  7. package/dist/children.js +286 -0
  8. package/dist/classes.d.ts +13 -0
  9. package/dist/classes.js +176 -0
  10. package/dist/cli.js +31 -22
  11. package/dist/client.d.ts +35 -2
  12. package/dist/client.js +77 -14
  13. package/dist/compiler.d.ts +1 -1
  14. package/dist/compiler.js +31 -92
  15. package/dist/component.d.ts +3 -1
  16. package/dist/component.js +123 -104
  17. package/dist/constants.d.ts +42 -0
  18. package/dist/constants.js +292 -0
  19. package/dist/context.d.ts +29 -67
  20. package/dist/context.js +13 -38
  21. package/dist/diagnostics.d.ts +23 -0
  22. package/dist/diagnostics.js +30 -0
  23. package/dist/diff.d.ts +0 -3
  24. package/dist/diff.js +0 -9
  25. package/dist/emitter.d.ts +0 -8
  26. package/dist/emitter.js +0 -12
  27. package/dist/errors.d.ts +867 -0
  28. package/dist/errors.js +330 -0
  29. package/dist/expr.d.ts +1 -70
  30. package/dist/expr.js +142 -934
  31. package/dist/fallthrough.d.ts +9 -0
  32. package/dist/fallthrough.js +78 -0
  33. package/dist/fixture.d.ts +4 -10
  34. package/dist/fixture.js +38 -27
  35. package/dist/hydration.d.ts +15 -0
  36. package/dist/hydration.js +80 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.js +2 -0
  39. package/dist/islands.d.ts +1 -0
  40. package/dist/islands.js +1 -0
  41. package/dist/link-router.d.ts +24 -0
  42. package/dist/link-router.js +36 -0
  43. package/dist/lists.d.ts +0 -11
  44. package/dist/lists.js +34 -60
  45. package/dist/loops.d.ts +3 -0
  46. package/dist/loops.js +168 -0
  47. package/dist/model.d.ts +46 -144
  48. package/dist/model.js +150 -18
  49. package/dist/narrowing.d.ts +27 -0
  50. package/dist/narrowing.js +157 -0
  51. package/dist/numbers.d.ts +8 -0
  52. package/dist/numbers.js +73 -0
  53. package/dist/parens.d.ts +0 -27
  54. package/dist/parens.js +0 -44
  55. package/dist/plugin.d.ts +84 -0
  56. package/dist/plugin.js +26 -0
  57. package/dist/plugins/client-only.d.ts +8 -0
  58. package/dist/plugins/client-only.js +107 -0
  59. package/dist/plugins/i18n.d.ts +23 -0
  60. package/dist/{i18n.js → plugins/i18n.js} +88 -31
  61. package/dist/plugins/index.d.ts +2 -0
  62. package/dist/plugins/index.js +10 -0
  63. package/dist/plugins/router-link.d.ts +8 -0
  64. package/dist/{router.js → plugins/router-link.js} +52 -91
  65. package/dist/plugins/router.d.ts +42 -0
  66. package/dist/plugins/router.js +256 -0
  67. package/dist/plugins/scoped.d.ts +12 -0
  68. package/dist/plugins/scoped.js +125 -0
  69. package/dist/plugins/shared-types.d.ts +2 -0
  70. package/dist/plugins/shared-types.js +7 -0
  71. package/dist/plugins/stores.d.ts +37 -0
  72. package/dist/plugins/stores.js +379 -0
  73. package/dist/plugins/teleport.d.ts +5 -0
  74. package/dist/plugins/teleport.js +55 -0
  75. package/dist/plugins/twins.d.ts +10 -0
  76. package/dist/plugins/twins.js +164 -0
  77. package/dist/routes.d.ts +10 -0
  78. package/dist/routes.js +6 -0
  79. package/dist/rust.d.ts +1 -7
  80. package/dist/rust.js +56 -174
  81. package/dist/script.d.ts +3 -6
  82. package/dist/script.js +127 -140
  83. package/dist/settle.d.ts +6 -0
  84. package/dist/settle.js +34 -0
  85. package/dist/slots.d.ts +13 -0
  86. package/dist/slots.js +160 -0
  87. package/dist/strings.d.ts +13 -0
  88. package/dist/strings.js +208 -0
  89. package/dist/styles.d.ts +14 -0
  90. package/dist/styles.js +181 -0
  91. package/dist/template.d.ts +1 -42
  92. package/dist/template.js +25 -749
  93. package/dist/testing.d.ts +2 -0
  94. package/dist/testing.js +4 -10
  95. package/dist/types.d.ts +2 -0
  96. package/dist/typescript.d.ts +2 -14
  97. package/dist/typescript.js +53 -59
  98. package/dist/vite.d.ts +2 -1
  99. package/dist/vite.js +11 -32
  100. package/package.json +15 -6
  101. package/dist/i18n.d.ts +0 -27
  102. package/dist/router.d.ts +0 -17
  103. package/dist/scoped.d.ts +0 -24
  104. package/dist/scoped.js +0 -177
  105. package/dist/stores.d.ts +0 -6
  106. package/dist/stores.js +0 -229
package/dist/model.d.ts CHANGED
@@ -1,99 +1,63 @@
1
1
  import type { SourceMapConsumer } from "source-map-js";
2
+ import type { Plugin } from "./plugin.ts";
3
+ import type { Const } from "./constants.ts";
4
+ import type { Code } from "./errors.ts";
2
5
  export type N = any;
3
6
  export type Ty = {
4
7
  k: "str";
5
8
  } | {
6
9
  k: "int";
7
- }
8
- /** `Float` from `ferrovue/types`, or a fractional literal: an `f64`, a JavaScript number. */
9
- | {
10
+ } | {
10
11
  k: "float";
11
12
  } | {
12
13
  k: "bool";
13
14
  } | {
14
15
  k: "undef";
16
+ } | {
17
+ k: "null";
15
18
  } | {
16
19
  k: "opt";
17
20
  of: Ty;
21
+ none?: "null" | "either";
18
22
  } | {
19
23
  k: "list";
20
24
  of: Ty;
21
- }
22
- /** `Record<string, T>`: an object used as a dictionary, `ferrovue::Record` in Rust, its keys in
23
- * JavaScript's order. */
24
- | {
25
+ } | {
25
26
  k: "record";
26
27
  of: Ty;
27
- }
28
- /** `store` marks a Pinia store's state, or a type inside it, declared in a store's own file;
29
- * `home` a type declared elsewhere: `"types"` for a shared `.ts` file, or the component whose
30
- * `.vue` file declares it. */
31
- | {
32
- k: "struct";
33
- name: string;
34
- store?: true;
35
- home?: string;
36
- }
37
- /** `TrustedHtml` from `ferrovue/types`: the configured Rust type, which `v-html` writes raw. */
38
- | {
28
+ } | StructTy | {
39
29
  k: "html";
40
- }
41
- /** Another component's `Props`, imported from its `.vue` file: what `v-bind` hands that child. */
42
- | {
30
+ } | {
43
31
  k: "child";
44
32
  name: string;
33
+ } | PluginTys[keyof PluginTys];
34
+ export interface StructTy {
35
+ k: "struct";
36
+ name: string;
37
+ home?: string;
45
38
  }
46
- /** `useRoute()` or `$route`: the reader's location, as vue-router resolved it. */
47
- | {
48
- k: "route";
49
- }
50
- /** `route.params`. */
51
- | {
52
- k: "params";
39
+ export interface PluginTys {
53
40
  }
54
- /** `route.query`, and one of its values: a string, \`null\`, an array of those, or absent. */
55
- | {
56
- k: "queryobj";
57
- } | {
58
- k: "query";
59
- };
60
41
  export interface Field {
61
42
  js: string;
62
43
  rust: string;
63
44
  ty: Ty;
64
- /** For an optional prop: the Rust value Vue uses when it is absent — its declared default, or
65
- * `false` for a boolean, which Vue casts. Reading the prop reads this in place of `None`. */
66
45
  dflt?: string;
67
46
  }
68
47
  export interface Struct {
69
48
  name: string;
70
49
  fields: Field[];
71
- /** The props a scoped slot's outlet passes: fields that borrow from the render, read as they are. */
72
50
  slot?: true;
73
51
  }
74
- /** A translated expression: Rust source, and the type it evaluates to. `konst` is a boolean known
75
- * at generation time, which is how `Array.isArray` on a value that is never an array folds away. */
76
52
  export interface Val {
77
53
  code: string;
78
54
  ty: Ty;
79
55
  konst?: boolean;
80
- /** For an integer computed from others: the same computation on doubles, before it is rounded
81
- * back to an \`i64\` — which keeps JavaScript's \`-0\`, so a division by it is \`-Infinity\`. */
82
56
  f64?: string;
83
- /** For a list a template computes — `filter`, `map`, `split`, `Object.keys` — an iterator of its
84
- * items, each a `Cow<str>`, a copied number or boolean, or a borrowed object. `code` is then those
85
- * items collected into a `Vec`. */
86
57
  iter?: string;
87
- /** For a string, or a list of strings: it may hold half of a surrogate pair, which JavaScript
88
- * keeps and ferrovue writes as U+FFFD — so two such values are never compared or joined. */
89
58
  lone?: boolean;
90
- /** For an optional string a temporary owns: that `Option<Cow<str>>`, whose `.as_deref()` is
91
- * `code`. A setup `let` keeps this. */
92
59
  held?: string;
93
- /** A number known at generation time: a literal, or literals JavaScript computed. */
94
60
  num?: number;
95
- /** For a string built by `format!`: its format string and arguments, which a string built from it
96
- * joins rather than formatting it again. */
97
61
  format?: {
98
62
  text: string;
99
63
  args: string[];
@@ -104,166 +68,104 @@ export declare const INT: Ty;
104
68
  export declare const FLOAT: Ty;
105
69
  export declare const BOOL: Ty;
106
70
  export declare const UNDEF: Ty;
71
+ export declare const NULL: Ty;
72
+ export type Absence = "undefined" | "null" | "either";
107
73
  export declare const opt: (of: Ty) => Ty;
74
+ export declare function absence(ty: Ty): Absence | null;
75
+ export declare function joinAbsence(a: Absence | null, b: Absence | null): Absence | null;
76
+ export declare function withAbsence(of: Ty, none: Absence | null): Ty;
77
+ export declare function nothing(ty: Ty): boolean;
108
78
  export declare const RUST_KEYWORDS: Set<string>;
109
- /** Types the generated code names, which an interface of the same name would shadow. */
110
79
  export declare const RUST_PRELUDE: Set<string>;
111
80
  export declare function snake(js: string): string;
112
- /** `camelize` in `@vue/shared`: how Vue matches an attribute a parent passes to a declared prop. */
113
81
  export declare function camelize(key: string): string;
114
- /** Whether a component declares the prop a parent passes as `key`, as `setFullProps` decides. */
115
82
  export declare function declares(comp: Component, key: string): boolean;
116
- /** Whether a component may be passed attributes beyond its props, and so takes `fv::Attrs`. */
117
83
  export declare function takesAttrs(comp: Component): boolean;
118
84
  export declare function rustStr(s: string): string;
119
- /** One character as a Rust `char` literal. */
120
85
  export declare function rustChar(ch: string): string;
86
+ /** Where in a file an error is: lines and columns count from 1, and the end is exclusive. */
87
+ export interface Location {
88
+ file: string;
89
+ line?: number;
90
+ column?: number;
91
+ endLine?: number;
92
+ endColumn?: number;
93
+ }
94
+ /** A construct the compiler refuses: its stable code, the message as shown, where it is, and what
95
+ * is refused without the location and the quoted line. */
121
96
  export declare class GenError extends Error {
97
+ readonly code: Code;
98
+ readonly at: Location | null;
99
+ readonly what: string;
100
+ constructor(code: Code, message: string, at?: Location | null, what?: string);
122
101
  }
102
+ /** Refuse something in a file that has no source to quote, as a locale or routes file. */
103
+ export declare function failIn(file: string, code: Code, what: string): never;
123
104
  export interface Component {
124
- /** `TagEditor` */
125
105
  name: string;
126
- /** `tag_editor` */
127
106
  module: string;
128
107
  file: string;
129
- /** The file's text, which an error quotes. */
130
108
  source?: string;
131
- /** Vue's compiled template mapped back to the template, and where the template's content starts
132
- * in the file, for an error found in the compiled code. */
133
109
  templateMap?: SourceMapConsumer;
134
110
  templateStart?: {
135
111
  line: number;
136
112
  column: number;
137
113
  };
138
- /** A root `<Transition>` or `<KeepAlive>` around anything but one plain element — a `v-if`, a
139
- * `<template>` — whose content Vue's server gives no attributes and its client does. */
114
+ templateAst?: N;
140
115
  attrsDropped?: N;
141
116
  props: Struct;
142
117
  structs: Map<string, Struct>;
143
- /** The local name `TrustedHtml` was imported under from `ferrovue/types`, if it was. */
144
118
  trustedName: string | null;
145
- /** The local name `Float` was imported under from `ferrovue/types`, if it was. */
146
119
  floatName: string | null;
147
- /** Types imported as another component's `Props`: local name → that component's name. */
148
120
  childProps: Map<string, string>;
149
- /** The components it imports, by name. */
150
121
  imports: Set<string>;
151
- /** The slots its template renders with `<slot>`, by name, in order of first appearance. */
152
122
  slotNames: string[];
153
- /** Whether its template holds `<RouterView>`: the page, which the server supplies. */
154
- routerView: boolean;
155
- /** Whether its template holds `<RouterLink>`. */
156
- routerLink: boolean;
157
- /** Whether it reads the route itself: `useRoute()`, or `$route` in the template. */
158
- readsRoute: boolean;
159
- /** Whether it renders a `<RouterLink>` or reads the route, itself or through a child, and so
160
- * takes the route. */
161
- usesRoute: boolean;
162
- /** Whether its setup reads a store. */
163
- readsStores: boolean;
164
- /** Whether it reads a store, itself or through a child, and so takes the stores' state. */
165
- usesStores: boolean;
166
- /** Whether it translates — \`$t\`, or \`useI18n()\` in setup — and whether it or a child does, and
167
- * so takes the request's \`I18n\`. */
168
- readsI18n: boolean;
169
- usesI18n: boolean;
170
- /** Whether it renders a \`<Teleport>\`, and whether it or a child does, and so takes the page's
171
- * \`Teleports\`. */
172
- readsTeleports: boolean;
173
- usesTeleports: boolean;
174
- /** `defineModel` bindings: local name → the prop it reads. */
123
+ takes: Set<string>;
175
124
  models: Map<string, string>;
176
- /** Type aliases it declares: name → the type. */
177
125
  aliases: Map<string, N>;
178
- /** Types it imports from elsewhere: local name → the type, marked with where it lives. */
179
126
  importedTypes: Map<string, Ty>;
180
- /** Its scoped slots, by name: the props each one's outlets pass, known once its render is
181
- * generated, which is why a child is generated before its parents. */
182
127
  slotShapes: Map<string, Struct>;
183
- /** `data-v-…`, the id its `<style scoped>` gives its elements, or `null` without one. */
184
- scopeId: string | null;
185
- /** Whether its scoped styles use `:slotted()`, so that its outlets pass a slot scope id. */
186
- slotted: boolean;
187
- /** `inheritAttrs` from `defineOptions`: `false` drops what a parent passes on to its root. */
188
128
  inheritAttrs: boolean;
189
- /** Whether a parent may hand its root scope ids — its own, those passed on to it, or a slot's —
190
- * which it then takes as `fv_attrs`. Known once every component is read (`scopeFlow`). */
191
129
  inherits: boolean;
192
- /** Whether its outlets may pass slot content a slot scope id, which the content then takes as
193
- * `fv_sid`. Known once every component is read, as `inherits` is. */
194
130
  passesSlotIds: boolean;
195
- /** The attributes a parent may pass it beyond its props, by name: what falls through to its root,
196
- * or to the element that binds `$attrs`. With any, its `render_scoped` takes them as
197
- * `fv::Attrs`. Known once every component is read (`attrsFlow`). */
198
131
  attrNames: Set<string>;
199
- /** Whether its `$attrs` may hold scope ids: it is the root of a component that may be handed
200
- * some, which passes them on as attributes. */
201
132
  idsInAttrs: boolean;
202
133
  }
134
+ export declare function blankComponent(name: string, module: string, file: string, structs?: Map<string, Struct>): Component;
203
135
  export interface Scope {
204
136
  comp: Component;
205
137
  components: Map<string, Component>;
206
- /** Setup bindings the server can evaluate: `ref(...)` and `computed(() => ...)`. */
207
138
  setup: Map<string, Val>;
208
- /** Imported child components, by local name. */
209
139
  children: Map<string, string>;
210
- /** Imported helpers, by local name → helper name. */
211
140
  helpers: Map<string, string>;
212
- /** Loop variables in scope. */
213
141
  locals: Map<string, Val>;
214
- /** The identifier `defineProps` was assigned to in the script, if any. */
215
142
  propsIdent: string | null;
216
- /** Setup bindings that are refs — `ref`, `computed`, `storeToRefs`, `defineModel` — which code in
217
- * the script reads through `.value`, and the template unwrapped. */
218
143
  refs: Set<string>;
219
- /** Setup bindings the server does not evaluate, and why. The template may name one only from an
220
- * event handler, which the server drops. */
221
144
  clientOnly: Map<string, string>;
222
- /** Optional values a surrounding `v-if` has found present, by the JavaScript path that named them
223
- * — what TypeScript narrows, and what the generated `if let` bound. */
224
145
  narrowed: Map<string, Val>;
225
- /** The name the compiled template resolved this component itself under, when it uses itself. */
226
146
  selfAlias: {
227
147
  name: string | null;
228
148
  };
229
- /** What the helper calls translated so far can write, shared by every copy of the scope. */
230
149
  helperBytes: {
231
150
  n: number;
232
151
  };
233
- /** The names the compiled template resolved `RouterLink` and `RouterView` under. */
234
- router: Map<string, "RouterLink" | "RouterView">;
235
- /** Directives the compiled template resolved by name: local → the directive's name. */
236
152
  directives: Map<string, string>;
237
- /** Setup bindings that are vue-i18n's \`t\`, from \`const { t } = useI18n()\`. */
238
- i18nT: Set<string>;
239
- /** Inside slot content whose emptiness is decided at run time: each push that is not a comment
240
- * sets the closure's `filled`, which is how Vue tells content from nothing (`ssrRenderSlot`). */
241
153
  fill: boolean;
242
- /** Inside a `<RouterLink>`'s slot, which Vue renders from virtual nodes rather than pushes: an
243
- * untaken `v-if` is `<!--v-if-->` there, not `<!---->`. */
244
154
  vnode: boolean;
245
- /** The Rust value of `_attrs`, the scope ids this render's root inherits, as `ssrRenderAttrs`
246
- * writes them; `null` when the component inherits none. */
247
155
  attrs: string | null;
248
- /** The Rust `fv::Attrs` a parent passes this render beyond its props — `$attrs`, and what
249
- * `_attrs` holds before the scope ids — or `null` when no parent passes any. */
250
156
  fallthrough: string | null;
251
- /** Setup bindings holding `useAttrs()`, which the template may bind as `$attrs`. */
252
157
  attrsBindings: Set<string>;
253
- /** The Rust value of `_scopeId` inside slot content: the slot scope id the content is given, or
254
- * `null` where it is always empty. */
158
+ slotsBindings: Set<string>;
159
+ consts: Map<string, Const>;
255
160
  sid: string | null;
256
- /** Inside a `v-for` over a list of the props: the Rust name of its item, and the list. */
257
161
  loop?: {
258
162
  item: string;
259
163
  over: string;
260
164
  };
165
+ plugins: Map<Plugin, unknown>;
261
166
  }
262
- /** Where a node came from, which decides how its position is read: `source` for an AST parsed
263
- * from the file itself (a script block, a store, a type file), whose positions are the file's;
264
- * `template` for Vue's compiled template, whose positions map back through its source map. */
265
167
  export type Origin = "source" | "template";
266
- /** Mark every node of an AST with where it came from, so that an error can point into the file. */
267
168
  export declare function tagAst(node: N, origin: Origin, seen?: WeakSet<object>): void;
268
- export declare function fail(comp: Component, what: string, node?: N): never;
169
+ export declare function sourceAt(source: string, offset: number): N;
170
+ export declare function fail(comp: Component, code: Code, what: string, node?: N): never;
269
171
  export declare function sameTy(a: Ty, b: Ty): boolean;
package/dist/model.js CHANGED
@@ -1,27 +1,50 @@
1
- /* The compiler's model: the types it gives values, the shapes of components and scopes, and the errors it raises. (Not `types.ts`, which is the public `ferrovue/types` module.) */
2
1
  export const STR = { k: "str" };
3
2
  export const INT = { k: "int" };
4
3
  export const FLOAT = { k: "float" };
5
4
  export const BOOL = { k: "bool" };
6
5
  export const UNDEF = { k: "undef" };
7
- export const opt = (of) => (of.k === "opt" ? of : { k: "opt", of });
6
+ export const NULL = { k: "null" };
7
+ export const opt = (of) => (of.k !== "opt" ? { k: "opt", of } : of.none === undefined ? of : { k: "opt", of: of.of, none: "either" });
8
+ export function absence(ty) {
9
+ if (ty.k === "undef")
10
+ return "undefined";
11
+ if (ty.k === "null")
12
+ return "null";
13
+ if (ty.k === "opt")
14
+ return ty.none ?? "undefined";
15
+ return null;
16
+ }
17
+ export function joinAbsence(a, b) {
18
+ if (a === null || a === b)
19
+ return b;
20
+ if (b === null)
21
+ return a;
22
+ return "either";
23
+ }
24
+ export function withAbsence(of, none) {
25
+ if (none === null)
26
+ return of;
27
+ return none === "undefined" ? { k: "opt", of } : { k: "opt", of, none };
28
+ }
29
+ export function nothing(ty) {
30
+ return ty.k === "undef" || ty.k === "null";
31
+ }
8
32
  export const RUST_KEYWORDS = new Set("as break const continue crate else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while async await dyn abstract become box do final macro override priv typeof unsized virtual yield try".split(" "));
9
- /** Types the generated code names, which an interface of the same name would shadow. */
33
+ const NOT_RAW = new Set(["self", "Self", "super", "crate"]);
10
34
  export const RUST_PRELUDE = new Set(["Option", "Some", "None", "Vec", "String", "Box", "Result", "Ok", "Err", "Cow", "Slots", "Default"]);
11
35
  export function snake(js) {
12
36
  const s = js.replace(/[A-Z]/g, (c) => "_" + c.toLowerCase()).replace(/^_/, "");
37
+ if (NOT_RAW.has(s))
38
+ return `${s}_`;
13
39
  return RUST_KEYWORDS.has(s) ? `r#${s}` : s;
14
40
  }
15
- /** `camelize` in `@vue/shared`: how Vue matches an attribute a parent passes to a declared prop. */
16
41
  export function camelize(key) {
17
42
  return key.replace(/-(\w)/g, (_, c) => c.toUpperCase());
18
43
  }
19
- /** Whether a component declares the prop a parent passes as `key`, as `setFullProps` decides. */
20
44
  export function declares(comp, key) {
21
45
  const camel = camelize(key);
22
46
  return comp.props.fields.some((f) => camelize(f.js) === camel);
23
47
  }
24
- /** Whether a component may be passed attributes beyond its props, and so takes `fv::Attrs`. */
25
48
  export function takesAttrs(comp) {
26
49
  return comp.attrNames.size > 0;
27
50
  }
@@ -46,7 +69,6 @@ export function rustStr(s) {
46
69
  }
47
70
  return out + '"';
48
71
  }
49
- /** One character as a Rust `char` literal. */
50
72
  export function rustChar(ch) {
51
73
  if (ch === "'")
52
74
  return "'\\''";
@@ -54,11 +76,48 @@ export function rustChar(ch) {
54
76
  return `'"'`;
55
77
  return `'${rustStr(ch).slice(1, -1)}'`;
56
78
  }
79
+ /** A construct the compiler refuses: its stable code, the message as shown, where it is, and what
80
+ * is refused without the location and the quoted line. */
57
81
  export class GenError extends Error {
82
+ code;
83
+ at;
84
+ what;
85
+ constructor(code, message, at = null, what = message) {
86
+ super(message);
87
+ this.code = code;
88
+ this.at = at;
89
+ this.what = what;
90
+ }
91
+ }
92
+ /** Refuse something in a file that has no source to quote, as a locale or routes file. */
93
+ export function failIn(file, code, what) {
94
+ throw new GenError(code, `${file}: ${what}`, { file }, what);
95
+ }
96
+ export function blankComponent(name, module, file, structs = new Map()) {
97
+ return {
98
+ name,
99
+ module,
100
+ file,
101
+ props: { name: "Props", fields: [] },
102
+ structs,
103
+ trustedName: null,
104
+ floatName: null,
105
+ childProps: new Map(),
106
+ imports: new Set(),
107
+ slotNames: [],
108
+ takes: new Set(),
109
+ models: new Map(),
110
+ aliases: new Map(),
111
+ importedTypes: new Map(),
112
+ slotShapes: new Map(),
113
+ inheritAttrs: true,
114
+ inherits: false,
115
+ passesSlotIds: false,
116
+ attrNames: new Set(),
117
+ idsInAttrs: false,
118
+ };
58
119
  }
59
- /** Mark every node of an AST with where it came from, so that an error can point into the file. */
60
120
  export function tagAst(node, origin, seen = new WeakSet()) {
61
- // `compileScript` links some nodes back to others, so the tree may hold cycles.
62
121
  if (!node || typeof node !== "object" || seen.has(node))
63
122
  return;
64
123
  seen.add(node);
@@ -77,7 +136,6 @@ export function tagAst(node, origin, seen = new WeakSet()) {
77
136
  tagAst(v, origin, seen);
78
137
  }
79
138
  }
80
- /** A node's position in the component's own file — line from 1, column from 0 — or `null`. */
81
139
  function locate(comp, node) {
82
140
  const start = node?.loc?.start;
83
141
  if (!start)
@@ -87,29 +145,103 @@ function locate(comp, node) {
87
145
  if (node.__fv === "template" && comp.templateMap && comp.templateStart) {
88
146
  const o = comp.templateMap.originalPositionFor({ line: start.line, column: start.column });
89
147
  if (o.line == null || o.column == null)
90
- return null;
91
- // The template's content starts partway along the `<template>` line.
148
+ return locateWithin(comp, node) ?? locate(comp, written(comp, node));
92
149
  const t = comp.templateStart;
93
150
  return o.line === 1 ? { line: t.line, column: t.column - 1 + o.column } : { line: t.line + o.line - 1, column: o.column };
94
151
  }
95
152
  return null;
96
153
  }
97
- /** The source line, with a caret under the column, as rustc shows it. */
154
+ function locateWithin(comp, node) {
155
+ const start = node.loc.start;
156
+ const inner = Object.entries(node)
157
+ .filter(([k, v]) => k !== "loc" && v && typeof v === "object")
158
+ .flatMap(([, v]) => (Array.isArray(v) ? v : [v]))
159
+ .filter((c) => c?.loc?.start)
160
+ .toSorted((a, b) => a.start - b.start);
161
+ for (const child of inner) {
162
+ const at = locate(comp, child);
163
+ if (!at)
164
+ continue;
165
+ const c = child.loc.start;
166
+ const back = c.line === start.line ? c.column - start.column : 0;
167
+ return { line: at.line, column: Math.max(0, at.column - back) };
168
+ }
169
+ return null;
170
+ }
171
+ export function sourceAt(source, offset) {
172
+ const before = source.slice(0, offset);
173
+ const line = before.split("\n").length;
174
+ return { type: "VueSource", loc: { start: { line, column: offset - before.lastIndexOf("\n") - 1 } }, __fv: "source" };
175
+ }
176
+ const plain = (name) => name.replace(/[-_]/g, "").toLowerCase();
177
+ function assetName(n, prefix) {
178
+ if (n?.type === "Identifier")
179
+ return n.name.replace(prefix, "");
180
+ if (n?.type === "MemberExpression")
181
+ return n.computed ? n.property.value : n.property.name;
182
+ return null;
183
+ }
184
+ function written(comp, node) {
185
+ const call = node.type === "VariableDeclaration" ? node.declarations[0]?.init : node.type === "ExpressionStatement" ? node.expression : node;
186
+ const callee = call?.type === "CallExpression" && call.callee.type === "Identifier" ? call.callee.name : null;
187
+ const tagged = (name) => name && templateNode(comp, (el) => (plain(el.tag) === plain(name) ? el : null));
188
+ const directive = (name) => name && templateNode(comp, (el) => el.props.find((p) => p.type === 7 && plain(p.name) === plain(name)));
189
+ switch (callee) {
190
+ case "_ssrRenderComponent":
191
+ return tagged(assetName(call.arguments[0], /^_component_/));
192
+ case "_resolveComponent":
193
+ return tagged(call.arguments[0]?.value ?? null);
194
+ case "_resolveDirective":
195
+ return directive(call.arguments[0]?.value ?? null);
196
+ case "_ssrGetDirectiveProps":
197
+ return directive(assetName(call.arguments[1], /^_directive_/)?.replace(/^v(?=[A-Z])/, "") ?? null);
198
+ case "_ssrRenderSlot": {
199
+ const name = call.arguments[1]?.value;
200
+ return templateNode(comp, (el) => (el.tag === "slot" && (el.props.find((p) => p.type === 6 && p.name === "name")?.value?.content ?? "default") === name ? el : null));
201
+ }
202
+ default:
203
+ return null;
204
+ }
205
+ }
206
+ function templateNode(comp, test) {
207
+ const visit = (n) => {
208
+ const found = n.type === 1 ? test(n) : null;
209
+ if (found)
210
+ return found;
211
+ for (const c of n.children ?? []) {
212
+ const inner = visit(c);
213
+ if (inner)
214
+ return inner;
215
+ }
216
+ return null;
217
+ };
218
+ const found = comp.templateAst ? visit(comp.templateAst) : null;
219
+ return found && { type: "VueTemplate", loc: { start: { line: found.loc.start.line, column: found.loc.start.column - 1 } }, __fv: "source" };
220
+ }
98
221
  function snippet(comp, at) {
99
222
  const text = comp.source?.split("\n")[at.line - 1];
100
223
  if (text === undefined)
101
224
  return "";
102
225
  const n = String(at.line);
103
- // Tabs are kept, so the caret lines up whatever the tab width.
104
226
  const lead = text.slice(0, at.column).replace(/[^\t]/g, " ");
105
227
  return `\n ${n} | ${text}\n ${" ".repeat(n.length)} | ${lead}^`;
106
228
  }
107
- export function fail(comp, what, node) {
229
+ function locateEnd(node) {
230
+ const end = node?.__fv === "source" ? node.loc?.end : null;
231
+ return end ? { endLine: end.line, endColumn: end.column + 1 } : null;
232
+ }
233
+ export function fail(comp, code, what, node) {
108
234
  const at = locate(comp, node);
109
235
  if (!at)
110
- throw new GenError(`${comp.file}: ${what}`);
111
- throw new GenError(`${comp.file}:${at.line}:${at.column + 1}: ${what}${snippet(comp, at)}`);
236
+ throw new GenError(code, `${comp.file}: ${what}`, { file: comp.file }, what);
237
+ const where = { file: comp.file, line: at.line, column: at.column + 1, ...locateEnd(node) };
238
+ throw new GenError(code, `${comp.file}:${at.line}:${at.column + 1}: ${what}${snippet(comp, at)}`, where, what);
239
+ }
240
+ function canonical(_key, value) {
241
+ if (!value || typeof value !== "object" || Array.isArray(value))
242
+ return value;
243
+ return Object.fromEntries(Object.entries(value).toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
112
244
  }
113
245
  export function sameTy(a, b) {
114
- return JSON.stringify(a) === JSON.stringify(b);
246
+ return JSON.stringify(a, canonical) === JSON.stringify(b, canonical);
115
247
  }
@@ -0,0 +1,27 @@
1
+ import { type N, type Scope, type Ty, type Val } from "./model.ts";
2
+ export declare function known(v: Val): boolean | undefined;
3
+ export declare function truthy(v: Val): string;
4
+ export declare function pathOf(n: N): string | null;
5
+ export interface NullTest {
6
+ target: N;
7
+ literal: "null" | "undefined";
8
+ strict: boolean;
9
+ is: boolean;
10
+ }
11
+ export declare function nullTest(n: N): NullTest | null;
12
+ export declare function checkNullTest(s: Scope, t: NullTest, v: Val): void;
13
+ export interface Presence {
14
+ path: string;
15
+ pattern: (name: string) => string;
16
+ option: string;
17
+ present: string;
18
+ of: Ty;
19
+ negated: boolean;
20
+ truthy: boolean;
21
+ }
22
+ export declare function presence(s: Scope, n: N): Presence | null;
23
+ export declare function narrowTo(s: Scope, p: Presence, name: string): Scope;
24
+ export declare function narrowing(s: Scope, p: Presence, present: (s: Scope) => string, absent: (s: Scope) => string): string;
25
+ export declare function choice(test: string, yes: string, no: string): string;
26
+ export declare function cond(s: Scope, n: N): string;
27
+ export declare function boolOf(code: string): Val;