@idlizer/core 2.0.15
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/build/lib/src/IndentedPrinter.d.ts +28 -0
- package/build/lib/src/IndentedPrinter.js +81 -0
- package/build/lib/src/Language.d.ts +15 -0
- package/build/lib/src/Language.js +42 -0
- package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +318 -0
- package/build/lib/src/LanguageWriters/ArgConvertors.js +1015 -0
- package/build/lib/src/LanguageWriters/InteropConvertor.d.ts +36 -0
- package/build/lib/src/LanguageWriters/InteropConvertor.js +174 -0
- package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +386 -0
- package/build/lib/src/LanguageWriters/LanguageWriter.js +746 -0
- package/build/lib/src/LanguageWriters/common.d.ts +17 -0
- package/build/lib/src/LanguageWriters/common.js +33 -0
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +8 -0
- package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +35 -0
- package/build/lib/src/LanguageWriters/index.d.ts +3 -0
- package/build/lib/src/LanguageWriters/index.js +3 -0
- package/build/lib/src/LanguageWriters/nameConvertor.d.ts +28 -0
- package/build/lib/src/LanguageWriters/nameConvertor.js +61 -0
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +147 -0
- package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +578 -0
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +46 -0
- package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +121 -0
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +116 -0
- package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +472 -0
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +72 -0
- package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +329 -0
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +83 -0
- package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +275 -0
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +93 -0
- package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +390 -0
- package/build/lib/src/LibraryInterface.d.ts +21 -0
- package/build/lib/src/LibraryInterface.js +2 -0
- package/build/lib/src/config.d.ts +8 -0
- package/build/lib/src/config.js +20 -0
- package/build/lib/src/from-idl/DtsPrinter.d.ts +35 -0
- package/build/lib/src/from-idl/DtsPrinter.js +375 -0
- package/build/lib/src/from-idl/common.d.ts +5 -0
- package/build/lib/src/from-idl/common.js +83 -0
- package/build/lib/src/from-idl/deserialize.d.ts +9 -0
- package/build/lib/src/from-idl/deserialize.js +384 -0
- package/build/lib/src/from-idl/toString.d.ts +2 -0
- package/build/lib/src/from-idl/toString.js +18 -0
- package/build/lib/src/from-idl/webidl2-utils.d.ts +20 -0
- package/build/lib/src/from-idl/webidl2-utils.js +84 -0
- package/build/lib/src/idl.d.ts +404 -0
- package/build/lib/src/idl.js +1015 -0
- package/build/lib/src/idlize.d.ts +4 -0
- package/build/lib/src/idlize.js +52 -0
- package/build/lib/src/index.d.ts +34 -0
- package/build/lib/src/index.js +48 -0
- package/build/lib/src/inheritance.d.ts +16 -0
- package/build/lib/src/inheritance.js +76 -0
- package/build/lib/src/languageSpecificKeywords.d.ts +5 -0
- package/build/lib/src/languageSpecificKeywords.js +51 -0
- package/build/lib/src/library.d.ts +67 -0
- package/build/lib/src/library.js +190 -0
- package/build/lib/src/options.d.ts +11 -0
- package/build/lib/src/options.js +16 -0
- package/build/lib/src/peer-generation/PrimitiveType.d.ts +24 -0
- package/build/lib/src/peer-generation/PrimitiveType.js +52 -0
- package/build/lib/src/peer-generation/ReferenceResolver.d.ts +9 -0
- package/build/lib/src/peer-generation/ReferenceResolver.js +37 -0
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +28 -0
- package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +82 -0
- package/build/lib/src/peer-generation/idl/common.d.ts +9 -0
- package/build/lib/src/peer-generation/idl/common.js +70 -0
- package/build/lib/src/peer-generation/unions.d.ts +29 -0
- package/build/lib/src/peer-generation/unions.js +111 -0
- package/build/lib/src/rand_utils.d.ts +6 -0
- package/build/lib/src/rand_utils.js +100 -0
- package/build/lib/src/util.d.ts +75 -0
- package/build/lib/src/util.js +622 -0
- package/package.json +49 -0
- package/webidl2.js/LICENSE +21 -0
- package/webidl2.js/README.md +827 -0
- package/webidl2.js/dist/package.json +3 -0
- package/webidl2.js/dist/webidl2.js +4547 -0
- package/webidl2.js/package.json +55 -0
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import * as fs from "fs";
|
|
17
|
+
import * as ts from "typescript";
|
|
18
|
+
import { Language } from './Language';
|
|
19
|
+
/** True if this is visible outside this file, false otherwise */
|
|
20
|
+
export function isNodePublic(node) {
|
|
21
|
+
return (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Public) !== 0;
|
|
22
|
+
}
|
|
23
|
+
export function nameOrNull(name) {
|
|
24
|
+
if (name == undefined)
|
|
25
|
+
return undefined;
|
|
26
|
+
if (ts.isIdentifier(name)) {
|
|
27
|
+
return ts.idText(name);
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
export function isNamedDeclaration(node) {
|
|
32
|
+
return ("name" in node);
|
|
33
|
+
}
|
|
34
|
+
export function asString(node) {
|
|
35
|
+
if (node === undefined)
|
|
36
|
+
return "undefined node";
|
|
37
|
+
if (ts.isIdentifier(node))
|
|
38
|
+
return ts.idText(node);
|
|
39
|
+
if (ts.isQualifiedName(node))
|
|
40
|
+
return `${identName(node.left)}.${identName(node.right)}`;
|
|
41
|
+
if (ts.isStringLiteral(node))
|
|
42
|
+
return node.text;
|
|
43
|
+
if (ts.isTypeReferenceNode(node))
|
|
44
|
+
return `${ts.SyntaxKind[node.kind]}(${asString(node.typeName)})`;
|
|
45
|
+
if (ts.isImportTypeNode(node))
|
|
46
|
+
return `${ts.SyntaxKind[node.kind]}(${asString(node.qualifier)})`;
|
|
47
|
+
if (isNamedDeclaration(node)) {
|
|
48
|
+
if (node.name === undefined) {
|
|
49
|
+
return `${ts.SyntaxKind[node.kind]}(undefined name)`;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return `${ts.SyntaxKind[node.kind]}(${asString(node.name)})`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return `${ts.SyntaxKind[node.kind]}`;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function arrayAt(array, index) {
|
|
60
|
+
return array ? array[index >= 0 ? index : array.length + index] : undefined;
|
|
61
|
+
}
|
|
62
|
+
export function getComment(sourceFile, node) {
|
|
63
|
+
const commentRanges = ts.getLeadingCommentRanges(sourceFile.getFullText(), node.getFullStart());
|
|
64
|
+
if (!commentRanges)
|
|
65
|
+
return "";
|
|
66
|
+
return commentRanges
|
|
67
|
+
.map(range => sourceFile.getFullText().slice(range.pos, range.end))
|
|
68
|
+
.join("\n");
|
|
69
|
+
}
|
|
70
|
+
export function getSymbolByNode(typechecker, node) {
|
|
71
|
+
return typechecker.getSymbolAtLocation(node);
|
|
72
|
+
}
|
|
73
|
+
export function getDeclarationsByNode(typechecker, node) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
return (_b = (_a = getSymbolByNode(typechecker, node)) === null || _a === void 0 ? void 0 : _a.getDeclarations()) !== null && _b !== void 0 ? _b : [];
|
|
76
|
+
}
|
|
77
|
+
export function findRealDeclarations(typechecker, node) {
|
|
78
|
+
const declarations = getDeclarationsByNode(typechecker, node);
|
|
79
|
+
const first = declarations[0];
|
|
80
|
+
if (first && ts.isExportAssignment(first)) {
|
|
81
|
+
return findRealDeclarations(typechecker, first.expression);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
return declarations;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export function getExportedDeclarationNameByDecl(declaration) {
|
|
88
|
+
let declName = declaration.name ? ts.idText(declaration.name) : undefined;
|
|
89
|
+
let current = declaration;
|
|
90
|
+
while (current != undefined && !ts.isSourceFile(current)) {
|
|
91
|
+
current = current.parent;
|
|
92
|
+
}
|
|
93
|
+
let source = current;
|
|
94
|
+
let exportedName = declName;
|
|
95
|
+
source.forEachChild(it => {
|
|
96
|
+
if (ts.isExportDeclaration(it)) {
|
|
97
|
+
let clause = it.exportClause;
|
|
98
|
+
if (clause && ts.isNamedExportBindings(clause) && ts.isNamedExports(clause)) {
|
|
99
|
+
clause.elements.forEach(it => {
|
|
100
|
+
let propName = it.propertyName ? ts.idText(it.propertyName) : undefined;
|
|
101
|
+
let property = ts.idText(it.name);
|
|
102
|
+
if (propName == declName) {
|
|
103
|
+
exportedName = property;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return exportedName;
|
|
110
|
+
}
|
|
111
|
+
export function getExportedDeclarationNameByNode(typechecker, node) {
|
|
112
|
+
let declarations = getDeclarationsByNode(typechecker, node);
|
|
113
|
+
if (declarations.length == 0)
|
|
114
|
+
return undefined;
|
|
115
|
+
return getExportedDeclarationNameByDecl(declarations[0]);
|
|
116
|
+
}
|
|
117
|
+
function hasModifier(modifierLikes, modifier) {
|
|
118
|
+
return (modifierLikes === null || modifierLikes === void 0 ? void 0 : modifierLikes.find(it => it.kind === modifier)) != undefined;
|
|
119
|
+
}
|
|
120
|
+
export function isReadonly(modifierLikes) {
|
|
121
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.ReadonlyKeyword);
|
|
122
|
+
}
|
|
123
|
+
export function isExport(modifierLikes) {
|
|
124
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.ExportKeyword);
|
|
125
|
+
}
|
|
126
|
+
export function isAbstract(modifierLikes) {
|
|
127
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.AbstractKeyword);
|
|
128
|
+
}
|
|
129
|
+
export function isStatic(modifierLikes) {
|
|
130
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.StaticKeyword);
|
|
131
|
+
}
|
|
132
|
+
export function isAsync(modifierLikes) {
|
|
133
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.AsyncKeyword);
|
|
134
|
+
}
|
|
135
|
+
export function isPrivate(modifierLikes) {
|
|
136
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.PrivateKeyword);
|
|
137
|
+
}
|
|
138
|
+
export function isProtected(modifierLikes) {
|
|
139
|
+
return hasModifier(modifierLikes, ts.SyntaxKind.ProtectedKeyword);
|
|
140
|
+
}
|
|
141
|
+
export function getLineNumberString(sourceFile, position) {
|
|
142
|
+
let pos = ts.getLineAndCharacterOfPosition(sourceFile, position);
|
|
143
|
+
return `${pos.line + 1}:${pos.character}`;
|
|
144
|
+
}
|
|
145
|
+
export function isDefined(value) {
|
|
146
|
+
return !!value;
|
|
147
|
+
}
|
|
148
|
+
export function capitalize(string) {
|
|
149
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
150
|
+
}
|
|
151
|
+
export function capitalizeConstantName(string) {
|
|
152
|
+
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
|
|
153
|
+
}
|
|
154
|
+
export function dropLast(text, chars) {
|
|
155
|
+
return text.substring(0, text.length - chars);
|
|
156
|
+
}
|
|
157
|
+
export function dropSuffix(text, suffix) {
|
|
158
|
+
if (!text.endsWith(suffix))
|
|
159
|
+
return text;
|
|
160
|
+
return dropLast(text, suffix.length);
|
|
161
|
+
}
|
|
162
|
+
export function toSet(option) {
|
|
163
|
+
let set = new Set();
|
|
164
|
+
if (option) {
|
|
165
|
+
option
|
|
166
|
+
.split(",")
|
|
167
|
+
.forEach(it => set.add(it));
|
|
168
|
+
}
|
|
169
|
+
return set;
|
|
170
|
+
}
|
|
171
|
+
export function getOrPut(map, key, create) {
|
|
172
|
+
const gotten = map.get(key);
|
|
173
|
+
if (gotten) {
|
|
174
|
+
return gotten;
|
|
175
|
+
}
|
|
176
|
+
const created = create(key);
|
|
177
|
+
map.set(key, created);
|
|
178
|
+
return created;
|
|
179
|
+
}
|
|
180
|
+
export function indentedBy(input, indentedBy) {
|
|
181
|
+
if (input.length > 0 || input.endsWith('\n')) {
|
|
182
|
+
let space = "";
|
|
183
|
+
for (let i = 0; i < indentedBy; i++)
|
|
184
|
+
space += " ";
|
|
185
|
+
return `${space}${input}`;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
return "";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export function typeOrUndefined(type) {
|
|
192
|
+
var _a;
|
|
193
|
+
let needUndefined = true;
|
|
194
|
+
if (ts.isUnionTypeNode(type)) {
|
|
195
|
+
(_a = type.types) === null || _a === void 0 ? void 0 : _a.forEach(it => {
|
|
196
|
+
if (it.kind == ts.SyntaxKind.UndefinedKeyword)
|
|
197
|
+
needUndefined = false;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (!needUndefined)
|
|
201
|
+
return type;
|
|
202
|
+
return ts.factory.createUnionTypeNode([
|
|
203
|
+
type,
|
|
204
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
|
|
205
|
+
]);
|
|
206
|
+
}
|
|
207
|
+
export function forEachExpanding(array, action) {
|
|
208
|
+
let i = 0;
|
|
209
|
+
while (true) {
|
|
210
|
+
if (i === array.length)
|
|
211
|
+
break;
|
|
212
|
+
action(array[i]);
|
|
213
|
+
i += 1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export function isTypeParamSuitableType(type) {
|
|
217
|
+
if (ts.isTypeReferenceNode(type)) {
|
|
218
|
+
return !['boolean', 'number', 'string', 'undefined', 'any'].includes(type.typeName.getText());
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
export function heritageTypes(typechecker, clause) {
|
|
223
|
+
return clause
|
|
224
|
+
.types
|
|
225
|
+
.map(it => {
|
|
226
|
+
let type = typechecker.getTypeAtLocation(it.expression);
|
|
227
|
+
let typeNode = typechecker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.NoTruncation);
|
|
228
|
+
if (typeNode && ts.isTypeReferenceNode(typeNode))
|
|
229
|
+
return typeNode;
|
|
230
|
+
return undefined;
|
|
231
|
+
})
|
|
232
|
+
.filter(it => it != undefined);
|
|
233
|
+
}
|
|
234
|
+
export function heritageDeclarations(typechecker, clause) {
|
|
235
|
+
return clause
|
|
236
|
+
.types
|
|
237
|
+
.map(it => {
|
|
238
|
+
var _a;
|
|
239
|
+
let decls = getDeclarationsByNode(typechecker, it.expression);
|
|
240
|
+
return (_a = decls[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
241
|
+
})
|
|
242
|
+
.filter(isDefined);
|
|
243
|
+
}
|
|
244
|
+
export function typeName(type) {
|
|
245
|
+
const entityName = typeEntityName(type);
|
|
246
|
+
if (!entityName)
|
|
247
|
+
return undefined;
|
|
248
|
+
if (ts.isIdentifier(entityName))
|
|
249
|
+
return ts.idText(entityName);
|
|
250
|
+
if (ts.isQualifiedName(entityName)) {
|
|
251
|
+
// a.b.c is QualifiedName((QualifiedName a, b), c) so the right one is always an Identifier?
|
|
252
|
+
if (!ts.isIdentifier(entityName.right))
|
|
253
|
+
throw new Error(`Unexpected right of QualifiedName ${asString(entityName.right)}`);
|
|
254
|
+
return ts.idText(entityName.right);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
export function typeEntityName(type) {
|
|
258
|
+
if (ts.isTypeReferenceNode(type))
|
|
259
|
+
return type.typeName;
|
|
260
|
+
if (ts.isTypeQueryNode(type))
|
|
261
|
+
return type.exprName;
|
|
262
|
+
if (ts.isImportTypeNode(type))
|
|
263
|
+
return type.qualifier;
|
|
264
|
+
throw new Error("unsupported");
|
|
265
|
+
}
|
|
266
|
+
export function zip(left, right) {
|
|
267
|
+
if (left.length != right.length)
|
|
268
|
+
throw new Error("Arrays of different length");
|
|
269
|
+
return left.map((_, i) => [left[i], right[i]]);
|
|
270
|
+
}
|
|
271
|
+
export function identNameWithNamespace(node, language) {
|
|
272
|
+
let parent = node.parent;
|
|
273
|
+
while (parent && !ts.isModuleDeclaration(parent))
|
|
274
|
+
parent = parent.parent;
|
|
275
|
+
if (parent) {
|
|
276
|
+
const separator = language === Language.CPP ? '_' : '.';
|
|
277
|
+
return `${identName(parent.name)}${separator}${identName(node)}`;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
return identName(node);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
export function identName(node) {
|
|
284
|
+
if (!node)
|
|
285
|
+
return undefined;
|
|
286
|
+
if (node.kind == ts.SyntaxKind.AnyKeyword)
|
|
287
|
+
return `any`;
|
|
288
|
+
if (node.kind == ts.SyntaxKind.ObjectKeyword)
|
|
289
|
+
return `object`;
|
|
290
|
+
if (node.kind == ts.SyntaxKind.StringKeyword)
|
|
291
|
+
return `string`;
|
|
292
|
+
if (node.kind == ts.SyntaxKind.BooleanKeyword)
|
|
293
|
+
return `boolean`;
|
|
294
|
+
if (node.kind == ts.SyntaxKind.BigIntKeyword)
|
|
295
|
+
return `bigint`;
|
|
296
|
+
if (node.kind == ts.SyntaxKind.NumberKeyword)
|
|
297
|
+
return `number`;
|
|
298
|
+
if (node.kind == ts.SyntaxKind.VoidKeyword)
|
|
299
|
+
return `void`;
|
|
300
|
+
if (node.kind == ts.SyntaxKind.UndefinedKeyword)
|
|
301
|
+
return `undefined`;
|
|
302
|
+
if (ts.isVariableDeclaration(node)) {
|
|
303
|
+
return identString(node.name);
|
|
304
|
+
}
|
|
305
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
306
|
+
return identString(node.typeName);
|
|
307
|
+
}
|
|
308
|
+
if (ts.isArrayTypeNode(node)) {
|
|
309
|
+
return `Array`;
|
|
310
|
+
}
|
|
311
|
+
if (ts.isQualifiedName(node)) {
|
|
312
|
+
return identName(node.right);
|
|
313
|
+
}
|
|
314
|
+
if (ts.isModuleDeclaration(node)) {
|
|
315
|
+
return identString(node.name);
|
|
316
|
+
}
|
|
317
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
318
|
+
return identString(node.name);
|
|
319
|
+
}
|
|
320
|
+
if (ts.isPropertyDeclaration(node)) {
|
|
321
|
+
// TODO: mention parent's name
|
|
322
|
+
return identString(node.name);
|
|
323
|
+
}
|
|
324
|
+
if (ts.isInterfaceDeclaration(node)) {
|
|
325
|
+
return identString(node.name);
|
|
326
|
+
}
|
|
327
|
+
if (ts.isClassDeclaration(node)) {
|
|
328
|
+
return identString(node.name);
|
|
329
|
+
}
|
|
330
|
+
if (ts.isMethodDeclaration(node)) {
|
|
331
|
+
return identString(node.name);
|
|
332
|
+
}
|
|
333
|
+
if (ts.isEnumDeclaration(node)) {
|
|
334
|
+
return identString(node.name);
|
|
335
|
+
}
|
|
336
|
+
if (ts.isEnumMember(node)) {
|
|
337
|
+
return identString(node.name);
|
|
338
|
+
}
|
|
339
|
+
if (ts.isComputedPropertyName(node)) {
|
|
340
|
+
return identString(node);
|
|
341
|
+
}
|
|
342
|
+
if (ts.isUnionTypeNode(node)) {
|
|
343
|
+
return `UnionType`;
|
|
344
|
+
}
|
|
345
|
+
if (ts.isFunctionTypeNode(node)) {
|
|
346
|
+
return `FunctionType`;
|
|
347
|
+
}
|
|
348
|
+
if (ts.isIdentifier(node))
|
|
349
|
+
return identString(node);
|
|
350
|
+
if (ts.isImportTypeNode(node))
|
|
351
|
+
return `imported ${identString(node.qualifier)}`;
|
|
352
|
+
if (ts.isTypeLiteralNode(node))
|
|
353
|
+
return `TypeLiteral`;
|
|
354
|
+
if (ts.isTupleTypeNode(node))
|
|
355
|
+
return `TupleType`;
|
|
356
|
+
if (ts.isIndexSignatureDeclaration(node))
|
|
357
|
+
return `IndexSignature`;
|
|
358
|
+
if (ts.isIndexedAccessTypeNode(node))
|
|
359
|
+
return `IndexedAccess`;
|
|
360
|
+
if (ts.isTemplateLiteralTypeNode(node))
|
|
361
|
+
return `TemplateLiteral`;
|
|
362
|
+
if (ts.isParameter(node))
|
|
363
|
+
return `Parameter`;
|
|
364
|
+
if (ts.isParenthesizedTypeNode(node))
|
|
365
|
+
return identName(node.type);
|
|
366
|
+
if (ts.isIntersectionTypeNode(node))
|
|
367
|
+
return node.types.map(it => identName(it)).join("&");
|
|
368
|
+
if (node.kind === ts.SyntaxKind.UnknownKeyword)
|
|
369
|
+
return `UnknownKeyword`;
|
|
370
|
+
throw new Error(`Unknown: ${ts.SyntaxKind[node.kind]}`);
|
|
371
|
+
}
|
|
372
|
+
export function identString(node) {
|
|
373
|
+
if (!node)
|
|
374
|
+
return undefined;
|
|
375
|
+
if (ts.isStringLiteral(node))
|
|
376
|
+
return node.text;
|
|
377
|
+
if (ts.isNumericLiteral(node))
|
|
378
|
+
return node.text;
|
|
379
|
+
if (ts.isIdentifier(node))
|
|
380
|
+
return ts.idText(node);
|
|
381
|
+
if (ts.isQualifiedName(node))
|
|
382
|
+
return `${identString(node.left)}.${identName(node.right)}`;
|
|
383
|
+
if (ts.isComputedPropertyName(node))
|
|
384
|
+
return "<computed property>";
|
|
385
|
+
throw new Error("Unknown");
|
|
386
|
+
}
|
|
387
|
+
export const defaultCompilerOptions = {
|
|
388
|
+
target: ts.ScriptTarget.ES5,
|
|
389
|
+
module: ts.ModuleKind.CommonJS,
|
|
390
|
+
noLib: true,
|
|
391
|
+
types: []
|
|
392
|
+
};
|
|
393
|
+
export function serializerBaseMethods() {
|
|
394
|
+
const program = ts.createProgram([
|
|
395
|
+
"./utils/ts/SerializerBase.ts",
|
|
396
|
+
"./utils/ts/types.ts",
|
|
397
|
+
], defaultCompilerOptions);
|
|
398
|
+
const serializerDecl = program.getSourceFiles()
|
|
399
|
+
.find(it => it.fileName.includes("SerializerBase"));
|
|
400
|
+
// TODO: pack classes with npm package
|
|
401
|
+
if (serializerDecl === undefined)
|
|
402
|
+
return [];
|
|
403
|
+
const methods = [];
|
|
404
|
+
visit(serializerDecl);
|
|
405
|
+
return methods;
|
|
406
|
+
function visit(node) {
|
|
407
|
+
if (ts.isSourceFile(node))
|
|
408
|
+
node.statements.forEach(visit);
|
|
409
|
+
if (ts.isClassDeclaration(node))
|
|
410
|
+
node.members.filter(ts.isMethodDeclaration).forEach(visit);
|
|
411
|
+
if (ts.isMethodDeclaration(node))
|
|
412
|
+
methods.push(node.name.getText(serializerDecl));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
export function getNameWithoutQualifiersRight(node) {
|
|
416
|
+
if (!node)
|
|
417
|
+
return undefined;
|
|
418
|
+
if (ts.isQualifiedName(node)) {
|
|
419
|
+
return identName(node.right);
|
|
420
|
+
}
|
|
421
|
+
if (ts.isIdentifier(node)) {
|
|
422
|
+
return identName(node);
|
|
423
|
+
}
|
|
424
|
+
throw new Error("Impossible");
|
|
425
|
+
}
|
|
426
|
+
export function getNameWithoutQualifiersLeft(node) {
|
|
427
|
+
if (!node)
|
|
428
|
+
return undefined;
|
|
429
|
+
if (ts.isQualifiedName(node)) {
|
|
430
|
+
return identName(node.left);
|
|
431
|
+
}
|
|
432
|
+
if (ts.isIdentifier(node)) {
|
|
433
|
+
return identName(node);
|
|
434
|
+
}
|
|
435
|
+
throw new Error("Impossible");
|
|
436
|
+
}
|
|
437
|
+
export function snakeCaseToCamelCase(input, tailToLowerCase = false) {
|
|
438
|
+
return input
|
|
439
|
+
.split("_")
|
|
440
|
+
.map(it => capitalize(tailToLowerCase ? it.toLowerCase() : it))
|
|
441
|
+
.join("");
|
|
442
|
+
}
|
|
443
|
+
export function toCamelCase(input) {
|
|
444
|
+
return input
|
|
445
|
+
.replace(/([-_][a-z])/g, group => group.toUpperCase().replace('-', '').replace('_', ''))
|
|
446
|
+
.replace(/^[A-Z]/, match => match.toLowerCase());
|
|
447
|
+
}
|
|
448
|
+
export function isUpperCase(s) {
|
|
449
|
+
return s === s.toUpperCase();
|
|
450
|
+
}
|
|
451
|
+
function isLowerCase(s) {
|
|
452
|
+
return s === s.toLowerCase();
|
|
453
|
+
}
|
|
454
|
+
function isDigit(s) {
|
|
455
|
+
return s >= '0' && s <= '9';
|
|
456
|
+
}
|
|
457
|
+
export function camelCaseToUpperSnakeCase(input) {
|
|
458
|
+
function boundaryFromLowerToUpperCase(s1, s2) {
|
|
459
|
+
return s2 !== undefined && (isLowerCase(s1) && !isDigit(s1)) && (isUpperCase(s2)) ? '_' : '';
|
|
460
|
+
}
|
|
461
|
+
function toUpperSnakeCase(s) {
|
|
462
|
+
return Array.from(s)
|
|
463
|
+
.map((c, i) => `${c.toUpperCase()}${boundaryFromLowerToUpperCase(c, s[i + 1])}`)
|
|
464
|
+
.join('');
|
|
465
|
+
}
|
|
466
|
+
return input.split('_')
|
|
467
|
+
.filter(s => s !== "")
|
|
468
|
+
.map(s => toUpperSnakeCase(s))
|
|
469
|
+
.join('_');
|
|
470
|
+
}
|
|
471
|
+
export function renameDtsToPeer(fileName, language, withFileExtension = true) {
|
|
472
|
+
const renamed = "Ark"
|
|
473
|
+
.concat(snakeCaseToCamelCase(fileName))
|
|
474
|
+
.replace(".d.ts", "")
|
|
475
|
+
.replace(".idl", "")
|
|
476
|
+
.concat("Peer");
|
|
477
|
+
if (withFileExtension) {
|
|
478
|
+
return renamed.concat(language.extension);
|
|
479
|
+
}
|
|
480
|
+
return renamed;
|
|
481
|
+
}
|
|
482
|
+
export function renameDtsToComponent(fileName, language, withFileExtension = true) {
|
|
483
|
+
const renamed = "Ark"
|
|
484
|
+
.concat(snakeCaseToCamelCase(fileName))
|
|
485
|
+
.replace(".d.ts", "")
|
|
486
|
+
.replace(".idl", "");
|
|
487
|
+
if (withFileExtension) {
|
|
488
|
+
return renamed.concat(language.extension);
|
|
489
|
+
}
|
|
490
|
+
return renamed;
|
|
491
|
+
}
|
|
492
|
+
export function renameDtsToInterfaces(fileName, language, withFileExtension = true) {
|
|
493
|
+
const renamed = "Ark"
|
|
494
|
+
.concat(snakeCaseToCamelCase(fileName), "Interfaces")
|
|
495
|
+
.replace(".d.ts", "")
|
|
496
|
+
.replace(".idl", "");
|
|
497
|
+
if (withFileExtension) {
|
|
498
|
+
return renamed.concat(language.extension);
|
|
499
|
+
}
|
|
500
|
+
return renamed;
|
|
501
|
+
}
|
|
502
|
+
export function renameClassToBuilderClass(className, language, withFileExtension = true) {
|
|
503
|
+
const renamed = "Ark"
|
|
504
|
+
.concat(snakeCaseToCamelCase(className))
|
|
505
|
+
.concat("Builder");
|
|
506
|
+
if (withFileExtension) {
|
|
507
|
+
return renamed.concat(language.extension);
|
|
508
|
+
}
|
|
509
|
+
return renamed;
|
|
510
|
+
}
|
|
511
|
+
export function renameClassToMaterialized(className, language, withFileExtension = true) {
|
|
512
|
+
const name = className.endsWith("Internal") ? className.substring(0, className.length - "Internal".length) : className;
|
|
513
|
+
const renamed = "Ark"
|
|
514
|
+
.concat(snakeCaseToCamelCase(name))
|
|
515
|
+
.concat("Materialized");
|
|
516
|
+
if (withFileExtension) {
|
|
517
|
+
return renamed.concat(language.extension);
|
|
518
|
+
}
|
|
519
|
+
return renamed;
|
|
520
|
+
}
|
|
521
|
+
export function importTypeName(type, asType = false) {
|
|
522
|
+
return asType ? "object" : identName(type.qualifier);
|
|
523
|
+
}
|
|
524
|
+
export function throwException(message) {
|
|
525
|
+
throw new Error(message);
|
|
526
|
+
}
|
|
527
|
+
export function className(node) {
|
|
528
|
+
var _a;
|
|
529
|
+
return (_a = nameOrNull(node.name)) !== null && _a !== void 0 ? _a : throwException(`Nameless component ${asString(node)}`);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Add a prefix to an enum value which camel case name coincidence
|
|
533
|
+
* with the the same upper case name for an another enum value
|
|
534
|
+
*/
|
|
535
|
+
export function nameEnumValues(enumTarget) {
|
|
536
|
+
const prefix = "LEGACY";
|
|
537
|
+
const nameToIndex = new Map();
|
|
538
|
+
enumTarget.members
|
|
539
|
+
.map(it => identName(it.name))
|
|
540
|
+
.forEach((name, index) => {
|
|
541
|
+
let upperCaseName;
|
|
542
|
+
if (isUpperCase(name)) {
|
|
543
|
+
upperCaseName = name;
|
|
544
|
+
const i = nameToIndex.get(upperCaseName);
|
|
545
|
+
if (i !== undefined) {
|
|
546
|
+
nameToIndex.set(`${prefix}_${upperCaseName}`, i);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
upperCaseName = camelCaseToUpperSnakeCase(name);
|
|
551
|
+
if (nameToIndex.has(upperCaseName)) {
|
|
552
|
+
upperCaseName = `${prefix}_${upperCaseName}`;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
nameToIndex.set(upperCaseName, index);
|
|
556
|
+
});
|
|
557
|
+
const enumValues = new Array(nameToIndex.size);
|
|
558
|
+
for (const [name, index] of nameToIndex.entries()) {
|
|
559
|
+
enumValues[index] = name;
|
|
560
|
+
}
|
|
561
|
+
return enumValues;
|
|
562
|
+
}
|
|
563
|
+
export function groupBy(values, selector) {
|
|
564
|
+
const map = new Map();
|
|
565
|
+
values.forEach(value => {
|
|
566
|
+
const key = selector(value);
|
|
567
|
+
getOrPut(map, key, it => []).push(value);
|
|
568
|
+
});
|
|
569
|
+
return map;
|
|
570
|
+
}
|
|
571
|
+
export function removeExt(filename) {
|
|
572
|
+
return filename.replaceAll(path.extname(filename), '');
|
|
573
|
+
}
|
|
574
|
+
export function warn(message) {
|
|
575
|
+
console.log(`WARNING: ${message}`);
|
|
576
|
+
}
|
|
577
|
+
export function hashCodeFromString(value) {
|
|
578
|
+
let hash = 5381;
|
|
579
|
+
for (let i = 0; i < value.length; i++) {
|
|
580
|
+
hash = (hash * 33) ^ value.charCodeAt(i);
|
|
581
|
+
hash |= 0;
|
|
582
|
+
}
|
|
583
|
+
return hash;
|
|
584
|
+
}
|
|
585
|
+
export function forceWriteFile(filePath, content) {
|
|
586
|
+
path.dirname(filePath).split(path.sep).reduce((last, dir) => {
|
|
587
|
+
const dirPath = path.join(last, dir);
|
|
588
|
+
if (!fs.existsSync(dirPath)) {
|
|
589
|
+
fs.mkdirSync(dirPath);
|
|
590
|
+
}
|
|
591
|
+
return dirPath;
|
|
592
|
+
}, ".");
|
|
593
|
+
fs.writeFileSync(filePath, content);
|
|
594
|
+
}
|
|
595
|
+
export function findVersion() {
|
|
596
|
+
if (process.env.npm_package_version)
|
|
597
|
+
return process.env.npm_package_version;
|
|
598
|
+
let packageJson = path.join(__dirname, '..', 'package.json');
|
|
599
|
+
try {
|
|
600
|
+
let json = fs.readFileSync(packageJson).toString();
|
|
601
|
+
return json ? JSON.parse(json).version : undefined;
|
|
602
|
+
}
|
|
603
|
+
catch (e) {
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
export function zipMany(...xs) {
|
|
608
|
+
const max = xs.reduce((max, it) => it.length > max ? it.length : max, 0);
|
|
609
|
+
const result = [];
|
|
610
|
+
for (let i = 0; i < max; ++i) {
|
|
611
|
+
const row = [];
|
|
612
|
+
for (const x of xs) {
|
|
613
|
+
const element = i < x.length
|
|
614
|
+
? x[i]
|
|
615
|
+
: undefined;
|
|
616
|
+
row.push(element);
|
|
617
|
+
}
|
|
618
|
+
result.push(row);
|
|
619
|
+
}
|
|
620
|
+
return result;
|
|
621
|
+
}
|
|
622
|
+
//# sourceMappingURL=util.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@idlizer/core",
|
|
3
|
+
"version": "2.0.15",
|
|
4
|
+
"description": "",
|
|
5
|
+
"types": "build/lib/src/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": [
|
|
8
|
+
"./build/lib/src/index.js"
|
|
9
|
+
],
|
|
10
|
+
"./idl": [
|
|
11
|
+
"./build/lib/src/idl.js"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"typesVersions": {
|
|
15
|
+
"*": {
|
|
16
|
+
"idl": [
|
|
17
|
+
"build/lib/src/idl.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"*": [
|
|
20
|
+
"build/lib/src/index.d.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"build/lib/**/*.js",
|
|
26
|
+
"build/lib/**/*.d.ts",
|
|
27
|
+
"webidl2.js/dist/**/*.js",
|
|
28
|
+
"webidl2.js/package.json"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"engines": {
|
|
32
|
+
"npm": ">= 8",
|
|
33
|
+
"node": ">= 18"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@koalaui/interop": "2.0.15",
|
|
38
|
+
"typescript": "4.9.5",
|
|
39
|
+
"@types/node": "^18.0.0",
|
|
40
|
+
"commander": "^10.0.0",
|
|
41
|
+
"comment-parser": "^1.4.1",
|
|
42
|
+
"cross-env": "^7.0.3",
|
|
43
|
+
"minimist": "^1.2.8"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"clean": "rimraf -g build",
|
|
47
|
+
"compile": "tsc -b ."
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Robin Berjon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|