@alloy-js/json 0.4.0 → 0.6.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/package.json CHANGED
@@ -1,14 +1,12 @@
1
1
  {
2
2
  "name": "@alloy-js/json",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
7
- "development": "./src/index.ts",
8
7
  "import": "./dist/src/index.js"
9
8
  },
10
9
  "./stc": {
11
- "development": "./src/components/stc/index.ts",
12
10
  "import": "./dist/src/components/stc/index.js"
13
11
  }
14
12
  },
@@ -17,7 +15,7 @@
17
15
  "license": "MIT",
18
16
  "dependencies": {
19
17
  "pathe": "^1.1.2",
20
- "@alloy-js/core": "~0.4.0"
18
+ "@alloy-js/core": "~0.6.0"
21
19
  },
22
20
  "devDependencies": {
23
21
  "@babel/cli": "^7.24.7",
@@ -28,7 +26,7 @@
28
26
  "concurrently": "^8.2.2",
29
27
  "typescript": "^5.7.3",
30
28
  "vitest": "^3.0.4",
31
- "@alloy-js/babel-preset": "~0.1.1"
29
+ "@alloy-js/babel-preset": "~0.2.0"
32
30
  },
33
31
  "type": "module",
34
32
  "scripts": {
@@ -1,8 +1,11 @@
1
1
  import {
2
2
  Children,
3
- mapJoin,
3
+ For,
4
+ Indent,
5
+ List,
4
6
  MemberDeclaration,
5
7
  MemberScope,
8
+ onCleanup,
6
9
  OutputSymbolFlags,
7
10
  Refkey,
8
11
  useMemberDeclaration,
@@ -62,56 +65,61 @@ export function JsonArray(props: JsonArrayProps) {
62
65
  }
63
66
 
64
67
  if (!("jsValue" in props)) {
65
- return <MemberScope owner={memberSymbol}>
66
- [
67
- {props.children}
68
- ]
69
- </MemberScope>;
68
+ return (
69
+ <MemberScope owner={memberSymbol}>
70
+ <group>
71
+ [
72
+ <Indent break="soft" trailingBreak>
73
+ <List comma line>
74
+ {props.children}
75
+ </List>
76
+ </Indent>
77
+ ]
78
+ </group>
79
+ </MemberScope>
80
+ );
70
81
  }
71
82
 
72
83
  const jsValue = props.jsValue ?? [];
73
84
 
74
- if (jsValue.length === 0) {
75
- return "[]";
76
- }
77
-
78
- const elements = mapJoin(
79
- jsValue,
80
- (value) => {
81
- return <JsonArrayElement><JsonValue jsValue={value} /></JsonArrayElement>;
82
- },
83
- { joiner: ",\n" },
85
+ return (
86
+ <MemberScope owner={memberSymbol}>
87
+ <group>
88
+ [
89
+ <Indent break="soft" trailingBreak>
90
+ <For each={jsValue} comma line>
91
+ {(value) => <JsonArrayElement jsValue={value} />}
92
+ </For>
93
+ </Indent>
94
+ ]
95
+ </group>
96
+ </MemberScope>
84
97
  );
98
+ }
85
99
 
86
- let contents;
87
- if (jsValue.length === 1) {
88
- contents = <>[{elements}]</>;
89
- } else {
90
- contents = <>
91
- [
92
- {elements}
93
- ]
94
- </>;
95
- }
96
-
97
- return <MemberScope owner={memberSymbol}>
98
- {contents}
99
- </MemberScope>;
100
+ /**
101
+ * Create a JSON Array element by providing a JS value that will be serialized
102
+ * to JSON.
103
+ */
104
+ export interface JsonArrayElementPropsWithJsValue {
105
+ jsValue: unknown;
100
106
  }
101
107
 
102
- export interface JsonArrayElementProps {
108
+ /**
109
+ * Create a JSON Array element by providing children.
110
+ */
111
+ export interface JsonArrayElementPropsWithChildren {
103
112
  children: Children;
104
113
  }
105
114
 
115
+ export type JsonArrayElementProps =
116
+ | JsonArrayElementPropsWithJsValue
117
+ | JsonArrayElementPropsWithChildren;
118
+
106
119
  /**
107
120
  * Create a JSON Array element. This component should be used inside of a
108
- * {@link JsonArray} component.
109
- *
110
- * @remarks
111
- *
112
- * It does not add any syntax, but is required in
113
- * order to establish the correct scope for the array element, allowing
114
- * references to array elements to work properly.
121
+ * {@link JsonArray} component. Can either be provided `children`, or a JS
122
+ * value which will be serialized to JSON.
115
123
  *
116
124
  * @see {@link @alloy-js/core#(MemberDeclarationContext:variable)}
117
125
  */
@@ -132,7 +140,16 @@ export function JsonArrayElement(props: JsonArrayElementProps) {
132
140
  flags: OutputSymbolFlags.StaticMember,
133
141
  });
134
142
 
135
- return <MemberDeclaration symbol={sym}>
136
- {props.children}
137
- </MemberDeclaration>;
143
+ onCleanup(() => {
144
+ sym.binder.deleteSymbol(sym);
145
+ });
146
+
147
+ function wrap(children: Children) {
148
+ return <MemberDeclaration symbol={sym}>{children}</MemberDeclaration>;
149
+ }
150
+ if (!("jsValue" in props)) {
151
+ return wrap(() => props.children);
152
+ } else {
153
+ return wrap(<JsonValue jsValue={props.jsValue} />);
154
+ }
138
155
  }
@@ -1,9 +1,12 @@
1
1
  import {
2
2
  Children,
3
3
  computed,
4
- mapJoin,
4
+ For,
5
+ Indent,
6
+ List,
5
7
  MemberDeclaration,
6
8
  MemberScope,
9
+ onCleanup,
7
10
  OutputSymbolFlags,
8
11
  Refkey,
9
12
  useMemberDeclaration,
@@ -16,6 +19,7 @@ export interface JsonObjectPropsBase {
16
19
  * elsewhere via this refkey.
17
20
  **/
18
21
  refkey?: Refkey;
22
+ style?: { concise?: boolean };
19
23
  }
20
24
 
21
25
  /**
@@ -65,41 +69,49 @@ export function JsonObject(props: JsonObjectProps) {
65
69
  }
66
70
 
67
71
  if (!("jsValue" in props)) {
68
- return <MemberScope owner={memberSymbol}>
69
- {"{"}
70
- {props.children}
71
- {"}"}
72
- </MemberScope>;
72
+ return (
73
+ <MemberScope owner={memberSymbol}>
74
+ <group>
75
+ {"{"}
76
+ <Indent break={props.style?.concise ? "soft" : "hard"} trailingBreak>
77
+ <List comma softline>
78
+ {props.children}
79
+ </List>
80
+ </Indent>
81
+ {"}"}
82
+ </group>
83
+ </MemberScope>
84
+ );
73
85
  }
74
86
 
75
- const elements = computed(() => {
87
+ const entries = computed(() => {
76
88
  const jsValue = props.jsValue;
77
- let properties: [string, unknown][];
78
89
  if (Array.isArray(jsValue)) {
79
- properties = jsValue;
90
+ return jsValue;
80
91
  } else if (jsValue instanceof Map) {
81
- properties = [...jsValue.entries()];
92
+ return [...jsValue.entries()];
82
93
  } else if (jsValue !== undefined) {
83
- properties = globalThis.Object.entries(jsValue);
94
+ return globalThis.Object.entries(jsValue);
84
95
  } else {
85
- properties = [];
96
+ return [];
86
97
  }
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
98
  });
97
99
 
98
- return <MemberScope owner={memberSymbol}>
99
- {"{"}
100
- {elements}
101
- {"}"}
102
- </MemberScope>;
100
+ return (
101
+ <MemberScope owner={memberSymbol}>
102
+ <group>
103
+ {"{"}
104
+ <Indent break={props.style?.concise ? "soft" : "hard"} trailingBreak>
105
+ <For each={entries} comma softline>
106
+ {([name, value]) => (
107
+ <JsonObjectProperty name={name} jsValue={value} />
108
+ )}
109
+ </For>
110
+ </Indent>
111
+ {"}"}
112
+ </group>
113
+ </MemberScope>
114
+ );
103
115
  }
104
116
 
105
117
  /**
@@ -136,10 +148,16 @@ export function JsonObjectProperty(props: ObjectPropertyProps) {
136
148
  flags: OutputSymbolFlags.StaticMember,
137
149
  });
138
150
 
151
+ onCleanup(() => {
152
+ sym.binder.deleteSymbol(sym);
153
+ });
154
+
139
155
  const wrap = (children: Children) => {
140
- return <MemberDeclaration symbol={sym}>
141
- "{props.name}": {children}
142
- </MemberDeclaration>;
156
+ return (
157
+ <MemberDeclaration symbol={sym}>
158
+ "{props.name}": {children}
159
+ </MemberDeclaration>
160
+ );
143
161
  };
144
162
 
145
163
  if (!("jsValue" in props)) {
@@ -38,11 +38,14 @@ export function SourceFile(props: SourceFileProps) {
38
38
  jsonValueSym.name = context.path;
39
39
  });
40
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>;
41
+ return (
42
+ <CoreSourceFile filetype="json" path={props.path} reference={Reference}>
43
+ <SfTapper />
44
+ <JsonFileContext.Provider value={fileContext}>
45
+ <MemberDeclaration symbol={jsonValueSym}>
46
+ {props.children}
47
+ </MemberDeclaration>
48
+ </JsonFileContext.Provider>
49
+ </CoreSourceFile>
50
+ );
48
51
  }
package/temp/api.json CHANGED
@@ -329,7 +329,7 @@
329
329
  {
330
330
  "kind": "Function",
331
331
  "canonicalReference": "@alloy-js/json!JsonArrayElement:function(1)",
332
- "docComment": "/**\n * Create a JSON Array element. This component should be used inside of a {@link JsonArray} component.\n *\n * @remarks\n *\n * It does not add any syntax, but is required in order to establish the correct scope for the array element, allowing references to array elements to work properly.\n *\n * @see\n *\n * {@link @alloy-js/core#(MemberDeclarationContext:variable)}\n */\n",
332
+ "docComment": "/**\n * Create a JSON Array element. This component should be used inside of a {@link JsonArray} component. Can either be provided `children`, or a JS value which will be serialized to JSON.\n *\n * @see\n *\n * {@link @alloy-js/core#(MemberDeclarationContext:variable)}\n */\n",
333
333
  "excerptTokens": [
334
334
  {
335
335
  "kind": "Content",
@@ -338,7 +338,7 @@
338
338
  {
339
339
  "kind": "Reference",
340
340
  "text": "JsonArrayElementProps",
341
- "canonicalReference": "@alloy-js/json!JsonArrayElementProps:interface"
341
+ "canonicalReference": "@alloy-js/json!JsonArrayElementProps:type"
342
342
  },
343
343
  {
344
344
  "kind": "Content",
@@ -374,23 +374,59 @@
374
374
  "name": "JsonArrayElement"
375
375
  },
376
376
  {
377
- "kind": "Interface",
378
- "canonicalReference": "@alloy-js/json!JsonArrayElementProps:interface",
377
+ "kind": "TypeAlias",
378
+ "canonicalReference": "@alloy-js/json!JsonArrayElementProps:type",
379
379
  "docComment": "",
380
380
  "excerptTokens": [
381
381
  {
382
382
  "kind": "Content",
383
- "text": "export interface JsonArrayElementProps "
383
+ "text": "export type JsonArrayElementProps = "
384
+ },
385
+ {
386
+ "kind": "Reference",
387
+ "text": "JsonArrayElementPropsWithJsValue",
388
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithJsValue:interface"
389
+ },
390
+ {
391
+ "kind": "Content",
392
+ "text": " | "
393
+ },
394
+ {
395
+ "kind": "Reference",
396
+ "text": "JsonArrayElementPropsWithChildren",
397
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithChildren:interface"
398
+ },
399
+ {
400
+ "kind": "Content",
401
+ "text": ";"
384
402
  }
385
403
  ],
386
404
  "fileUrlPath": "src/components/JsonArray.tsx",
387
405
  "releaseTag": "Public",
388
406
  "name": "JsonArrayElementProps",
407
+ "typeTokenRange": {
408
+ "startIndex": 1,
409
+ "endIndex": 4
410
+ }
411
+ },
412
+ {
413
+ "kind": "Interface",
414
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithChildren:interface",
415
+ "docComment": "/**\n * Create a JSON Array element by providing children.\n */\n",
416
+ "excerptTokens": [
417
+ {
418
+ "kind": "Content",
419
+ "text": "export interface JsonArrayElementPropsWithChildren "
420
+ }
421
+ ],
422
+ "fileUrlPath": "src/components/JsonArray.tsx",
423
+ "releaseTag": "Public",
424
+ "name": "JsonArrayElementPropsWithChildren",
389
425
  "preserveMemberOrder": false,
390
426
  "members": [
391
427
  {
392
428
  "kind": "PropertySignature",
393
- "canonicalReference": "@alloy-js/json!JsonArrayElementProps#children:member",
429
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithChildren#children:member",
394
430
  "docComment": "",
395
431
  "excerptTokens": [
396
432
  {
@@ -419,6 +455,51 @@
419
455
  ],
420
456
  "extendsTokenRanges": []
421
457
  },
458
+ {
459
+ "kind": "Interface",
460
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithJsValue:interface",
461
+ "docComment": "/**\n * Create a JSON Array element by providing a JS value that will be serialized to JSON.\n */\n",
462
+ "excerptTokens": [
463
+ {
464
+ "kind": "Content",
465
+ "text": "export interface JsonArrayElementPropsWithJsValue "
466
+ }
467
+ ],
468
+ "fileUrlPath": "src/components/JsonArray.tsx",
469
+ "releaseTag": "Public",
470
+ "name": "JsonArrayElementPropsWithJsValue",
471
+ "preserveMemberOrder": false,
472
+ "members": [
473
+ {
474
+ "kind": "PropertySignature",
475
+ "canonicalReference": "@alloy-js/json!JsonArrayElementPropsWithJsValue#jsValue:member",
476
+ "docComment": "",
477
+ "excerptTokens": [
478
+ {
479
+ "kind": "Content",
480
+ "text": "jsValue: "
481
+ },
482
+ {
483
+ "kind": "Content",
484
+ "text": "unknown"
485
+ },
486
+ {
487
+ "kind": "Content",
488
+ "text": ";"
489
+ }
490
+ ],
491
+ "isReadonly": false,
492
+ "isOptional": false,
493
+ "releaseTag": "Public",
494
+ "name": "jsValue",
495
+ "propertyTypeTokenRange": {
496
+ "startIndex": 1,
497
+ "endIndex": 2
498
+ }
499
+ }
500
+ ],
501
+ "extendsTokenRanges": []
502
+ },
422
503
  {
423
504
  "kind": "TypeAlias",
424
505
  "canonicalReference": "@alloy-js/json!JsonArrayProps:type",
@@ -929,6 +1010,33 @@
929
1010
  "startIndex": 1,
930
1011
  "endIndex": 2
931
1012
  }
1013
+ },
1014
+ {
1015
+ "kind": "PropertySignature",
1016
+ "canonicalReference": "@alloy-js/json!JsonObjectPropsBase#style:member",
1017
+ "docComment": "",
1018
+ "excerptTokens": [
1019
+ {
1020
+ "kind": "Content",
1021
+ "text": "style?: "
1022
+ },
1023
+ {
1024
+ "kind": "Content",
1025
+ "text": "{\n concise?: boolean;\n }"
1026
+ },
1027
+ {
1028
+ "kind": "Content",
1029
+ "text": ";"
1030
+ }
1031
+ ],
1032
+ "isReadonly": false,
1033
+ "isOptional": true,
1034
+ "releaseTag": "Public",
1035
+ "name": "style",
1036
+ "propertyTypeTokenRange": {
1037
+ "startIndex": 1,
1038
+ "endIndex": 2
1039
+ }
932
1040
  }
933
1041
  ],
934
1042
  "extendsTokenRanges": []
@@ -1,4 +1,6 @@
1
+ import { reactive, renderTree } from "@alloy-js/core";
1
2
  import "@alloy-js/core/testing";
3
+ import { d, printTree } from "@alloy-js/core/testing";
2
4
  import { expect, it } from "vitest";
3
5
  import { jsonTest } from "./utils.jsx";
4
6
 
@@ -12,20 +14,48 @@ it("renders arrays", () => {
12
14
  const template = jsonTest([1, "hello"]);
13
15
 
14
16
  expect(template).toRenderTo(`
17
+ [1, "hello"]
18
+ `);
19
+ });
20
+
21
+ it("renders nested arrays", () => {
22
+ const template = jsonTest([1, ["hello there this is a long thing"]]);
23
+
24
+ expect(template).toRenderTo(
25
+ `
15
26
  [
16
27
  1,
17
- "hello"
28
+ [
29
+ "hello there this is a long thing"
30
+ ]
18
31
  ]
19
- `);
32
+ `,
33
+ { printWidth: 20 },
34
+ );
20
35
  });
21
36
 
22
- it("renders nested arrays", () => {
23
- const template = jsonTest([1, ["hello"]]);
37
+ it("works reactively", () => {
38
+ const arr = reactive([] as (number | string)[]);
24
39
 
25
- expect(template).toRenderTo(`
40
+ const template = jsonTest(arr);
41
+ const tree = renderTree(template);
42
+
43
+ expect(printTree(tree)).toEqual(`[]`);
44
+
45
+ arr.push(1);
46
+ expect(printTree(tree)).toEqual(`[1]`);
47
+
48
+ arr.push(2);
49
+ expect(printTree(tree)).toEqual(d`
50
+ [1, 2]
51
+ `);
52
+
53
+ arr.push("this is a long item that should be wrapped");
54
+ expect(printTree(tree, { printWidth: 20 })).toEqual(d`
26
55
  [
27
56
  1,
28
- ["hello"]
57
+ 2,
58
+ "this is a long item that should be wrapped"
29
59
  ]
30
60
  `);
31
61
  });
@@ -1,5 +1,6 @@
1
- import { Output } from "@alloy-js/core";
1
+ import { List, Output, reactive, renderTree } from "@alloy-js/core";
2
2
  import "@alloy-js/core/testing";
3
+ import { d, printTree } from "@alloy-js/core/testing";
3
4
  import { expect, it } from "vitest";
4
5
  import {
5
6
  JsonObject,
@@ -9,10 +10,7 @@ import { SourceFile } from "../src/components/SourceFile.jsx";
9
10
  import { jsonTest } from "./utils.jsx";
10
11
 
11
12
  it("renders objects", () => {
12
- const template = jsonTest({
13
- a: 1,
14
- b: "hello",
15
- });
13
+ const template = jsonTest({ a: 1, b: "hello" });
16
14
 
17
15
  expect(template).toRenderTo(`
18
16
  {
@@ -39,17 +37,18 @@ it("renders nested objects", () => {
39
37
  });
40
38
 
41
39
  it("can can manually assemble objects", () => {
42
- const template =
40
+ const template = (
43
41
  <Output>
44
42
  <SourceFile path="test.json">
45
43
  <JsonObject>
46
- <JsonObjectProperty name="foo">12</JsonObjectProperty>,
47
- <JsonObjectProperty name="bar">
48
- 13
49
- </JsonObjectProperty>
44
+ <List comma softline>
45
+ <JsonObjectProperty name="foo">12</JsonObjectProperty>
46
+ <JsonObjectProperty name="bar">13</JsonObjectProperty>
47
+ </List>
50
48
  </JsonObject>
51
49
  </SourceFile>
52
- </Output>;
50
+ </Output>
51
+ );
53
52
 
54
53
  expect(template).toRenderTo(`
55
54
  {
@@ -58,3 +57,33 @@ it("can can manually assemble objects", () => {
58
57
  }
59
58
  `);
60
59
  });
60
+
61
+ it("works reactively", () => {
62
+ const obj = reactive({ a: 1 } as Record<string, unknown>);
63
+
64
+ const template = jsonTest(obj);
65
+ const tree = renderTree(template);
66
+
67
+ expect(printTree(tree)).toEqual(d`
68
+ {
69
+ "a": 1
70
+ }
71
+ `);
72
+
73
+ (obj.a as any)++;
74
+
75
+ expect(printTree(tree)).toEqual(d`
76
+ {
77
+ "a": 2
78
+ }
79
+ `);
80
+
81
+ obj.b = "hello";
82
+
83
+ expect(printTree(tree)).toEqual(d`
84
+ {
85
+ "a": 2,
86
+ "b": "hello"
87
+ }
88
+ `);
89
+ });