@alloy-js/core 0.5.0 → 0.7.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.
- package/CHANGELOG.md +55 -0
- package/babel.config.cjs +4 -1
- package/dist/src/binder.d.ts +8 -2
- package/dist/src/binder.d.ts.map +1 -1
- package/dist/src/binder.js +41 -15
- package/dist/src/binder.js.map +1 -1
- package/dist/src/code.d.ts +2 -2
- package/dist/src/code.d.ts.map +1 -1
- package/dist/src/code.js +4 -4
- package/dist/src/code.js.map +1 -1
- package/dist/src/components/Block.d.ts +25 -0
- package/dist/src/components/Block.d.ts.map +1 -0
- package/dist/src/components/Block.js +25 -0
- package/dist/src/components/Block.js.map +1 -0
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +4 -0
- package/dist/src/components/Declaration.js.map +1 -1
- package/dist/src/components/For.d.ts +44 -0
- package/dist/src/components/For.d.ts.map +1 -0
- package/dist/src/components/For.js +41 -0
- package/dist/src/components/For.js.map +1 -0
- package/dist/src/components/Indent.d.ts +5 -9
- package/dist/src/components/Indent.d.ts.map +1 -1
- package/dist/src/components/Indent.js +7 -18
- package/dist/src/components/Indent.js.map +1 -1
- package/dist/src/components/List.d.ts +38 -0
- package/dist/src/components/List.d.ts.map +1 -0
- package/dist/src/components/List.js +40 -0
- package/dist/src/components/List.js.map +1 -0
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.js.map +1 -1
- package/dist/src/components/MemberName.js +1 -1
- package/dist/src/components/MemberName.js.map +1 -1
- package/dist/src/components/MemberScope.d.ts.map +1 -1
- package/dist/src/components/MemberScope.js.map +1 -1
- package/dist/src/components/Name.js +1 -1
- package/dist/src/components/Name.js.map +1 -1
- package/dist/src/components/Output.d.ts +2 -1
- package/dist/src/components/Output.d.ts.map +1 -1
- package/dist/src/components/Output.js +9 -1
- package/dist/src/components/Output.js.map +1 -1
- package/dist/src/components/Scope.d.ts.map +1 -1
- package/dist/src/components/Scope.js.map +1 -1
- package/dist/src/components/Show.d.ts +8 -0
- package/dist/src/components/Show.d.ts.map +1 -0
- package/dist/src/components/Show.js +4 -0
- package/dist/src/components/Show.js.map +1 -0
- package/dist/src/components/SourceDirectory.d.ts.map +1 -1
- package/dist/src/components/SourceDirectory.js +1 -0
- package/dist/src/components/SourceDirectory.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts +2 -6
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +6 -13
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/StatementList.d.ts +9 -0
- package/dist/src/components/StatementList.d.ts.map +1 -0
- package/dist/src/components/StatementList.js +17 -0
- package/dist/src/components/StatementList.js.map +1 -0
- package/dist/src/components/Switch.d.ts +41 -0
- package/dist/src/components/Switch.d.ts.map +1 -0
- package/dist/src/components/Switch.js +41 -0
- package/dist/src/components/Switch.js.map +1 -0
- package/dist/src/components/Wrap.d.ts +20 -0
- package/dist/src/components/Wrap.d.ts.map +1 -0
- package/dist/src/components/Wrap.js +15 -0
- package/dist/src/components/Wrap.js.map +1 -0
- package/dist/src/components/index.d.ts +8 -1
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +7 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/stc/index.d.ts +77 -6
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +17 -1
- package/dist/src/components/stc/index.js.map +1 -1
- package/dist/src/context/index.d.ts +0 -1
- package/dist/src/context/index.d.ts.map +1 -1
- package/dist/src/context/index.js +0 -1
- package/dist/src/context/index.js.map +1 -1
- package/dist/src/context.d.ts.map +1 -1
- package/dist/src/context.js +3 -3
- package/dist/src/context.js.map +1 -1
- package/dist/src/index.browser.d.ts +3 -0
- package/dist/src/index.browser.d.ts.map +1 -0
- package/dist/src/index.browser.js +3 -0
- package/dist/src/index.browser.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/jsx-runtime.d.ts +139 -8
- package/dist/src/jsx-runtime.d.ts.map +1 -1
- package/dist/src/jsx-runtime.js +102 -12
- package/dist/src/jsx-runtime.js.map +1 -1
- package/dist/src/render.d.ts +107 -132
- package/dist/src/render.d.ts.map +1 -1
- package/dist/src/render.js +281 -177
- package/dist/src/render.js.map +1 -1
- package/dist/src/stc.d.ts +14 -0
- package/dist/src/stc.d.ts.map +1 -0
- package/dist/src/stc.js +52 -0
- package/dist/src/stc.js.map +1 -0
- package/dist/src/utils.d.ts +22 -15
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +95 -59
- package/dist/src/utils.js.map +1 -1
- package/dist/src/write-output.js +3 -3
- package/dist/src/write-output.js.map +1 -1
- package/dist/test/browser-build.test.d.ts +2 -0
- package/dist/test/browser-build.test.d.ts.map +1 -0
- package/dist/test/components/block.test.d.ts +2 -0
- package/dist/test/components/block.test.d.ts.map +1 -0
- package/dist/test/components/declaration.test.d.ts +2 -0
- package/dist/test/components/declaration.test.d.ts.map +1 -0
- package/dist/test/components/list.test.d.ts +2 -0
- package/dist/test/components/list.test.d.ts.map +1 -0
- package/dist/test/components/wrap.test.d.ts +2 -0
- package/dist/test/components/wrap.test.d.ts.map +1 -0
- package/dist/test/control-flow/for.test.d.ts +2 -0
- package/dist/test/control-flow/for.test.d.ts.map +1 -0
- package/dist/test/control-flow/match.test.d.ts +2 -0
- package/dist/test/control-flow/match.test.d.ts.map +1 -0
- package/dist/test/control-flow/show.test.d.ts +2 -0
- package/dist/test/control-flow/show.test.d.ts.map +1 -0
- package/dist/test/reactivity/cleanup.test.d.ts +2 -0
- package/dist/test/reactivity/cleanup.test.d.ts.map +1 -0
- package/dist/test/reactivity/memo.test.d.ts +2 -0
- package/dist/test/reactivity/memo.test.d.ts.map +1 -0
- package/dist/test/reactivity/untrack.test.d.ts +2 -0
- package/dist/test/reactivity/untrack.test.d.ts.map +1 -0
- package/dist/test/rendering/formatting.test.d.ts +2 -0
- package/dist/test/rendering/formatting.test.d.ts.map +1 -0
- package/dist/test/rendering/memoization.test.d.ts +2 -0
- package/dist/test/rendering/memoization.test.d.ts.map +1 -0
- package/dist/test/split-props.test.d.ts +2 -0
- package/dist/test/split-props.test.d.ts.map +1 -0
- package/dist/test/stc.test.d.ts.map +1 -1
- package/dist/test/utils.test.d.ts.map +1 -1
- package/dist/testing/extend-expect.js +4 -4
- package/dist/testing/extend-expect.js.map +1 -1
- package/dist/testing/render.d.ts +2 -3
- package/dist/testing/render.d.ts.map +1 -1
- package/dist/testing/render.js +2 -4
- package/dist/testing/render.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -8
- package/src/binder.ts +54 -18
- package/src/code.ts +17 -12
- package/src/components/Block.tsx +44 -0
- package/src/components/Declaration.tsx +10 -4
- package/src/components/For.tsx +81 -0
- package/src/components/Indent.tsx +20 -27
- package/src/components/List.tsx +94 -0
- package/src/components/MemberDeclaration.tsx +9 -6
- package/src/components/MemberScope.tsx +4 -2
- package/src/components/Output.tsx +25 -13
- package/src/components/Scope.tsx +4 -2
- package/src/components/Show.tsx +11 -0
- package/src/components/SourceDirectory.tsx +5 -1
- package/src/components/SourceFile.tsx +12 -16
- package/src/components/StatementList.tsx +16 -0
- package/src/components/Switch.tsx +62 -0
- package/src/components/Wrap.tsx +29 -0
- package/src/components/index.tsx +8 -1
- package/src/components/stc/index.ts +18 -1
- package/src/context/index.ts +0 -1
- package/src/context.ts +2 -3
- package/src/index.browser.ts +2 -0
- package/src/index.ts +1 -0
- package/src/jsx-runtime.ts +245 -23
- package/src/render.ts +392 -198
- package/src/stc.ts +95 -0
- package/src/utils.ts +162 -95
- package/src/write-output.ts +3 -3
- package/temp/api.json +8407 -3301
- package/test/browser-build.test.ts +91 -0
- package/test/children.test.tsx +8 -10
- package/test/components/block.test.tsx +48 -0
- package/test/components/declaration.test.tsx +37 -0
- package/test/components/list.test.tsx +91 -0
- package/test/components/slot.test.tsx +31 -25
- package/test/components/source-file.test.tsx +11 -31
- package/test/components/wrap.test.tsx +42 -0
- package/test/control-flow/for.test.tsx +194 -0
- package/test/control-flow/match.test.tsx +49 -0
- package/test/control-flow/show.test.tsx +25 -0
- package/test/name-policy.test.tsx +5 -5
- package/test/reactivity/cleanup.test.tsx +91 -0
- package/test/reactivity/memo.test.tsx +17 -0
- package/test/reactivity/ref-rendering.test.tsx +3 -8
- package/test/reactivity/test.test.tsx +7 -6
- package/test/reactivity/untrack.test.ts +33 -0
- package/test/rendering/basic.test.tsx +25 -47
- package/test/rendering/code.test.tsx +3 -3
- package/test/rendering/formatting.test.tsx +487 -0
- package/test/rendering/indent.test.tsx +42 -529
- package/test/rendering/memoization.test.tsx +30 -0
- package/test/split-props.test.ts +87 -0
- package/test/stc.test.tsx +29 -8
- package/test/symbols.test.ts +87 -8
- package/test/utils.test.tsx +129 -20
- package/testing/extend-expect.ts +14 -4
- package/testing/render.ts +2 -4
- package/testing/vitest.d.ts +6 -1
- package/vitest.config.ts +1 -1
- package/dist/src/context/indent.d.ts +0 -5
- package/dist/src/context/indent.d.ts.map +0 -1
- package/dist/src/context/indent.js +0 -8
- package/dist/src/context/indent.js.map +0 -1
- package/dist/test/rendering/linebreaks.test.d.ts +0 -2
- package/dist/test/rendering/linebreaks.test.d.ts.map +0 -1
- package/src/context/indent.ts +0 -17
- package/test/rendering/linebreaks.test.tsx +0 -72
package/src/components/Scope.tsx
CHANGED
|
@@ -21,7 +21,9 @@ export function Scope(props: ScopeProps) {
|
|
|
21
21
|
scope = binder.createScope({ kind, name: props.name! });
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
return
|
|
24
|
+
return (
|
|
25
|
+
<ScopeContext.Provider value={scope}>
|
|
25
26
|
{props.children}
|
|
26
|
-
</ScopeContext.Provider
|
|
27
|
+
</ScopeContext.Provider>
|
|
28
|
+
);
|
|
27
29
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Children } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
export interface ShowProps {
|
|
4
|
+
children: Children;
|
|
5
|
+
fallback?: Children;
|
|
6
|
+
when: boolean | undefined | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function Show(props: ShowProps) {
|
|
10
|
+
return () => (props.when ? props.children : props.fallback);
|
|
11
|
+
}
|
|
@@ -20,7 +20,11 @@ export function SourceDirectory(props: SourceDirectoryProps) {
|
|
|
20
20
|
nodeContext.meta.directory = {
|
|
21
21
|
path: sdPath,
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return (
|
|
24
|
+
<SourceDirectoryContext.Provider value={context}>
|
|
25
|
+
{props.children}
|
|
26
|
+
</SourceDirectoryContext.Provider>
|
|
27
|
+
);
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
function createSourceDirectoryContext(
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { join } from "pathe";
|
|
2
2
|
import { useContext } from "../context.js";
|
|
3
|
-
import { IndentContext } from "../context/indent.js";
|
|
4
3
|
import { SourceDirectoryContext } from "../context/source-directory.js";
|
|
5
4
|
import { SourceFileContext } from "../context/source-file.js";
|
|
6
5
|
import { Children, ComponentDefinition, getContext } from "../jsx-runtime.js";
|
|
7
6
|
import { Refkey } from "../refkey.js";
|
|
7
|
+
import { PrintTreeOptions } from "../render.js";
|
|
8
8
|
|
|
9
|
-
export interface SourceFileProps {
|
|
9
|
+
export interface SourceFileProps extends PrintTreeOptions {
|
|
10
10
|
/**
|
|
11
11
|
* The path of this file relative to its parent directory
|
|
12
12
|
*/
|
|
@@ -24,12 +24,6 @@ export interface SourceFileProps {
|
|
|
24
24
|
* contents.
|
|
25
25
|
*/
|
|
26
26
|
reference?: ComponentDefinition<{ refkey: Refkey }>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* A string representing one indent level, used when reindenting contents of
|
|
30
|
-
* this file.
|
|
31
|
-
*/
|
|
32
|
-
indent?: string;
|
|
33
27
|
}
|
|
34
28
|
|
|
35
29
|
export function SourceFile(props: SourceFileProps) {
|
|
@@ -43,13 +37,15 @@ export function SourceFile(props: SourceFileProps) {
|
|
|
43
37
|
const nodeContext = getContext()!;
|
|
44
38
|
nodeContext.meta ??= {};
|
|
45
39
|
nodeContext.meta.sourceFile = context;
|
|
40
|
+
nodeContext.meta.printOptions = {
|
|
41
|
+
printWidth: props.printWidth,
|
|
42
|
+
tabWidth: props.tabWidth,
|
|
43
|
+
useTabs: props.useTabs,
|
|
44
|
+
};
|
|
46
45
|
|
|
47
|
-
return
|
|
48
|
-
{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
: props.children
|
|
53
|
-
}
|
|
54
|
-
</SourceFileContext.Provider>;
|
|
46
|
+
return (
|
|
47
|
+
<SourceFileContext.Provider value={context}>
|
|
48
|
+
{props.children}
|
|
49
|
+
</SourceFileContext.Provider>
|
|
50
|
+
);
|
|
55
51
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Children, List } from "@alloy-js/core";
|
|
2
|
+
|
|
3
|
+
export interface StatementListProps {
|
|
4
|
+
children: Children;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Join child elements with semicolons and hardlines.
|
|
9
|
+
*/
|
|
10
|
+
export function StatementList(props: StatementListProps) {
|
|
11
|
+
return (
|
|
12
|
+
<List semicolon hardline enderPunctuation>
|
|
13
|
+
{props.children}
|
|
14
|
+
</List>
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Children, memo, taggedComponent } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
import { childrenArray, findKeyedChildren } from "../utils.js";
|
|
3
|
+
|
|
4
|
+
export interface SwitchProps {
|
|
5
|
+
children: Children;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Conditionally render blocks of content based on the `when` prop of nested
|
|
10
|
+
* {@link Match} components.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```tsx
|
|
15
|
+
* <Switch>
|
|
16
|
+
* <Match when={someCondition}>
|
|
17
|
+
* <div>Condition met!</div>
|
|
18
|
+
* </Match>
|
|
19
|
+
* <Match else>
|
|
20
|
+
* <div>Condition not met!</div>
|
|
21
|
+
* </Match>
|
|
22
|
+
* </Switch>
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function Switch(props: SwitchProps) {
|
|
26
|
+
const children = childrenArray(() => props.children);
|
|
27
|
+
const matches = findKeyedChildren(children, matchTag);
|
|
28
|
+
|
|
29
|
+
return memo(() => {
|
|
30
|
+
for (const match of matches) {
|
|
31
|
+
if (match.props.when || match.props.else) {
|
|
32
|
+
return match.props.children;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return undefined;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface MatchProps {
|
|
41
|
+
/**
|
|
42
|
+
* Condition under which the children of this element will be rendered.
|
|
43
|
+
*/
|
|
44
|
+
when?: boolean;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* If no `when` is matched, the children of this element will be rendered.
|
|
48
|
+
* The `else` match must be placed last.
|
|
49
|
+
*/
|
|
50
|
+
else?: boolean;
|
|
51
|
+
children: Children;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const matchTag = Symbol();
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The Match component is used inside of a {@link Switch} component to
|
|
58
|
+
* define conditionally rendered blocks of content.
|
|
59
|
+
*/
|
|
60
|
+
export const Match = taggedComponent(matchTag, (props: MatchProps) => {
|
|
61
|
+
return () => (props.when ? props.children : undefined);
|
|
62
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Children, ComponentDefinition } from "@alloy-js/core/jsx-runtime";
|
|
2
|
+
|
|
3
|
+
export interface WrapProps<TProps> {
|
|
4
|
+
/**
|
|
5
|
+
* When true, the children will be wrapped with the provided component.
|
|
6
|
+
* Otherwise, the children will be rendered as is.
|
|
7
|
+
*/
|
|
8
|
+
when: boolean;
|
|
9
|
+
|
|
10
|
+
/** Children to be wrapped. */
|
|
11
|
+
children: Children;
|
|
12
|
+
|
|
13
|
+
/** Component to be used for wrapping. */
|
|
14
|
+
with: ComponentDefinition<TProps>;
|
|
15
|
+
|
|
16
|
+
/** Props to pass to the wrapper component. */
|
|
17
|
+
props?: Omit<TProps, "children">;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Conditionally wrap the children of this component with the component given to
|
|
22
|
+
* `with` and passing `props` to it.
|
|
23
|
+
*/
|
|
24
|
+
export function Wrap<TProps>(props: WrapProps<TProps>) {
|
|
25
|
+
const Wrapper = props.with as any;
|
|
26
|
+
return props.when ?
|
|
27
|
+
<Wrapper {...(props.props ?? {})}>{props.children}</Wrapper>
|
|
28
|
+
: props.children;
|
|
29
|
+
}
|
package/src/components/index.tsx
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
+
export * from "./Block.js";
|
|
1
2
|
export * from "./Declaration.js";
|
|
2
|
-
export * from "./
|
|
3
|
+
export * from "./For.js";
|
|
4
|
+
export * from "./Indent.jsx";
|
|
5
|
+
export * from "./List.jsx";
|
|
3
6
|
export * from "./MemberDeclaration.jsx";
|
|
4
7
|
export * from "./MemberName.jsx";
|
|
5
8
|
export * from "./MemberScope.jsx";
|
|
6
9
|
export * from "./Name.jsx";
|
|
7
10
|
export * from "./Output.js";
|
|
8
11
|
export * from "./Scope.js";
|
|
12
|
+
export * from "./Show.jsx";
|
|
9
13
|
export * from "./SourceDirectory.js";
|
|
10
14
|
export * from "./SourceFile.js";
|
|
15
|
+
export * from "./StatementList.jsx";
|
|
16
|
+
export * from "./Switch.jsx";
|
|
17
|
+
export * from "./Wrap.jsx";
|
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import { stc } from "../../
|
|
1
|
+
import { stc, sti } from "../../stc.js";
|
|
2
2
|
import * as base from "../index.js";
|
|
3
3
|
|
|
4
|
+
export const Block = stc(base.Block);
|
|
4
5
|
export const Declaration = stc(base.Declaration);
|
|
6
|
+
export const For = stc(base.For);
|
|
5
7
|
export const Indent = stc(base.Indent);
|
|
8
|
+
export const List = stc(base.List);
|
|
9
|
+
export const MemberDeclaration = stc(base.MemberDeclaration);
|
|
10
|
+
export const MemberName = stc(base.MemberName);
|
|
11
|
+
export const MemberScope = stc(base.MemberScope);
|
|
12
|
+
export const Name = stc(base.Name);
|
|
6
13
|
export const Output = stc(base.Output);
|
|
7
14
|
export const Scope = stc(base.Scope);
|
|
15
|
+
export const Show = stc(base.Show);
|
|
16
|
+
export const StatementList = stc(base.StatementList);
|
|
8
17
|
export const SourceDirectory = stc(base.SourceDirectory);
|
|
9
18
|
export const SourceFile = stc(base.SourceFile);
|
|
19
|
+
export const Switch = stc(base.Switch);
|
|
20
|
+
export const Wrap = stc(base.Wrap);
|
|
21
|
+
|
|
22
|
+
export const indent = sti("indent");
|
|
23
|
+
export const hbr = sti("hbr");
|
|
24
|
+
export const sbr = sti("sbr");
|
|
25
|
+
export const lbr = sti("lbr");
|
|
26
|
+
export const br = sti("br");
|
package/src/context/index.ts
CHANGED
package/src/context.ts
CHANGED
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
ComponentDefinition,
|
|
5
5
|
effect,
|
|
6
6
|
getContext,
|
|
7
|
-
untrack,
|
|
8
7
|
} from "./jsx-runtime.js";
|
|
9
8
|
|
|
10
9
|
export interface ComponentContext<T> {
|
|
@@ -49,8 +48,8 @@ export function createContext<T = unknown>(
|
|
|
49
48
|
const rendered = shallowRef();
|
|
50
49
|
effect(() => {
|
|
51
50
|
context!.context![id] = props.value;
|
|
52
|
-
rendered.value =
|
|
53
|
-
});
|
|
51
|
+
rendered.value = () => props.children;
|
|
52
|
+
}, undefined);
|
|
54
53
|
|
|
55
54
|
return rendered.value;
|
|
56
55
|
},
|
package/src/index.ts
CHANGED
package/src/jsx-runtime.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
// Much of the implementations in this file are inspired by vuerx-js
|
|
2
2
|
// See: https://github.com/ryansolid/vuerx-jsx.
|
|
3
3
|
import {
|
|
4
|
+
isReactive,
|
|
4
5
|
pauseTracking,
|
|
6
|
+
proxyRefs,
|
|
5
7
|
Ref,
|
|
6
8
|
resetTracking,
|
|
7
9
|
shallowRef,
|
|
8
10
|
stop,
|
|
11
|
+
toRefs,
|
|
9
12
|
effect as vueEffect,
|
|
10
13
|
} from "@vue/reactivity";
|
|
11
14
|
import { Refkey } from "./refkey.js";
|
|
15
|
+
import { RenderedTextTree } from "./render.js";
|
|
12
16
|
|
|
13
17
|
if ((globalThis as any).ALLOY) {
|
|
14
18
|
throw new Error(
|
|
@@ -31,6 +35,11 @@ export interface Context {
|
|
|
31
35
|
// store random info about the node
|
|
32
36
|
meta?: Record<string, any>;
|
|
33
37
|
|
|
38
|
+
/**
|
|
39
|
+
* A cache of RenderTextTree nodes created within this context,
|
|
40
|
+
* indexed by the component or function which created them.
|
|
41
|
+
*/
|
|
42
|
+
elementCache: ElementCache;
|
|
34
43
|
/**
|
|
35
44
|
* When this context was created by a component, this will
|
|
36
45
|
* be the component that created it.
|
|
@@ -43,21 +52,35 @@ export function getContext() {
|
|
|
43
52
|
return globalContext;
|
|
44
53
|
}
|
|
45
54
|
|
|
46
|
-
export function
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
export function getElementCache() {
|
|
56
|
+
return getContext()!.elementCache;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type ElementCacheKey =
|
|
60
|
+
| ComponentCreator
|
|
61
|
+
| (() => unknown)
|
|
62
|
+
| CustomContext;
|
|
63
|
+
export type ElementCache = Map<ElementCacheKey, RenderedTextTree>;
|
|
64
|
+
|
|
65
|
+
export interface RootOptions {
|
|
66
|
+
componentOwner?: ComponentCreator<any>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function root<T>(fn: (d: Disposable) => T, options?: RootOptions): T {
|
|
70
|
+
const context: Context = {
|
|
71
|
+
componentOwner: options?.componentOwner,
|
|
52
72
|
disposables: [],
|
|
53
73
|
owner: globalContext,
|
|
54
74
|
context: {},
|
|
75
|
+
elementCache: new Map(),
|
|
55
76
|
};
|
|
77
|
+
|
|
78
|
+
globalContext = context;
|
|
56
79
|
let ret;
|
|
57
80
|
try {
|
|
58
81
|
ret = untrack(() =>
|
|
59
82
|
fn(() => {
|
|
60
|
-
for (const d of
|
|
83
|
+
for (const d of context!.disposables) {
|
|
61
84
|
d();
|
|
62
85
|
}
|
|
63
86
|
}),
|
|
@@ -83,28 +106,31 @@ export function memo<T>(fn: () => T, equal?: boolean): () => T {
|
|
|
83
106
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
84
107
|
(!equal || prev !== res) && (o.value = res);
|
|
85
108
|
return res;
|
|
86
|
-
});
|
|
109
|
+
}, undefined as T);
|
|
87
110
|
return () => o.value;
|
|
88
111
|
}
|
|
89
112
|
|
|
90
113
|
export function effect<T>(fn: (prev?: T) => T, current?: T) {
|
|
91
|
-
const context = {
|
|
92
|
-
src: "effect",
|
|
114
|
+
const context: Context = {
|
|
93
115
|
context: {},
|
|
94
116
|
disposables: [] as (() => void)[],
|
|
95
117
|
owner: globalContext,
|
|
96
|
-
|
|
118
|
+
elementCache: new Map(),
|
|
119
|
+
};
|
|
97
120
|
|
|
98
121
|
const cleanupFn = (final: boolean) => {
|
|
99
122
|
const d = context.disposables;
|
|
100
123
|
context.disposables = [];
|
|
101
124
|
for (let k = 0, len = d.length; k < len; k++) d[k]();
|
|
125
|
+
|
|
102
126
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
103
127
|
final && stop(c);
|
|
104
128
|
};
|
|
105
129
|
|
|
130
|
+
onCleanup(() => cleanupFn(true));
|
|
106
131
|
const c = vueEffect(() => {
|
|
107
132
|
cleanupFn(false);
|
|
133
|
+
|
|
108
134
|
const oldContext = globalContext;
|
|
109
135
|
globalContext = context;
|
|
110
136
|
try {
|
|
@@ -112,17 +138,64 @@ export function effect<T>(fn: (prev?: T) => T, current?: T) {
|
|
|
112
138
|
} finally {
|
|
113
139
|
globalContext = oldContext;
|
|
114
140
|
}
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
cleanup(() => cleanupFn(true));
|
|
141
|
+
}, {});
|
|
118
142
|
}
|
|
119
143
|
|
|
120
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Register a cleanup function which is called when the current reactive scope
|
|
146
|
+
* is recalculated or disposed. This is useful to clean up any side effects
|
|
147
|
+
* created in the reactive scope.
|
|
148
|
+
*
|
|
149
|
+
* @remarks
|
|
150
|
+
*
|
|
151
|
+
* When onCleanup is called inside a component definition, the provided function
|
|
152
|
+
* is called when the component is removed from the tree. This can be useful to
|
|
153
|
+
* clean up any side effects created as a result of rendering the component. For
|
|
154
|
+
* example, if rendering a component creates a symbol, `onCleanup` can be used
|
|
155
|
+
* to remove the symbol when the component is removed from the tree.
|
|
156
|
+
*
|
|
157
|
+
* When onCleanup is called inside a memo or effect, the function is called when
|
|
158
|
+
* the effect is refreshed (e.g. when a memo or computed recalculates) or
|
|
159
|
+
* disposed (e.g. it is no longer needed because it is attached to a component
|
|
160
|
+
* which was removed).
|
|
161
|
+
*/
|
|
162
|
+
export function onCleanup(fn: Disposable) {
|
|
121
163
|
if (globalContext != null) {
|
|
122
164
|
globalContext.disposables.push(fn);
|
|
123
165
|
}
|
|
124
166
|
}
|
|
125
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Create a custom reactive context for the children returned by
|
|
170
|
+
* the provided context.
|
|
171
|
+
*/
|
|
172
|
+
export interface CustomContext {
|
|
173
|
+
[CUSTOM_CONTEXT_SYM]: true;
|
|
174
|
+
useCustomContext: (useCb: CustomContextChildrenCallback) => void;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export type CustomContextChildrenCallback = (child: Children) => void;
|
|
178
|
+
const CUSTOM_CONTEXT_SYM = Symbol();
|
|
179
|
+
|
|
180
|
+
export function createCustomContext(
|
|
181
|
+
useCallback: (useChildren: CustomContextChildrenCallback) => void,
|
|
182
|
+
): CustomContext {
|
|
183
|
+
return {
|
|
184
|
+
[CUSTOM_CONTEXT_SYM]: true,
|
|
185
|
+
useCustomContext(useCb: (children: Children) => void): void {
|
|
186
|
+
useCallback(useCb);
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function isCustomContext(child: Children): child is CustomContext {
|
|
192
|
+
return (
|
|
193
|
+
typeof child === "object" &&
|
|
194
|
+
child !== null &&
|
|
195
|
+
Object.hasOwn(child, CUSTOM_CONTEXT_SYM)
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
126
199
|
export type Child =
|
|
127
200
|
| string
|
|
128
201
|
| boolean
|
|
@@ -131,23 +204,24 @@ export type Child =
|
|
|
131
204
|
| null
|
|
132
205
|
| void
|
|
133
206
|
| (() => Children)
|
|
134
|
-
| Child[]
|
|
135
207
|
| Ref
|
|
136
|
-
| Refkey
|
|
208
|
+
| Refkey
|
|
209
|
+
| CustomContext
|
|
210
|
+
| IntrinsicElement;
|
|
137
211
|
|
|
138
|
-
export type Children = Child |
|
|
212
|
+
export type Children = Child | Children[];
|
|
139
213
|
export type Props = Record<string, any>;
|
|
140
214
|
|
|
141
215
|
export interface ComponentDefinition<TProps = Props> {
|
|
142
|
-
(props: TProps):
|
|
216
|
+
(props: TProps): Children;
|
|
143
217
|
}
|
|
144
218
|
export interface Component<TProps = Props> {
|
|
145
|
-
(props: TProps):
|
|
219
|
+
(props: TProps): Children;
|
|
146
220
|
tag?: symbol;
|
|
147
221
|
}
|
|
148
222
|
export interface ComponentCreator<TProps = Props> {
|
|
149
223
|
component: Component<TProps>;
|
|
150
|
-
():
|
|
224
|
+
(): Children;
|
|
151
225
|
props: Props;
|
|
152
226
|
tag?: symbol;
|
|
153
227
|
}
|
|
@@ -219,8 +293,31 @@ export function isComponentCreator(item: unknown): item is ComponentCreator {
|
|
|
219
293
|
*/
|
|
220
294
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
221
295
|
export namespace JSX {
|
|
222
|
-
export interface IntrinsicElements {
|
|
223
|
-
|
|
296
|
+
export interface IntrinsicElements {
|
|
297
|
+
group: { shouldBreak?: boolean; id?: symbol; children: Children };
|
|
298
|
+
line: {};
|
|
299
|
+
br: {};
|
|
300
|
+
hardline: {};
|
|
301
|
+
hbr: {};
|
|
302
|
+
softline: {};
|
|
303
|
+
sbr: {};
|
|
304
|
+
literalline: {};
|
|
305
|
+
lbr: {};
|
|
306
|
+
indent: { children: Children };
|
|
307
|
+
indentIfBreak: { children: Children; groupId: symbol; negate?: boolean };
|
|
308
|
+
fill: { children: Children };
|
|
309
|
+
breakParent: {};
|
|
310
|
+
ifBreak: { children: Children; flatContents?: Children; groupId?: symbol };
|
|
311
|
+
lineSuffix: { children: Children };
|
|
312
|
+
lineSuffixBoundary: {};
|
|
313
|
+
dedent: { children: Children };
|
|
314
|
+
align:
|
|
315
|
+
| { children: Children; width: number }
|
|
316
|
+
| { children: Children; string: string };
|
|
317
|
+
markAsRoot: { children: Children };
|
|
318
|
+
dedentToRoot: { children: Children };
|
|
319
|
+
}
|
|
320
|
+
export type ElementType = string | ComponentDefinition<any>;
|
|
224
321
|
export type Element = Children;
|
|
225
322
|
export interface ElementChildrenAttribute {
|
|
226
323
|
children: {};
|
|
@@ -258,6 +355,76 @@ export function taggedComponent<TProps = Props>(
|
|
|
258
355
|
return component;
|
|
259
356
|
}
|
|
260
357
|
|
|
358
|
+
export const intrinsicElementKey = Symbol();
|
|
359
|
+
|
|
360
|
+
export type IndentIntrinsicElement = IntrinsicElementBase<"indent">;
|
|
361
|
+
export type IndentIfBreakIntrinsicElement =
|
|
362
|
+
IntrinsicElementBase<"indentIfBreak">;
|
|
363
|
+
export type BrIntrinsicElement = IntrinsicElementBase<"br">;
|
|
364
|
+
export type LineIntrinsicElement = IntrinsicElementBase<"line">;
|
|
365
|
+
export type HbrIntrinsicElement = IntrinsicElementBase<"hbr">;
|
|
366
|
+
export type HardlineIntrinsicElement = IntrinsicElementBase<"hardline">;
|
|
367
|
+
export type SbrIntrinsicElement = IntrinsicElementBase<"sbr">;
|
|
368
|
+
export type SoftlineIntrinsicElement = IntrinsicElementBase<"softline">;
|
|
369
|
+
export type GroupIntrinsicElement = IntrinsicElementBase<"group">;
|
|
370
|
+
export type AlignIntrinsicElement = IntrinsicElementBase<"align">;
|
|
371
|
+
export type FillIntrinsicElement = IntrinsicElementBase<"fill">;
|
|
372
|
+
export type BreakParentIntrinsicElement = IntrinsicElementBase<"breakParent">;
|
|
373
|
+
export type LineSuffixIntrinsicElement = IntrinsicElementBase<"lineSuffix">;
|
|
374
|
+
export type LineSuffixBoundaryIntrinsicElement =
|
|
375
|
+
IntrinsicElementBase<"lineSuffixBoundary">;
|
|
376
|
+
export type DedentIntrinsicElement = IntrinsicElementBase<"dedent">;
|
|
377
|
+
export type DedentToRootIntrinsicElement = IntrinsicElementBase<"dedentToRoot">;
|
|
378
|
+
export type MarkAsRootIntrinsicElement = IntrinsicElementBase<"markAsRoot">;
|
|
379
|
+
export type LiterallineIntrinsicElement = IntrinsicElementBase<"literalline">;
|
|
380
|
+
export type LbrIntrinsicElement = IntrinsicElementBase<"lbr">;
|
|
381
|
+
export type IfBreakIntrinsicElement = IntrinsicElementBase<"ifBreak">;
|
|
382
|
+
|
|
383
|
+
export type IntrinsicElement =
|
|
384
|
+
| IndentIntrinsicElement
|
|
385
|
+
| IndentIfBreakIntrinsicElement
|
|
386
|
+
| BrIntrinsicElement
|
|
387
|
+
| LineIntrinsicElement
|
|
388
|
+
| HbrIntrinsicElement
|
|
389
|
+
| HardlineIntrinsicElement
|
|
390
|
+
| SbrIntrinsicElement
|
|
391
|
+
| SoftlineIntrinsicElement
|
|
392
|
+
| GroupIntrinsicElement
|
|
393
|
+
| AlignIntrinsicElement
|
|
394
|
+
| FillIntrinsicElement
|
|
395
|
+
| BreakParentIntrinsicElement
|
|
396
|
+
| LineSuffixIntrinsicElement
|
|
397
|
+
| LineSuffixBoundaryIntrinsicElement
|
|
398
|
+
| DedentIntrinsicElement
|
|
399
|
+
| LiterallineIntrinsicElement
|
|
400
|
+
| LbrIntrinsicElement
|
|
401
|
+
| DedentToRootIntrinsicElement
|
|
402
|
+
| MarkAsRootIntrinsicElement
|
|
403
|
+
| IfBreakIntrinsicElement;
|
|
404
|
+
|
|
405
|
+
export interface IntrinsicElementBase<
|
|
406
|
+
TKey extends keyof JSX.IntrinsicElements = keyof JSX.IntrinsicElements,
|
|
407
|
+
> {
|
|
408
|
+
[intrinsicElementKey]: true;
|
|
409
|
+
name: TKey;
|
|
410
|
+
props: JSX.IntrinsicElements[TKey];
|
|
411
|
+
}
|
|
412
|
+
export function createIntrinsic<TKey extends keyof JSX.IntrinsicElements>(
|
|
413
|
+
name: TKey,
|
|
414
|
+
props: JSX.IntrinsicElements[TKey],
|
|
415
|
+
): IntrinsicElementBase<TKey> {
|
|
416
|
+
return {
|
|
417
|
+
[intrinsicElementKey]: true,
|
|
418
|
+
name,
|
|
419
|
+
props,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export function isIntrinsicElement(type: unknown): type is IntrinsicElement {
|
|
424
|
+
return (
|
|
425
|
+
typeof type === "object" && type !== null && intrinsicElementKey in type
|
|
426
|
+
);
|
|
427
|
+
}
|
|
261
428
|
export function mergeProps<T, U>(source: T, source1: U): T & U;
|
|
262
429
|
export function mergeProps<T, U, V>(
|
|
263
430
|
source: T,
|
|
@@ -296,6 +463,61 @@ export function mergeProps(...sources: any): any {
|
|
|
296
463
|
return target;
|
|
297
464
|
}
|
|
298
465
|
|
|
466
|
+
export type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
|
|
467
|
+
...{
|
|
468
|
+
[P in keyof K]: P extends `${number}` ?
|
|
469
|
+
Pick<T, Extract<K[P], readonly (keyof T)[]>[number]>
|
|
470
|
+
: never;
|
|
471
|
+
},
|
|
472
|
+
{ [P in keyof T as Exclude<P, K[number][number]>]: T[P] },
|
|
473
|
+
];
|
|
474
|
+
|
|
475
|
+
export function splitProps<
|
|
476
|
+
T extends Record<any, any>,
|
|
477
|
+
K extends [readonly (keyof T)[], ...(readonly (keyof T)[])[]],
|
|
478
|
+
>(props: T, ...keys: K): SplitProps<T, K> {
|
|
479
|
+
if (isReactive(props)) {
|
|
480
|
+
const refs = untrack(() => toRefs(props));
|
|
481
|
+
const remainingKeys = new Set(Object.keys(refs));
|
|
482
|
+
|
|
483
|
+
const result: any = keys.map((keySet) => {
|
|
484
|
+
const resultSet: any = {};
|
|
485
|
+
for (const key of keySet) {
|
|
486
|
+
resultSet[key] = refs[key];
|
|
487
|
+
remainingKeys.delete(key as string);
|
|
488
|
+
}
|
|
489
|
+
return proxyRefs(resultSet);
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
const remaining: any = {};
|
|
493
|
+
for (const key of remainingKeys) {
|
|
494
|
+
remaining[key] = refs[key];
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return [...result, proxyRefs(remaining)] as any;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const descriptors = Object.getOwnPropertyDescriptors(props);
|
|
501
|
+
const remainingKeys = new Set(Object.keys(descriptors));
|
|
502
|
+
const result: any = keys.map((keySet) => {
|
|
503
|
+
const resultSet: any = {};
|
|
504
|
+
for (const key of keySet) {
|
|
505
|
+
if (key in descriptors) {
|
|
506
|
+
Object.defineProperty(resultSet, key, descriptors[key]);
|
|
507
|
+
remainingKeys.delete(key as string);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return resultSet;
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
const remaining: any = {};
|
|
514
|
+
for (const key of remainingKeys) {
|
|
515
|
+
Object.defineProperty(remaining, key, descriptors[key]);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return [...result, remaining] as any;
|
|
519
|
+
}
|
|
520
|
+
|
|
299
521
|
function shouldDebug() {
|
|
300
522
|
return typeof process !== "undefined" && !!process.env?.ALLOY_DEBUG;
|
|
301
523
|
}
|