@idlizer/core 2.1.2 → 2.1.7

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 (80) hide show
  1. package/build/lib/src/LanguageWriters/ArgConvertors.d.ts +3 -4
  2. package/build/lib/src/LanguageWriters/ArgConvertors.js +43 -32
  3. package/build/lib/src/LanguageWriters/LanguageWriter.d.ts +35 -17
  4. package/build/lib/src/LanguageWriters/LanguageWriter.js +30 -56
  5. package/build/lib/src/LanguageWriters/convertors/CJConvertors.d.ts +3 -1
  6. package/build/lib/src/LanguageWriters/convertors/CJConvertors.js +32 -11
  7. package/build/lib/src/LanguageWriters/convertors/CppConvertors.d.ts +5 -2
  8. package/build/lib/src/LanguageWriters/convertors/CppConvertors.js +71 -62
  9. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.d.ts +1 -1
  10. package/build/lib/src/LanguageWriters/convertors/ETSConvertors.js +9 -19
  11. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.d.ts +4 -2
  12. package/build/lib/src/LanguageWriters/convertors/InteropConvertors.js +10 -3
  13. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.d.ts +2 -1
  14. package/build/lib/src/LanguageWriters/convertors/JavaConvertors.js +24 -4
  15. package/build/lib/src/LanguageWriters/convertors/TSConvertors.d.ts +8 -4
  16. package/build/lib/src/LanguageWriters/convertors/TSConvertors.js +87 -33
  17. package/build/lib/src/LanguageWriters/nameConvertor.d.ts +3 -1
  18. package/build/lib/src/LanguageWriters/nameConvertor.js +5 -1
  19. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.d.ts +19 -11
  20. package/build/lib/src/LanguageWriters/writers/CJLanguageWriter.js +34 -47
  21. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.d.ts +4 -1
  22. package/build/lib/src/LanguageWriters/writers/CLikeLanguageWriter.js +1 -1
  23. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.d.ts +9 -9
  24. package/build/lib/src/LanguageWriters/writers/CppLanguageWriter.js +15 -24
  25. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.d.ts +17 -8
  26. package/build/lib/src/LanguageWriters/writers/ETSLanguageWriter.js +48 -23
  27. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.d.ts +4 -6
  28. package/build/lib/src/LanguageWriters/writers/JavaLanguageWriter.js +15 -14
  29. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.d.ts +13 -8
  30. package/build/lib/src/LanguageWriters/writers/TsLanguageWriter.js +33 -52
  31. package/build/lib/src/LibraryInterface.d.ts +1 -4
  32. package/build/lib/src/config.d.ts +813 -110
  33. package/build/lib/src/config.js +25 -3
  34. package/build/lib/src/configDescriber.d.ts +31 -4
  35. package/build/lib/src/configDescriber.js +101 -3
  36. package/build/lib/src/from-idl/DtsPrinter.js +31 -18
  37. package/build/lib/src/from-idl/IDLLinter.d.ts +40 -7
  38. package/build/lib/src/from-idl/IDLLinter.js +211 -25
  39. package/build/lib/src/from-idl/common.js +1 -1
  40. package/build/lib/src/from-idl/deserialize.d.ts +10 -1
  41. package/build/lib/src/from-idl/deserialize.js +486 -352
  42. package/build/lib/src/idl.d.ts +29 -19
  43. package/build/lib/src/idl.js +404 -119
  44. package/build/lib/src/idlize.d.ts +2 -1
  45. package/build/lib/src/idlize.js +82 -26
  46. package/build/lib/src/index.d.ts +4 -3
  47. package/build/lib/src/index.js +3 -2
  48. package/build/lib/src/options.d.ts +1 -1
  49. package/build/lib/src/peer-generation/BuilderClass.d.ts +0 -2
  50. package/build/lib/src/peer-generation/BuilderClass.js +0 -8
  51. package/build/lib/src/peer-generation/LayoutManager.d.ts +10 -3
  52. package/build/lib/src/peer-generation/LayoutManager.js +3 -2
  53. package/build/lib/src/peer-generation/Materialized.d.ts +1 -1
  54. package/build/lib/src/peer-generation/Materialized.js +3 -3
  55. package/build/lib/src/peer-generation/PeerClass.d.ts +3 -8
  56. package/build/lib/src/peer-generation/PeerClass.js +0 -1
  57. package/build/lib/src/peer-generation/PeerFile.d.ts +1 -2
  58. package/build/lib/src/peer-generation/PeerFile.js +1 -1
  59. package/build/lib/src/peer-generation/PeerLibrary.d.ts +12 -7
  60. package/build/lib/src/peer-generation/PeerLibrary.js +131 -67
  61. package/build/lib/src/peer-generation/ReferenceResolver.d.ts +1 -1
  62. package/build/lib/src/peer-generation/ReferenceResolver.js +2 -2
  63. package/build/lib/src/peer-generation/idl/IdlNameConvertor.d.ts +1 -0
  64. package/build/lib/src/peer-generation/idl/IdlNameConvertor.js +6 -2
  65. package/build/lib/src/peer-generation/idl/common.d.ts +3 -1
  66. package/build/lib/src/peer-generation/idl/common.js +21 -4
  67. package/build/lib/src/peer-generation/isMaterialized.js +28 -7
  68. package/build/lib/src/peer-generation/unions.d.ts +3 -2
  69. package/build/lib/src/peer-generation/unions.js +7 -3
  70. package/build/lib/src/resolveNamedNode.d.ts +3 -0
  71. package/build/lib/src/resolveNamedNode.js +105 -0
  72. package/build/lib/src/util.d.ts +8 -1
  73. package/build/lib/src/util.js +41 -3
  74. package/build/lib/src/visitor.d.ts +2 -1
  75. package/build/lib/src/visitor.js +109 -7
  76. package/package.json +2 -2
  77. package/webidl2.js/LICENSE +21 -0
  78. package/webidl2.js/README.md +827 -0
  79. package/webidl2.js/dist/package.json +3 -0
  80. package/webidl2.js/dist/webidl2.js +93 -19
@@ -1,26 +1,76 @@
1
+ import { EOL } from "node:os";
1
2
  import * as idl from "../idl";
3
+ export var IDLValidationDiagnosticsCode;
4
+ (function (IDLValidationDiagnosticsCode) {
5
+ IDLValidationDiagnosticsCode[IDLValidationDiagnosticsCode["INVALID_EXTENDED_ATTRIBUTE"] = 1000] = "INVALID_EXTENDED_ATTRIBUTE";
6
+ IDLValidationDiagnosticsCode[IDLValidationDiagnosticsCode["ENUM_IS_NOT_CONSISTENT"] = 1001] = "ENUM_IS_NOT_CONSISTENT";
7
+ IDLValidationDiagnosticsCode[IDLValidationDiagnosticsCode["REFERENCE_IS_NOT_RESOLVED"] = 1002] = "REFERENCE_IS_NOT_RESOLVED";
8
+ })(IDLValidationDiagnosticsCode || (IDLValidationDiagnosticsCode = {}));
9
+ const ENG_ErrorDescription = {
10
+ [IDLValidationDiagnosticsCode.INVALID_EXTENDED_ATTRIBUTE]: "Invalid extended attribute",
11
+ [IDLValidationDiagnosticsCode.ENUM_IS_NOT_CONSISTENT]: "Enum includes both string and number values",
12
+ [IDLValidationDiagnosticsCode.REFERENCE_IS_NOT_RESOLVED]: "Can not resolve reference",
13
+ };
14
+ export const IDLValidationErrorDescription = {
15
+ en_EN: ENG_ErrorDescription
16
+ };
17
+ class IDLLinterContext {
18
+ constructor() {
19
+ this.stack = [{ typeParameters: new Set() }];
20
+ }
21
+ enter(rec) {
22
+ this.stack.push(rec);
23
+ }
24
+ leave() {
25
+ this.stack.pop();
26
+ }
27
+ get current() {
28
+ return this.stack.at(-1);
29
+ }
30
+ hasTypeParameter(name) {
31
+ for (let i = this.stack.length - 1; i >= 0; --i) {
32
+ if (this.stack[i].typeParameters.has(name)) {
33
+ return true;
34
+ }
35
+ }
36
+ return false;
37
+ }
38
+ }
39
+ function parseTypeParameter(param) {
40
+ const extendsIdx = param.indexOf('extends');
41
+ if (extendsIdx !== -1) {
42
+ return param.substring(0, extendsIdx).trim();
43
+ }
44
+ const eqIdx = param.indexOf('=');
45
+ if (eqIdx !== -1) {
46
+ return param.substring(0, eqIdx).trim();
47
+ }
48
+ return param;
49
+ }
2
50
  export class IDLLinter {
3
- constructor(node, resolver, options) {
4
- this.node = node;
51
+ constructor(file, resolver, options, info) {
52
+ this.file = file;
5
53
  this.resolver = resolver;
6
54
  this.options = options;
7
- this.result = "";
8
- }
9
- pushError(node, error) {
10
- const name = typeof node === 'string' ? node : idl.getFQName(node);
11
- if (!this.entryErrors)
12
- this.entryErrors = {};
13
- if (!this.entryErrors[name])
14
- this.entryErrors[name] = [];
15
- this.entryErrors[name].push(error);
55
+ this.info = info;
56
+ this.context = new IDLLinterContext();
57
+ this.diagnostics = [];
16
58
  }
59
+ ///
17
60
  visit() {
18
- this.check(this.node);
19
- idl.forEachChild(this.node, node => {
61
+ idl.forEachChild(this.file, node => {
62
+ const leave = this.enter(node);
20
63
  this.check(node);
64
+ return leave;
21
65
  });
22
- this.result = !this.entryErrors ? "" : JSON.stringify(this.entryErrors, undefined, 2);
23
- return this.result;
66
+ return this.diagnostics;
67
+ }
68
+ enter(node) {
69
+ var _a, _b;
70
+ if (idl.isInterface(node) || idl.isTypedef(node) || idl.isMethod(node) || idl.isCallable(node) || idl.isCallback(node)) {
71
+ this.context.enter({ typeParameters: new Set((_b = (_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.map(parseTypeParameter)) !== null && _b !== void 0 ? _b : []) });
72
+ return () => this.context.leave();
73
+ }
24
74
  }
25
75
  check(node) {
26
76
  var _a;
@@ -35,14 +85,26 @@ export class IDLLinter {
35
85
  }
36
86
  }
37
87
  checkValidAttributes(entry, validAttributes) {
88
+ var _a, _b, _c;
38
89
  if (!entry.extendedAttributes)
39
90
  return;
40
91
  for (const attr of entry.extendedAttributes) {
41
- if (!validAttributes.includes(attr.name))
42
- this.pushError(entry, `${entry.kind}: Invalid attribute '${attr.name}'`);
92
+ if (!validAttributes.includes(attr.name)) {
93
+ const tokens = (_a = this.info) === null || _a === void 0 ? void 0 : _a.get(attr);
94
+ const ident = tokens === null || tokens === void 0 ? void 0 : tokens.name;
95
+ const file = idl.getFileFor(entry);
96
+ this.diagnostics.push({
97
+ code: IDLValidationDiagnosticsCode.INVALID_EXTENDED_ATTRIBUTE,
98
+ file: (_b = file === null || file === void 0 ? void 0 : file.fileName) !== null && _b !== void 0 ? _b : '',
99
+ message: `Invalid attribute '${attr.name}'`,
100
+ position: [(_c = ident === null || ident === void 0 ? void 0 : ident.position) !== null && _c !== void 0 ? _c : 0, attr.name.length]
101
+ });
102
+ }
43
103
  }
44
104
  }
45
105
  checkEnumConsistency(entry) {
106
+ var _a, _b, _c, _d;
107
+ const fileName = (_a = idl.getFileFor(entry)) === null || _a === void 0 ? void 0 : _a.fileName;
46
108
  let hasNumber = false;
47
109
  let hasString = false;
48
110
  for (const element of entry.elements) {
@@ -51,18 +113,46 @@ export class IDLLinter {
51
113
  if (typeof element.initializer === 'number')
52
114
  hasNumber = true;
53
115
  }
54
- if (hasNumber && hasString)
55
- this.pushError(entry, "Enum includes both string and number values");
116
+ if (hasNumber && hasString) {
117
+ const tokens = (_b = this.info) === null || _b === void 0 ? void 0 : _b.get(entry);
118
+ const ident = tokens === null || tokens === void 0 ? void 0 : tokens.name;
119
+ this.diagnostics.push({
120
+ code: IDLValidationDiagnosticsCode.ENUM_IS_NOT_CONSISTENT,
121
+ file: fileName !== null && fileName !== void 0 ? fileName : '',
122
+ message: "Enum includes both string and number values",
123
+ position: [
124
+ (_c = ident === null || ident === void 0 ? void 0 : ident.position) !== null && _c !== void 0 ? _c : 0,
125
+ (_d = ident === null || ident === void 0 ? void 0 : ident.value.length) !== null && _d !== void 0 ? _d : 0
126
+ ]
127
+ });
128
+ }
56
129
  }
57
130
  checkReferenceResolved(reference) {
131
+ var _a, _b, _c, _d, _e, _f;
58
132
  if (IDLLinter.builtinReferences.includes(reference.name))
59
133
  return;
134
+ if (this.context.hasTypeParameter(reference.name)) {
135
+ return;
136
+ }
60
137
  const resolved = this.resolver.resolveTypeReference(reference);
61
138
  if (resolved === undefined) {
62
139
  const parentFile = idl.getFileFor(reference);
63
140
  const parentNamespace = idl.fetchNamespaceFrom(reference);
64
141
  const scopeName = parentNamespace ? idl.getFQName(parentNamespace) : idl.getPackageName(parentFile);
65
- this.pushError(scopeName, `Can not resolve reference '${reference.name}' defined in scope '${scopeName}' in file '${parentFile.fileName}''`);
142
+ let current = reference;
143
+ let tokens = (_a = this.info) === null || _a === void 0 ? void 0 : _a.get(current);
144
+ let location = tokens === null || tokens === void 0 ? void 0 : tokens.base;
145
+ while (current !== undefined && tokens === undefined) {
146
+ current = current.parent;
147
+ tokens = (_b = this.info) === null || _b === void 0 ? void 0 : _b.get(current);
148
+ location = tokens === null || tokens === void 0 ? void 0 : tokens.name;
149
+ }
150
+ this.diagnostics.push({
151
+ code: IDLValidationDiagnosticsCode.REFERENCE_IS_NOT_RESOLVED,
152
+ file: (_c = parentFile.fileName) !== null && _c !== void 0 ? _c : '',
153
+ message: `Can not resolve reference '${reference.name}' defined in scope '${scopeName}'`,
154
+ position: [(_d = location === null || location === void 0 ? void 0 : location.position) !== null && _d !== void 0 ? _d : 0, (_f = (_e = location === null || location === void 0 ? void 0 : location.value) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0]
155
+ });
66
156
  }
67
157
  }
68
158
  }
@@ -70,12 +160,108 @@ IDLLinter.builtinReferences = [
70
160
  idl.IDLTopType.name,
71
161
  idl.IDLObjectType.name,
72
162
  ];
73
- class IDLLinterError extends Error {
163
+ export class IDLLinterError extends Error {
164
+ constructor(message, size) {
165
+ super(message);
166
+ this.size = size;
167
+ }
74
168
  }
75
- export function verifyIDLLinter(node, resolver, options) {
76
- const result = new IDLLinter(node, resolver, options).visit();
77
- if (result.length)
78
- throw new IDLLinterError(result);
169
+ function printErrors(errors, text) {
170
+ errors.sort((a, b) => a.position[0] - b.position[0]);
171
+ let ptr = 0;
172
+ let lines = 1;
173
+ let cols = 1;
174
+ return errors.map(error => {
175
+ while (ptr < error.position[0] && ptr < text.length) {
176
+ if (text[ptr] === '\n') {
177
+ ++lines;
178
+ cols = 0;
179
+ }
180
+ ++cols;
181
+ ++ptr;
182
+ }
183
+ return `E: IDL${error.code} ${IDLValidationErrorDescription.en_EN[error.code]} -- ${error.file}:${lines}:${cols} ${error.message}`;
184
+ }).join(EOL);
185
+ }
186
+ function printErrorWithoutLocation(errors) {
187
+ errors.sort((a, b) => a.position[0] - b.position[0]);
188
+ return errors.map(error => {
189
+ return `E: IDL${error.code} ${IDLValidationErrorDescription.en_EN[error.code]} -- ${error.file} ${error.message}`;
190
+ }).join(EOL);
191
+ }
192
+ function prettyPrintErrors(errors, text) {
193
+ errors.sort((a, b) => a.position[0] - b.position[0]);
194
+ const windowSize = 2;
195
+ let window = [];
196
+ let ptr = 0;
197
+ let lines = 1;
198
+ let cols = 1;
199
+ let lineBuffer = '';
200
+ return errors.map(error => {
201
+ while (ptr < error.position[0] && ptr < text.length) {
202
+ if (text[ptr] === '\n') {
203
+ ++lines;
204
+ cols = 0;
205
+ while (window.length > windowSize) {
206
+ window.shift();
207
+ }
208
+ window.push(lineBuffer);
209
+ lineBuffer = '';
210
+ }
211
+ else {
212
+ if (text[ptr] !== '\r') {
213
+ lineBuffer += text[ptr];
214
+ }
215
+ }
216
+ ++cols;
217
+ ++ptr;
218
+ }
219
+ let currentLine = lineBuffer;
220
+ let ii = ptr;
221
+ while (ii < text.length && text[ii] !== '\n') {
222
+ currentLine += text[ii];
223
+ ++ii;
224
+ }
225
+ let errorLine = ' ';
226
+ ii = 0;
227
+ while (ii < cols) {
228
+ errorLine += ' ';
229
+ ++ii;
230
+ }
231
+ ii = 0;
232
+ errorLine += '\x1b[31m';
233
+ while (ii < error.position[1]) {
234
+ errorLine += '~';
235
+ ++ii;
236
+ }
237
+ errorLine += '\x1b[0m';
238
+ const windowLines = [...window, currentLine].map((line, i) => {
239
+ const idx = (lines - (window.length - i));
240
+ const idxText = idx.toString().padStart(5, ' ');
241
+ const idxColored = idx === lines
242
+ ? '\x1b[1m' + idxText + '\x1b[0m'
243
+ : idxText;
244
+ return idxColored + ' | ' + line;
245
+ });
246
+ let errorText = ''
247
+ + windowLines.join(EOL)
248
+ + EOL + errorLine + EOL
249
+ + `\x1b[31mERROR\x1b[0m: IDL${error.code} ${IDLValidationErrorDescription.en_EN[error.code]}`
250
+ + EOL + `${error.file}:${lines}:${cols} ${error.message}`
251
+ + EOL;
252
+ return errorText;
253
+ }).join(EOL);
254
+ }
255
+ export function verifyIDLLinter(file, resolver, options, info) {
256
+ var _a, _b;
257
+ const result = new IDLLinter(file, resolver, options, info).visit();
258
+ if (result.length) {
259
+ const isTTY = Boolean(process.stdout.isTTY);
260
+ const errorMessage = info
261
+ ? isTTY ? prettyPrintErrors(result, (_a = file.text) !== null && _a !== void 0 ? _a : '') : printErrors(result, (_b = file.text) !== null && _b !== void 0 ? _b : '')
262
+ : printErrorWithoutLocation(result);
263
+ throw new IDLLinterError(errorMessage, result.length);
264
+ }
79
265
  return true;
80
266
  }
81
267
  //# sourceMappingURL=IDLLinter.js.map
@@ -65,7 +65,7 @@ export function scanIDL(inputDir, inputFile) {
65
65
  ? [path.join(inputDir, inputFile)]
66
66
  : fs.readdirSync(inputDir)
67
67
  .map((elem) => path.join(inputDir, elem));
68
- return files.map(it => toIDLFile(it));
68
+ return files.map(it => toIDLFile(it)[0]);
69
69
  }
70
70
  export const licence = `/*
71
71
  * Copyright (c) 2024 Huawei Device Co., Ltd.
@@ -1,5 +1,14 @@
1
+ import * as webidl2 from "webidl2";
1
2
  import * as idl from "../idl";
3
+ export type WebIDLTokenCollection = Record<string, webidl2.Token | null | undefined>;
4
+ export type IDLTokenInfoMap = Map<unknown, WebIDLTokenCollection>;
2
5
  export declare function addSyntheticType(name: string, type: idl.IDLEntry): void;
3
6
  export declare function resolveSyntheticType(type: idl.IDLReferenceType): idl.IDLEntry | undefined;
4
- export declare function toIDLFile(fileName: string, content?: string): idl.IDLFile;
7
+ type IDLInheritanceMode = 'single' | 'multiple';
8
+ interface ToIDLFileProps {
9
+ inheritanceMode?: IDLInheritanceMode;
10
+ content?: string;
11
+ }
12
+ export declare function toIDLFile(fileName: string, { content, inheritanceMode }?: ToIDLFileProps): [idl.IDLFile, IDLTokenInfoMap];
13
+ export {};
5
14
  //# sourceMappingURL=deserialize.d.ts.map