@fluidframework/tree-agent 2.80.0 → 2.81.0-374083
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/api-report/tree-agent.alpha.api.md +163 -20
- package/dist/alpha.d.ts +26 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/methodBinding.d.ts +54 -10
- package/dist/methodBinding.d.ts.map +1 -1
- package/dist/methodBinding.js.map +1 -1
- package/dist/propertyBinding.d.ts +52 -2
- package/dist/propertyBinding.d.ts.map +1 -1
- package/dist/propertyBinding.js +28 -3
- package/dist/propertyBinding.js.map +1 -1
- package/dist/renderSchemaTypeScript.d.ts.map +1 -1
- package/dist/renderSchemaTypeScript.js +16 -7
- package/dist/renderSchemaTypeScript.js.map +1 -1
- package/dist/renderTypeFactoryTypeScript.d.ts +13 -0
- package/dist/renderTypeFactoryTypeScript.d.ts.map +1 -0
- package/dist/renderTypeFactoryTypeScript.js +222 -0
- package/dist/renderTypeFactoryTypeScript.js.map +1 -0
- package/dist/subtree.d.ts.map +1 -1
- package/dist/subtree.js +4 -4
- package/dist/subtree.js.map +1 -1
- package/dist/treeAgentTypes.d.ts +345 -0
- package/dist/treeAgentTypes.d.ts.map +1 -0
- package/dist/treeAgentTypes.js +190 -0
- package/dist/treeAgentTypes.js.map +1 -0
- package/dist/utils.d.ts +0 -4
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +2 -9
- package/dist/utils.js.map +1 -1
- package/eslint.config.mts +4 -4
- package/lib/alpha.d.ts +26 -1
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/methodBinding.d.ts +54 -10
- package/lib/methodBinding.d.ts.map +1 -1
- package/lib/methodBinding.js.map +1 -1
- package/lib/propertyBinding.d.ts +52 -2
- package/lib/propertyBinding.d.ts.map +1 -1
- package/lib/propertyBinding.js +28 -3
- package/lib/propertyBinding.js.map +1 -1
- package/lib/renderSchemaTypeScript.d.ts.map +1 -1
- package/lib/renderSchemaTypeScript.js +16 -7
- package/lib/renderSchemaTypeScript.js.map +1 -1
- package/lib/renderTypeFactoryTypeScript.d.ts +13 -0
- package/lib/renderTypeFactoryTypeScript.d.ts.map +1 -0
- package/lib/renderTypeFactoryTypeScript.js +217 -0
- package/lib/renderTypeFactoryTypeScript.js.map +1 -0
- package/lib/subtree.d.ts.map +1 -1
- package/lib/subtree.js +4 -4
- package/lib/subtree.js.map +1 -1
- package/lib/treeAgentTypes.d.ts +345 -0
- package/lib/treeAgentTypes.d.ts.map +1 -0
- package/lib/treeAgentTypes.js +186 -0
- package/lib/treeAgentTypes.js.map +1 -0
- package/lib/utils.d.ts +0 -4
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +2 -8
- package/lib/utils.js.map +1 -1
- package/package.json +12 -12
- package/src/index.ts +31 -0
- package/src/methodBinding.ts +93 -15
- package/src/propertyBinding.ts +66 -9
- package/src/renderSchemaTypeScript.ts +24 -8
- package/src/renderTypeFactoryTypeScript.ts +259 -0
- package/src/subtree.ts +5 -4
- package/src/treeAgentTypes.ts +490 -0
- package/src/utils.ts +2 -9
- package/.eslintrc.cjs +0 -48
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { UsageError } from "@fluidframework/telemetry-utils/internal";
|
|
7
|
+
import type { ObjectNodeSchema, TreeNodeSchema } from "@fluidframework/tree/alpha";
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
TypeFactoryType,
|
|
11
|
+
TypeFactoryArray,
|
|
12
|
+
TypeFactoryObject,
|
|
13
|
+
TypeFactoryTuple,
|
|
14
|
+
TypeFactoryRecord,
|
|
15
|
+
TypeFactoryMap,
|
|
16
|
+
TypeFactoryLiteral,
|
|
17
|
+
TypeFactoryOptional,
|
|
18
|
+
TypeFactoryReadonly,
|
|
19
|
+
TypeFactoryUnion,
|
|
20
|
+
} from "./treeAgentTypes.js";
|
|
21
|
+
|
|
22
|
+
export { instanceOfsTypeFactory } from "./treeAgentTypes.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Converts type factory type definitions into TypeScript declaration text.
|
|
26
|
+
* @alpha
|
|
27
|
+
*/
|
|
28
|
+
export function renderTypeFactoryTypeScript(
|
|
29
|
+
typeFactoryType: TypeFactoryType,
|
|
30
|
+
getFriendlyName: (schema: TreeNodeSchema) => string,
|
|
31
|
+
instanceOfLookup: WeakMap<TypeFactoryType, ObjectNodeSchema>,
|
|
32
|
+
): string {
|
|
33
|
+
let result = "";
|
|
34
|
+
let startOfLine = true;
|
|
35
|
+
let indent = 0;
|
|
36
|
+
|
|
37
|
+
appendType(typeFactoryType, TypePrecedence.Union);
|
|
38
|
+
return result;
|
|
39
|
+
|
|
40
|
+
function appendType(type: TypeFactoryType, minPrecedence = TypePrecedence.Object): void {
|
|
41
|
+
const shouldParenthesize = getTypePrecedence(type) < minPrecedence;
|
|
42
|
+
if (shouldParenthesize) {
|
|
43
|
+
append("(");
|
|
44
|
+
}
|
|
45
|
+
appendTypeDefinition(type);
|
|
46
|
+
if (shouldParenthesize) {
|
|
47
|
+
append(")");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function append(s: string): void {
|
|
52
|
+
if (startOfLine) {
|
|
53
|
+
result += " ".repeat(indent);
|
|
54
|
+
startOfLine = false;
|
|
55
|
+
}
|
|
56
|
+
result += s;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function appendNewLine(): void {
|
|
60
|
+
append("\n");
|
|
61
|
+
startOfLine = true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function appendTypeDefinition(type: TypeFactoryType): void {
|
|
65
|
+
switch (type._kind) {
|
|
66
|
+
case "string": {
|
|
67
|
+
append("string");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
case "number": {
|
|
71
|
+
append("number");
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
case "boolean": {
|
|
75
|
+
append("boolean");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
case "void": {
|
|
79
|
+
append("void");
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
case "undefined": {
|
|
83
|
+
append("undefined");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
case "null": {
|
|
87
|
+
append("null");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
case "unknown": {
|
|
91
|
+
append("unknown");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
case "array": {
|
|
95
|
+
appendArrayType(type as TypeFactoryArray);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
case "object": {
|
|
99
|
+
appendObjectType(type as TypeFactoryObject);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
case "union": {
|
|
103
|
+
appendUnionTypes((type as TypeFactoryUnion).options, TypePrecedence.Union);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
case "tuple": {
|
|
107
|
+
appendTupleType(type as TypeFactoryTuple);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
case "record": {
|
|
111
|
+
appendRecordType(type as TypeFactoryRecord);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
case "map": {
|
|
115
|
+
appendMapType(type as TypeFactoryMap);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
case "literal": {
|
|
119
|
+
appendLiteral((type as TypeFactoryLiteral).value);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
case "optional": {
|
|
123
|
+
appendUnionTypes(
|
|
124
|
+
[(type as TypeFactoryOptional).innerType, { _kind: "undefined" }],
|
|
125
|
+
TypePrecedence.Union,
|
|
126
|
+
);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
case "readonly": {
|
|
130
|
+
appendReadonlyType(type as TypeFactoryReadonly);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
case "instanceof": {
|
|
134
|
+
const schema = instanceOfLookup.get(type);
|
|
135
|
+
if (schema === undefined) {
|
|
136
|
+
throw new UsageError(
|
|
137
|
+
"instanceof type not found in lookup - this typically indicates the type was not created via typeFactory.instanceOf",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
append(getFriendlyName(schema));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
default: {
|
|
144
|
+
throw new UsageError(
|
|
145
|
+
`Unsupported type when formatting helper types: ${String(type._kind ?? "unknown")}. Expected one of: string, number, boolean, void, undefined, null, unknown, array, object, union, tuple, record, map, literal, optional, readonly, instanceof.`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function appendArrayType(arrayType: TypeFactoryArray): void {
|
|
152
|
+
appendType(arrayType.element, TypePrecedence.Object);
|
|
153
|
+
append("[]");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function appendObjectType(objectType: TypeFactoryObject): void {
|
|
157
|
+
append("{");
|
|
158
|
+
appendNewLine();
|
|
159
|
+
indent++;
|
|
160
|
+
for (const [name, propertyType] of Object.entries(objectType.shape)) {
|
|
161
|
+
append(name);
|
|
162
|
+
if (propertyType._kind === "optional") {
|
|
163
|
+
append("?");
|
|
164
|
+
append(": ");
|
|
165
|
+
appendType((propertyType as TypeFactoryOptional).innerType);
|
|
166
|
+
} else {
|
|
167
|
+
append(": ");
|
|
168
|
+
appendType(propertyType);
|
|
169
|
+
}
|
|
170
|
+
append(";");
|
|
171
|
+
appendNewLine();
|
|
172
|
+
}
|
|
173
|
+
indent--;
|
|
174
|
+
append("}");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function appendUnionTypes(
|
|
178
|
+
types: readonly TypeFactoryType[],
|
|
179
|
+
minPrecedence: TypePrecedence,
|
|
180
|
+
): void {
|
|
181
|
+
let first = true;
|
|
182
|
+
for (const innerType of types) {
|
|
183
|
+
if (!first) {
|
|
184
|
+
append(" | ");
|
|
185
|
+
}
|
|
186
|
+
appendType(innerType, minPrecedence);
|
|
187
|
+
first = false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function appendTupleType(tupleType: TypeFactoryTuple): void {
|
|
192
|
+
append("[");
|
|
193
|
+
let first = true;
|
|
194
|
+
for (const innerType of tupleType.items) {
|
|
195
|
+
if (!first) {
|
|
196
|
+
append(", ");
|
|
197
|
+
}
|
|
198
|
+
if (innerType._kind === "optional") {
|
|
199
|
+
appendType((innerType as TypeFactoryOptional).innerType, TypePrecedence.Object);
|
|
200
|
+
append("?");
|
|
201
|
+
} else {
|
|
202
|
+
appendType(innerType);
|
|
203
|
+
}
|
|
204
|
+
first = false;
|
|
205
|
+
}
|
|
206
|
+
if (tupleType.rest !== undefined) {
|
|
207
|
+
if (!first) {
|
|
208
|
+
append(", ");
|
|
209
|
+
}
|
|
210
|
+
append("...");
|
|
211
|
+
appendType(tupleType.rest, TypePrecedence.Object);
|
|
212
|
+
append("[]");
|
|
213
|
+
}
|
|
214
|
+
append("]");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function appendRecordType(recordType: TypeFactoryRecord): void {
|
|
218
|
+
append("Record<");
|
|
219
|
+
appendType(recordType.keyType, TypePrecedence.Union);
|
|
220
|
+
append(", ");
|
|
221
|
+
appendType(recordType.valueType, TypePrecedence.Union);
|
|
222
|
+
append(">");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function appendMapType(mapType: TypeFactoryMap): void {
|
|
226
|
+
append("Map<");
|
|
227
|
+
appendType(mapType.keyType, TypePrecedence.Union);
|
|
228
|
+
append(", ");
|
|
229
|
+
appendType(mapType.valueType, TypePrecedence.Union);
|
|
230
|
+
append(">");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function appendLiteral(value: string | number | boolean): void {
|
|
234
|
+
append(JSON.stringify(value));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function appendReadonlyType(readonlyType: TypeFactoryReadonly): void {
|
|
238
|
+
append("Readonly<");
|
|
239
|
+
appendType(readonlyType.innerType);
|
|
240
|
+
append(">");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const enum TypePrecedence {
|
|
245
|
+
Union = 0,
|
|
246
|
+
Intersection = 1,
|
|
247
|
+
Object = 2,
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function getTypePrecedence(type: TypeFactoryType): TypePrecedence {
|
|
251
|
+
switch (type._kind) {
|
|
252
|
+
case "union": {
|
|
253
|
+
return TypePrecedence.Union;
|
|
254
|
+
}
|
|
255
|
+
default: {
|
|
256
|
+
return TypePrecedence.Object;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
package/src/subtree.ts
CHANGED
|
@@ -34,7 +34,7 @@ export class Subtree<TRoot extends ImplicitFieldSchema> {
|
|
|
34
34
|
|
|
35
35
|
public get branch(): TreeBranchAlpha {
|
|
36
36
|
return this.viewOrTree instanceof TreeNode
|
|
37
|
-
? (TreeAlpha.branch(this.viewOrTree) ?? fail(
|
|
37
|
+
? (TreeAlpha.branch(this.viewOrTree) ?? fail(0xcb3 /* Node cannot be raw. */))
|
|
38
38
|
: this.viewOrTree;
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -89,7 +89,7 @@ export class Subtree<TRoot extends ImplicitFieldSchema> {
|
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
default: {
|
|
92
|
-
fail(
|
|
92
|
+
fail(0xcb4 /* Unexpected node kind */);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
}
|
|
@@ -108,10 +108,11 @@ export class Subtree<TRoot extends ImplicitFieldSchema> {
|
|
|
108
108
|
|
|
109
109
|
public fork(): Subtree<TRoot> {
|
|
110
110
|
if (this.viewOrTree instanceof TreeNode) {
|
|
111
|
-
const branch =
|
|
111
|
+
const branch =
|
|
112
|
+
TreeAlpha.branch(this.viewOrTree) ?? fail(0xcb5 /* Node cannot be raw. */);
|
|
112
113
|
const node =
|
|
113
114
|
getNodeOnBranch(this.viewOrTree, branch.fork()) ??
|
|
114
|
-
fail(
|
|
115
|
+
fail(0xcb6 /* Expected node to be on new fork. */);
|
|
115
116
|
|
|
116
117
|
return new Subtree<TRoot>(node);
|
|
117
118
|
} else {
|