@player-tools/xlr-utils 0.0.2-next.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/README.md +3 -0
- package/dist/index.cjs.js +526 -0
- package/dist/index.d.ts +126 -0
- package/dist/index.esm.js +477 -0
- package/package.json +35 -0
- package/src/annotations.ts +230 -0
- package/src/index.ts +5 -0
- package/src/test-helpers.ts +72 -0
- package/src/ts-helpers.ts +356 -0
- package/src/type-checks.ts +106 -0
- package/src/validation-helpers.ts +88 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
import ts__default from 'typescript';
|
|
3
|
+
|
|
4
|
+
var __defProp$1 = Object.defineProperty;
|
|
5
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues$1 = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
12
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols$1)
|
|
14
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
15
|
+
if (__propIsEnum$1.call(b, prop))
|
|
16
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
function extractDescription(text) {
|
|
21
|
+
if (!text) {
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
return { description: text };
|
|
25
|
+
}
|
|
26
|
+
function parentIsNonObjectPath(node) {
|
|
27
|
+
return node.parent && (ts.isArrayTypeNode(node.parent) || ts.isTupleTypeNode(node.parent) || ts.isOptionalTypeNode(node.parent) || ts.isRestTypeNode(node.parent) || ts.isUnionTypeNode(node.parent));
|
|
28
|
+
}
|
|
29
|
+
function recurseTypeChain(node, child) {
|
|
30
|
+
if (!node) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
if (ts.isArrayTypeNode(node) && node.parent && ts.isRestTypeNode(node.parent)) {
|
|
34
|
+
return recurseTypeChain(node.parent, node);
|
|
35
|
+
}
|
|
36
|
+
if (ts.isRestTypeNode(node)) {
|
|
37
|
+
return recurseTypeChain(node.parent, node);
|
|
38
|
+
}
|
|
39
|
+
if (ts.isOptionalTypeNode(node)) {
|
|
40
|
+
return recurseTypeChain(node.parent, node);
|
|
41
|
+
}
|
|
42
|
+
if (ts.isUnionTypeNode(node)) {
|
|
43
|
+
return recurseTypeChain(node.parent, node);
|
|
44
|
+
}
|
|
45
|
+
if (ts.isParenthesizedTypeNode(node)) {
|
|
46
|
+
return recurseTypeChain(node.parent, node);
|
|
47
|
+
}
|
|
48
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
49
|
+
return recurseTypeChain(node.parent, node);
|
|
50
|
+
}
|
|
51
|
+
if (ts.isArrayTypeNode(node)) {
|
|
52
|
+
return ["[]", ...recurseTypeChain(node.parent, node)];
|
|
53
|
+
}
|
|
54
|
+
if (ts.isTupleTypeNode(node)) {
|
|
55
|
+
const pos = node.elements.indexOf(child);
|
|
56
|
+
return [
|
|
57
|
+
...pos === -1 ? [] : [`${pos}`],
|
|
58
|
+
...recurseTypeChain(node.parent, node)
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
if (ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isPropertySignature(node)) {
|
|
62
|
+
return [node.name.getText(), ...recurseTypeChain(node.parent, node)];
|
|
63
|
+
}
|
|
64
|
+
if (parentIsNonObjectPath(node)) {
|
|
65
|
+
return recurseTypeChain(node.parent, node);
|
|
66
|
+
}
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
function extractTitle(node) {
|
|
70
|
+
const typeNames = recurseTypeChain(node, void 0).reverse().join(".");
|
|
71
|
+
if (!typeNames.length) {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
return { title: typeNames };
|
|
75
|
+
}
|
|
76
|
+
function stringifyDoc(docString) {
|
|
77
|
+
if (typeof docString === "undefined" || typeof docString === "string") {
|
|
78
|
+
return docString;
|
|
79
|
+
}
|
|
80
|
+
return docString.map(({ text }) => text).join(" ");
|
|
81
|
+
}
|
|
82
|
+
function extractTags(tags) {
|
|
83
|
+
const descriptions = [];
|
|
84
|
+
const examples = [];
|
|
85
|
+
const _default = [];
|
|
86
|
+
const see = [];
|
|
87
|
+
const extractSee = (tag) => {
|
|
88
|
+
var _a, _b, _c;
|
|
89
|
+
return `${tag.tagName ? `${(_a = tag.tagName) == null ? void 0 : _a.getText()} ` : ""}${(_c = (_b = stringifyDoc(tag.comment)) == null ? void 0 : _b.trim()) != null ? _c : ""}`;
|
|
90
|
+
};
|
|
91
|
+
tags.forEach((tag) => {
|
|
92
|
+
var _a, _b, _c, _d, _e, _f;
|
|
93
|
+
if (!tag.comment) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (tag.tagName.text === "example") {
|
|
97
|
+
examples.push((_b = (_a = stringifyDoc(tag.comment)) == null ? void 0 : _a.trim()) != null ? _b : "");
|
|
98
|
+
} else if (tag.tagName.text === "default") {
|
|
99
|
+
_default.push((_d = (_c = stringifyDoc(tag.comment)) == null ? void 0 : _c.trim()) != null ? _d : "");
|
|
100
|
+
} else if (tag.tagName.text === "see") {
|
|
101
|
+
see.push(extractSee(tag));
|
|
102
|
+
} else {
|
|
103
|
+
const text = (_f = (_e = stringifyDoc(tag.comment)) == null ? void 0 : _e.trim()) != null ? _f : "";
|
|
104
|
+
descriptions.push(`@${tag.tagName.text} ${text}`);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
return __spreadValues$1(__spreadValues$1(__spreadValues$1(__spreadValues$1({}, descriptions.length === 0 ? {} : { description: descriptions.join("\n") }), examples.length === 0 ? {} : { examples }), _default.length === 0 ? {} : { default: _default.join("\n") }), see.length === 0 ? {} : { see });
|
|
108
|
+
}
|
|
109
|
+
function join(t, separator = "\n") {
|
|
110
|
+
const unique = new Set(t).values();
|
|
111
|
+
return Array.from(unique).filter((s) => s !== void 0).join(separator).trim();
|
|
112
|
+
}
|
|
113
|
+
function mergeAnnotations(nodes) {
|
|
114
|
+
var _a;
|
|
115
|
+
const name = (_a = nodes.find((n) => n.name)) == null ? void 0 : _a.name;
|
|
116
|
+
const title = join(nodes.map((n) => n.title), ", ");
|
|
117
|
+
const description = join(nodes.map((n) => n.description));
|
|
118
|
+
const _default = join(nodes.map((n) => n.default));
|
|
119
|
+
const comment = join(nodes.map((n) => n.comment));
|
|
120
|
+
const examples = join(nodes.map((n) => Array.isArray(n.examples) ? join(n.examples) : n.examples));
|
|
121
|
+
const see = join(nodes.map((n) => Array.isArray(n.see) ? join(n.see) : n.see));
|
|
122
|
+
return __spreadValues$1(__spreadValues$1(__spreadValues$1(__spreadValues$1(__spreadValues$1(__spreadValues$1(__spreadValues$1({}, name ? { name } : {}), title ? { title } : {}), description ? { description } : {}), examples ? { examples } : {}), _default ? { default: _default } : {}), see ? { see } : {}), comment ? { comment } : {});
|
|
123
|
+
}
|
|
124
|
+
function decorateNode(node) {
|
|
125
|
+
var _a;
|
|
126
|
+
const { jsDoc } = node;
|
|
127
|
+
const titleAnnotation = extractTitle(node);
|
|
128
|
+
if (jsDoc && jsDoc.length) {
|
|
129
|
+
const first = jsDoc[0];
|
|
130
|
+
return mergeAnnotations([
|
|
131
|
+
extractDescription(stringifyDoc(first.comment)),
|
|
132
|
+
titleAnnotation,
|
|
133
|
+
extractTags((_a = first.tags) != null ? _a : [])
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
return titleAnnotation;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function isOptionalProperty(node) {
|
|
140
|
+
var _a;
|
|
141
|
+
return ((_a = node.questionToken) == null ? void 0 : _a.kind) === ts__default.SyntaxKind.QuestionToken;
|
|
142
|
+
}
|
|
143
|
+
function isGenericInterfaceDeclaration(node) {
|
|
144
|
+
var _a;
|
|
145
|
+
const length = (_a = node.typeParameters) == null ? void 0 : _a.length;
|
|
146
|
+
return length ? length > 0 : false;
|
|
147
|
+
}
|
|
148
|
+
function isGenericTypeDeclaration(node) {
|
|
149
|
+
var _a;
|
|
150
|
+
const length = (_a = node.typeParameters) == null ? void 0 : _a.length;
|
|
151
|
+
return length ? length > 0 : false;
|
|
152
|
+
}
|
|
153
|
+
function isTypeReferenceGeneric(node, typeChecker) {
|
|
154
|
+
const symbol = typeChecker.getSymbolAtLocation(node.typeName);
|
|
155
|
+
if (symbol && symbol.declarations) {
|
|
156
|
+
return symbol.declarations[0].kind === ts__default.SyntaxKind.TypeParameter;
|
|
157
|
+
}
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
function isTopLevelNode(node) {
|
|
161
|
+
return node.kind === ts__default.SyntaxKind.InterfaceDeclaration || node.kind === ts__default.SyntaxKind.TypeAliasDeclaration;
|
|
162
|
+
}
|
|
163
|
+
function isGenericNodeType(nt) {
|
|
164
|
+
var _a;
|
|
165
|
+
return ((_a = nt.genericTokens) == null ? void 0 : _a.length) > 0;
|
|
166
|
+
}
|
|
167
|
+
function isGenericNamedType(nt) {
|
|
168
|
+
var _a;
|
|
169
|
+
return ((_a = nt.genericTokens) == null ? void 0 : _a.length) > 0;
|
|
170
|
+
}
|
|
171
|
+
function isPrimitiveTypeNode(node) {
|
|
172
|
+
return node.type === "string" || node.type === "number" || node.type === "boolean" || node.type === "null" || node.type === "template" || node.type === "any" || node.type === "never" || node.type === "undefined" || node.type === "unknown";
|
|
173
|
+
}
|
|
174
|
+
function isNonNullable(a) {
|
|
175
|
+
return a !== null || a !== void 0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function propertyToTuple(node) {
|
|
179
|
+
var _a, _b;
|
|
180
|
+
let key = (_a = node.children) == null ? void 0 : _a[0].value;
|
|
181
|
+
if (key.includes("-")) {
|
|
182
|
+
key = `'${key}'`;
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
key,
|
|
186
|
+
value: (_b = node.children) == null ? void 0 : _b[1]
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
function makePropertyMap(node) {
|
|
190
|
+
var _a;
|
|
191
|
+
const m = new Map();
|
|
192
|
+
(_a = node.children) == null ? void 0 : _a.forEach((child) => {
|
|
193
|
+
const property = propertyToTuple(child);
|
|
194
|
+
m.set(property.key, property.value);
|
|
195
|
+
});
|
|
196
|
+
return m;
|
|
197
|
+
}
|
|
198
|
+
function isNode(obj) {
|
|
199
|
+
return typeof obj !== "string" || typeof obj !== "number" || typeof obj !== "boolean";
|
|
200
|
+
}
|
|
201
|
+
function resolveConditional(conditional) {
|
|
202
|
+
const { left, right } = conditional.check;
|
|
203
|
+
if (isPrimitiveTypeNode(left) && isPrimitiveTypeNode(right)) {
|
|
204
|
+
if (left.const && right.const) {
|
|
205
|
+
return left.const === right.const ? conditional.value.true : conditional.value.false;
|
|
206
|
+
}
|
|
207
|
+
if ((left.type === "any" || left.type === "unknown") && (right.type === "any" || right.type === "unknown")) {
|
|
208
|
+
return conditional.value.true;
|
|
209
|
+
}
|
|
210
|
+
if ((left.type === "null" || left.type === "undefined") && (right.type === "null" || right.type === "undefined")) {
|
|
211
|
+
return conditional.value.true;
|
|
212
|
+
}
|
|
213
|
+
if (left.type === right.type) {
|
|
214
|
+
return conditional.value.true;
|
|
215
|
+
}
|
|
216
|
+
return conditional.value.false;
|
|
217
|
+
}
|
|
218
|
+
return conditional;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
var __defProp = Object.defineProperty;
|
|
222
|
+
var __defProps = Object.defineProperties;
|
|
223
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
224
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
225
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
226
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
227
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
228
|
+
var __spreadValues = (a, b) => {
|
|
229
|
+
for (var prop in b || (b = {}))
|
|
230
|
+
if (__hasOwnProp.call(b, prop))
|
|
231
|
+
__defNormalProp(a, prop, b[prop]);
|
|
232
|
+
if (__getOwnPropSymbols)
|
|
233
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
234
|
+
if (__propIsEnum.call(b, prop))
|
|
235
|
+
__defNormalProp(a, prop, b[prop]);
|
|
236
|
+
}
|
|
237
|
+
return a;
|
|
238
|
+
};
|
|
239
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
240
|
+
function tsStripOptionalType(node) {
|
|
241
|
+
return ts.isOptionalTypeNode(node) ? node.type : node;
|
|
242
|
+
}
|
|
243
|
+
function isExportedDeclaration(node) {
|
|
244
|
+
var _a;
|
|
245
|
+
return !!((_a = node.modifiers) == null ? void 0 : _a.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword));
|
|
246
|
+
}
|
|
247
|
+
function isNodeExported(node) {
|
|
248
|
+
return (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 || !!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile;
|
|
249
|
+
}
|
|
250
|
+
function getReferencedType(node, typeChecker) {
|
|
251
|
+
var _a;
|
|
252
|
+
let symbol = typeChecker.getSymbolAtLocation(node.typeName);
|
|
253
|
+
if (symbol && (symbol.flags & ts.SymbolFlags.Alias) === ts.SymbolFlags.Alias) {
|
|
254
|
+
symbol = typeChecker.getAliasedSymbol(symbol);
|
|
255
|
+
}
|
|
256
|
+
const varDecl = (_a = symbol == null ? void 0 : symbol.declarations) == null ? void 0 : _a[0];
|
|
257
|
+
if (varDecl && (ts.isInterfaceDeclaration(varDecl) || ts.isTypeAliasDeclaration(varDecl))) {
|
|
258
|
+
return { declaration: varDecl, exported: isNodeExported(varDecl) };
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function getStringLiteralsFromUnion(node) {
|
|
262
|
+
if (ts.isUnionTypeNode(node)) {
|
|
263
|
+
return new Set(node.types.map((type) => {
|
|
264
|
+
if (ts.isLiteralTypeNode(type) && ts.isStringLiteral(type.literal)) {
|
|
265
|
+
return type.literal.text;
|
|
266
|
+
}
|
|
267
|
+
return "";
|
|
268
|
+
}));
|
|
269
|
+
}
|
|
270
|
+
if (ts.isLiteralTypeNode(node) && ts.isStringLiteral(node.literal)) {
|
|
271
|
+
return new Set([node.literal.text]);
|
|
272
|
+
}
|
|
273
|
+
return new Set();
|
|
274
|
+
}
|
|
275
|
+
function buildTemplateRegex(node, typeChecker) {
|
|
276
|
+
let regex = node.head.text;
|
|
277
|
+
node.templateSpans.forEach((span) => {
|
|
278
|
+
var _a;
|
|
279
|
+
let type = span.type.kind;
|
|
280
|
+
if (ts.isTypeReferenceNode(span.type)) {
|
|
281
|
+
let symbol = typeChecker.getSymbolAtLocation(span.type.typeName);
|
|
282
|
+
if (symbol && (symbol.flags & ts.SymbolFlags.Alias) === ts.SymbolFlags.Alias) {
|
|
283
|
+
symbol = typeChecker.getAliasedSymbol(symbol);
|
|
284
|
+
}
|
|
285
|
+
type = ((_a = symbol == null ? void 0 : symbol.declarations) == null ? void 0 : _a[0]).type.kind;
|
|
286
|
+
}
|
|
287
|
+
if (type === ts.SyntaxKind.StringKeyword) {
|
|
288
|
+
regex += ".*";
|
|
289
|
+
} else if (type === ts.SyntaxKind.NumberKeyword) {
|
|
290
|
+
regex += "[0-9]*";
|
|
291
|
+
} else if (type === ts.SyntaxKind.BooleanKeyword) {
|
|
292
|
+
regex += "true|false";
|
|
293
|
+
}
|
|
294
|
+
regex += span.literal.text;
|
|
295
|
+
});
|
|
296
|
+
return regex;
|
|
297
|
+
}
|
|
298
|
+
function fillInGenerics(xlrNode, generics) {
|
|
299
|
+
var _a;
|
|
300
|
+
let localGenerics;
|
|
301
|
+
if (generics) {
|
|
302
|
+
localGenerics = new Map(generics);
|
|
303
|
+
} else {
|
|
304
|
+
localGenerics = new Map();
|
|
305
|
+
if (isGenericNodeType(xlrNode)) {
|
|
306
|
+
(_a = xlrNode.genericTokens) == null ? void 0 : _a.forEach((token) => {
|
|
307
|
+
var _a2;
|
|
308
|
+
localGenerics.set(token.symbol, (_a2 = token.default) != null ? _a2 : token.constraints);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (xlrNode.type === "ref") {
|
|
313
|
+
if (localGenerics.has(xlrNode.ref)) {
|
|
314
|
+
return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, localGenerics.get(xlrNode.ref)), xlrNode.genericArguments ? {
|
|
315
|
+
genericArguments: xlrNode.genericArguments.map((ga) => fillInGenerics(ga, localGenerics))
|
|
316
|
+
} : {}), xlrNode.title ? { title: xlrNode.title } : {}), xlrNode.name ? { name: xlrNode.name } : {}), xlrNode.description ? { description: xlrNode.description } : {}), xlrNode.comment ? { comment: xlrNode.comment } : {});
|
|
317
|
+
}
|
|
318
|
+
return __spreadValues(__spreadValues({}, xlrNode), xlrNode.genericArguments ? {
|
|
319
|
+
genericArguments: xlrNode.genericArguments.map((ga) => fillInGenerics(ga, localGenerics))
|
|
320
|
+
} : {});
|
|
321
|
+
}
|
|
322
|
+
if (xlrNode.type === "object") {
|
|
323
|
+
const newProperties = {};
|
|
324
|
+
Object.getOwnPropertyNames(xlrNode.properties).forEach((propName) => {
|
|
325
|
+
const prop = xlrNode.properties[propName];
|
|
326
|
+
newProperties[propName] = {
|
|
327
|
+
required: prop.required,
|
|
328
|
+
node: fillInGenerics(prop.node, localGenerics)
|
|
329
|
+
};
|
|
330
|
+
});
|
|
331
|
+
return __spreadProps(__spreadValues({}, xlrNode), {
|
|
332
|
+
properties: newProperties
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
if (xlrNode.type === "array") {
|
|
336
|
+
xlrNode.elementType = fillInGenerics(xlrNode.elementType, localGenerics);
|
|
337
|
+
} else if (xlrNode.type === "or" || xlrNode.type === "and") {
|
|
338
|
+
let pointer;
|
|
339
|
+
if (xlrNode.type === "or") {
|
|
340
|
+
pointer = xlrNode.or;
|
|
341
|
+
} else {
|
|
342
|
+
pointer = xlrNode.and;
|
|
343
|
+
}
|
|
344
|
+
return __spreadProps(__spreadValues({}, xlrNode), {
|
|
345
|
+
[xlrNode.type]: pointer.map((prop) => {
|
|
346
|
+
return fillInGenerics(prop, localGenerics);
|
|
347
|
+
})
|
|
348
|
+
});
|
|
349
|
+
} else if (xlrNode.type === "record") {
|
|
350
|
+
return __spreadProps(__spreadValues({}, xlrNode), {
|
|
351
|
+
keyType: fillInGenerics(xlrNode.keyType, localGenerics),
|
|
352
|
+
valueType: fillInGenerics(xlrNode.valueType, localGenerics)
|
|
353
|
+
});
|
|
354
|
+
} else if (xlrNode.type === "conditional") {
|
|
355
|
+
const filledInConditional = __spreadProps(__spreadValues({}, xlrNode), {
|
|
356
|
+
check: {
|
|
357
|
+
left: fillInGenerics(xlrNode.check.left, localGenerics),
|
|
358
|
+
right: fillInGenerics(xlrNode.check.right, localGenerics)
|
|
359
|
+
},
|
|
360
|
+
value: {
|
|
361
|
+
true: fillInGenerics(xlrNode.value.true, localGenerics),
|
|
362
|
+
false: fillInGenerics(xlrNode.value.false, localGenerics)
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
if (filledInConditional.check.left.type !== "ref" && filledInConditional.check.right.type !== "ref") {
|
|
366
|
+
return __spreadValues({
|
|
367
|
+
name: xlrNode.name,
|
|
368
|
+
title: xlrNode.title
|
|
369
|
+
}, resolveConditional(filledInConditional));
|
|
370
|
+
}
|
|
371
|
+
return filledInConditional;
|
|
372
|
+
}
|
|
373
|
+
return xlrNode;
|
|
374
|
+
}
|
|
375
|
+
function applyPickOrOmitToNodeType(baseObject, operation, properties) {
|
|
376
|
+
if (baseObject.type === "object") {
|
|
377
|
+
const newObject = __spreadValues({}, baseObject);
|
|
378
|
+
Object.keys(baseObject.properties).forEach((key) => {
|
|
379
|
+
if (operation === "Omit" && properties.has(key) || operation === "Pick" && !properties.has(key)) {
|
|
380
|
+
delete newObject.properties[key];
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
if (Object.keys(newObject.properties).length === 0 && (operation !== "Omit" || newObject.additionalProperties === false)) {
|
|
384
|
+
return void 0;
|
|
385
|
+
}
|
|
386
|
+
return newObject;
|
|
387
|
+
}
|
|
388
|
+
let pointer;
|
|
389
|
+
if (baseObject.type === "and") {
|
|
390
|
+
pointer = baseObject.and;
|
|
391
|
+
} else if (baseObject.type === "or") {
|
|
392
|
+
pointer = baseObject.or;
|
|
393
|
+
} else {
|
|
394
|
+
throw new Error(`Error: Can not apply ${operation} to type ${baseObject.type}`);
|
|
395
|
+
}
|
|
396
|
+
const pickedTypes = pointer.map((type) => {
|
|
397
|
+
const node = applyPickOrOmitToNodeType(type, operation, properties);
|
|
398
|
+
if (node === void 0) {
|
|
399
|
+
return void 0;
|
|
400
|
+
}
|
|
401
|
+
return __spreadProps(__spreadValues({}, node), { additionalProperties: false });
|
|
402
|
+
}).filter((type) => type !== void 0);
|
|
403
|
+
if (pickedTypes.length === 0) {
|
|
404
|
+
return void 0;
|
|
405
|
+
}
|
|
406
|
+
if (pickedTypes.length === 1) {
|
|
407
|
+
return pickedTypes[0];
|
|
408
|
+
}
|
|
409
|
+
if (baseObject.type === "and") {
|
|
410
|
+
return __spreadProps(__spreadValues({}, baseObject), { and: pickedTypes });
|
|
411
|
+
}
|
|
412
|
+
return __spreadProps(__spreadValues({}, baseObject), { or: pickedTypes });
|
|
413
|
+
}
|
|
414
|
+
function applyPartialOrRequiredToNodeType(baseObject, modifier) {
|
|
415
|
+
if (baseObject.type === "object") {
|
|
416
|
+
const newObject = __spreadValues({}, baseObject);
|
|
417
|
+
Object.keys(baseObject.properties).forEach((key) => {
|
|
418
|
+
newObject.properties[key].required = modifier;
|
|
419
|
+
});
|
|
420
|
+
return newObject;
|
|
421
|
+
}
|
|
422
|
+
if (baseObject.type === "and") {
|
|
423
|
+
const pickedTypes = baseObject.and.map((type) => applyPartialOrRequiredToNodeType(type, modifier));
|
|
424
|
+
return __spreadProps(__spreadValues({}, baseObject), { and: pickedTypes });
|
|
425
|
+
}
|
|
426
|
+
if (baseObject.type === "or") {
|
|
427
|
+
const pickedTypes = baseObject.or.map((type) => applyPartialOrRequiredToNodeType(type, modifier));
|
|
428
|
+
return __spreadProps(__spreadValues({}, baseObject), { or: pickedTypes });
|
|
429
|
+
}
|
|
430
|
+
throw new Error(`Error: Can not apply ${modifier ? "Required" : "Partial"} to type ${baseObject.type}`);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function setupTestEnv(sourceCode, mockFileName = "filename.ts") {
|
|
434
|
+
const sourceFile = ts.createSourceFile(mockFileName, sourceCode, ts.ScriptTarget.Latest, true);
|
|
435
|
+
const compilerHost = {
|
|
436
|
+
getSourceFile(filename) {
|
|
437
|
+
if (filename === mockFileName) {
|
|
438
|
+
return sourceFile;
|
|
439
|
+
}
|
|
440
|
+
return void 0;
|
|
441
|
+
},
|
|
442
|
+
writeFile(name, text, writeByteOrderMark) {
|
|
443
|
+
},
|
|
444
|
+
getDefaultLibFileName() {
|
|
445
|
+
return "lib.d.ts";
|
|
446
|
+
},
|
|
447
|
+
useCaseSensitiveFileNames() {
|
|
448
|
+
return false;
|
|
449
|
+
},
|
|
450
|
+
getCanonicalFileName(filename) {
|
|
451
|
+
return filename;
|
|
452
|
+
},
|
|
453
|
+
getCurrentDirectory() {
|
|
454
|
+
return "";
|
|
455
|
+
},
|
|
456
|
+
getNewLine() {
|
|
457
|
+
return "\n";
|
|
458
|
+
},
|
|
459
|
+
fileExists(fileName) {
|
|
460
|
+
if (fileName === mockFileName) {
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
return false;
|
|
464
|
+
},
|
|
465
|
+
readFile(fileName) {
|
|
466
|
+
if (fileName === mockFileName) {
|
|
467
|
+
return sourceCode;
|
|
468
|
+
}
|
|
469
|
+
return void 0;
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
const program = ts.createProgram([mockFileName], {}, compilerHost);
|
|
473
|
+
return { sf: sourceFile, tc: program.getTypeChecker() };
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export { applyPartialOrRequiredToNodeType, applyPickOrOmitToNodeType, buildTemplateRegex, decorateNode, fillInGenerics, getReferencedType, getStringLiteralsFromUnion, isExportedDeclaration, isGenericInterfaceDeclaration, isGenericNamedType, isGenericNodeType, isGenericTypeDeclaration, isNode, isNodeExported, isNonNullable, isOptionalProperty, isPrimitiveTypeNode, isTopLevelNode, isTypeReferenceGeneric, makePropertyMap, propertyToTuple, resolveConditional, setupTestEnv, tsStripOptionalType };
|
|
477
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-tools/xlr-utils",
|
|
3
|
+
"version": "0.0.2-next.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"typescript": "4.4.4",
|
|
10
|
+
"jsonc-parser": "^2.3.1"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@player-tools/xlr": "0.0.2-next.0",
|
|
14
|
+
"@babel/runtime": "7.15.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.cjs.js",
|
|
17
|
+
"module": "dist/index.esm.js",
|
|
18
|
+
"typings": "dist/index.d.ts",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/player-ui/tools"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/player-ui/tools/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://player-ui.github.io",
|
|
29
|
+
"contributors": [
|
|
30
|
+
{
|
|
31
|
+
"name": "Ketan Reddy",
|
|
32
|
+
"url": "https://github.com/KetanReddy"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|