@alloy-js/core 0.7.0 → 0.9.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 (131) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/babel.config.cjs +1 -4
  3. package/dist/src/binder.d.ts +14 -12
  4. package/dist/src/binder.d.ts.map +1 -1
  5. package/dist/src/binder.js +26 -23
  6. package/dist/src/binder.js.map +1 -1
  7. package/dist/src/code.d.ts +11 -2
  8. package/dist/src/code.d.ts.map +1 -1
  9. package/dist/src/code.js +27 -2
  10. package/dist/src/code.js.map +1 -1
  11. package/dist/src/components/Block.d.ts +2 -2
  12. package/dist/src/components/Block.d.ts.map +1 -1
  13. package/dist/src/components/Block.js +6 -5
  14. package/dist/src/components/Block.js.map +1 -1
  15. package/dist/src/components/Declaration.d.ts +31 -7
  16. package/dist/src/components/Declaration.d.ts.map +1 -1
  17. package/dist/src/components/Declaration.js +15 -7
  18. package/dist/src/components/Declaration.js.map +1 -1
  19. package/dist/src/components/For.d.ts +8 -2
  20. package/dist/src/components/For.d.ts.map +1 -1
  21. package/dist/src/components/For.js +2 -3
  22. package/dist/src/components/For.js.map +1 -1
  23. package/dist/src/components/Indent.d.ts +29 -1
  24. package/dist/src/components/Indent.d.ts.map +1 -1
  25. package/dist/src/components/Indent.js +7 -2
  26. package/dist/src/components/Indent.js.map +1 -1
  27. package/dist/src/components/List.d.ts +7 -3
  28. package/dist/src/components/List.d.ts.map +1 -1
  29. package/dist/src/components/List.js +1 -16
  30. package/dist/src/components/List.js.map +1 -1
  31. package/dist/src/components/MemberDeclaration.d.ts +35 -5
  32. package/dist/src/components/MemberDeclaration.d.ts.map +1 -1
  33. package/dist/src/components/MemberDeclaration.js +18 -7
  34. package/dist/src/components/MemberDeclaration.js.map +1 -1
  35. package/dist/src/components/MemberScope.d.ts +2 -0
  36. package/dist/src/components/MemberScope.d.ts.map +1 -1
  37. package/dist/src/components/MemberScope.js +2 -0
  38. package/dist/src/components/MemberScope.js.map +1 -1
  39. package/dist/src/components/Prose.d.ts +10 -0
  40. package/dist/src/components/Prose.d.ts.map +1 -0
  41. package/dist/src/components/Prose.js +23 -0
  42. package/dist/src/components/Prose.js.map +1 -0
  43. package/dist/src/components/Scope.d.ts +33 -2
  44. package/dist/src/components/Scope.d.ts.map +1 -1
  45. package/dist/src/components/Scope.js +20 -4
  46. package/dist/src/components/Scope.js.map +1 -1
  47. package/dist/src/components/SourceFile.d.ts +5 -0
  48. package/dist/src/components/SourceFile.d.ts.map +1 -1
  49. package/dist/src/components/SourceFile.js +10 -1
  50. package/dist/src/components/SourceFile.js.map +1 -1
  51. package/dist/src/components/index.d.ts +1 -0
  52. package/dist/src/components/index.d.ts.map +1 -1
  53. package/dist/src/components/index.js +1 -0
  54. package/dist/src/components/index.js.map +1 -1
  55. package/dist/src/components/stc/index.d.ts +19 -95
  56. package/dist/src/components/stc/index.d.ts.map +1 -1
  57. package/dist/src/components/stc/index.js +3 -6
  58. package/dist/src/components/stc/index.js.map +1 -1
  59. package/dist/src/components/stc/sti.d.ts +9 -0
  60. package/dist/src/components/stc/sti.d.ts.map +1 -0
  61. package/dist/src/components/stc/sti.js +10 -0
  62. package/dist/src/components/stc/sti.js.map +1 -0
  63. package/dist/src/context/assignment.d.ts +6 -0
  64. package/dist/src/context/assignment.d.ts.map +1 -1
  65. package/dist/src/context/assignment.js +7 -0
  66. package/dist/src/context/assignment.js.map +1 -1
  67. package/dist/src/context.d.ts +2 -0
  68. package/dist/src/context.d.ts.map +1 -1
  69. package/dist/src/context.js +12 -9
  70. package/dist/src/context.js.map +1 -1
  71. package/dist/src/index.d.ts +2 -1
  72. package/dist/src/index.d.ts.map +1 -1
  73. package/dist/src/index.js +2 -1
  74. package/dist/src/index.js.map +1 -1
  75. package/dist/src/jsx-runtime.d.ts +93 -2
  76. package/dist/src/jsx-runtime.d.ts.map +1 -1
  77. package/dist/src/jsx-runtime.js +51 -3
  78. package/dist/src/jsx-runtime.js.map +1 -1
  79. package/dist/src/stc.d.ts +5 -7
  80. package/dist/src/stc.d.ts.map +1 -1
  81. package/dist/src/stc.js +11 -23
  82. package/dist/src/stc.js.map +1 -1
  83. package/dist/src/sti.d.ts +11 -0
  84. package/dist/src/sti.d.ts.map +1 -0
  85. package/dist/src/sti.js +31 -0
  86. package/dist/src/sti.js.map +1 -0
  87. package/dist/src/tap.d.ts +69 -6
  88. package/dist/src/tap.d.ts.map +1 -1
  89. package/dist/src/tap.js +70 -0
  90. package/dist/src/tap.js.map +1 -1
  91. package/dist/src/utils.d.ts +5 -0
  92. package/dist/src/utils.d.ts.map +1 -1
  93. package/dist/src/utils.js +20 -0
  94. package/dist/src/utils.js.map +1 -1
  95. package/dist/test/components/prose.test.d.ts +2 -0
  96. package/dist/test/components/prose.test.d.ts.map +1 -0
  97. package/dist/test/props-with-defaults.test.d.ts +2 -0
  98. package/dist/test/props-with-defaults.test.d.ts.map +1 -0
  99. package/dist/tsconfig.tsbuildinfo +1 -1
  100. package/package.json +3 -3
  101. package/src/binder.ts +44 -29
  102. package/src/code.ts +37 -3
  103. package/src/components/Block.tsx +3 -6
  104. package/src/components/Declaration.tsx +43 -11
  105. package/src/components/For.tsx +16 -4
  106. package/src/components/Indent.tsx +38 -5
  107. package/src/components/List.tsx +14 -40
  108. package/src/components/MemberDeclaration.tsx +51 -12
  109. package/src/components/MemberScope.tsx +2 -0
  110. package/src/components/Prose.tsx +35 -0
  111. package/src/components/Scope.tsx +45 -5
  112. package/src/components/SourceFile.tsx +10 -0
  113. package/src/components/index.tsx +1 -0
  114. package/src/components/stc/index.ts +3 -6
  115. package/src/components/stc/sti.ts +10 -0
  116. package/src/context/assignment.ts +7 -1
  117. package/src/context.ts +15 -11
  118. package/src/index.ts +3 -0
  119. package/src/jsx-runtime.ts +168 -3
  120. package/src/stc.ts +38 -59
  121. package/src/sti.ts +63 -0
  122. package/src/tap.ts +69 -6
  123. package/src/{utils.ts → utils.tsx} +45 -0
  124. package/temp/api.json +1657 -440
  125. package/test/components/declaration.test.tsx +1 -1
  126. package/test/components/prose.test.tsx +36 -0
  127. package/test/components/source-file.test.tsx +17 -0
  128. package/test/control-flow/for.test.tsx +17 -0
  129. package/test/props-with-defaults.test.ts +97 -0
  130. package/test/symbols.test.ts +0 -25
  131. 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
- export interface Handler<T> {
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?: Handler<T>,
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?: Handler<TSymbol>) {
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?: Handler<TSymbol>,
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?: Handler<TScope>,
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
- export function createSourceFileTap(handler?: Handler<SourceFileContext>) {
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
+ }