@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,375 @@
|
|
|
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 { indentedBy } from "../util";
|
|
16
|
+
import { IDLEntity, IDLKind, getExtAttribute, getVerbatimDts, hasExtAttribute, isCallback, isConstructor, isContainerType, isEnum, isInterface, isMethod, isPrimitiveType, isProperty, isReferenceType, isSyntheticEntry, isTypeParameterType, isTypedef, isUnionType, isPackage, isImport, isVersion, isNamespace, IDLExtendedAttributes, IDLAccessorAttribute, IDLVoidType, IDLStringType, IDLUndefinedType, isCallable, getSuperType, IDLAnyType, IDLContainerUtils, DebugUtils, mixMethodParametersAndTags, createReferenceType, transformMethodsAsync2ReturnPromise, isNamedNode, IDLThisType, isOptionalType, IDLI8Type, IDLU8Type, IDLI16Type, IDLU16Type, IDLI32Type, IDLU32Type, IDLI64Type, IDLU64Type, IDLF16Type, IDLF32Type, IDLF64Type, IDLBufferType, isUnspecifiedGenericType, IDLUnknownType, IDLBooleanType, IDLNumberType, IDLPointerType, IDLInterfaceSubkind, escapeIDLKeyword, getNamespacesPathFor } from "../idl";
|
|
17
|
+
import * as webidl2 from "webidl2";
|
|
18
|
+
import { resolveSyntheticType, toIDLNode } from "./deserialize";
|
|
19
|
+
import { Language } from "../Language";
|
|
20
|
+
export class CustomPrintVisitor {
|
|
21
|
+
constructor(resolver, language) {
|
|
22
|
+
this.resolver = resolver;
|
|
23
|
+
this.language = language;
|
|
24
|
+
this.output = [];
|
|
25
|
+
this.indent = 0;
|
|
26
|
+
}
|
|
27
|
+
visit(node, wrapNamespaces = false) {
|
|
28
|
+
const namespacesPath = wrapNamespaces ? getNamespacesPathFor(node) : [];
|
|
29
|
+
for (const namespace of namespacesPath) {
|
|
30
|
+
this.print(`${namespace.namespace ? "" : "declare "}namespace ${namespace.name} {`);
|
|
31
|
+
this.pushIndent();
|
|
32
|
+
}
|
|
33
|
+
if (hasExtAttribute(node, IDLExtendedAttributes.TSType) && this.language == Language.TS)
|
|
34
|
+
return;
|
|
35
|
+
if (isInterface(node)) {
|
|
36
|
+
this.printInterface(node);
|
|
37
|
+
}
|
|
38
|
+
else if (isMethod(node) || isConstructor(node) || isCallable(node)) {
|
|
39
|
+
this.printMethod(node);
|
|
40
|
+
}
|
|
41
|
+
else if (isPackage(node)) {
|
|
42
|
+
this.printPackage(node);
|
|
43
|
+
}
|
|
44
|
+
else if (isImport(node)) {
|
|
45
|
+
this.printImport(node);
|
|
46
|
+
}
|
|
47
|
+
else if (isProperty(node)) {
|
|
48
|
+
this.printProperty(node);
|
|
49
|
+
}
|
|
50
|
+
else if (isTypedef(node)) {
|
|
51
|
+
this.printTypedef(node);
|
|
52
|
+
}
|
|
53
|
+
else if (isEnum(node)) {
|
|
54
|
+
this.printEnum(node);
|
|
55
|
+
}
|
|
56
|
+
else if (isCallback(node)) {
|
|
57
|
+
this.printTypedef(node);
|
|
58
|
+
}
|
|
59
|
+
else if (isVersion(node)) {
|
|
60
|
+
this.printVersion(node);
|
|
61
|
+
}
|
|
62
|
+
else if (isNamespace(node)) {
|
|
63
|
+
this.printNamespace(node);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
throw new Error(`Unexpected node kind: ${IDLKind[node.kind]}`);
|
|
67
|
+
}
|
|
68
|
+
for (const namespace of namespacesPath) {
|
|
69
|
+
this.popIndent();
|
|
70
|
+
this.print("}");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
printMetadata(node) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
const imports = (_a = node.elements
|
|
76
|
+
.find(it => it.name === "imports")) === null || _a === void 0 ? void 0 : _a.initializer;
|
|
77
|
+
if (imports)
|
|
78
|
+
this.print(imports);
|
|
79
|
+
const exports = (_b = node.elements
|
|
80
|
+
.find(it => it.name === "exports")) === null || _b === void 0 ? void 0 : _b.initializer;
|
|
81
|
+
if (exports)
|
|
82
|
+
this.print(exports);
|
|
83
|
+
}
|
|
84
|
+
printConstant(node) {
|
|
85
|
+
this.print(`${node.namespace ? "" : "declare "}const ${node.name} : ${isPrimitiveType(node.type) ? "" : "typeof"} ${this.printTypeForTS(node.type)} = ${node.value}`);
|
|
86
|
+
}
|
|
87
|
+
printInterface(node) {
|
|
88
|
+
var _a;
|
|
89
|
+
let typeSpec = this.toTypeName(node);
|
|
90
|
+
// Workaround for an SDK declaration clash between `WrappedBuilder` and `ContentModifier`
|
|
91
|
+
if (node.name === "WrappedBuilder")
|
|
92
|
+
typeSpec = "WrappedBuilder<Args extends any[]>";
|
|
93
|
+
const entity = (_a = getExtAttribute(node, IDLExtendedAttributes.Entity)) !== null && _a !== void 0 ? _a : IDLEntity.Interface;
|
|
94
|
+
if (entity === IDLEntity.Literal) {
|
|
95
|
+
this.print(`${node.namespace ? "" : "declare "}type ${typeSpec} = ${this.literal(node, false, true)}`);
|
|
96
|
+
}
|
|
97
|
+
else if (entity === IDLEntity.Tuple) {
|
|
98
|
+
this.print(`${node.namespace ? "" : "declare "}type ${typeSpec} = ${this.literal(node, true, false)}`);
|
|
99
|
+
}
|
|
100
|
+
else if (entity === IDLEntity.NamedTuple) {
|
|
101
|
+
this.print(`${node.namespace ? "" : "declare "}type ${typeSpec} = ${this.literal(node, true, true)}`);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// restore globalScope
|
|
105
|
+
if (hasExtAttribute(node, IDLExtendedAttributes.GlobalScope)) {
|
|
106
|
+
node.methods.map(it => this.visit(it, true));
|
|
107
|
+
node.constants.map(it => this.visit(it, true));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
let interfaces = node.inheritance;
|
|
111
|
+
let keyword = "extends";
|
|
112
|
+
if (node.subkind === IDLInterfaceSubkind.Class) {
|
|
113
|
+
const superType = getSuperType(node);
|
|
114
|
+
if (superType)
|
|
115
|
+
typeSpec += ` extends ${this.printTypeForTS(superType)}`;
|
|
116
|
+
interfaces = interfaces.slice(1);
|
|
117
|
+
keyword = "implements";
|
|
118
|
+
}
|
|
119
|
+
if (interfaces.length > 0)
|
|
120
|
+
typeSpec += ` ${keyword} ${interfaces.map(it => this.toTypeName(it)).join(", ")}`;
|
|
121
|
+
this.print(`${node.namespace ? "" : "declare "}${entity.toLowerCase()} ${typeSpec} {`);
|
|
122
|
+
this.currentInterface = node;
|
|
123
|
+
this.pushIndent();
|
|
124
|
+
node.constructors.map(it => this.visit(it));
|
|
125
|
+
node.properties.map(it => this.visit(it));
|
|
126
|
+
node.methods.map(it => this.visit(it));
|
|
127
|
+
node.callables.map(it => this.visit(it));
|
|
128
|
+
let verbatim = getVerbatimDts(node);
|
|
129
|
+
if (verbatim) {
|
|
130
|
+
verbatim
|
|
131
|
+
.split("\n")
|
|
132
|
+
.map(it => this.print(it));
|
|
133
|
+
}
|
|
134
|
+
this.popIndent();
|
|
135
|
+
this.print("}");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// TODO: namespace-related-to-rework: drop 'isGlobal'
|
|
139
|
+
printMethod(node, isGlobal = false) {
|
|
140
|
+
const returnType = node.returnType && !(isConstructor(node) && this.currentInterface.subkind === IDLInterfaceSubkind.Class)
|
|
141
|
+
? `: ${this.printTypeForTS(node.returnType, true)}` : "";
|
|
142
|
+
const name = isConstructor(node)
|
|
143
|
+
? this.currentInterface.subkind === IDLInterfaceSubkind.Class ? "constructor" : "new"
|
|
144
|
+
: getName(node);
|
|
145
|
+
const typeParams = (node.typeParameters && node.typeParameters.length > 0) ? `<${node.typeParameters.join(",")}>` : "";
|
|
146
|
+
let preamble = "";
|
|
147
|
+
if (!isCallable(node)) {
|
|
148
|
+
const isStatic = isMethod(node) && node.isStatic && !node.isFree;
|
|
149
|
+
const isProtected = hasExtAttribute(node, IDLExtendedAttributes.Protected);
|
|
150
|
+
const isOptional = isMethod(node) && node.isOptional;
|
|
151
|
+
const isFree = isMethod(node) && node.isFree;
|
|
152
|
+
if (isGlobal && !isFree) // TODO: namespace-related-to-rework
|
|
153
|
+
throw new Error("internal error");
|
|
154
|
+
preamble = `${isFree ? `${node.namespace ? "" : "declare "}function ` : ""}${isProtected ? "protected " : ""}${isStatic ? "static " : ""}${name}${isOptional ? "?" : ""}`;
|
|
155
|
+
}
|
|
156
|
+
this.print(`${preamble}${typeParams}(${mixMethodParametersAndTags(node).map(p => this.paramText(p)).join(", ")})${returnType};`);
|
|
157
|
+
}
|
|
158
|
+
paramText(paramOrTag) {
|
|
159
|
+
const param = paramOrTag;
|
|
160
|
+
if (param.kind === IDLKind.Parameter)
|
|
161
|
+
return `${param.isVariadic ? "..." : ""}${getName(param)}${param.isOptional ? "?" : ""}: ${this.printTypeForTS(param.type)}`;
|
|
162
|
+
const tag = paramOrTag;
|
|
163
|
+
return `${tag.name}: ${tag.value}`;
|
|
164
|
+
}
|
|
165
|
+
printProperty(node) {
|
|
166
|
+
var _a;
|
|
167
|
+
const isCommonMethod = hasExtAttribute(node, IDLExtendedAttributes.CommonMethod);
|
|
168
|
+
let isProtected = hasExtAttribute(node, IDLExtendedAttributes.Protected);
|
|
169
|
+
if (isCommonMethod) {
|
|
170
|
+
// TODO: not very clean, but we don't need to print these so far.
|
|
171
|
+
// if (PeerGeneratorConfig.ignorePeerMethod.includes(node.name)) return
|
|
172
|
+
const typeParams = (_a = this.currentInterface) === null || _a === void 0 ? void 0 : _a.typeParameters;
|
|
173
|
+
const returnType = typeParams && typeParams.length > 0 ? typeParams[0] : this.currentInterface.name;
|
|
174
|
+
this.print(`${getName(node)}(value: ${this.printTypeForTS(node.type, undefined, undefined, isCommonMethod)}): ${returnType};`);
|
|
175
|
+
}
|
|
176
|
+
else if (hasExtAttribute(node, IDLExtendedAttributes.Accessor)) {
|
|
177
|
+
const accessorName = getExtAttribute(node, IDLExtendedAttributes.Accessor);
|
|
178
|
+
if (accessorName == IDLAccessorAttribute.Getter) {
|
|
179
|
+
this.print(`get ${getName(node)}(): ${this.printTypeForTS(node.type)};`);
|
|
180
|
+
}
|
|
181
|
+
else if (accessorName == IDLAccessorAttribute.Setter) {
|
|
182
|
+
this.print(`set ${getName(node)}(value: ${this.printTypeForTS(node.type)});`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.print(`${isProtected ? "protected " : ""}${node.isStatic ? "static " : ""}${node.isReadonly ? "readonly " : ""}${getName(node)}${node.isOptional ? "?" : ""}: ${this.printTypeForTS(node.type)};`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
printEnum(node) {
|
|
190
|
+
this.print(`${node.namespace ? "" : "declare "}enum ${node.name} {`);
|
|
191
|
+
this.pushIndent();
|
|
192
|
+
node.elements.forEach(it => {
|
|
193
|
+
const initializer = (it.type === IDLStringType ? `"${it.initializer}"` : `${it.initializer}`);
|
|
194
|
+
this.print(`${getName(it)} = ${initializer},`);
|
|
195
|
+
let originalName = getExtAttribute(it, IDLExtendedAttributes.OriginalEnumMemberName);
|
|
196
|
+
if (originalName && originalName != getName(it)) {
|
|
197
|
+
this.print(`${originalName} = ${initializer},`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
this.popIndent();
|
|
201
|
+
this.print("}");
|
|
202
|
+
}
|
|
203
|
+
printTypedef(node) {
|
|
204
|
+
// Let's skip imported declarations
|
|
205
|
+
if (isTypedef(node) &&
|
|
206
|
+
hasExtAttribute(node, IDLExtendedAttributes.Import)) {
|
|
207
|
+
let definition = this.resolver(createReferenceType(node.name, undefined, node));
|
|
208
|
+
// TODO: handle namespace case better!
|
|
209
|
+
// TODO: namespace-related-to-rework
|
|
210
|
+
throw new Error("not implemented yet");
|
|
211
|
+
// if (definition && !isTypedef(definition) && !hasExtAttribute(definition, IDLExtendedAttributes.Namespace)) {
|
|
212
|
+
// console.log(`Has better definition for ${node.name}: ${definition.fileName} ${definition.kind}`)
|
|
213
|
+
// return
|
|
214
|
+
// }
|
|
215
|
+
}
|
|
216
|
+
const text = isCallback(node) ? this.callback(node)
|
|
217
|
+
: hasExtAttribute(node, IDLExtendedAttributes.Import) ? IDLAnyType.name
|
|
218
|
+
: this.printTypeForTS(node.type);
|
|
219
|
+
const typeParams = node.typeParameters && node.typeParameters.length > 0 ? `<${node.typeParameters.join(",")}>` : "";
|
|
220
|
+
this.print(`${node.namespace ? '' : 'declare '}type ${getName(node)}${typeParams} = ${text};`);
|
|
221
|
+
}
|
|
222
|
+
printVersion(node) {
|
|
223
|
+
let text = node.value.join(".");
|
|
224
|
+
this.print(`// version ${text}`);
|
|
225
|
+
}
|
|
226
|
+
printNamespace(node) {
|
|
227
|
+
let verbatimDts = getVerbatimDts(node);
|
|
228
|
+
if (verbatimDts) {
|
|
229
|
+
this.print(verbatimDts);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this.print(`${node.namespace ? "" : "declare "} namespace ${node.name} {`);
|
|
233
|
+
this.pushIndent();
|
|
234
|
+
node.members.forEach(member => this.visit(member));
|
|
235
|
+
this.popIndent();
|
|
236
|
+
this.print("}");
|
|
237
|
+
}
|
|
238
|
+
printImport(node) {
|
|
239
|
+
this.print(`// import ${node.name}`);
|
|
240
|
+
}
|
|
241
|
+
printPackage(node) {
|
|
242
|
+
this.print(`// package ${node.name}`);
|
|
243
|
+
}
|
|
244
|
+
checkVerbatim(node) {
|
|
245
|
+
let verbatim = getExtAttribute(node, IDLExtendedAttributes.VerbatimDts);
|
|
246
|
+
if (verbatim) {
|
|
247
|
+
verbatim
|
|
248
|
+
.substring(1, verbatim.length - 2)
|
|
249
|
+
.split('\n')
|
|
250
|
+
.forEach(it => this.print(it));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
indented(input) {
|
|
254
|
+
return indentedBy(input, this.indent);
|
|
255
|
+
}
|
|
256
|
+
pushIndent() {
|
|
257
|
+
this.indent++;
|
|
258
|
+
}
|
|
259
|
+
popIndent() {
|
|
260
|
+
this.indent--;
|
|
261
|
+
}
|
|
262
|
+
print(value) {
|
|
263
|
+
if (value)
|
|
264
|
+
this.output.push(this.indented(value));
|
|
265
|
+
}
|
|
266
|
+
printTypeForTS(type, undefinedToVoid, sequenceToArrayInterface = false, isCommonMethod = false) {
|
|
267
|
+
if (!type)
|
|
268
|
+
throw new Error("Missing type");
|
|
269
|
+
if (isOptionalType(type))
|
|
270
|
+
return `${this.printTypeForTS(type.type, undefinedToVoid, sequenceToArrayInterface)} | undefined`;
|
|
271
|
+
if (isPrimitiveType(type)) {
|
|
272
|
+
switch (type) {
|
|
273
|
+
case IDLU8Type:
|
|
274
|
+
case IDLI8Type:
|
|
275
|
+
case IDLU16Type:
|
|
276
|
+
case IDLI16Type:
|
|
277
|
+
case IDLU32Type:
|
|
278
|
+
case IDLI32Type:
|
|
279
|
+
case IDLU64Type:
|
|
280
|
+
case IDLI64Type:
|
|
281
|
+
case IDLF16Type:
|
|
282
|
+
case IDLF32Type:
|
|
283
|
+
case IDLF64Type:
|
|
284
|
+
case IDLNumberType:
|
|
285
|
+
return "number";
|
|
286
|
+
case IDLAnyType: return "any";
|
|
287
|
+
case IDLUnknownType: return "unknown";
|
|
288
|
+
case IDLBufferType: return "ArrayBuffer";
|
|
289
|
+
case IDLBooleanType: return "boolean";
|
|
290
|
+
case IDLUndefinedType: return undefinedToVoid ? "void" : "undefined";
|
|
291
|
+
case IDLStringType: return "string";
|
|
292
|
+
case IDLVoidType: return "void";
|
|
293
|
+
case IDLThisType: return "T";
|
|
294
|
+
case IDLPointerType: return "number|bigint";
|
|
295
|
+
default: throw new Error(`Unknown primitive type ${DebugUtils.debugPrintType(type)}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (isContainerType(type)) {
|
|
299
|
+
if (!sequenceToArrayInterface && IDLContainerUtils.isSequence(type))
|
|
300
|
+
return `${type.elementType.map(it => this.printTypeForTS(it)).join(",")}[]`;
|
|
301
|
+
return `${mapContainerType(type)}<${type.elementType.map(it => this.printTypeForTS(it)).join(",")}>`;
|
|
302
|
+
}
|
|
303
|
+
if (isUnspecifiedGenericType(type))
|
|
304
|
+
return `${type.name}<${type.typeArguments.map(it => this.printTypeForTS(it)).join(",")}>`;
|
|
305
|
+
if (isReferenceType(type))
|
|
306
|
+
return this.toTypeName(type);
|
|
307
|
+
if (isUnionType(type))
|
|
308
|
+
return `(${type.types.map(it => this.printTypeForTS(it)).join("|")})`;
|
|
309
|
+
if (isTypeParameterType(type))
|
|
310
|
+
return type.name;
|
|
311
|
+
throw new Error(`Cannot map type: ${IDLKind[type.kind]}`);
|
|
312
|
+
}
|
|
313
|
+
toTypeName(node) {
|
|
314
|
+
var _a, _b;
|
|
315
|
+
if (isReferenceType(node)) {
|
|
316
|
+
const synthDecl = this.resolver(node);
|
|
317
|
+
if (synthDecl && isSyntheticEntry(synthDecl)) {
|
|
318
|
+
if (isInterface(synthDecl)) {
|
|
319
|
+
const isTuple = getExtAttribute(synthDecl, IDLExtendedAttributes.Entity) === IDLEntity.Tuple;
|
|
320
|
+
return this.literal(synthDecl, isTuple, !isTuple);
|
|
321
|
+
}
|
|
322
|
+
if (isCallback(synthDecl)) {
|
|
323
|
+
return this.callback(synthDecl);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (hasExtAttribute(node, IDLExtendedAttributes.Import)) {
|
|
328
|
+
return IDLAnyType.name;
|
|
329
|
+
}
|
|
330
|
+
let typeSpec = isNamedNode(node) ? node.name : "MISSING_TYPE_NAME";
|
|
331
|
+
if ((isInterface(node) || isCallback(node) || isTypedef(node)) && ((_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.length))
|
|
332
|
+
typeSpec = `${typeSpec}<${(_b = node.typeParameters) === null || _b === void 0 ? void 0 : _b.join(",")}>`;
|
|
333
|
+
if (isReferenceType(node) && node.typeArguments)
|
|
334
|
+
typeSpec = `${typeSpec}<${node.typeArguments.map(it => this.toTypeName(it))}>`;
|
|
335
|
+
return typeSpec;
|
|
336
|
+
}
|
|
337
|
+
callback(node) {
|
|
338
|
+
return `((${node.parameters.map(p => this.paramText(p)).join(", ")}) => ${this.printTypeForTS(node.returnType)})`;
|
|
339
|
+
}
|
|
340
|
+
literal(node, isTuple, includeFieldNames) {
|
|
341
|
+
return `${isTuple ? "[" : "{"} ${node.properties.map(it => {
|
|
342
|
+
const questionMark = it.isOptional ? "?" : "";
|
|
343
|
+
const type = this.printTypeForTS(it.type);
|
|
344
|
+
return includeFieldNames ? `${getName(it)}${questionMark}: ${type}` : `${type}${questionMark}`;
|
|
345
|
+
}).join(", ")} ${isTuple ? "]" : "}"}`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
export function idlToDtsString(name, content) {
|
|
349
|
+
let printer = new CustomPrintVisitor(resolveSyntheticType, Language.TS);
|
|
350
|
+
webidl2.parse(content)
|
|
351
|
+
.filter(it => !!it.type)
|
|
352
|
+
.map(it => toIDLNode(name, it))
|
|
353
|
+
.forEach(it => {
|
|
354
|
+
transformMethodsAsync2ReturnPromise(it);
|
|
355
|
+
printer.visit(it);
|
|
356
|
+
});
|
|
357
|
+
return printer.output.join("\n");
|
|
358
|
+
}
|
|
359
|
+
function mapContainerType(idlType) {
|
|
360
|
+
if (IDLContainerUtils.isSequence(idlType)) {
|
|
361
|
+
return "Array";
|
|
362
|
+
}
|
|
363
|
+
if (IDLContainerUtils.isRecord(idlType)) {
|
|
364
|
+
return "Map";
|
|
365
|
+
}
|
|
366
|
+
if (IDLContainerUtils.isPromise(idlType)) {
|
|
367
|
+
return "Promise";
|
|
368
|
+
}
|
|
369
|
+
throw new Error(`Unmapped container type: ${DebugUtils.debugPrintType(idlType)}`);
|
|
370
|
+
}
|
|
371
|
+
function getName(node) {
|
|
372
|
+
var _a;
|
|
373
|
+
return escapeIDLKeyword((_a = getExtAttribute(node, IDLExtendedAttributes.DtsName)) !== null && _a !== void 0 ? _a : node.name);
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=DtsPrinter.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { IDLEntry } from "../idl";
|
|
2
|
+
export declare function fromIDL(inputDir: string, inputFile: string | undefined, outputDir: string, extension: string, verbose: boolean, transform: (name: string, content: string) => string): void;
|
|
3
|
+
export declare function scanIDL(inputDir: string, inputFile: string | undefined): IDLEntry[][];
|
|
4
|
+
export declare const licence = "/*\n * Copyright (c) 2024 Huawei Device Co., Ltd.\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n";
|
|
5
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
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 fs from "fs";
|
|
16
|
+
import * as path from "path";
|
|
17
|
+
import * as webidl2 from "webidl2";
|
|
18
|
+
import { toIDLNode } from "./deserialize";
|
|
19
|
+
import { zip } from "../util";
|
|
20
|
+
function getFilesRecursive(dirPath, arrayOfFiles = []) {
|
|
21
|
+
let files = fs.readdirSync(dirPath);
|
|
22
|
+
arrayOfFiles = arrayOfFiles || [];
|
|
23
|
+
files.forEach((file) => {
|
|
24
|
+
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
|
|
25
|
+
arrayOfFiles = getFilesRecursive(dirPath + "/" + file, arrayOfFiles);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
arrayOfFiles.push(path.join(dirPath, file));
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return arrayOfFiles;
|
|
32
|
+
}
|
|
33
|
+
export function fromIDL(inputDir, inputFile, outputDir, extension, verbose, transform) {
|
|
34
|
+
inputDir = path.resolve(inputDir);
|
|
35
|
+
const files = inputFile
|
|
36
|
+
? [path.join(inputDir, inputFile)]
|
|
37
|
+
: getFilesRecursive(inputDir);
|
|
38
|
+
const results = files
|
|
39
|
+
.map((file) => transform(file, fs.readFileSync(file).toString()));
|
|
40
|
+
zip(files, results)
|
|
41
|
+
.forEach(([fileName, output]) => {
|
|
42
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
43
|
+
console.log('producing', path.relative(inputDir, fileName));
|
|
44
|
+
const outFile = path.join(outputDir, path.relative(inputDir, fileName).replace(".idl", extension));
|
|
45
|
+
if (verbose)
|
|
46
|
+
console.log(output);
|
|
47
|
+
if (!fs.existsSync(path.dirname(outFile))) {
|
|
48
|
+
fs.mkdirSync(path.dirname(outFile), { recursive: true });
|
|
49
|
+
}
|
|
50
|
+
fs.writeFileSync(outFile, licence.concat(output));
|
|
51
|
+
console.log("saved", outFile);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
export function scanIDL(inputDir, inputFile) {
|
|
55
|
+
inputDir = path.resolve(inputDir);
|
|
56
|
+
const files = inputFile
|
|
57
|
+
? [path.join(inputDir, inputFile)]
|
|
58
|
+
: fs.readdirSync(inputDir)
|
|
59
|
+
.map((elem) => path.join(inputDir, elem));
|
|
60
|
+
return files
|
|
61
|
+
.map((file) => {
|
|
62
|
+
let content = fs.readFileSync(file).toString();
|
|
63
|
+
let parsed = webidl2.parse(content);
|
|
64
|
+
return parsed.filter(it => !!it.type).map(it => toIDLNode(file, it));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export const licence = `/*
|
|
68
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
69
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
70
|
+
* you may not use this file except in compliance with the License.
|
|
71
|
+
* You may obtain a copy of the License at
|
|
72
|
+
*
|
|
73
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
74
|
+
*
|
|
75
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
76
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
77
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
78
|
+
* See the License for the specific language governing permissions and
|
|
79
|
+
* limitations under the License.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
`;
|
|
83
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as webidl2 from "webidl2";
|
|
2
|
+
import * as idl from "../idl";
|
|
3
|
+
import * as lib from "../library";
|
|
4
|
+
export declare function addSyntheticType(name: string, type: idl.IDLEntry): void;
|
|
5
|
+
export declare function resolveSyntheticType(type: idl.IDLReferenceType): idl.IDLEntry | undefined;
|
|
6
|
+
export declare function toIDLNode(file: string, node: webidl2.IDLRootType): idl.IDLEntry;
|
|
7
|
+
export declare function toIDL(file: string): idl.IDLEntry[];
|
|
8
|
+
export declare function toIDLFile(fileName: string): lib.IDLFile;
|
|
9
|
+
//# sourceMappingURL=deserialize.d.ts.map
|