@alloy-js/core 0.17.0 → 0.18.0-dev.3
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/src/binder.d.ts +2 -1
- package/dist/src/binder.d.ts.map +1 -1
- package/dist/src/binder.js +2 -2
- package/dist/src/code.d.ts +1 -1
- package/dist/src/code.d.ts.map +1 -1
- package/dist/src/components/Block.d.ts +1 -1
- package/dist/src/components/Block.d.ts.map +1 -1
- package/dist/src/components/Declaration.d.ts +1 -1
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +1 -1
- package/dist/src/components/For.d.ts +1 -1
- package/dist/src/components/For.d.ts.map +1 -1
- package/dist/src/components/For.js +1 -1
- package/dist/src/components/Indent.d.ts +1 -1
- package/dist/src/components/Indent.d.ts.map +1 -1
- package/dist/src/components/List.d.ts +1 -1
- package/dist/src/components/List.d.ts.map +1 -1
- package/dist/src/components/List.js +2 -1
- package/dist/src/components/MemberDeclaration.d.ts +2 -2
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.js +2 -1
- package/dist/src/components/MemberScope.d.ts +2 -2
- package/dist/src/components/MemberScope.d.ts.map +1 -1
- package/dist/src/components/Output.d.ts +1 -1
- package/dist/src/components/Output.d.ts.map +1 -1
- package/dist/src/components/Output.js +1 -1
- package/dist/src/components/Prose.d.ts +1 -1
- package/dist/src/components/Prose.d.ts.map +1 -1
- package/dist/src/components/ReferenceOrContent.d.ts +1 -1
- package/dist/src/components/ReferenceOrContent.d.ts.map +1 -1
- package/dist/src/components/Scope.d.ts +1 -1
- package/dist/src/components/Scope.d.ts.map +1 -1
- package/dist/src/components/Show.d.ts +1 -1
- package/dist/src/components/Show.d.ts.map +1 -1
- package/dist/src/components/SourceDirectory.d.ts +1 -1
- package/dist/src/components/SourceDirectory.d.ts.map +1 -1
- package/dist/src/components/SourceDirectory.js +1 -1
- package/dist/src/components/SourceFile.d.ts +1 -1
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +1 -1
- package/dist/src/components/StatementList.d.ts +1 -1
- package/dist/src/components/StatementList.d.ts.map +1 -1
- package/dist/src/components/Switch.d.ts +2 -2
- package/dist/src/components/Switch.d.ts.map +1 -1
- package/dist/src/components/Switch.js +2 -1
- package/dist/src/components/Wrap.d.ts +1 -1
- package/dist/src/components/Wrap.d.ts.map +1 -1
- package/dist/src/context/binder.d.ts +1 -1
- package/dist/src/context/binder.d.ts.map +1 -1
- package/dist/src/context/source-file.d.ts +1 -1
- package/dist/src/context/source-file.d.ts.map +1 -1
- package/dist/src/context.d.ts +2 -2
- package/dist/src/context.d.ts.map +1 -1
- package/dist/src/context.js +1 -1
- package/dist/src/debug.d.ts +1 -0
- package/dist/src/debug.d.ts.map +1 -1
- package/dist/src/debug.js +4 -1
- package/dist/src/index.d.ts +4 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +4 -1
- package/dist/src/jsx-runtime.d.ts +4 -281
- package/dist/src/jsx-runtime.d.ts.map +1 -1
- package/dist/src/jsx-runtime.js +3 -319
- package/dist/src/props-combinators.d.ts +19 -0
- package/dist/src/props-combinators.d.ts.map +1 -0
- package/dist/src/props-combinators.js +108 -0
- package/dist/src/reactive-union-set.d.ts.map +1 -1
- package/dist/src/reactive-union-set.js +1 -1
- package/dist/src/reactivity.d.ts +75 -0
- package/dist/src/reactivity.d.ts.map +1 -0
- package/dist/src/reactivity.js +141 -0
- package/dist/src/render.d.ts +5 -1
- package/dist/src/render.d.ts.map +1 -1
- package/dist/src/render.js +68 -15
- package/dist/src/runtime/component.d.ts +24 -0
- package/dist/src/runtime/component.d.ts.map +1 -0
- package/dist/src/runtime/component.js +19 -0
- package/dist/src/runtime/intrinsic.d.ts +168 -0
- package/dist/src/runtime/intrinsic.d.ts.map +1 -0
- package/dist/src/runtime/intrinsic.js +11 -0
- package/dist/src/slot.d.ts +2 -2
- package/dist/src/slot.d.ts.map +1 -1
- package/dist/src/slot.js +1 -1
- package/dist/src/stc.d.ts +1 -1
- package/dist/src/stc.d.ts.map +1 -1
- package/dist/src/sti.d.ts +7 -6
- package/dist/src/sti.d.ts.map +1 -1
- package/dist/src/sti.js +1 -1
- package/dist/src/symbols/flags.d.ts +70 -0
- package/dist/src/symbols/flags.d.ts.map +1 -0
- package/dist/src/symbols/flags.js +72 -0
- package/dist/src/symbols/index.d.ts +1 -0
- package/dist/src/symbols/index.d.ts.map +1 -1
- package/dist/src/symbols/index.js +1 -0
- package/dist/src/symbols/output-scope.d.ts +2 -24
- package/dist/src/symbols/output-scope.d.ts.map +1 -1
- package/dist/src/symbols/output-scope.js +1 -25
- package/dist/src/symbols/output-symbol.d.ts +2 -47
- package/dist/src/symbols/output-symbol.d.ts.map +1 -1
- package/dist/src/symbols/output-symbol.js +2 -48
- package/dist/src/symbols/symbol-flow.d.ts.map +1 -1
- package/dist/src/symbols/symbol-flow.js +2 -2
- package/dist/src/symbols/symbol-slot.d.ts +1 -1
- package/dist/src/symbols/symbol-slot.d.ts.map +1 -1
- package/dist/src/symbols/symbol-slot.js +1 -1
- package/dist/src/symbols/symbol-table.d.ts +3 -3
- package/dist/src/symbols/symbol-table.d.ts.map +1 -1
- package/dist/src/tap.d.ts +1 -1
- package/dist/src/tap.d.ts.map +1 -1
- package/dist/src/tracer.d.ts +60 -3
- package/dist/src/tracer.d.ts.map +1 -1
- package/dist/src/tracer.js +60 -5
- package/dist/src/utils.d.ts +4 -3
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +2 -1
- package/dist/test/props-with-defaults.test.js +1 -1
- package/dist/test/reactive-union-set.test.js +1 -1
- package/dist/test/reactivity/cleanup.test.js +2 -1
- package/dist/test/reactivity/memo.test.js +1 -1
- package/dist/test/reactivity/untrack.test.js +1 -1
- package/dist/test/rendering/memoization.test.js +2 -1
- package/dist/test/split-props.test.js +1 -1
- package/dist/test/symbols/output-scope.test.js +2 -1
- package/dist/test/symbols/output-symbol.test.js +4 -3
- package/dist/test/symbols/resolution.test.js +2 -1
- package/dist/test/symbols/utils.d.ts +3 -2
- package/dist/test/symbols/utils.d.ts.map +1 -1
- package/dist/test/symbols/utils.js +2 -1
- package/dist/testing/extend-expect.d.ts +15 -0
- package/dist/testing/extend-expect.d.ts.map +1 -1
- package/dist/testing/extend-expect.js +2 -1
- package/dist/testing/render.d.ts +1 -1
- package/dist/testing/render.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -4
- package/src/binder.ts +3 -5
- package/src/code.ts +1 -1
- package/src/components/Block.tsx +1 -1
- package/src/components/Declaration.tsx +2 -1
- package/src/components/For.tsx +2 -1
- package/src/components/Indent.tsx +1 -1
- package/src/components/List.tsx +3 -1
- package/src/components/MemberDeclaration.tsx +4 -3
- package/src/components/MemberScope.tsx +2 -2
- package/src/components/Output.tsx +2 -1
- package/src/components/Prose.tsx +1 -1
- package/src/components/ReferenceOrContent.tsx +1 -1
- package/src/components/Scope.tsx +1 -1
- package/src/components/Show.tsx +1 -1
- package/src/components/SourceDirectory.tsx +2 -1
- package/src/components/SourceFile.tsx +2 -1
- package/src/components/StatementList.tsx +1 -1
- package/src/components/Switch.tsx +2 -1
- package/src/components/Wrap.tsx +1 -1
- package/src/context/binder.ts +1 -1
- package/src/context/source-file.ts +1 -1
- package/src/context.ts +3 -7
- package/src/debug.ts +5 -1
- package/src/index.ts +4 -1
- package/src/jsx-runtime.ts +15 -690
- package/src/props-combinators.ts +148 -0
- package/src/reactive-union-set.ts +1 -1
- package/src/reactivity.ts +230 -0
- package/src/render.ts +97 -26
- package/src/runtime/component.ts +67 -0
- package/src/runtime/intrinsic.ts +199 -0
- package/src/slot.ts +3 -4
- package/src/stc.ts +2 -2
- package/src/sti.ts +11 -11
- package/src/symbols/flags.ts +82 -0
- package/src/symbols/index.ts +1 -0
- package/src/symbols/output-scope.ts +2 -29
- package/src/symbols/output-symbol.ts +2 -55
- package/src/symbols/symbol-flow.ts +3 -7
- package/src/symbols/symbol-slot.tsx +2 -1
- package/src/symbols/symbol-table.ts +3 -3
- package/src/tap.ts +1 -1
- package/src/tracer.ts +38 -4
- package/src/utils.tsx +7 -5
- package/temp/api.json +984 -1261
- package/test/props-with-defaults.test.ts +1 -1
- package/test/reactive-union-set.test.tsx +1 -1
- package/test/reactivity/cleanup.test.tsx +2 -1
- package/test/reactivity/memo.test.tsx +1 -1
- package/test/reactivity/untrack.test.ts +1 -1
- package/test/rendering/basic.test.tsx +1 -1
- package/test/rendering/memoization.test.tsx +1 -1
- package/test/split-props.test.ts +1 -1
- package/test/symbols/output-scope.test.ts +2 -4
- package/test/symbols/output-symbol.test.ts +4 -7
- package/test/symbols/resolution.test.ts +2 -4
- package/test/symbols/utils.ts +3 -5
- package/test/utils.test.tsx +1 -1
- package/testing/extend-expect.ts +16 -0
- package/testing/render.ts +1 -2
package/src/jsx-runtime.ts
CHANGED
|
@@ -1,325 +1,19 @@
|
|
|
1
1
|
// Much of the implementations in this file are inspired by vuerx-js
|
|
2
2
|
// See: https://github.com/ryansolid/vuerx-jsx.
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from "@
|
|
17
|
-
import type { Refkey } from "./refkey.js";
|
|
18
|
-
import type { RenderedTextTree } from "./render.js";
|
|
19
|
-
import { scheduler } from "./scheduler.js";
|
|
20
|
-
import type { OutputSymbol } from "./symbols/output-symbol.js";
|
|
21
|
-
|
|
22
|
-
if ((globalThis as any).ALLOY) {
|
|
23
|
-
throw new Error(
|
|
24
|
-
"Multiple versions of the JSX Runtime have been loaded. This will likely cause undesirable behavior.",
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
(globalThis as any).ALLOY = true;
|
|
28
|
-
|
|
29
|
-
export interface Disposable {
|
|
30
|
-
(): void;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface Context {
|
|
34
|
-
disposables: Disposable[];
|
|
35
|
-
owner: Context | null;
|
|
36
|
-
|
|
37
|
-
// context providers
|
|
38
|
-
context?: Record<symbol, unknown>;
|
|
39
|
-
|
|
40
|
-
// store random info about the node
|
|
41
|
-
meta?: Record<string, any>;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A cache of RenderTextTree nodes created within this context,
|
|
45
|
-
* indexed by the component or function which created them.
|
|
46
|
-
*/
|
|
47
|
-
elementCache: ElementCache;
|
|
48
|
-
/**
|
|
49
|
-
* When this context was created by a component, this will
|
|
50
|
-
* be the component that created it.
|
|
51
|
-
*/
|
|
52
|
-
componentOwner?: ComponentCreator<unknown>;
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Whether this context will take an emitted symbol.
|
|
56
|
-
*/
|
|
57
|
-
takesSymbols: boolean;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The symbol that this component has taken.
|
|
61
|
-
*/
|
|
62
|
-
takenSymbols?: ShallowReactive<Set<OutputSymbol>>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let globalContext: Context | null = null;
|
|
66
|
-
export function getContext() {
|
|
67
|
-
return globalContext;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function getElementCache() {
|
|
71
|
-
return getContext()!.elementCache;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export type ElementCacheKey =
|
|
75
|
-
| ComponentCreator
|
|
76
|
-
| (() => unknown)
|
|
77
|
-
| CustomContext;
|
|
78
|
-
export type ElementCache = Map<ElementCacheKey, RenderedTextTree>;
|
|
79
|
-
|
|
80
|
-
export interface RootOptions {
|
|
81
|
-
componentOwner?: ComponentCreator<any>;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function root<T>(fn: (d: Disposable) => T, options?: RootOptions): T {
|
|
85
|
-
const context: Context = {
|
|
86
|
-
componentOwner: options?.componentOwner,
|
|
87
|
-
disposables: [],
|
|
88
|
-
owner: globalContext,
|
|
89
|
-
context: {},
|
|
90
|
-
elementCache: new Map(),
|
|
91
|
-
takesSymbols: false,
|
|
92
|
-
takenSymbols: undefined,
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
globalContext = context;
|
|
96
|
-
let ret;
|
|
97
|
-
try {
|
|
98
|
-
ret = untrack(() =>
|
|
99
|
-
fn(() => {
|
|
100
|
-
for (const d of context!.disposables) {
|
|
101
|
-
d();
|
|
102
|
-
}
|
|
103
|
-
}),
|
|
104
|
-
);
|
|
105
|
-
} finally {
|
|
106
|
-
globalContext = globalContext!.owner;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return ret;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function untrack<T>(fn: () => T): T {
|
|
113
|
-
pauseTracking();
|
|
114
|
-
const v = fn();
|
|
115
|
-
resetTracking();
|
|
116
|
-
return v;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function memo<T>(fn: () => T, equal?: boolean): () => T {
|
|
120
|
-
const o = shallowRef();
|
|
121
|
-
effect((prev) => {
|
|
122
|
-
const res = fn();
|
|
123
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
124
|
-
(!equal || prev !== res) && (o.value = res);
|
|
125
|
-
return res;
|
|
126
|
-
}, undefined as T);
|
|
127
|
-
return () => o.value;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function effect<T>(fn: (prev?: T) => T, current?: T) {
|
|
131
|
-
const context: Context = {
|
|
132
|
-
context: {},
|
|
133
|
-
disposables: [] as (() => void)[],
|
|
134
|
-
owner: globalContext,
|
|
135
|
-
elementCache: new Map(),
|
|
136
|
-
takesSymbols: false,
|
|
137
|
-
takenSymbols: undefined,
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const cleanupFn = (final: boolean) => {
|
|
141
|
-
const d = context.disposables;
|
|
142
|
-
context.disposables = [];
|
|
143
|
-
for (let k = 0, len = d.length; k < len; k++) d[k]();
|
|
144
|
-
|
|
145
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
146
|
-
final && stop(runner);
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
onCleanup(() => cleanupFn(true));
|
|
150
|
-
const runner: ReactiveEffectRunner<void> = vueEffect(
|
|
151
|
-
() => {
|
|
152
|
-
cleanupFn(false);
|
|
153
|
-
|
|
154
|
-
const oldContext = globalContext;
|
|
155
|
-
globalContext = context;
|
|
156
|
-
try {
|
|
157
|
-
current = fn(current);
|
|
158
|
-
} finally {
|
|
159
|
-
globalContext = oldContext;
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
{
|
|
163
|
-
scheduler: scheduler(() => runner),
|
|
164
|
-
},
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
// allow recursive effects (recursive option does nothing, possible bug)
|
|
168
|
-
(runner as any).effect.flags |= 1 << 5;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Register a cleanup function which is called when the current reactive scope
|
|
173
|
-
* is recalculated or disposed. This is useful to clean up any side effects
|
|
174
|
-
* created in the reactive scope.
|
|
175
|
-
*
|
|
176
|
-
* @remarks
|
|
177
|
-
*
|
|
178
|
-
* When onCleanup is called inside a component definition, the provided function
|
|
179
|
-
* is called when the component is removed from the tree. This can be useful to
|
|
180
|
-
* clean up any side effects created as a result of rendering the component. For
|
|
181
|
-
* example, if rendering a component creates a symbol, `onCleanup` can be used
|
|
182
|
-
* to remove the symbol when the component is removed from the tree.
|
|
183
|
-
*
|
|
184
|
-
* When onCleanup is called inside a memo or effect, the function is called when
|
|
185
|
-
* the effect is refreshed (e.g. when a memo or computed recalculates) or
|
|
186
|
-
* disposed (e.g. it is no longer needed because it is attached to a component
|
|
187
|
-
* which was removed).
|
|
188
|
-
*/
|
|
189
|
-
export function onCleanup(fn: Disposable) {
|
|
190
|
-
if (globalContext != null) {
|
|
191
|
-
globalContext.disposables.push(fn);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Create a custom reactive context for the children returned by
|
|
197
|
-
* the provided context.
|
|
198
|
-
*/
|
|
199
|
-
export interface CustomContext {
|
|
200
|
-
[CUSTOM_CONTEXT_SYM]: true;
|
|
201
|
-
useCustomContext: (useCb: CustomContextChildrenCallback) => void;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export type CustomContextChildrenCallback = (child: Children) => void;
|
|
205
|
-
const CUSTOM_CONTEXT_SYM = Symbol();
|
|
206
|
-
|
|
207
|
-
export function createCustomContext(
|
|
208
|
-
useCallback: (useChildren: CustomContextChildrenCallback) => void,
|
|
209
|
-
): CustomContext {
|
|
210
|
-
return {
|
|
211
|
-
[CUSTOM_CONTEXT_SYM]: true,
|
|
212
|
-
useCustomContext(useCb: (children: Children) => void): void {
|
|
213
|
-
useCallback(useCb);
|
|
214
|
-
},
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
export function isCustomContext(child: Children): child is CustomContext {
|
|
219
|
-
return (
|
|
220
|
-
typeof child === "object" &&
|
|
221
|
-
child !== null &&
|
|
222
|
-
Object.hasOwn(child, CUSTOM_CONTEXT_SYM)
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export type Child =
|
|
227
|
-
| string
|
|
228
|
-
| boolean
|
|
229
|
-
| number
|
|
230
|
-
| undefined
|
|
231
|
-
| null
|
|
232
|
-
| void
|
|
233
|
-
| (() => Children)
|
|
234
|
-
| Ref
|
|
235
|
-
| Refkey
|
|
236
|
-
| CustomContext
|
|
237
|
-
| IntrinsicElement;
|
|
238
|
-
|
|
239
|
-
export type Children = Child | Children[];
|
|
240
|
-
export type Props = Record<string, any>;
|
|
241
|
-
|
|
242
|
-
export interface ComponentDefinition<TProps = Props> {
|
|
243
|
-
(props: TProps): Children;
|
|
244
|
-
}
|
|
245
|
-
export interface Component<TProps = Props> {
|
|
246
|
-
(props: TProps): Children;
|
|
247
|
-
tag?: symbol;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export interface ComponentCreator<TProps = Props> {
|
|
251
|
-
component: Component<TProps>;
|
|
252
|
-
(): Children;
|
|
253
|
-
props: TProps;
|
|
254
|
-
tag?: symbol;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// debugging utilities
|
|
258
|
-
const renderStack: {
|
|
259
|
-
component: Component<any>;
|
|
260
|
-
props: Props;
|
|
261
|
-
}[] = [];
|
|
262
|
-
|
|
263
|
-
export function pushStack(component: Component<any>, props: Props) {
|
|
264
|
-
if (!shouldDebug()) return;
|
|
265
|
-
renderStack.push({ component, props });
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
export function popStack() {
|
|
269
|
-
if (!shouldDebug()) return;
|
|
270
|
-
renderStack.pop();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
export function printRenderStack() {
|
|
274
|
-
if (!shouldDebug()) return;
|
|
275
|
-
|
|
276
|
-
// eslint-disable-next-line no-console
|
|
277
|
-
console.error("Error rendering:");
|
|
278
|
-
for (let i = renderStack.length - 1; i >= 0; i--) {
|
|
279
|
-
const { component, props } = renderStack[i];
|
|
280
|
-
// eslint-disable-next-line no-console
|
|
281
|
-
console.error(` at ${component.name}(${inspectProps(props)})`);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function inspectProps(props: Props) {
|
|
286
|
-
return JSON.stringify(
|
|
287
|
-
Object.fromEntries(
|
|
288
|
-
Object.entries(props).map(([key, value]) => {
|
|
289
|
-
let safeValue;
|
|
290
|
-
switch (typeof value) {
|
|
291
|
-
case "string":
|
|
292
|
-
case "number":
|
|
293
|
-
case "boolean":
|
|
294
|
-
safeValue = value;
|
|
295
|
-
break;
|
|
296
|
-
case "undefined":
|
|
297
|
-
safeValue = "undefined";
|
|
298
|
-
break;
|
|
299
|
-
case "object":
|
|
300
|
-
safeValue = value ? "{...}" : null;
|
|
301
|
-
break;
|
|
302
|
-
case "function":
|
|
303
|
-
safeValue = "function";
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
return [key, safeValue];
|
|
307
|
-
}),
|
|
308
|
-
),
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// These can be removed with a smarter transform that encodes the information we
|
|
313
|
-
// need in the compiled JSX output.
|
|
314
|
-
export function isComponentCreator<TProps = any>(
|
|
315
|
-
item: unknown,
|
|
316
|
-
component?: Component<TProps>,
|
|
317
|
-
): item is ComponentCreator<TProps> {
|
|
318
|
-
if (!component) {
|
|
319
|
-
return typeof item === "function" && (item as any).component;
|
|
320
|
-
}
|
|
321
|
-
return typeof item === "function" && (item as any).component === component;
|
|
322
|
-
}
|
|
3
|
+
import type {
|
|
4
|
+
Children,
|
|
5
|
+
Component,
|
|
6
|
+
ComponentDefinition,
|
|
7
|
+
IntrinsicElements as CoreIntrinsicElements,
|
|
8
|
+
} from "@alloy-js/core";
|
|
9
|
+
import { createComponent } from "@alloy-js/core";
|
|
10
|
+
export {
|
|
11
|
+
createComponent,
|
|
12
|
+
createIntrinsic,
|
|
13
|
+
memo,
|
|
14
|
+
mergeProps,
|
|
15
|
+
type Children,
|
|
16
|
+
} from "@alloy-js/core";
|
|
323
17
|
|
|
324
18
|
/**
|
|
325
19
|
* This namespace is predominantly for interop with React tooling in VSCode
|
|
@@ -327,134 +21,7 @@ export function isComponentCreator<TProps = any>(
|
|
|
327
21
|
*/
|
|
328
22
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
329
23
|
export namespace JSX {
|
|
330
|
-
export
|
|
331
|
-
/**
|
|
332
|
-
* Attempt to render the children on a single line if possible. If a group
|
|
333
|
-
* contains `<breakParent />` or a hard line, or if the group exceeds the
|
|
334
|
-
* print width, all linebreaks in the group will be broken.
|
|
335
|
-
*/
|
|
336
|
-
group: { shouldBreak?: boolean; id?: symbol; children: Children };
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* A regular line break. This will break if the line exceeds the print
|
|
340
|
-
* width, otherwise it will be a space.
|
|
341
|
-
*/
|
|
342
|
-
line: {};
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* A regular line break. This will break if the line exceeds the print
|
|
346
|
-
* width, otherwise it will be a space.
|
|
347
|
-
*/
|
|
348
|
-
br: {};
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* A hard line break. This is a line that will always break, even if the
|
|
352
|
-
* group does not exceed print width.
|
|
353
|
-
*/
|
|
354
|
-
hardline: {};
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* A hard line break. This is a line that will always break, even if the
|
|
358
|
-
* group does not exceed print width.
|
|
359
|
-
*/
|
|
360
|
-
hbr: {};
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* A soft line break. This will break if the line exceeds the print width,
|
|
364
|
-
* otherwise it will be be nothing.
|
|
365
|
-
*/
|
|
366
|
-
softline: {};
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* A soft line break. This will break if the line exceeds the print width,
|
|
370
|
-
* otherwise it will be be nothing.
|
|
371
|
-
*/
|
|
372
|
-
sbr: {};
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* A literal line break. This will always break, even if the group does not
|
|
376
|
-
* exceed print width. The new line will ignore indentation.
|
|
377
|
-
*/
|
|
378
|
-
literalline: {};
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* A literal line break. This will always break, even if the group does not
|
|
382
|
-
* exceed print width. The new line will ignore indentation.
|
|
383
|
-
*/
|
|
384
|
-
lbr: {};
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Increase the indentation level of the children of this component.
|
|
388
|
-
* Indentation is determined by the print options provided to the Output
|
|
389
|
-
* component or source file.
|
|
390
|
-
*/
|
|
391
|
-
indent: { children: Children };
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* Indent the children of this component if the group specified by `groupId`
|
|
395
|
-
* is broken (or not broken if `negate` is passed). The specified group must
|
|
396
|
-
* already be printed.
|
|
397
|
-
*/
|
|
398
|
-
indentIfBreak: { children: Children; groupId: symbol; negate?: boolean };
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* Similar to `group`, but will only place a line break before the last
|
|
402
|
-
* segment to exceed the print width. This is useful for formatting
|
|
403
|
-
* paragraphs of text where breaks are inserted prior to words which would
|
|
404
|
-
* otherwise exceed the print width.
|
|
405
|
-
*/
|
|
406
|
-
fill: { children: Children };
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* Force the parent group to break.
|
|
410
|
-
*/
|
|
411
|
-
breakParent: {};
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Print children if the current group or already printed group specified by
|
|
415
|
-
* `groupId` is broken. Otherwise, `flatContents` is printed instead.
|
|
416
|
-
*/
|
|
417
|
-
ifBreak: { children: Children; flatContents?: Children; groupId?: symbol };
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Print this content at the end of the line. Useful for things like line
|
|
421
|
-
* comments.
|
|
422
|
-
*/
|
|
423
|
-
lineSuffix: { children: Children };
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Force any line suffixes to print at this point.
|
|
427
|
-
*/
|
|
428
|
-
lineSuffixBoundary: {};
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* Decrease the indentation level of the children of this component.
|
|
432
|
-
* Indentation is determined by the print options provided to the Output
|
|
433
|
-
* component or source file.
|
|
434
|
-
*/
|
|
435
|
-
dedent: { children: Children };
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Indent the children of this component by either the number of characters
|
|
439
|
-
* indicated by the `width` prop, or by the provided string indicated by the
|
|
440
|
-
* `string` prop.
|
|
441
|
-
*/
|
|
442
|
-
align:
|
|
443
|
-
| { children: Children; width: number }
|
|
444
|
-
| { children: Children; string: string };
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
* Mark the current indentation level as "root" for the purposes of literal
|
|
448
|
-
* line breaks and `dedentToRoot`.
|
|
449
|
-
*/
|
|
450
|
-
markAsRoot: { children: Children };
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
* Decrease the indentation level to the root level specified by
|
|
454
|
-
* `<markAsRoot />`, or else to no indentation.
|
|
455
|
-
*/
|
|
456
|
-
dedentToRoot: { children: Children };
|
|
457
|
-
}
|
|
24
|
+
export type IntrinsicElements = CoreIntrinsicElements;
|
|
458
25
|
export type ElementType = string | ComponentDefinition<any>;
|
|
459
26
|
export type Element = Children;
|
|
460
27
|
export interface ElementChildrenAttribute {
|
|
@@ -470,245 +37,3 @@ export function jsx(type: Component<any>, props: Record<string, unknown>) {
|
|
|
470
37
|
}
|
|
471
38
|
|
|
472
39
|
export const jsxs = jsx;
|
|
473
|
-
|
|
474
|
-
export function createComponent<TProps extends Props = Props>(
|
|
475
|
-
C: Component<TProps>,
|
|
476
|
-
props: TProps,
|
|
477
|
-
): ComponentCreator<TProps> {
|
|
478
|
-
const creator: ComponentCreator<TProps> = () => /* */ C(props);
|
|
479
|
-
creator.props = props;
|
|
480
|
-
creator.component = C;
|
|
481
|
-
if (C.tag) {
|
|
482
|
-
creator.tag = C.tag;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
return creator;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
export function taggedComponent<TProps = Props>(
|
|
489
|
-
tag: symbol,
|
|
490
|
-
component: Component<TProps>,
|
|
491
|
-
): Component<TProps> & Required<Pick<Component<TProps>, "tag">> {
|
|
492
|
-
component.tag = tag;
|
|
493
|
-
return component as any;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
export const intrinsicElementKey = Symbol();
|
|
497
|
-
|
|
498
|
-
export type IndentIntrinsicElement = IntrinsicElementBase<"indent">;
|
|
499
|
-
export type IndentIfBreakIntrinsicElement =
|
|
500
|
-
IntrinsicElementBase<"indentIfBreak">;
|
|
501
|
-
export type BrIntrinsicElement = IntrinsicElementBase<"br">;
|
|
502
|
-
export type LineIntrinsicElement = IntrinsicElementBase<"line">;
|
|
503
|
-
export type HbrIntrinsicElement = IntrinsicElementBase<"hbr">;
|
|
504
|
-
export type HardlineIntrinsicElement = IntrinsicElementBase<"hardline">;
|
|
505
|
-
export type SbrIntrinsicElement = IntrinsicElementBase<"sbr">;
|
|
506
|
-
export type SoftlineIntrinsicElement = IntrinsicElementBase<"softline">;
|
|
507
|
-
export type GroupIntrinsicElement = IntrinsicElementBase<"group">;
|
|
508
|
-
export type AlignIntrinsicElement = IntrinsicElementBase<"align">;
|
|
509
|
-
export type FillIntrinsicElement = IntrinsicElementBase<"fill">;
|
|
510
|
-
export type BreakParentIntrinsicElement = IntrinsicElementBase<"breakParent">;
|
|
511
|
-
export type LineSuffixIntrinsicElement = IntrinsicElementBase<"lineSuffix">;
|
|
512
|
-
export type LineSuffixBoundaryIntrinsicElement =
|
|
513
|
-
IntrinsicElementBase<"lineSuffixBoundary">;
|
|
514
|
-
export type DedentIntrinsicElement = IntrinsicElementBase<"dedent">;
|
|
515
|
-
export type DedentToRootIntrinsicElement = IntrinsicElementBase<"dedentToRoot">;
|
|
516
|
-
export type MarkAsRootIntrinsicElement = IntrinsicElementBase<"markAsRoot">;
|
|
517
|
-
export type LiterallineIntrinsicElement = IntrinsicElementBase<"literalline">;
|
|
518
|
-
export type LbrIntrinsicElement = IntrinsicElementBase<"lbr">;
|
|
519
|
-
export type IfBreakIntrinsicElement = IntrinsicElementBase<"ifBreak">;
|
|
520
|
-
|
|
521
|
-
export type IntrinsicElement =
|
|
522
|
-
| IndentIntrinsicElement
|
|
523
|
-
| IndentIfBreakIntrinsicElement
|
|
524
|
-
| BrIntrinsicElement
|
|
525
|
-
| LineIntrinsicElement
|
|
526
|
-
| HbrIntrinsicElement
|
|
527
|
-
| HardlineIntrinsicElement
|
|
528
|
-
| SbrIntrinsicElement
|
|
529
|
-
| SoftlineIntrinsicElement
|
|
530
|
-
| GroupIntrinsicElement
|
|
531
|
-
| AlignIntrinsicElement
|
|
532
|
-
| FillIntrinsicElement
|
|
533
|
-
| BreakParentIntrinsicElement
|
|
534
|
-
| LineSuffixIntrinsicElement
|
|
535
|
-
| LineSuffixBoundaryIntrinsicElement
|
|
536
|
-
| DedentIntrinsicElement
|
|
537
|
-
| LiterallineIntrinsicElement
|
|
538
|
-
| LbrIntrinsicElement
|
|
539
|
-
| DedentToRootIntrinsicElement
|
|
540
|
-
| MarkAsRootIntrinsicElement
|
|
541
|
-
| IfBreakIntrinsicElement;
|
|
542
|
-
|
|
543
|
-
export interface IntrinsicElementBase<
|
|
544
|
-
TKey extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
|
|
545
|
-
> {
|
|
546
|
-
[intrinsicElementKey]: true;
|
|
547
|
-
name: TKey;
|
|
548
|
-
props: JSX.IntrinsicElements[TKey];
|
|
549
|
-
}
|
|
550
|
-
export function createIntrinsic<TKey extends keyof JSX.IntrinsicElements>(
|
|
551
|
-
name: TKey,
|
|
552
|
-
props: JSX.IntrinsicElements[TKey],
|
|
553
|
-
): IntrinsicElementBase<TKey> {
|
|
554
|
-
return {
|
|
555
|
-
[intrinsicElementKey]: true,
|
|
556
|
-
name,
|
|
557
|
-
props,
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
export function isIntrinsicElement(type: unknown): type is IntrinsicElement {
|
|
562
|
-
return (
|
|
563
|
-
typeof type === "object" && type !== null && intrinsicElementKey in type
|
|
564
|
-
);
|
|
565
|
-
}
|
|
566
|
-
export function mergeProps<T, U>(source: T, source1: U): T & U;
|
|
567
|
-
export function mergeProps<T, U, V>(
|
|
568
|
-
source: T,
|
|
569
|
-
source1: U,
|
|
570
|
-
source2: V,
|
|
571
|
-
): T & U & V;
|
|
572
|
-
export function mergeProps<T, U, V, W>(
|
|
573
|
-
source: T,
|
|
574
|
-
source1: U,
|
|
575
|
-
source2: V,
|
|
576
|
-
source3: W,
|
|
577
|
-
): T & U & V & W;
|
|
578
|
-
export function mergeProps(...sources: any): any {
|
|
579
|
-
const target = {};
|
|
580
|
-
for (let i = 0; i < sources.length; i++) {
|
|
581
|
-
let source = sources[i];
|
|
582
|
-
if (typeof source === "function") source = source();
|
|
583
|
-
if (source) {
|
|
584
|
-
const descriptors = Object.getOwnPropertyDescriptors(source);
|
|
585
|
-
for (const key in descriptors) {
|
|
586
|
-
if (key in target) continue;
|
|
587
|
-
Object.defineProperty(target, key, {
|
|
588
|
-
enumerable: true,
|
|
589
|
-
get() {
|
|
590
|
-
for (let i = sources.length - 1; i >= 0; i--) {
|
|
591
|
-
let s = sources[i];
|
|
592
|
-
if (typeof s === "function") s = s();
|
|
593
|
-
const v = (s || {})[key];
|
|
594
|
-
if (v !== undefined) return v;
|
|
595
|
-
}
|
|
596
|
-
},
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
return target;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
export type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
|
|
605
|
-
...{
|
|
606
|
-
[P in keyof K]: P extends `${number}` ?
|
|
607
|
-
Pick<T, Extract<K[P], readonly (keyof T)[]>[number]>
|
|
608
|
-
: never;
|
|
609
|
-
},
|
|
610
|
-
{ [P in keyof T as Exclude<P, K[number][number]>]: T[P] },
|
|
611
|
-
];
|
|
612
|
-
|
|
613
|
-
export function splitProps<
|
|
614
|
-
T extends Record<any, any>,
|
|
615
|
-
K extends [readonly (keyof T)[], ...(readonly (keyof T)[])[]],
|
|
616
|
-
>(props: T, ...keys: K): SplitProps<T, K> {
|
|
617
|
-
if (isReactive(props)) {
|
|
618
|
-
const refs = untrack(() => toRefs(props));
|
|
619
|
-
const remainingKeys = new Set(Object.keys(refs));
|
|
620
|
-
|
|
621
|
-
const result: any = keys.map((keySet) => {
|
|
622
|
-
const resultSet: any = {};
|
|
623
|
-
for (const key of keySet) {
|
|
624
|
-
resultSet[key] = refs[key];
|
|
625
|
-
remainingKeys.delete(key as string);
|
|
626
|
-
}
|
|
627
|
-
return proxyRefs(resultSet);
|
|
628
|
-
});
|
|
629
|
-
|
|
630
|
-
const remaining: any = {};
|
|
631
|
-
for (const key of remainingKeys) {
|
|
632
|
-
remaining[key] = refs[key];
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
return [...result, proxyRefs(remaining)] as any;
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
const descriptors = Object.getOwnPropertyDescriptors(props);
|
|
639
|
-
const remainingKeys = new Set(Object.keys(descriptors));
|
|
640
|
-
const result: any = keys.map((keySet) => {
|
|
641
|
-
const resultSet: any = {};
|
|
642
|
-
for (const key of keySet) {
|
|
643
|
-
if (key in descriptors) {
|
|
644
|
-
Object.defineProperty(resultSet, key, descriptors[key]);
|
|
645
|
-
remainingKeys.delete(key as string);
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
return resultSet;
|
|
649
|
-
});
|
|
650
|
-
|
|
651
|
-
const remaining: any = {};
|
|
652
|
-
for (const key of remainingKeys) {
|
|
653
|
-
Object.defineProperty(remaining, key, descriptors[key]);
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
return [...result, remaining] as any;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Applies default values to a props object. Reactive props are handled properly
|
|
661
|
-
* by ensuring that their value is not accessed by `defaultProps`, avoiding any
|
|
662
|
-
* unintended side effects.
|
|
663
|
-
*/
|
|
664
|
-
export function defaultProps<T extends {}>(props: T, defaults: Partial<T>): T {
|
|
665
|
-
if (isReactive(props)) {
|
|
666
|
-
const refs = untrack(() => toRefs(props));
|
|
667
|
-
for (const key in defaults) {
|
|
668
|
-
const originalRef = refs[key];
|
|
669
|
-
refs[key] = computed(() =>
|
|
670
|
-
originalRef.value === undefined ? defaults[key] : originalRef.value,
|
|
671
|
-
) as any;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
return proxyRefs(refs);
|
|
675
|
-
}
|
|
676
|
-
const withDefaults = {} as T;
|
|
677
|
-
const copied = new Set<string>();
|
|
678
|
-
for (const key in defaults) {
|
|
679
|
-
copied.add(key);
|
|
680
|
-
const desc = Object.getOwnPropertyDescriptor(props, key);
|
|
681
|
-
if (!desc) {
|
|
682
|
-
withDefaults[key] = defaults[key]!;
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
if (desc.get) {
|
|
687
|
-
const originalGet = desc.get;
|
|
688
|
-
desc.get = function () {
|
|
689
|
-
const value = originalGet.call(this);
|
|
690
|
-
return value === undefined ? defaults[key as keyof T] : value;
|
|
691
|
-
};
|
|
692
|
-
Object.defineProperty(withDefaults, key, desc);
|
|
693
|
-
} else {
|
|
694
|
-
desc.value =
|
|
695
|
-
desc.value === undefined ? defaults[key as keyof T] : desc.value;
|
|
696
|
-
Object.defineProperty(withDefaults, key, desc);
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
const descriptors = Object.getOwnPropertyDescriptors(props);
|
|
701
|
-
for (const key in descriptors) {
|
|
702
|
-
if (copied.has(key)) {
|
|
703
|
-
continue;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
Object.defineProperty(withDefaults, key, descriptors[key]);
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
return withDefaults;
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
function shouldDebug() {
|
|
713
|
-
return typeof process !== "undefined" && !!process.env?.ALLOY_DEBUG;
|
|
714
|
-
}
|