@alloy-js/json 0.3.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 (85) hide show
  1. package/LICENSE +7 -0
  2. package/api-extractor.json +4 -0
  3. package/babel.config.cjs +4 -0
  4. package/dist/src/components/JsonArray.d.ts +52 -0
  5. package/dist/src/components/JsonArray.d.ts.map +1 -0
  6. package/dist/src/components/JsonArray.js +107 -0
  7. package/dist/src/components/JsonArray.js.map +1 -0
  8. package/dist/src/components/JsonObject.d.ts +63 -0
  9. package/dist/src/components/JsonObject.d.ts.map +1 -0
  10. package/dist/src/components/JsonObject.js +119 -0
  11. package/dist/src/components/JsonObject.js.map +1 -0
  12. package/dist/src/components/JsonValue.d.ts +21 -0
  13. package/dist/src/components/JsonValue.d.ts.map +1 -0
  14. package/dist/src/components/JsonValue.js +49 -0
  15. package/dist/src/components/JsonValue.js.map +1 -0
  16. package/dist/src/components/Reference.d.ts +18 -0
  17. package/dist/src/components/Reference.d.ts.map +1 -0
  18. package/dist/src/components/Reference.js +15 -0
  19. package/dist/src/components/Reference.js.map +1 -0
  20. package/dist/src/components/SourceFile.d.ts +17 -0
  21. package/dist/src/components/SourceFile.d.ts.map +1 -0
  22. package/dist/src/components/SourceFile.js +47 -0
  23. package/dist/src/components/SourceFile.js.map +1 -0
  24. package/dist/src/components/index.d.ts +6 -0
  25. package/dist/src/components/index.d.ts.map +1 -0
  26. package/dist/src/components/index.js +6 -0
  27. package/dist/src/components/index.js.map +1 -0
  28. package/dist/src/components/stc/index.d.ts +30 -0
  29. package/dist/src/components/stc/index.d.ts.map +1 -0
  30. package/dist/src/components/stc/index.js +10 -0
  31. package/dist/src/components/stc/index.js.map +1 -0
  32. package/dist/src/context/JsonFileContext.d.ts +17 -0
  33. package/dist/src/context/JsonFileContext.d.ts.map +1 -0
  34. package/dist/src/context/JsonFileContext.js +10 -0
  35. package/dist/src/context/JsonFileContext.js.map +1 -0
  36. package/dist/src/context/index.d.ts +2 -0
  37. package/dist/src/context/index.d.ts.map +1 -0
  38. package/dist/src/context/index.js +1 -0
  39. package/dist/src/context/index.js.map +1 -0
  40. package/dist/src/index.d.ts +4 -0
  41. package/dist/src/index.d.ts.map +1 -0
  42. package/dist/src/index.js +4 -0
  43. package/dist/src/index.js.map +1 -0
  44. package/dist/src/symbols/index.d.ts +4 -0
  45. package/dist/src/symbols/index.d.ts.map +1 -0
  46. package/dist/src/symbols/index.js +34 -0
  47. package/dist/src/symbols/index.js.map +1 -0
  48. package/dist/src/symbols/json-symbol.d.ts +22 -0
  49. package/dist/src/symbols/json-symbol.d.ts.map +1 -0
  50. package/dist/src/symbols/json-symbol.js +45 -0
  51. package/dist/src/symbols/json-symbol.js.map +1 -0
  52. package/dist/test/array.test.d.ts +2 -0
  53. package/dist/test/array.test.d.ts.map +1 -0
  54. package/dist/test/object.test.d.ts +2 -0
  55. package/dist/test/object.test.d.ts.map +1 -0
  56. package/dist/test/primitives.test.d.ts +2 -0
  57. package/dist/test/primitives.test.d.ts.map +1 -0
  58. package/dist/test/reference.test.d.ts +2 -0
  59. package/dist/test/reference.test.d.ts.map +1 -0
  60. package/dist/test/utils.d.ts +2 -0
  61. package/dist/test/utils.d.ts.map +1 -0
  62. package/dist/tsconfig.tsbuildinfo +1 -0
  63. package/package.json +45 -0
  64. package/src/components/JsonArray.tsx +138 -0
  65. package/src/components/JsonObject.tsx +150 -0
  66. package/src/components/JsonValue.tsx +59 -0
  67. package/src/components/Reference.tsx +23 -0
  68. package/src/components/SourceFile.tsx +48 -0
  69. package/src/components/index.ts +5 -0
  70. package/src/components/stc/index.ts +10 -0
  71. package/src/context/JsonFileContext.ts +28 -0
  72. package/src/context/index.ts +1 -0
  73. package/src/index.ts +3 -0
  74. package/src/symbols/index.ts +43 -0
  75. package/src/symbols/json-symbol.ts +68 -0
  76. package/temp/api.json +1896 -0
  77. package/test/array.test.tsx +31 -0
  78. package/test/object.test.tsx +60 -0
  79. package/test/primitives.test.tsx +16 -0
  80. package/test/reference.test.tsx +173 -0
  81. package/test/utils.tsx +11 -0
  82. package/test/vitest.d.ts +10 -0
  83. package/tsconfig.json +11 -0
  84. package/tsdoc-metadata.json +11 -0
  85. package/vitest.config.ts +18 -0
@@ -0,0 +1,150 @@
1
+ import {
2
+ Children,
3
+ computed,
4
+ mapJoin,
5
+ MemberDeclaration,
6
+ MemberScope,
7
+ OutputSymbolFlags,
8
+ Refkey,
9
+ useMemberDeclaration,
10
+ } from "@alloy-js/core";
11
+ import { createJsonOutputSymbol } from "../symbols/json-symbol.js";
12
+ import { JsonValue } from "./JsonValue.jsx";
13
+
14
+ export interface JsonObjectPropsBase {
15
+ /** The refkey for the JSON array. When provided, this value can be referenced
16
+ * elsewhere via this refkey.
17
+ **/
18
+ refkey?: Refkey;
19
+ }
20
+
21
+ /**
22
+ * Create a JSON object by providing children that define the object's contents.
23
+ * The children should use the {@link JsonObjectProperty} component to define
24
+ * individual object properties.
25
+ */
26
+ export interface JsonObjectPropsWithChildren extends JsonObjectPropsBase {
27
+ /**
28
+ * The contents of the JSON object. Likely to contain
29
+ * {@link JsonObjectProperty} components.
30
+ */
31
+ children: Children;
32
+ }
33
+
34
+ /**
35
+ * Create a JSON object by providing a JS value that will be serialized to JSON.
36
+ */
37
+ export interface JsonObjectPropsWithJsValue extends JsonObjectPropsBase {
38
+ /**
39
+ * The JS value to serialize to JSON. This can be an array of key-value pairs,
40
+ * a map, or a vanilla JS object.
41
+ */
42
+ jsValue: [string, unknown][] | Map<string, unknown> | Record<string, unknown>;
43
+ }
44
+
45
+ export type JsonObjectProps =
46
+ | JsonObjectPropsWithChildren
47
+ | JsonObjectPropsWithJsValue;
48
+
49
+ /**
50
+ * Create a JSON Object. An object can be created by providing a JS object
51
+ * value which will be serialized to JSON. Else, you can provide children that
52
+ * comprise the object's JSON representation.
53
+ *
54
+ * @see {@link @alloy-js/core#MemberScopeContext}
55
+ */
56
+ export function JsonObject(props: JsonObjectProps) {
57
+ const memberSymbol = useMemberDeclaration();
58
+ if (!memberSymbol) {
59
+ throw new Error("Missing assignment symbol.");
60
+ }
61
+ const binder = memberSymbol.binder;
62
+ binder.addStaticMembersToSymbol(memberSymbol);
63
+ if (props.refkey) {
64
+ memberSymbol.refkey = props.refkey;
65
+ }
66
+
67
+ if (!("jsValue" in props)) {
68
+ return <MemberScope owner={memberSymbol}>
69
+ {"{"}
70
+ {props.children}
71
+ {"}"}
72
+ </MemberScope>;
73
+ }
74
+
75
+ const elements = computed(() => {
76
+ const jsValue = props.jsValue;
77
+ let properties: [string, unknown][];
78
+ if (Array.isArray(jsValue)) {
79
+ properties = jsValue;
80
+ } else if (jsValue instanceof Map) {
81
+ properties = [...jsValue.entries()];
82
+ } else if (jsValue !== undefined) {
83
+ properties = globalThis.Object.entries(jsValue);
84
+ } else {
85
+ properties = [];
86
+ }
87
+ const elements = mapJoin(
88
+ properties,
89
+ ([name, value]) => {
90
+ return <JsonObjectProperty name={name} jsValue={value} />;
91
+ },
92
+ { joiner: ",\n" },
93
+ );
94
+
95
+ return elements;
96
+ });
97
+
98
+ return <MemberScope owner={memberSymbol}>
99
+ {"{"}
100
+ {elements}
101
+ {"}"}
102
+ </MemberScope>;
103
+ }
104
+
105
+ /**
106
+ * Create a JSON object property with a name and children. The children become
107
+ * the value of the property.
108
+ */
109
+ export interface ObjectPropertyPropsWithChildren {
110
+ name: string;
111
+ children: Children;
112
+ }
113
+
114
+ /**
115
+ * Create a JSON object property with a name and a JS value. The JS value is
116
+ * serialized to JSON for the property
117
+ */
118
+ export interface ObjectPropertyPropsWithJsValue {
119
+ name: string;
120
+ jsValue: unknown;
121
+ }
122
+
123
+ export type ObjectPropertyProps =
124
+ | ObjectPropertyPropsWithChildren
125
+ | ObjectPropertyPropsWithJsValue;
126
+
127
+ /**
128
+ * Create a JSON object property. A property can be created by providing a JS
129
+ * property value or children that defines the value.
130
+ *
131
+ * @see {@link @alloy-js/core#(MemberDeclarationContext:variable)}
132
+ */
133
+ export function JsonObjectProperty(props: ObjectPropertyProps) {
134
+ const sym = createJsonOutputSymbol({
135
+ name: props.name,
136
+ flags: OutputSymbolFlags.StaticMember,
137
+ });
138
+
139
+ const wrap = (children: Children) => {
140
+ return <MemberDeclaration symbol={sym}>
141
+ "{props.name}": {children}
142
+ </MemberDeclaration>;
143
+ };
144
+
145
+ if (!("jsValue" in props)) {
146
+ return wrap(props.children);
147
+ }
148
+
149
+ return wrap(<JsonValue jsValue={props.jsValue} />);
150
+ }
@@ -0,0 +1,59 @@
1
+ import { Refkey, useMemberDeclaration } from "@alloy-js/core";
2
+ import { JsonArray } from "./JsonArray.jsx";
3
+ import { JsonObject } from "./JsonObject.jsx";
4
+
5
+ export interface JsonValueProps {
6
+ /**
7
+ * The refkey for the JSON value. When provided, this value can be referenced
8
+ * elsewhere via this refkey.
9
+ **/
10
+ refkey?: Refkey;
11
+
12
+ /**
13
+ * The JS value to serialize to JSON. When provided, thi
14
+ */
15
+ jsValue: unknown;
16
+ }
17
+
18
+ /**
19
+ * Create a JSON value from a JS value. When the provided JS value is an object,
20
+ * the {@link JsonObject} component will be used. When the provided JS value is
21
+ * an array, the {@link JsonArray} component will be used. Otherwise, the JS
22
+ * value will be serialized to a string, number, boolean, or null as
23
+ * appropriate.
24
+ */
25
+ export function JsonValue(props: JsonValueProps) {
26
+ if (props.jsValue === null) {
27
+ setMemberRefkey(props.refkey);
28
+ return "null";
29
+ }
30
+
31
+ if (typeof props.jsValue === "number" || typeof props.jsValue === "boolean") {
32
+ setMemberRefkey(props.refkey);
33
+ return String(props.jsValue);
34
+ }
35
+
36
+ if (typeof props.jsValue === "string") {
37
+ setMemberRefkey(props.refkey);
38
+ return `"${props.jsValue}"`;
39
+ }
40
+
41
+ if (Array.isArray(props.jsValue)) {
42
+ return <JsonArray jsValue={props.jsValue} />;
43
+ }
44
+
45
+ if (typeof props.jsValue === "object") {
46
+ return <JsonObject jsValue={props.jsValue as any} />;
47
+ }
48
+
49
+ throw new Error("Cannot emit js value with type of " + typeof props.jsValue);
50
+ }
51
+
52
+ function setMemberRefkey(key?: Refkey) {
53
+ if (!key) return;
54
+ const member = useMemberDeclaration();
55
+ if (!member) {
56
+ throw new Error("Missing member declaration.");
57
+ }
58
+ member.refkey = key;
59
+ }
@@ -0,0 +1,23 @@
1
+ import { Refkey } from "@alloy-js/core";
2
+ import { ref } from "../symbols/index.js";
3
+
4
+ export interface ReferenceProps {
5
+ /**
6
+ * The refkey for the symbol to reference.
7
+ */
8
+ refkey: Refkey;
9
+ }
10
+
11
+ /**
12
+ * Emit a reference to a JSON value by its refkey.
13
+ *
14
+ * @remarks
15
+ *
16
+ * Note that when you place a refkey in a template, the refkey will be converted
17
+ * into a reference automatically. Using this component directly is generally
18
+ * not required or recommended.
19
+ */
20
+ export function Reference(props: ReferenceProps) {
21
+ const reference = ref(props.refkey);
22
+ return <>"{reference}"</>;
23
+ }
@@ -0,0 +1,48 @@
1
+ import {
2
+ Children,
3
+ SourceFile as CoreSourceFile,
4
+ createSourceFileTap,
5
+ MemberDeclaration,
6
+ } from "@alloy-js/core";
7
+ import { JsonFileContext } from "../context/JsonFileContext.js";
8
+ import { createJsonOutputSymbol } from "../symbols/json-symbol.js";
9
+ import { Reference } from "./Reference.jsx";
10
+
11
+ export interface SourceFileProps {
12
+ /** The path for this source file relative to the parent directory */
13
+ path: string;
14
+
15
+ /** The contents of the source file */
16
+ children?: Children;
17
+ }
18
+
19
+ /**
20
+ * Creates a JSON source file which defines a JSON value.
21
+ *
22
+ * @see {@link (JsonFileContext:variable)}
23
+ * @see {@link @alloy-js/core#(MemberDeclarationContext:variable)}
24
+ * @see {@link @alloy-js/core#(SourceFileContext:variable)}
25
+ *
26
+ */
27
+ export function SourceFile(props: SourceFileProps) {
28
+ const jsonValueSym = createJsonOutputSymbol({
29
+ name: "",
30
+ });
31
+
32
+ const fileContext: JsonFileContext = {
33
+ symbol: jsonValueSym,
34
+ path: props.path,
35
+ };
36
+
37
+ const SfTapper = createSourceFileTap((context) => {
38
+ jsonValueSym.name = context.path;
39
+ });
40
+
41
+ return <CoreSourceFile filetype="json" path={props.path} reference={Reference}>
42
+ <SfTapper /><JsonFileContext.Provider value={fileContext}>
43
+ <MemberDeclaration symbol={jsonValueSym}>
44
+ {props.children}
45
+ </MemberDeclaration>
46
+ </JsonFileContext.Provider>
47
+ </CoreSourceFile>;
48
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./JsonArray.jsx";
2
+ export * from "./JsonObject.jsx";
3
+ export * from "./JsonValue.jsx";
4
+ export * from "./Reference.jsx";
5
+ export * from "./SourceFile.jsx";
@@ -0,0 +1,10 @@
1
+ import { stc } from "@alloy-js/core";
2
+ import * as Jsx from "../index.js";
3
+
4
+ export const JsonArray = stc(Jsx.JsonArray);
5
+ export const JsonArrayElement = stc(Jsx.JsonArrayElement);
6
+ export const JsonObject = stc(Jsx.JsonObject);
7
+ export const JsonObjectProperty = stc(Jsx.JsonObjectProperty);
8
+ export const JsonValue = stc(Jsx.JsonValue);
9
+ export const Reference = stc(Jsx.Reference);
10
+ export const SourceFile = stc(Jsx.SourceFile);
@@ -0,0 +1,28 @@
1
+ import {
2
+ ComponentContext,
3
+ createNamedContext,
4
+ useContext,
5
+ } from "@alloy-js/core";
6
+ import { JsonOutputSymbol } from "../symbols/json-symbol.js";
7
+
8
+ export interface JsonFileContext {
9
+ /** The path of the current JSON file. May be different from the path on disk. */
10
+ path?: string;
11
+
12
+ /** The URL of the current JSON file */
13
+ url?: string;
14
+
15
+ /** The symbol for the value in this JSON file */
16
+ symbol: JsonOutputSymbol;
17
+ }
18
+
19
+ /**
20
+ * Provides information about the current JSON source file. This context is used
21
+ * to allow references between JSON files.
22
+ */
23
+ export const JsonFileContext: ComponentContext<JsonFileContext> =
24
+ createNamedContext<JsonFileContext>("JsonFile");
25
+
26
+ export function useJsonFileContext(): JsonFileContext | undefined {
27
+ return useContext(JsonFileContext);
28
+ }
@@ -0,0 +1 @@
1
+ export * from "./JsonFileContext.js";
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./components/index.js";
2
+ export * from "./context/index.js";
3
+ export * from "./symbols/index.js";
@@ -0,0 +1,43 @@
1
+ import { memo, OutputScope, Refkey, resolve } from "@alloy-js/core";
2
+ import { dirname, relative } from "pathe";
3
+ import { useJsonFileContext } from "../context/JsonFileContext.js";
4
+ import { JsonOutputSymbol } from "./json-symbol.js";
5
+ export * from "./json-symbol.js";
6
+
7
+ export function ref(refkey: Refkey) {
8
+ const sourceFile = useJsonFileContext();
9
+ if (!sourceFile) {
10
+ return "<Unresolved Symbol>";
11
+ }
12
+
13
+ const resolveResult = resolve<OutputScope, JsonOutputSymbol>(refkey);
14
+
15
+ return memo(() => {
16
+ if (resolveResult.value === undefined) {
17
+ return "<Unresolved Symbol>";
18
+ }
19
+ const { targetDeclaration, memberPath } = resolveResult.value;
20
+
21
+ let ref = "";
22
+ if (targetDeclaration !== sourceFile.symbol) {
23
+ const currentPath = sourceFile.symbol.name;
24
+ const targetPath = targetDeclaration.name;
25
+ ref += relative(dirname(currentPath), targetPath);
26
+ }
27
+
28
+ ref += "#";
29
+
30
+ if (!memberPath || memberPath.length === 0) {
31
+ return ref;
32
+ }
33
+
34
+ ref += "/";
35
+
36
+ ref += memberPath
37
+ .slice(1)
38
+ .map((segment) => segment.name)
39
+ .join("/");
40
+
41
+ return ref;
42
+ });
43
+ }
@@ -0,0 +1,68 @@
1
+ import {
2
+ CreateSymbolOptions,
3
+ OutputScope,
4
+ OutputSymbol,
5
+ OutputSymbolFlags,
6
+ useBinder,
7
+ useMemberScope,
8
+ } from "@alloy-js/core";
9
+
10
+ // prettier-ignore
11
+ export enum JsonSymbolFlags {
12
+ None = 0,
13
+ Object = 1 << 0,
14
+ Array = 1 << 1,
15
+ }
16
+
17
+ /**
18
+ * JsonOutputSymbol is created for JSON Values with a refkey. For JSON objects
19
+ * and arrays, the symbol will have the `StaticMemberContainer` flag. For JSON
20
+ * values that are members of an object or an array, the symbol will have the
21
+ * `StaticMember` flag.
22
+ */
23
+ export interface JsonOutputSymbol extends OutputSymbol {
24
+ scope: OutputScope;
25
+ jsonFlags: JsonSymbolFlags;
26
+ }
27
+
28
+ export interface createJsonSymbolOptions extends CreateSymbolOptions {
29
+ jsonFlags?: JsonSymbolFlags;
30
+ }
31
+
32
+ export function createJsonOutputSymbol(
33
+ options: createJsonSymbolOptions,
34
+ ): JsonOutputSymbol {
35
+ const scope =
36
+ options.scope ??
37
+ (useDefaultScope(options.flags) as OutputScope) ??
38
+ useBinder().globalScope;
39
+
40
+ const binder = scope.binder;
41
+ const sym = binder.createSymbol<JsonOutputSymbol>({
42
+ name: options.name,
43
+ scope,
44
+ refkey: options.refkey,
45
+ flags: options.flags ?? OutputSymbolFlags.None,
46
+ jsonFlags: options.jsonFlags ?? JsonSymbolFlags.None,
47
+ });
48
+
49
+ return sym;
50
+ }
51
+
52
+ export function useDefaultScope(
53
+ flags: OutputSymbolFlags = OutputSymbolFlags.None,
54
+ ) {
55
+ if ((flags & OutputSymbolFlags.Member) === 0) {
56
+ return useBinder().globalScope;
57
+ } else {
58
+ const memberScope = useMemberScope();
59
+ if (!memberScope) {
60
+ throw new Error("Cannot declare member symbols without a member scope");
61
+ }
62
+ if (flags & OutputSymbolFlags.InstanceMember) {
63
+ return memberScope.instanceMembers;
64
+ } else {
65
+ return memberScope.staticMembers;
66
+ }
67
+ }
68
+ }