@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,31 @@
1
+ import "@alloy-js/core/testing";
2
+ import { expect, it } from "vitest";
3
+ import { jsonTest } from "./utils.jsx";
4
+
5
+ it("renders empty arrays", () => {
6
+ const template = jsonTest([]);
7
+
8
+ expect(template).toRenderTo(`[]`);
9
+ });
10
+
11
+ it("renders arrays", () => {
12
+ const template = jsonTest([1, "hello"]);
13
+
14
+ expect(template).toRenderTo(`
15
+ [
16
+ 1,
17
+ "hello"
18
+ ]
19
+ `);
20
+ });
21
+
22
+ it("renders nested arrays", () => {
23
+ const template = jsonTest([1, ["hello"]]);
24
+
25
+ expect(template).toRenderTo(`
26
+ [
27
+ 1,
28
+ ["hello"]
29
+ ]
30
+ `);
31
+ });
@@ -0,0 +1,60 @@
1
+ import { Output } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import {
5
+ JsonObject,
6
+ JsonObjectProperty,
7
+ } from "../src/components/JsonObject.jsx";
8
+ import { SourceFile } from "../src/components/SourceFile.jsx";
9
+ import { jsonTest } from "./utils.jsx";
10
+
11
+ it("renders objects", () => {
12
+ const template = jsonTest({
13
+ a: 1,
14
+ b: "hello",
15
+ });
16
+
17
+ expect(template).toRenderTo(`
18
+ {
19
+ "a": 1,
20
+ "b": "hello"
21
+ }
22
+ `);
23
+ });
24
+
25
+ it("renders nested objects", () => {
26
+ const template = jsonTest({
27
+ a: {
28
+ b: 1,
29
+ },
30
+ });
31
+
32
+ expect(template).toRenderTo(`
33
+ {
34
+ "a": {
35
+ "b": 1
36
+ }
37
+ }
38
+ `);
39
+ });
40
+
41
+ it("can can manually assemble objects", () => {
42
+ const template =
43
+ <Output>
44
+ <SourceFile path="test.json">
45
+ <JsonObject>
46
+ <JsonObjectProperty name="foo">12</JsonObjectProperty>,
47
+ <JsonObjectProperty name="bar">
48
+ 13
49
+ </JsonObjectProperty>
50
+ </JsonObject>
51
+ </SourceFile>
52
+ </Output>;
53
+
54
+ expect(template).toRenderTo(`
55
+ {
56
+ "foo": 12,
57
+ "bar": 13
58
+ }
59
+ `);
60
+ });
@@ -0,0 +1,16 @@
1
+ import "@alloy-js/core/testing";
2
+ import { expect, it } from "vitest";
3
+ import { jsonTest } from "./utils.jsx";
4
+
5
+ it("renders primitives", () => {
6
+ expect(jsonTest(true)).toRenderTo("true");
7
+ expect(jsonTest(false)).toRenderTo("false");
8
+
9
+ expect(jsonTest(10)).toRenderTo("10");
10
+ expect(jsonTest(-10)).toRenderTo("-10");
11
+ expect(jsonTest(1.314)).toRenderTo("1.314");
12
+
13
+ expect(jsonTest("hello")).toRenderTo('"hello"');
14
+
15
+ expect(jsonTest(null)).toRenderTo("null");
16
+ });
@@ -0,0 +1,173 @@
1
+ import { Output, refkey, SourceDirectory } from "@alloy-js/core";
2
+ import "@alloy-js/core/testing";
3
+ import { expect, it } from "vitest";
4
+ import { JsonArray, JsonArrayElement } from "../src/components/JsonArray.jsx";
5
+ import {
6
+ JsonObject,
7
+ JsonObjectProperty,
8
+ } from "../src/components/JsonObject.jsx";
9
+ import { JsonValue } from "../src/components/JsonValue.jsx";
10
+ import { SourceFile } from "../src/components/SourceFile.jsx";
11
+
12
+ it("references objects within the same file", () => {
13
+ const objectValue = refkey();
14
+ const nullKey = refkey();
15
+ const template =
16
+ <Output>
17
+ <SourceFile path="test.json">
18
+ <JsonObject refkey={objectValue}>
19
+ <JsonObjectProperty name="nullValue">
20
+ <JsonValue jsValue={null} refkey={nullKey} />,
21
+ </JsonObjectProperty>
22
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
23
+ <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
24
+ </JsonObject>
25
+ </SourceFile>
26
+ </Output>;
27
+
28
+ expect(template).toRenderTo(`
29
+ {
30
+ "nullValue": null,
31
+ "refToNull": "#/nullValue",
32
+ "refToObject": "#"
33
+ }
34
+ `);
35
+ });
36
+
37
+ it("references objects across files", () => {
38
+ const objectValue = refkey();
39
+ const nullKey = refkey();
40
+ const template =
41
+ <Output>
42
+ <SourceFile path="test.json">
43
+ <JsonObject refkey={objectValue}>
44
+ <JsonObjectProperty name="nullValue">
45
+ <JsonValue jsValue={null} refkey={nullKey} />,
46
+ </JsonObjectProperty>
47
+ </JsonObject>
48
+ </SourceFile>
49
+ <SourceFile path="test.json">
50
+ <JsonObject>
51
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
52
+ <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
53
+ </JsonObject>
54
+ </SourceFile>
55
+ </Output>;
56
+
57
+ expect(template).toRenderTo(`
58
+ {
59
+ "nullValue": null,
60
+ }
61
+ {
62
+ "refToNull": "test.json#/nullValue",
63
+ "refToObject": "test.json#"
64
+ }
65
+ `);
66
+ });
67
+
68
+ it("references objects across files and directories", () => {
69
+ const objectValue = refkey();
70
+ const nullKey = refkey();
71
+ const template =
72
+ <Output>
73
+ <SourceDirectory path="subdir">
74
+ <SourceFile path="test.json">
75
+ <JsonObject refkey={objectValue}>
76
+ <JsonObjectProperty name="nullValue">
77
+ <JsonValue jsValue={null} refkey={nullKey} />,
78
+ </JsonObjectProperty>
79
+ </JsonObject>
80
+ </SourceFile>
81
+ </SourceDirectory>
82
+ <SourceFile path="test.json">
83
+ <JsonObject>
84
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
85
+ <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
86
+ </JsonObject>
87
+ </SourceFile>
88
+ </Output>;
89
+
90
+ expect(template).toRenderTo(`
91
+ {
92
+ "nullValue": null,
93
+ }
94
+ {
95
+ "refToNull": "subdir/test.json#/nullValue",
96
+ "refToObject": "subdir/test.json#"
97
+ }
98
+ `);
99
+ });
100
+
101
+ it("references arrays within the same file", () => {
102
+ const arrayValue = refkey();
103
+ const nullKey = refkey();
104
+ const template =
105
+ <Output>
106
+ <SourceFile path="test.json">
107
+ <JsonArray refkey={arrayValue}>
108
+ <JsonArrayElement>{nullKey}</JsonArrayElement>,
109
+ <JsonArrayElement>{arrayValue}</JsonArrayElement>,
110
+ <JsonArrayElement>
111
+ <JsonValue jsValue={null} refkey={nullKey} />
112
+ </JsonArrayElement>
113
+ </JsonArray>
114
+ </SourceFile>
115
+ </Output>;
116
+
117
+ expect(template).toRenderTo(`
118
+ [
119
+ "#/2",
120
+ "#",
121
+ null
122
+ ]
123
+ `);
124
+ });
125
+
126
+ it("does complex references", () => {
127
+ const objectValue = refkey();
128
+ const arrayValue = refkey();
129
+ const nullKey = refkey();
130
+ const nestedKey = refkey();
131
+ const template =
132
+ <Output>
133
+ <SourceFile path="test.json">
134
+ <JsonObject refkey={objectValue}>
135
+ <JsonObjectProperty name="nullValue">
136
+ <JsonValue jsValue={null} refkey={nullKey} />,
137
+ </JsonObjectProperty>
138
+ <JsonObjectProperty name="refToNull">{nullKey}</JsonObjectProperty>,
139
+ <JsonObjectProperty name="refToObject">{objectValue}</JsonObjectProperty>
140
+ <JsonObjectProperty name="refToNested">{nestedKey}</JsonObjectProperty>
141
+ <JsonObjectProperty name="nestedArray">
142
+ <JsonArray refkey={arrayValue}>
143
+ <JsonArrayElement>{nullKey}</JsonArrayElement>,
144
+ <JsonArrayElement>{arrayValue}</JsonArrayElement>
145
+ <JsonArrayElement>
146
+ <JsonObject>
147
+ <JsonObjectProperty name="nestedProp">
148
+ <JsonValue jsValue="hi" refkey={nestedKey} />
149
+ </JsonObjectProperty>
150
+ </JsonObject>
151
+ </JsonArrayElement>
152
+ </JsonArray>
153
+ </JsonObjectProperty>
154
+ </JsonObject>
155
+ </SourceFile>
156
+ </Output>;
157
+
158
+ expect(template).toRenderTo(`
159
+ {
160
+ "nullValue": null,
161
+ "refToNull": "#/nullValue",
162
+ "refToObject": "#"
163
+ "refToNested": "#/nestedArray/2/nestedProp"
164
+ "nestedArray": [
165
+ "#/nullValue",
166
+ "#/nestedArray"
167
+ {
168
+ "nestedProp": "hi"
169
+ }
170
+ ]
171
+ }
172
+ `);
173
+ });
package/test/utils.tsx ADDED
@@ -0,0 +1,11 @@
1
+ import { Output } from "@alloy-js/core";
2
+ import { JsonValue } from "../src/components/JsonValue.jsx";
3
+ import { SourceFile } from "../src/components/SourceFile.jsx";
4
+
5
+ export function jsonTest(jsValue: unknown) {
6
+ return <Output>
7
+ <SourceFile path="test.json">
8
+ <JsonValue jsValue={jsValue} />
9
+ </SourceFile>
10
+ </Output>;
11
+ }
@@ -0,0 +1,10 @@
1
+ import "vitest";
2
+
3
+ interface CustomMatchers<R = unknown> {
4
+ toRenderTo: (str: string) => R;
5
+ }
6
+
7
+ declare module "vitest" {
8
+ interface Assertion<T = any> extends CustomMatchers<T> {}
9
+ interface AsymmetricMatchersContaining extends CustomMatchers {}
10
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "emitDeclarationOnly": true,
5
+ "declaration": true,
6
+ "outDir": "dist"
7
+ },
8
+ "references": [{ "path": "../core" }],
9
+ "include": ["src/**/*.ts", "src/**/*.tsx", "test/**/*.ts", "test/**/*.tsx"],
10
+ "exclude": ["node_modules", "dist"]
11
+ }
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.47.7"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,18 @@
1
+ import { babel } from "@rollup/plugin-babel";
2
+ import { defineConfig } from "vitest/config";
3
+
4
+ export default defineConfig({
5
+ esbuild: {
6
+ jsx: "preserve",
7
+ sourcemap: "both",
8
+ },
9
+ plugins: [
10
+ babel({
11
+ inputSourceMap: true as any,
12
+ sourceMaps: "both",
13
+ babelHelpers: "bundled",
14
+ extensions: [".ts", ".tsx"],
15
+ presets: ["@babel/preset-typescript", "@alloy-js/babel-preset"],
16
+ }),
17
+ ],
18
+ });