@alloy-js/core 0.6.0 → 0.8.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 +37 -0
- package/babel.config.cjs +1 -4
- package/dist/src/binder.d.ts +15 -13
- package/dist/src/binder.d.ts.map +1 -1
- package/dist/src/binder.js +34 -34
- package/dist/src/binder.js.map +1 -1
- package/dist/src/code.d.ts +11 -2
- package/dist/src/code.d.ts.map +1 -1
- package/dist/src/code.js +27 -2
- package/dist/src/code.js.map +1 -1
- package/dist/src/components/Block.d.ts +2 -2
- package/dist/src/components/Block.d.ts.map +1 -1
- package/dist/src/components/Block.js +6 -5
- package/dist/src/components/Block.js.map +1 -1
- package/dist/src/components/Declaration.d.ts +31 -7
- package/dist/src/components/Declaration.d.ts.map +1 -1
- package/dist/src/components/Declaration.js +15 -7
- package/dist/src/components/Declaration.js.map +1 -1
- package/dist/src/components/For.d.ts +6 -0
- package/dist/src/components/For.d.ts.map +1 -1
- package/dist/src/components/For.js +2 -3
- package/dist/src/components/For.js.map +1 -1
- package/dist/src/components/Indent.d.ts +29 -1
- package/dist/src/components/Indent.d.ts.map +1 -1
- package/dist/src/components/Indent.js +7 -2
- package/dist/src/components/Indent.js.map +1 -1
- package/dist/src/components/List.d.ts +7 -3
- package/dist/src/components/List.d.ts.map +1 -1
- package/dist/src/components/List.js +1 -16
- package/dist/src/components/List.js.map +1 -1
- package/dist/src/components/MemberDeclaration.d.ts +35 -5
- package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
- package/dist/src/components/MemberDeclaration.js +18 -7
- package/dist/src/components/MemberDeclaration.js.map +1 -1
- package/dist/src/components/MemberScope.d.ts +2 -0
- package/dist/src/components/MemberScope.d.ts.map +1 -1
- package/dist/src/components/MemberScope.js +2 -0
- package/dist/src/components/MemberScope.js.map +1 -1
- package/dist/src/components/Prose.d.ts +10 -0
- package/dist/src/components/Prose.d.ts.map +1 -0
- package/dist/src/components/Prose.js +23 -0
- package/dist/src/components/Prose.js.map +1 -0
- package/dist/src/components/Scope.d.ts +33 -2
- package/dist/src/components/Scope.d.ts.map +1 -1
- package/dist/src/components/Scope.js +20 -4
- package/dist/src/components/Scope.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts +5 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +10 -1
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +1 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/components/stc/index.d.ts +19 -95
- package/dist/src/components/stc/index.d.ts.map +1 -1
- package/dist/src/components/stc/index.js +3 -6
- package/dist/src/components/stc/index.js.map +1 -1
- package/dist/src/components/stc/sti.d.ts +9 -0
- package/dist/src/components/stc/sti.d.ts.map +1 -0
- package/dist/src/components/stc/sti.js +10 -0
- package/dist/src/components/stc/sti.js.map +1 -0
- package/dist/src/context/assignment.d.ts +6 -0
- package/dist/src/context/assignment.d.ts.map +1 -1
- package/dist/src/context/assignment.js +7 -0
- package/dist/src/context/assignment.js.map +1 -1
- package/dist/src/context.d.ts +2 -0
- package/dist/src/context.d.ts.map +1 -1
- package/dist/src/context.js +12 -9
- package/dist/src/context.js.map +1 -1
- 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 +98 -1
- package/dist/src/jsx-runtime.d.ts.map +1 -1
- package/dist/src/jsx-runtime.js +46 -1
- package/dist/src/jsx-runtime.js.map +1 -1
- package/dist/src/render.d.ts.map +1 -1
- package/dist/src/render.js +12 -0
- package/dist/src/render.js.map +1 -1
- package/dist/src/stc.d.ts +5 -7
- package/dist/src/stc.d.ts.map +1 -1
- package/dist/src/stc.js +11 -23
- package/dist/src/stc.js.map +1 -1
- package/dist/src/sti.d.ts +11 -0
- package/dist/src/sti.d.ts.map +1 -0
- package/dist/src/sti.js +31 -0
- package/dist/src/sti.js.map +1 -0
- package/dist/src/tap.d.ts +69 -6
- package/dist/src/tap.d.ts.map +1 -1
- package/dist/src/tap.js +70 -0
- package/dist/src/tap.js.map +1 -1
- package/dist/src/utils.d.ts +5 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +20 -0
- 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/components/prose.test.d.ts +2 -0
- package/dist/test/components/prose.test.d.ts.map +1 -0
- package/dist/test/props-with-defaults.test.d.ts +2 -0
- package/dist/test/props-with-defaults.test.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/binder.ts +46 -40
- package/src/code.ts +37 -3
- package/src/components/Block.tsx +3 -6
- package/src/components/Declaration.tsx +43 -11
- package/src/components/For.tsx +10 -3
- package/src/components/Indent.tsx +38 -5
- package/src/components/List.tsx +14 -40
- package/src/components/MemberDeclaration.tsx +51 -12
- package/src/components/MemberScope.tsx +2 -0
- package/src/components/Prose.tsx +35 -0
- package/src/components/Scope.tsx +45 -5
- package/src/components/SourceFile.tsx +10 -0
- package/src/components/index.tsx +1 -0
- package/src/components/stc/index.ts +3 -6
- package/src/components/stc/sti.ts +10 -0
- package/src/context/assignment.ts +7 -1
- package/src/context.ts +15 -11
- package/src/index.ts +1 -0
- package/src/jsx-runtime.ts +162 -0
- package/src/render.ts +14 -0
- package/src/stc.ts +35 -56
- package/src/sti.ts +63 -0
- package/src/tap.ts +69 -6
- package/src/{utils.ts → utils.tsx} +45 -0
- package/src/write-output.ts +3 -3
- package/temp/api.json +1563 -393
- package/test/components/declaration.test.tsx +1 -1
- package/test/components/prose.test.tsx +36 -0
- package/test/components/source-file.test.tsx +17 -0
- package/test/props-with-defaults.test.ts +97 -0
- package/test/symbols.test.ts +14 -33
- package/vitest.config.ts +2 -10
package/src/tap.ts
CHANGED
|
@@ -7,21 +7,72 @@ import { useScope } from "./context/scope.js";
|
|
|
7
7
|
import { SourceFileContext } from "./context/source-file.js";
|
|
8
8
|
import { ComponentDefinition } from "./jsx-runtime.js";
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* The return value of {@link createTap}, this holds a reference to the tapped
|
|
12
|
+
* value. It will be undefined until the tapped value is initialized.
|
|
13
|
+
*/
|
|
10
14
|
export interface Tap<T> extends ComponentDefinition {
|
|
15
|
+
/** Ref for the tapped value */
|
|
11
16
|
ref: ShallowRef<T | undefined>;
|
|
12
17
|
}
|
|
13
18
|
|
|
19
|
+
/**
|
|
20
|
+
* A function called when the Tap is rendered.
|
|
21
|
+
*
|
|
22
|
+
* @returns The tapped value.
|
|
23
|
+
*/
|
|
14
24
|
export interface Tapper<T> {
|
|
15
25
|
(): T | undefined;
|
|
16
26
|
}
|
|
17
27
|
|
|
18
|
-
|
|
28
|
+
/**
|
|
29
|
+
* A function that is called when the tapped value is available.
|
|
30
|
+
*/
|
|
31
|
+
export interface TapHandler<T> {
|
|
19
32
|
(value: T): void;
|
|
20
33
|
}
|
|
21
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Create a component that when rendered, initializes the tapped value
|
|
37
|
+
* with the provided callback. This is useful for accessing context
|
|
38
|
+
* provided by child components inside a parent component.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* import { type Children, computed, createTap } from "@alloy-js/core";
|
|
43
|
+
*
|
|
44
|
+
* // context we will tap into
|
|
45
|
+
* const SomeContext = createContext<string>();
|
|
46
|
+
*
|
|
47
|
+
* // a component which provides some specific context
|
|
48
|
+
* function MyDeclaration(props: { children: Children }) {
|
|
49
|
+
* return <SomeContext.Provider value="Hello World">
|
|
50
|
+
* {props.children}
|
|
51
|
+
* </SomeContext.Provider>;
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* // a parent component which wants to know about the context set
|
|
55
|
+
* // by its children
|
|
56
|
+
* function MySpecialDeclaration() {
|
|
57
|
+
* const SomeContextTap = createTap(() => useContext(SomeContext));
|
|
58
|
+
*
|
|
59
|
+
* return <>
|
|
60
|
+
* The declaration context is: {SomeContextTap.ref}
|
|
61
|
+
* <MyDeclaration>
|
|
62
|
+
* <SomeContextTap />
|
|
63
|
+
* </MyDeclaration>
|
|
64
|
+
* </>
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @see {@link createDeclarationTap} for tapping {@link DeclarationContext}.
|
|
68
|
+
* @see {@link createMemberTap} for tapping {@link MemberDeclarationContext}.
|
|
69
|
+
* @see {@link createScopeTap} for tapping {@link OutputScope}.
|
|
70
|
+
* @see {@link createSourceFileTap} for tapping {@link SourceFileContext}.
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
22
73
|
export function createTap<T = unknown>(
|
|
23
74
|
tapper: Tapper<T>,
|
|
24
|
-
handler?:
|
|
75
|
+
handler?: TapHandler<T>,
|
|
25
76
|
): Tap<T> {
|
|
26
77
|
const signal = shallowRef<T | undefined>(undefined);
|
|
27
78
|
const fn = () => {
|
|
@@ -38,31 +89,43 @@ export function createTap<T = unknown>(
|
|
|
38
89
|
return fn;
|
|
39
90
|
}
|
|
40
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Create a tap for {@link DeclarationContext}.
|
|
94
|
+
*/
|
|
41
95
|
export function createDeclarationTap<
|
|
42
96
|
TSymbol extends OutputSymbol = OutputSymbol,
|
|
43
|
-
>(handler?:
|
|
97
|
+
>(handler?: TapHandler<TSymbol>) {
|
|
44
98
|
return createTap<TSymbol>(() => {
|
|
45
99
|
return useContext(DeclarationContext) as any;
|
|
46
100
|
}, handler);
|
|
47
101
|
}
|
|
48
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Create a tap for {@link MemberDeclarationContext}.
|
|
105
|
+
*/
|
|
49
106
|
export function createMemberTap<TSymbol extends OutputSymbol = OutputSymbol>(
|
|
50
|
-
handler?:
|
|
107
|
+
handler?: TapHandler<TSymbol>,
|
|
51
108
|
) {
|
|
52
109
|
return createTap<TSymbol>(() => {
|
|
53
110
|
return useContext(MemberDeclarationContext) as any;
|
|
54
111
|
}, handler);
|
|
55
112
|
}
|
|
56
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Create a tap for {@link OutputScope}.
|
|
116
|
+
*/
|
|
57
117
|
export function createScopeTap<TScope extends OutputScope = OutputScope>(
|
|
58
|
-
handler?:
|
|
118
|
+
handler?: TapHandler<TScope>,
|
|
59
119
|
) {
|
|
60
120
|
return createTap<TScope>(() => {
|
|
61
121
|
return useScope() as any;
|
|
62
122
|
}, handler);
|
|
63
123
|
}
|
|
64
124
|
|
|
65
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Create a tap for {@link (SourceFileContext:interface)}.
|
|
127
|
+
*/
|
|
128
|
+
export function createSourceFileTap(handler?: TapHandler<SourceFileContext>) {
|
|
66
129
|
return createTap(() => {
|
|
67
130
|
return useContext(SourceFileContext);
|
|
68
131
|
}, handler);
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
untrack,
|
|
13
13
|
} from "./jsx-runtime.js";
|
|
14
14
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
+
import { BaseListProps } from "./components/List.jsx";
|
|
15
16
|
import { OutputDirectory, OutputFile, render } from "./render.js";
|
|
16
17
|
|
|
17
18
|
export interface JoinOptions {
|
|
@@ -321,3 +322,47 @@ export function traverseOutput(
|
|
|
321
322
|
}
|
|
322
323
|
}
|
|
323
324
|
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Convert a list of props to a joiner and ender for use in {@link (mapJoin:1)}.
|
|
328
|
+
*/
|
|
329
|
+
export function baseListPropsToMapJoinArgs(props: BaseListProps): JoinOptions {
|
|
330
|
+
let joiner, punctuation;
|
|
331
|
+
if ("joiner" in props) {
|
|
332
|
+
joiner = props.joiner;
|
|
333
|
+
} else {
|
|
334
|
+
punctuation =
|
|
335
|
+
props.comma ? ","
|
|
336
|
+
: props.semicolon ? ";"
|
|
337
|
+
: "";
|
|
338
|
+
|
|
339
|
+
joiner = (
|
|
340
|
+
<>
|
|
341
|
+
{punctuation}
|
|
342
|
+
{props.softline ?
|
|
343
|
+
<sbr />
|
|
344
|
+
: props.hardline ?
|
|
345
|
+
<hbr />
|
|
346
|
+
: props.literalline ?
|
|
347
|
+
<lbr />
|
|
348
|
+
: props.line ?
|
|
349
|
+
<br />
|
|
350
|
+
: props.space ?
|
|
351
|
+
<> </>
|
|
352
|
+
: props.doubleHardline ?
|
|
353
|
+
<>
|
|
354
|
+
<hbr />
|
|
355
|
+
<hbr />
|
|
356
|
+
</>
|
|
357
|
+
: <hbr />}
|
|
358
|
+
</>
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const ender =
|
|
363
|
+
"ender" in props ? props.ender
|
|
364
|
+
: props.enderPunctuation ? punctuation
|
|
365
|
+
: undefined;
|
|
366
|
+
|
|
367
|
+
return { joiner, ender };
|
|
368
|
+
}
|
package/src/write-output.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { relative, resolve } from "pathe";
|
|
3
3
|
import { OutputDirectory } from "./render.js";
|
|
4
4
|
import { traverseOutput } from "./utils.js";
|
|
@@ -10,7 +10,7 @@ export function writeOutput(output: OutputDirectory, basePath: string = "") {
|
|
|
10
10
|
traverseOutput(output, {
|
|
11
11
|
visitDirectory(directory) {
|
|
12
12
|
const path = resolve(basePath, directory.path);
|
|
13
|
-
if (
|
|
13
|
+
if (existsSync(path)) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
16
|
// eslint-disable-next-line no-console
|
|
@@ -19,7 +19,7 @@ export function writeOutput(output: OutputDirectory, basePath: string = "") {
|
|
|
19
19
|
},
|
|
20
20
|
visitFile(file) {
|
|
21
21
|
const path = resolve(basePath, file.path);
|
|
22
|
-
if (
|
|
22
|
+
if (existsSync(path)) {
|
|
23
23
|
// eslint-disable-next-line no-console
|
|
24
24
|
console.log("overwrite", relative(process.cwd(), path));
|
|
25
25
|
} else {
|