@microsoft/fast-html 1.0.0-alpha.3 → 1.0.0-alpha.30

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 (58) hide show
  1. package/README.md +255 -18
  2. package/dist/dts/components/element.d.ts +10 -0
  3. package/dist/dts/components/index.d.ts +3 -1
  4. package/dist/dts/components/observer-map.d.ts +26 -0
  5. package/dist/dts/components/request-idle-callback.d.ts +41 -0
  6. package/dist/dts/components/schema.d.ts +144 -0
  7. package/dist/dts/components/template.d.ts +83 -7
  8. package/dist/dts/components/utilities.d.ts +109 -18
  9. package/dist/dts/fixtures/lifecycle-callbacks/lifecycle-callbacks.spec.d.ts +1 -0
  10. package/dist/dts/fixtures/lifecycle-callbacks/main.d.ts +5 -0
  11. package/dist/dts/fixtures/observer-map/main.d.ts +1 -0
  12. package/dist/dts/fixtures/observer-map/observer-map.spec.d.ts +1 -0
  13. package/dist/dts/index.d.ts +1 -1
  14. package/dist/esm/components/element.js +23 -0
  15. package/dist/esm/components/index.js +3 -1
  16. package/dist/esm/components/observer-map.js +53 -0
  17. package/dist/esm/components/observer-map.spec.js +19 -0
  18. package/dist/esm/components/request-idle-callback.js +72 -0
  19. package/dist/esm/components/schema.js +250 -0
  20. package/dist/esm/components/schema.spec.js +485 -0
  21. package/dist/esm/components/template.js +199 -111
  22. package/dist/esm/components/utilities.js +741 -43
  23. package/dist/esm/components/utilities.spec.js +317 -44
  24. package/dist/esm/fixtures/attribute/main.js +3 -2
  25. package/dist/esm/fixtures/binding/binding.spec.js +6 -0
  26. package/dist/esm/fixtures/binding/main.js +13 -2
  27. package/dist/esm/fixtures/children/children.spec.js +4 -0
  28. package/dist/esm/fixtures/children/main.js +3 -2
  29. package/dist/esm/fixtures/dot-syntax/dot-syntax.spec.js +109 -2
  30. package/dist/esm/fixtures/dot-syntax/main.js +30 -4
  31. package/dist/esm/fixtures/event/event.spec.js +28 -5
  32. package/dist/esm/fixtures/event/main.js +21 -5
  33. package/dist/esm/fixtures/lifecycle-callbacks/lifecycle-callbacks.spec.js +166 -0
  34. package/dist/esm/fixtures/lifecycle-callbacks/main.js +126 -0
  35. package/dist/esm/fixtures/observer-map/main.js +375 -0
  36. package/dist/esm/fixtures/observer-map/observer-map.spec.js +251 -0
  37. package/dist/esm/fixtures/ref/main.js +3 -2
  38. package/dist/esm/fixtures/ref/ref.spec.js +2 -6
  39. package/dist/esm/fixtures/repeat/main.js +27 -2
  40. package/dist/esm/fixtures/repeat/repeat.spec.js +16 -6
  41. package/dist/esm/fixtures/slotted/main.js +15 -4
  42. package/dist/esm/fixtures/slotted/slotted.spec.js +18 -19
  43. package/dist/esm/fixtures/when/main.js +139 -2
  44. package/dist/esm/fixtures/when/when.spec.js +64 -1
  45. package/dist/esm/index.js +1 -1
  46. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  47. package/dist/fast-html.api.json +333 -0
  48. package/dist/fast-html.d.ts +282 -6
  49. package/dist/fast-html.untrimmed.d.ts +282 -6
  50. package/package.json +12 -7
  51. package/rules/attribute-directives.yml +38 -0
  52. package/rules/call-expression-with-event-argument.yml +41 -0
  53. package/rules/member-expression.yml +33 -0
  54. package/rules/tag-function-to-template-literal.yml +16 -0
  55. package/dist/esm/fixtures/partial/main.js +0 -31
  56. package/dist/esm/fixtures/partial/partial.spec.js +0 -14
  57. /package/dist/dts/{fixtures/partial/main.d.ts → components/observer-map.spec.d.ts} +0 -0
  58. /package/dist/dts/{fixtures/partial/partial.spec.d.ts → components/schema.spec.d.ts} +0 -0
@@ -0,0 +1,250 @@
1
+ // The context, in most cases the array property e.g. users
2
+ export const fastContextMetaData = "$fast_context";
3
+ // The list of contexts preceeding this context, the first of which should be the root property
4
+ export const fastContextsMetaData = "$fast_parent_contexts";
5
+ export const defsPropertyName = "$defs";
6
+ export const refPropertyName = "$ref";
7
+ /**
8
+ * A constructed JSON schema from a template
9
+ */
10
+ export class Schema {
11
+ constructor(name) {
12
+ this.customElementName = name;
13
+ Schema.jsonSchemaMap.set(this.customElementName, new Map());
14
+ }
15
+ /**
16
+ * Add a path to a schema
17
+ * @param config RegisterPathConfig
18
+ */
19
+ addPath(config) {
20
+ var _a, _b, _c;
21
+ const splitPath = this.getSplitPath(config.pathConfig.path);
22
+ let schema = Schema.jsonSchemaMap.get(this.customElementName).get(config.rootPropertyName);
23
+ let childRef = null;
24
+ // Create a root level property JSON
25
+ if (!schema) {
26
+ this.addNewSchema(config.rootPropertyName);
27
+ schema = Schema.jsonSchemaMap.get(this.customElementName).get(config.rootPropertyName);
28
+ }
29
+ if (config.childrenMap) {
30
+ childRef = this.getSchemaId(config.childrenMap.customElementName, config.childrenMap.attributeName);
31
+ if (splitPath.length === 1) {
32
+ schema.anyOf
33
+ ? schema.anyOf.push({ [refPropertyName]: childRef })
34
+ : (schema.anyOf = [{ [refPropertyName]: childRef }]);
35
+ }
36
+ }
37
+ switch (config.pathConfig.type) {
38
+ case "default":
39
+ case "access": {
40
+ if (splitPath.length > 1) {
41
+ if (config.pathConfig.currentContext === null) {
42
+ this.addPropertiesToAnObject(schema, splitPath.slice(1), config.pathConfig.currentContext, childRef);
43
+ }
44
+ else {
45
+ if (!((_a = schema[defsPropertyName]) === null || _a === void 0 ? void 0 : _a[splitPath[0]])) {
46
+ schema[defsPropertyName] = {
47
+ [splitPath[0]]: {},
48
+ };
49
+ }
50
+ this.addPropertiesToAContext(schema[defsPropertyName][splitPath[0]], splitPath.slice(1), config.pathConfig.currentContext, childRef);
51
+ }
52
+ }
53
+ break;
54
+ }
55
+ case "repeat": {
56
+ this.addContext(schema, splitPath.at(-1), // example items
57
+ config.pathConfig.currentContext, // example item
58
+ config.pathConfig.parentContext);
59
+ if (splitPath.length > 2) {
60
+ let updatedSchema = schema;
61
+ const hasParentContext = !!config.pathConfig.parentContext;
62
+ if (hasParentContext) {
63
+ updatedSchema = this.addPropertiesToAnObject((_b = schema[defsPropertyName]) === null || _b === void 0 ? void 0 : _b[config.pathConfig.parentContext], splitPath.slice(1, -1), config.pathConfig.parentContext, childRef);
64
+ }
65
+ this.addPropertiesToAnObject(updatedSchema, hasParentContext ? splitPath.slice(2) : splitPath.slice(1), config.pathConfig.currentContext, childRef, "array");
66
+ }
67
+ else if (splitPath.length > 1) {
68
+ let schemaDefinition;
69
+ if (config.pathConfig.parentContext) {
70
+ schemaDefinition = (_c = schema === null || schema === void 0 ? void 0 : schema[defsPropertyName]) === null || _c === void 0 ? void 0 : _c[config.pathConfig.parentContext];
71
+ }
72
+ this.addPropertiesToAnObject(schemaDefinition !== null && schemaDefinition !== void 0 ? schemaDefinition : schema, splitPath.slice(1), config.pathConfig.currentContext, childRef, "array");
73
+ }
74
+ else {
75
+ schema.type = "array";
76
+ schema[refPropertyName] = this.getDefsPath(config.pathConfig.currentContext);
77
+ }
78
+ break;
79
+ }
80
+ }
81
+ }
82
+ /**
83
+ * Gets the JSON schema for a property name
84
+ * @param rootPropertyName - the root property the JSON schema is mapped to
85
+ * @returns The JSON schema for the root property
86
+ */
87
+ getSchema(rootPropertyName) {
88
+ var _a;
89
+ return ((_a = Schema.jsonSchemaMap.get(this.customElementName).get(rootPropertyName)) !== null && _a !== void 0 ? _a : null);
90
+ }
91
+ /**
92
+ * Gets root properties
93
+ * @returns IterableIterator<string>
94
+ */
95
+ getRootProperties() {
96
+ return Schema.jsonSchemaMap.get(this.customElementName).keys();
97
+ }
98
+ /**
99
+ * Get a path split into property names
100
+ * @param path The dot syntax path e.g. a.b.c
101
+ * @returns An array of items in the path
102
+ */
103
+ getSplitPath(path) {
104
+ return path.split(".");
105
+ }
106
+ /**
107
+ * Gets the path to the $def
108
+ * @param context The context name e.g. {{item in items}} in a repeat creates the "item" context
109
+ * @returns A string to use as a $ref
110
+ */
111
+ getDefsPath(context) {
112
+ return `#/${defsPropertyName}/${context}`;
113
+ }
114
+ /**
115
+ * Get the schema $id
116
+ * @param customElementName - The custom element name
117
+ * @param propertyName - The property name
118
+ * @returns The ID that can be used in the JSON schema as $id
119
+ */
120
+ getSchemaId(customElementName, propertyName) {
121
+ return `https://fast.design/schemas/${customElementName}/${propertyName}.json`;
122
+ }
123
+ /**
124
+ * Add a new JSON schema to the JSON schema map
125
+ * @param propertyName The name of the property to assign this JSON schema to
126
+ */
127
+ addNewSchema(propertyName) {
128
+ Schema.jsonSchemaMap.get(this.customElementName).set(propertyName, {
129
+ $schema: "https://json-schema.org/draft/2019-09/schema",
130
+ $id: this.getSchemaId(this.customElementName, propertyName),
131
+ [defsPropertyName]: {},
132
+ });
133
+ }
134
+ /**
135
+ * Add properties to a context
136
+ * @param schema The schema to add the properties to
137
+ * @param splitPath The path split into property/context names
138
+ * @param context The paths context
139
+ */
140
+ addPropertiesToAContext(schema, splitPath, context, childRef) {
141
+ schema.type = "object";
142
+ if (schema.properties && !schema.properties[splitPath[0]]) {
143
+ schema.properties[splitPath[0]] = {};
144
+ }
145
+ else if (!schema.properties) {
146
+ schema.properties = {
147
+ [splitPath[0]]: {},
148
+ };
149
+ }
150
+ if (splitPath.length > 1) {
151
+ this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef);
152
+ }
153
+ else if (childRef) {
154
+ if (schema.properties[splitPath[0]].anyOf) {
155
+ schema.properties[splitPath[0]].anyOf.push({
156
+ [refPropertyName]: childRef,
157
+ });
158
+ }
159
+ else {
160
+ schema.properties[splitPath[0]].anyOf = [{ [refPropertyName]: childRef }];
161
+ }
162
+ }
163
+ }
164
+ /**
165
+ * Add properties to an object
166
+ * @param schema The schema to add the properties to
167
+ * @param splitPath The path split into property/context names
168
+ * @param context The paths context
169
+ * @param type The data type (see JSON schema for details)
170
+ */
171
+ addPropertiesToAnObject(schema, splitPath, context, childRef, type = "object") {
172
+ schema.type = "object";
173
+ if (schema.properties && !schema.properties[splitPath[0]]) {
174
+ schema.properties[splitPath[0]] = {};
175
+ }
176
+ else if (!schema.properties) {
177
+ schema.properties = {
178
+ [splitPath[0]]: {},
179
+ };
180
+ }
181
+ if (type === "object" && splitPath.length > 1) {
182
+ return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef, type);
183
+ }
184
+ else if (type === "array") {
185
+ if (splitPath.length > 1) {
186
+ return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef, type);
187
+ }
188
+ else {
189
+ return this.addArrayToAnObject(schema.properties[splitPath[0]], context);
190
+ }
191
+ }
192
+ if (schema.properties[splitPath[0]].anyOf && childRef) {
193
+ schema.properties[splitPath[0]].anyOf.push({ [refPropertyName]: childRef });
194
+ }
195
+ else if (childRef) {
196
+ schema.properties[splitPath[0]].anyOf = [{ [refPropertyName]: childRef }];
197
+ }
198
+ return schema.properties[splitPath[0]];
199
+ }
200
+ /**
201
+ * Add an array to an object property
202
+ * @param schema The schema to add the properties to
203
+ * @param context The name of the context
204
+ */
205
+ addArrayToAnObject(schema, context) {
206
+ schema.type = "array";
207
+ schema.items = {
208
+ [refPropertyName]: this.getDefsPath(context),
209
+ };
210
+ return schema.items;
211
+ }
212
+ /**
213
+ * Add a context to the $defs property
214
+ * @param schema The schema to use
215
+ * @param propertyName The name of the property the context belongs to
216
+ * @param currentContext The current context
217
+ * @param parentContext The parent context
218
+ * @returns
219
+ */
220
+ addContext(schema, propertyName, // e.g items
221
+ currentContext, // e.g. item
222
+ parentContext) {
223
+ if (schema[defsPropertyName][currentContext]) {
224
+ return;
225
+ }
226
+ schema[defsPropertyName][currentContext] = {
227
+ [fastContextMetaData]: propertyName,
228
+ [fastContextsMetaData]: this.getParentContexts(schema, parentContext),
229
+ };
230
+ }
231
+ /**
232
+ * Get parent contexts
233
+ * @param schema The schema to use
234
+ * @param parentContext The parent context
235
+ * @param contexts A list of parent contexts
236
+ * @returns
237
+ */
238
+ getParentContexts(schema, parentContext, contexts = []) {
239
+ var _a;
240
+ if (parentContext === null) {
241
+ return [null, ...contexts];
242
+ }
243
+ const parentParentContext = (_a = schema === null || schema === void 0 ? void 0 : schema[defsPropertyName]) === null || _a === void 0 ? void 0 : _a[parentContext][fastContextsMetaData];
244
+ return this.getParentContexts(schema, parentParentContext.at(-1), [parentContext, ...contexts]);
245
+ }
246
+ }
247
+ /**
248
+ * A JSON schema describing each root schema
249
+ */
250
+ Schema.jsonSchemaMap = new Map();