jsii 6.0.21 → 7.1.0-dev.1

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 (107) hide show
  1. package/lib/assembler.d.ts +254 -196
  2. package/lib/assembler.js +2105 -2327
  3. package/lib/assembler.js.map +1 -1
  4. package/lib/case.js.map +1 -1
  5. package/lib/common/find-utils.js.map +1 -1
  6. package/lib/common/index.d.ts +1 -1
  7. package/lib/common/index.js +2 -1
  8. package/lib/common/index.js.map +1 -1
  9. package/lib/common/symbol-id.d.ts +23 -4
  10. package/lib/common/symbol-id.js +13 -9
  11. package/lib/common/symbol-id.js.map +1 -1
  12. package/lib/compiler-host.d.ts +60 -0
  13. package/lib/compiler-host.js +201 -0
  14. package/lib/compiler-host.js.map +1 -0
  15. package/lib/compiler.d.ts +74 -62
  16. package/lib/compiler.js +332 -254
  17. package/lib/compiler.js.map +1 -1
  18. package/lib/diagnostic.d.ts +34 -0
  19. package/lib/diagnostic.js +141 -0
  20. package/lib/diagnostic.js.map +1 -0
  21. package/lib/emitter.d.ts +40 -10
  22. package/lib/emitter.js +188 -0
  23. package/lib/emitter.js.map +1 -1
  24. package/lib/helpers.d.ts +1 -1
  25. package/lib/helpers.js +2 -2
  26. package/lib/helpers.js.map +1 -1
  27. package/lib/index.js.map +1 -1
  28. package/lib/jsii-diagnostic.d.ts +27 -22
  29. package/lib/jsii-diagnostic.js +22 -38
  30. package/lib/jsii-diagnostic.js.map +1 -1
  31. package/lib/literate.js.map +1 -1
  32. package/lib/main.js +3 -2
  33. package/lib/main.js.map +1 -1
  34. package/lib/project-info.d.ts +23 -4
  35. package/lib/project-info.js +5 -5
  36. package/lib/project-info.js.map +1 -1
  37. package/lib/support-types.d.ts +2 -0
  38. package/lib/support-types.js +3 -0
  39. package/lib/support-types.js.map +1 -0
  40. package/lib/support.js.map +1 -1
  41. package/lib/tsconfig/compiler-options.d.ts +12 -46
  42. package/lib/tsconfig/compiler-options.js +45 -87
  43. package/lib/tsconfig/compiler-options.js.map +1 -1
  44. package/lib/tsconfig/index.d.ts +24 -5
  45. package/lib/tsconfig/index.js.map +1 -1
  46. package/lib/tsconfig/read.d.ts +15 -0
  47. package/lib/tsconfig/read.js +196 -0
  48. package/lib/tsconfig/read.js.map +1 -0
  49. package/lib/tsconfig/rule-set-format.js.map +1 -1
  50. package/lib/tsconfig/rulesets/configurable-options.js.map +1 -1
  51. package/lib/tsconfig/rulesets/deprecated-options.js.map +1 -1
  52. package/lib/tsconfig/rulesets/generated.public.js.map +1 -1
  53. package/lib/tsconfig/rulesets/incompatible-options.js.map +1 -1
  54. package/lib/tsconfig/rulesets/jsii-configured-options.js.map +1 -1
  55. package/lib/tsconfig/rulesets/minimal.public.js.map +1 -1
  56. package/lib/tsconfig/rulesets/strict-family-options.js.map +1 -1
  57. package/lib/tsconfig/rulesets/strict.public.js.map +1 -1
  58. package/lib/tsconfig/tsconfig-validator.js +2 -50
  59. package/lib/tsconfig/tsconfig-validator.js.map +1 -1
  60. package/lib/tsconfig/validator.js.map +1 -1
  61. package/lib/type-analysis.js.map +1 -1
  62. package/lib/type-reference.js.map +1 -1
  63. package/lib/type-visitor.js.map +1 -1
  64. package/lib/utils.d.ts +6 -6
  65. package/lib/utils.js +8 -13
  66. package/lib/utils.js.map +1 -1
  67. package/lib/version.d.ts +3 -3
  68. package/lib/version.js +4 -4
  69. package/lib/version.js.map +1 -1
  70. package/lib/warnings.d.ts +2 -2
  71. package/lib/warnings.js +3 -77
  72. package/lib/warnings.js.map +1 -1
  73. package/package.json +15 -10
  74. package/scripts/ts7-compare-jsii.mjs +101 -0
  75. package/lib/directives.d.ts +0 -24
  76. package/lib/directives.js +0 -139
  77. package/lib/directives.js.map +0 -1
  78. package/lib/docs.d.ts +0 -64
  79. package/lib/docs.js +0 -293
  80. package/lib/docs.js.map +0 -1
  81. package/lib/node-bindings.d.ts +0 -19
  82. package/lib/node-bindings.js +0 -49
  83. package/lib/node-bindings.js.map +0 -1
  84. package/lib/reserved-words.d.ts +0 -6
  85. package/lib/reserved-words.js +0 -258
  86. package/lib/reserved-words.js.map +0 -1
  87. package/lib/sets.d.ts +0 -16
  88. package/lib/sets.js +0 -41
  89. package/lib/sets.js.map +0 -1
  90. package/lib/transforms/deprecated-remover.d.ts +0 -44
  91. package/lib/transforms/deprecated-remover.js +0 -545
  92. package/lib/transforms/deprecated-remover.js.map +0 -1
  93. package/lib/transforms/deprecation-warnings.d.ts +0 -37
  94. package/lib/transforms/deprecation-warnings.js +0 -616
  95. package/lib/transforms/deprecation-warnings.js.map +0 -1
  96. package/lib/transforms/runtime-info.d.ts +0 -39
  97. package/lib/transforms/runtime-info.js +0 -128
  98. package/lib/transforms/runtime-info.js.map +0 -1
  99. package/lib/transforms/utils.d.ts +0 -11
  100. package/lib/transforms/utils.js +0 -31
  101. package/lib/transforms/utils.js.map +0 -1
  102. package/lib/type-tracker.d.ts +0 -13
  103. package/lib/type-tracker.js +0 -67
  104. package/lib/type-tracker.js.map +0 -1
  105. package/lib/validator.d.ts +0 -19
  106. package/lib/validator.js +0 -602
  107. package/lib/validator.js.map +0 -1
@@ -1,545 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.DeprecatedRemover = void 0;
37
- const node_path_1 = require("node:path");
38
- const spec_1 = require("@jsii/spec");
39
- const ts = __importStar(require("typescript"));
40
- const jsii_diagnostic_1 = require("../jsii-diagnostic");
41
- const bindings = __importStar(require("../node-bindings"));
42
- const utils_1 = require("../utils");
43
- class DeprecatedRemover {
44
- typeChecker;
45
- allowlistedDeprecations;
46
- transformations = new Array();
47
- nodesToRemove = new Set();
48
- constructor(typeChecker, allowlistedDeprecations) {
49
- this.typeChecker = typeChecker;
50
- this.allowlistedDeprecations = allowlistedDeprecations;
51
- }
52
- /**
53
- * Obtains the configuration for the TypeScript transform(s) that will remove
54
- * `@deprecated` members from the generated declarations (`.d.ts`) files. It
55
- * will leverage information accumulated during `#removeFrom(Assembly)` in
56
- * order to apply corrections to inheritance chains, ensuring a valid output
57
- * is produced.
58
- */
59
- get customTransformers() {
60
- return {
61
- afterDeclarations: [
62
- (context) => {
63
- const transformer = new DeprecationRemovalTransformer(this.typeChecker, context, this.transformations, this.nodesToRemove);
64
- return transformer.transform.bind(transformer);
65
- },
66
- ],
67
- };
68
- }
69
- /**
70
- * Removes all `@deprecated` API elements from the provided assembly, and
71
- * records the operations needed in order to fix the inheritance chains that
72
- * mix `@deprecated` and non-`@deprecated` types.
73
- *
74
- * @param assembly the assembly to be modified.
75
- *
76
- * @returns diagnostic messages produced when validating no remaining API
77
- * makes use of a `@deprecated` type that was removed.
78
- */
79
- removeFrom(assembly) {
80
- if (assembly.types == null) {
81
- return [];
82
- }
83
- const strippedFqns = new Set();
84
- const replaceWithClass = new Map();
85
- const replaceWithInterfaces = new Map();
86
- // Find all types that will be stripped out
87
- for (const [fqn, typeInfo] of Object.entries(assembly.types)) {
88
- if (typeInfo.docs?.stability === spec_1.Stability.Deprecated) {
89
- if (!this.shouldFqnBeStripped(fqn)) {
90
- continue;
91
- }
92
- strippedFqns.add(fqn);
93
- if ((0, spec_1.isClassType)(typeInfo) && typeInfo.base != null) {
94
- replaceWithClass.set(fqn, typeInfo.base);
95
- }
96
- if ((0, spec_1.isClassOrInterfaceType)(typeInfo) && typeInfo.interfaces != null) {
97
- replaceWithInterfaces.set(fqn, typeInfo.interfaces);
98
- }
99
- this.nodesToRemove.add(bindings.getRelatedNode(typeInfo));
100
- }
101
- }
102
- for (const [fqn, typeInfo] of Object.entries(assembly.types)) {
103
- // Ignore `@deprecated` types
104
- if (strippedFqns.has(fqn)) {
105
- continue;
106
- }
107
- // Enums cannot have references to `@deprecated` types, but can have deprecated members
108
- if ((0, spec_1.isEnumType)(typeInfo)) {
109
- const enumNode = bindings.getEnumRelatedNode(typeInfo);
110
- const members = [];
111
- for (const mem of typeInfo.members) {
112
- if (mem.docs?.stability === spec_1.Stability.Deprecated && this.shouldFqnBeStripped(`${fqn}#${mem.name}`)) {
113
- const matchingMemberNode = enumNode.members.find((enumMem) => enumMem.name.getText() === mem.name);
114
- if (matchingMemberNode) {
115
- this.nodesToRemove.add(matchingMemberNode);
116
- }
117
- }
118
- else {
119
- members.push(mem);
120
- }
121
- }
122
- typeInfo.members = members;
123
- continue;
124
- }
125
- // For classes, we erase `@deprecated` base classes, replacing as needed
126
- const additionalInterfaces = new Set();
127
- if ((0, spec_1.isClassType)(typeInfo) && typeInfo.base != null && strippedFqns.has(typeInfo.base)) {
128
- while (typeInfo.base != null && strippedFqns.has(typeInfo.base)) {
129
- const oldBase = assembly.types[typeInfo.base];
130
- if (oldBase.interfaces)
131
- for (const addFqn of oldBase.interfaces)
132
- additionalInterfaces.add(addFqn);
133
- typeInfo.base = replaceWithClass.get(typeInfo.base);
134
- }
135
- this.transformations.push(typeInfo.base != null
136
- ? Transformation.replaceBaseClass(this.typeChecker, bindings.getClassRelatedNode(typeInfo), typeInfo.base in assembly.types
137
- ? bindings.getClassRelatedNode(assembly.types[typeInfo.base]) ?? typeInfo.base
138
- : typeInfo.base)
139
- : Transformation.removeBaseClass(this.typeChecker, bindings.getClassRelatedNode(typeInfo)));
140
- }
141
- // Be defensive in case we add other kinds in the future
142
- if (!(0, spec_1.isClassOrInterfaceType)(typeInfo)) {
143
- throw new Error(`Unhandled type encountered! ${JSON.stringify(typeInfo, null, 2)}`);
144
- }
145
- // Strip all `@deprecated` interfaces from the inheritance tree, replacing as needed
146
- if (typeInfo.interfaces?.some((addFqn) => strippedFqns.has(addFqn)) || additionalInterfaces.size > 0) {
147
- const originalSet = new Set(typeInfo.interfaces ?? []);
148
- const newSet = new Set();
149
- const candidates = Array.from(new Set([...originalSet, ...additionalInterfaces]));
150
- while (candidates.length > 0) {
151
- const candidateFqn = candidates.pop();
152
- if (!strippedFqns.has(candidateFqn)) {
153
- newSet.add(candidateFqn);
154
- if (!originalSet.has(candidateFqn)) {
155
- this.transformations.push(Transformation.addInterface(this.typeChecker, bindings.getClassOrInterfaceRelatedNode(typeInfo), candidateFqn in assembly.types
156
- ? bindings.getInterfaceRelatedNode(assembly.types[candidateFqn]) ?? candidateFqn
157
- : candidateFqn));
158
- }
159
- continue;
160
- }
161
- if (originalSet.has(candidateFqn)) {
162
- this.transformations.push(Transformation.removeInterface(this.typeChecker, bindings.getClassOrInterfaceRelatedNode(typeInfo), bindings.getInterfaceRelatedNode(assembly.types[candidateFqn])));
163
- }
164
- const replacement = replaceWithInterfaces.get(candidateFqn);
165
- if (replacement != null) {
166
- candidates.push(...replacement);
167
- }
168
- }
169
- typeInfo.interfaces = newSet.size > 0 ? Array.from(newSet).sort() : undefined;
170
- }
171
- // Drop all `@deprecated` members, and remove "overrides" from stripped types
172
- const methods = [];
173
- const properties = [];
174
- if (typeInfo.methods) {
175
- for (const meth of typeInfo.methods) {
176
- if (meth.docs?.stability === spec_1.Stability.Deprecated && this.shouldFqnBeStripped(`${fqn}#${meth.name}`)) {
177
- this.nodesToRemove.add(bindings.getMethodRelatedNode(meth));
178
- }
179
- else {
180
- methods.push(meth.overrides != null && strippedFqns.has(meth.overrides) ? { ...meth, overrides: undefined } : meth);
181
- }
182
- }
183
- }
184
- typeInfo.methods = typeInfo.methods ? methods : undefined;
185
- if (typeInfo.properties) {
186
- for (const prop of typeInfo.properties) {
187
- if (prop.docs?.stability === spec_1.Stability.Deprecated && this.shouldFqnBeStripped(`${fqn}#${prop.name}`)) {
188
- this.nodesToRemove.add(bindings.getParameterRelatedNode(prop));
189
- }
190
- else {
191
- properties.push(prop.overrides != null && strippedFqns.has(prop.overrides) ? { ...prop, overrides: undefined } : prop);
192
- }
193
- }
194
- }
195
- typeInfo.properties = typeInfo.properties ? properties : undefined;
196
- }
197
- const diagnostics = this.findLeftoverUseOfDeprecatedAPIs(assembly, strippedFqns);
198
- // Remove all `@deprecated` types, after we did everything, so we could
199
- // still access the related nodes from the assembly object.
200
- for (const fqn of strippedFqns) {
201
- if (this.shouldFqnBeStripped(fqn)) {
202
- delete assembly.types[fqn];
203
- }
204
- }
205
- return diagnostics;
206
- }
207
- findLeftoverUseOfDeprecatedAPIs(assembly, strippedFqns) {
208
- if (assembly.types == null) {
209
- return [];
210
- }
211
- const result = new Array();
212
- for (const type of Object.values(assembly.types)) {
213
- if ((0, spec_1.isEnumType)(type) || strippedFqns.has(type.fqn)) {
214
- continue;
215
- }
216
- if ((0, spec_1.isClassType)(type) && type.initializer) {
217
- result.push(...this.verifyCallable(assembly, strippedFqns, type.initializer));
218
- }
219
- if (type.methods) {
220
- for (const method of type.methods)
221
- result.push(...this.verifyCallable(assembly, strippedFqns, method));
222
- }
223
- if (type.properties) {
224
- for (const property of type.properties)
225
- result.push(...this.verifyProperty(assembly, strippedFqns, property));
226
- }
227
- }
228
- return result;
229
- }
230
- verifyCallable(assembly, strippedFqns, method) {
231
- const diagnostics = new Array();
232
- const deprecatedReturnFqn = (0, spec_1.isMethod)(method) && method.returns && this.tryFindReference(method.returns.type, strippedFqns);
233
- if (deprecatedReturnFqn) {
234
- diagnostics.push(this.makeDiagnostic(deprecatedReturnFqn, 'Method', method, assembly));
235
- }
236
- if (method.parameters) {
237
- for (const parameter of method.parameters) {
238
- const deprecatedTypeFqn = this.tryFindReference(parameter.type, strippedFqns);
239
- if (deprecatedTypeFqn) {
240
- diagnostics.push(this.makeDiagnostic(deprecatedTypeFqn, 'Parameter', parameter, assembly));
241
- }
242
- }
243
- }
244
- return diagnostics;
245
- }
246
- verifyProperty(assembly, strippedFqns, property) {
247
- const deprecatedTypeFqn = this.tryFindReference(property.type, strippedFqns);
248
- if (deprecatedTypeFqn) {
249
- return [this.makeDiagnostic(deprecatedTypeFqn, 'Property', property, assembly)];
250
- }
251
- return [];
252
- }
253
- /**
254
- * Determines whether a `TypeReference` contains an FQN within a given set.
255
- *
256
- * @param ref the tested `TypeReference`.
257
- * @param fqns the set of FQNs that are being searched for.
258
- *
259
- * @returns the first FQN that was identified.
260
- */
261
- tryFindReference(ref, fqns) {
262
- if ((0, spec_1.isNamedTypeReference)(ref)) {
263
- return fqns.has(ref.fqn) ? ref.fqn : undefined;
264
- }
265
- if ((0, spec_1.isPrimitiveTypeReference)(ref)) {
266
- return undefined;
267
- }
268
- if ((0, spec_1.isCollectionTypeReference)(ref)) {
269
- return this.tryFindReference(ref.collection.elementtype, fqns);
270
- }
271
- return setTypeMembers(ref)
272
- .map((type) => this.tryFindReference(type, fqns))
273
- .find((typeRef) => typeRef != null);
274
- }
275
- shouldFqnBeStripped(fqn) {
276
- return this.allowlistedDeprecations?.has(fqn) ?? true;
277
- }
278
- makeDiagnostic(fqn, messagePrefix, context, assembly) {
279
- const node = bindings.getRelatedNode(context);
280
- const diagnostic = jsii_diagnostic_1.JsiiDiagnostic.JSII_3999_INCOHERENT_TYPE_MODEL.create(node?.type ?? node, `${messagePrefix} has @deprecated type ${fqn}, and it is erased by --strip-deprecated.`);
281
- const typeInfo = assembly.types?.[fqn];
282
- const typeNode = typeInfo && bindings.getTypeRelatedNode(typeInfo);
283
- if (typeNode == null) {
284
- return diagnostic;
285
- }
286
- return diagnostic.addRelatedInformation(ts.getNameOfDeclaration(typeNode) ?? typeNode, 'The @deprecated type is declared here');
287
- }
288
- }
289
- exports.DeprecatedRemover = DeprecatedRemover;
290
- class Transformation {
291
- typeChecker;
292
- apply;
293
- static addInterface(typeChecker, node, iface) {
294
- return new Transformation(typeChecker, node, (declaration) => {
295
- if (!ts.isClassDeclaration(declaration) && !ts.isInterfaceDeclaration(declaration)) {
296
- throw new utils_1.JsiiError(`Expected a ClassDeclaration or InterfaceDeclaration, found a ${ts.SyntaxKind[declaration.kind]}`);
297
- }
298
- const { typeExpression: newInterface, syntheticImport } = Transformation.typeReference(iface, declaration, typeChecker);
299
- if (ts.isClassDeclaration(declaration)) {
300
- return {
301
- node: ts.factory.updateClassDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, addInterfaceTo(ts.SyntaxKind.ImplementsKeyword, declaration.heritageClauses), declaration.members),
302
- syntheticImport,
303
- };
304
- }
305
- return {
306
- node: ts.factory.updateInterfaceDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, addInterfaceTo(ts.SyntaxKind.ExtendsKeyword, declaration.heritageClauses), declaration.members),
307
- syntheticImport,
308
- };
309
- function addInterfaceTo(token, clauses = []) {
310
- const existingClause = clauses.find((clause) => clause.token === token);
311
- if (existingClause == null) {
312
- return [...clauses, ts.factory.createHeritageClause(token, [newInterface])];
313
- }
314
- return [
315
- ...clauses.filter((clause) => clause !== existingClause),
316
- ts.factory.updateHeritageClause(existingClause, [...existingClause.types, newInterface]),
317
- ];
318
- }
319
- });
320
- }
321
- static replaceBaseClass(typeChecker, node, baseClass) {
322
- return new Transformation(typeChecker, node, (declaration) => {
323
- if (!ts.isClassDeclaration(declaration)) {
324
- throw new utils_1.JsiiError(`Expected a ClassDeclaration, found a ${ts.SyntaxKind[declaration.kind]}`);
325
- }
326
- const { typeExpression: newBaseClass, syntheticImport } = Transformation.typeReference(baseClass, declaration, typeChecker);
327
- const existingClause = declaration.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
328
- return {
329
- node: ts.factory.updateClassDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, [
330
- ...(declaration.heritageClauses ?? []).filter((clause) => clause !== existingClause),
331
- existingClause
332
- ? ts.factory.updateHeritageClause(existingClause, [newBaseClass])
333
- : ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, [newBaseClass]),
334
- ], declaration.members),
335
- syntheticImports: syntheticImport && [syntheticImport],
336
- };
337
- });
338
- }
339
- static removeBaseClass(typeChecker, node) {
340
- return new Transformation(typeChecker, node, (declaration) => {
341
- if (!ts.isClassDeclaration(declaration)) {
342
- throw new utils_1.JsiiError(`Expected a ClassDeclaration, found a ${ts.SyntaxKind[declaration.kind]}`);
343
- }
344
- return {
345
- node: ts.factory.updateClassDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, declaration.heritageClauses?.filter((clause) => clause.token !== ts.SyntaxKind.ExtendsKeyword), declaration.members),
346
- };
347
- });
348
- }
349
- static removeInterface(typeChecker, node, iface) {
350
- const ifaceName = Transformation.fullyQualifiedName(typeChecker, iface);
351
- return new Transformation(typeChecker, node, (declaration) => {
352
- if (ts.isClassDeclaration(declaration)) {
353
- return {
354
- node: ts.factory.updateClassDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, removeInterfaceHeritage(declaration.heritageClauses), declaration.members),
355
- };
356
- }
357
- else if (ts.isInterfaceDeclaration(declaration)) {
358
- return {
359
- node: ts.factory.updateInterfaceDeclaration(declaration, declaration.modifiers, declaration.name, declaration.typeParameters, removeInterfaceHeritage(declaration.heritageClauses), declaration.members),
360
- };
361
- }
362
- throw new utils_1.JsiiError(`Expected a ClassDeclaration or InterfaceDeclaration, found a ${ts.SyntaxKind[declaration.kind]}`);
363
- });
364
- function removeInterfaceHeritage(clauses) {
365
- if (clauses == null) {
366
- return clauses;
367
- }
368
- return clauses
369
- .map((clause) => {
370
- const types = clause.types.filter((type) => Transformation.fullyQualifiedName(typeChecker, type.expression) !== ifaceName);
371
- if (types.length === clause.types.length) {
372
- // Means the interface was only transitively present...
373
- return clause;
374
- }
375
- if (types.length === 0) {
376
- return undefined;
377
- }
378
- return ts.factory.updateHeritageClause(clause, types);
379
- })
380
- .filter((clause) => clause != null);
381
- }
382
- }
383
- static fullyQualifiedName(typeChecker, node) {
384
- const symbol = typeChecker.getSymbolAtLocation(ts.getNameOfDeclaration(node) ?? node);
385
- // This symbol ☝️ does not contain enough information in some cases - when
386
- // an imported type is part of a heritage clause - to produce the fqn.
387
- // Round tripping this to its type and back to a symbol seems to fix this.
388
- const type = symbol && typeChecker.getDeclaredTypeOfSymbol(symbol);
389
- return type?.symbol && typeChecker.getFullyQualifiedName(type.symbol);
390
- }
391
- static typeReference(type, context, typeChecker) {
392
- context = ts.getOriginalNode(context);
393
- const [, contextSource] = /^"([^"]+)"\..*$/.exec(typeChecker.getFullyQualifiedName(typeChecker.getSymbolAtLocation(ts.getNameOfDeclaration(context))));
394
- let expression;
395
- let syntheticImport;
396
- if (typeof type === 'string') {
397
- const [root, ...tail] = type.split('.');
398
- const syntheticImportName = ts.factory.createUniqueName(root);
399
- syntheticImport = ts.factory.createImportDeclaration(undefined /* decorators */, ts.factory.createImportClause(false, undefined, ts.factory.createNamespaceImport(syntheticImportName)), ts.factory.createStringLiteral(root));
400
- expression = tail.reduce((curr, elt) => ts.factory.createPropertyAccessExpression(curr, elt), syntheticImportName);
401
- }
402
- else {
403
- const [, typeSource, qualifiedName] = /^"([^"]+)"\.(.*)$/.exec(typeChecker.getFullyQualifiedName(typeChecker.getSymbolAtLocation(ts.getNameOfDeclaration(type))));
404
- if (typeSource === contextSource) {
405
- const [root, ...tail] = qualifiedName.split('.');
406
- expression = tail.reduce((curr, elt) => ts.factory.createPropertyAccessExpression(curr, elt), ts.factory.createIdentifier(root));
407
- }
408
- else {
409
- const syntheticImportName = ts.factory.createUniqueName((0, node_path_1.basename)(typeSource));
410
- syntheticImport = ts.factory.createImportDeclaration(undefined /* modifiers */, ts.factory.createImportClause(false, undefined, ts.factory.createNamespaceImport(syntheticImportName)), ts.factory.createStringLiteral(`./${(0, node_path_1.relative)((0, node_path_1.dirname)(contextSource), typeSource)}`), undefined);
411
- expression = qualifiedName
412
- .split('.')
413
- .reduce((curr, elt) => ts.factory.createPropertyAccessExpression(curr, elt), syntheticImportName);
414
- }
415
- }
416
- return {
417
- typeExpression: ts.factory.createExpressionWithTypeArguments(expression, undefined),
418
- syntheticImport,
419
- };
420
- }
421
- nodeName;
422
- constructor(typeChecker, node, apply) {
423
- this.typeChecker = typeChecker;
424
- this.apply = apply;
425
- this.nodeName = Transformation.fullyQualifiedName(typeChecker, node);
426
- }
427
- targets(node) {
428
- return this.nodeName === Transformation.fullyQualifiedName(this.typeChecker, node);
429
- }
430
- }
431
- class DeprecationRemovalTransformer {
432
- typeChecker;
433
- context;
434
- transformations;
435
- nodesToRemove;
436
- /**
437
- * A list of SyntaxKinds for which it is not necessary to evaluate children,
438
- * since they are never of interest to this transform. This opens up a wee
439
- * optimization, which is particularly useful when trying to troubleshoot the
440
- * transform in a debugger (saves a TON of time stepping into useless nodes
441
- * then).
442
- */
443
- static IGNORE_CHILDREN = new Set([
444
- ts.SyntaxKind.Constructor,
445
- ts.SyntaxKind.FunctionDeclaration,
446
- ts.SyntaxKind.GetAccessor,
447
- ts.SyntaxKind.MethodDeclaration,
448
- ts.SyntaxKind.MethodSignature,
449
- ts.SyntaxKind.PropertySignature,
450
- ts.SyntaxKind.PropertyDeclaration,
451
- ts.SyntaxKind.SetAccessor,
452
- ts.SyntaxKind.VariableDeclaration,
453
- ]);
454
- syntheticImports = new Array();
455
- constructor(typeChecker, context, transformations, nodesToRemove) {
456
- this.typeChecker = typeChecker;
457
- this.context = context;
458
- this.transformations = transformations;
459
- this.nodesToRemove = nodesToRemove;
460
- }
461
- transform(node) {
462
- let result = this.visitEachChild(node);
463
- // If there are any synthetic imports, add them to the source file
464
- if (ts.isSourceFile(result) && this.syntheticImports.length > 0) {
465
- result = this.context.factory.updateSourceFile(result, [...this.syntheticImports, ...result.statements], result.isDeclarationFile, result.referencedFiles, result.typeReferenceDirectives, result.hasNoDefaultLib, result.libReferenceDirectives);
466
- this.syntheticImports = new Array();
467
- }
468
- return result;
469
- }
470
- visitEachChild(node) {
471
- return ts.visitEachChild(node, this.visitor.bind(this), this.context);
472
- }
473
- visitor(node) {
474
- if (this.isDeprecated(node)) {
475
- // Removing deprecated members by substituting "nothing" to them
476
- return [];
477
- }
478
- if (ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) {
479
- for (const transformation of this.transformations) {
480
- // 👇 as any because the assignment below confuses type checker
481
- if (transformation.targets(node)) {
482
- const { node: transformedNode, syntheticImport } = transformation.apply(node);
483
- node = transformedNode;
484
- if (syntheticImport) {
485
- this.syntheticImports.push(syntheticImport);
486
- }
487
- }
488
- }
489
- }
490
- // Remove named imports of `@deprecated` members from the source...
491
- if (ts.isImportDeclaration(node) &&
492
- node.importClause &&
493
- node.importClause.namedBindings &&
494
- ts.isNamedImports(node.importClause.namedBindings)) {
495
- const filteredElements = node.importClause.namedBindings.elements.filter((element) => {
496
- // This symbol is local (it's declaration points back to the named import)
497
- const symbol = this.typeChecker.getSymbolAtLocation(element.name);
498
- const exportedSymbol =
499
- // This "resolves" the imported type, so we can get to it's declaration(s)
500
- symbol && this.typeChecker.getDeclaredTypeOfSymbol(symbol)?.symbol;
501
- return !exportedSymbol?.declarations?.some((decl) => this.isDeprecated(decl));
502
- });
503
- if (filteredElements.length !== node.importClause.namedBindings.elements.length) {
504
- return this.context.factory.updateImportDeclaration(node, node.modifiers, node.importClause.name != null || filteredElements.length > 0
505
- ? this.context.factory.updateImportClause(node.importClause, node.importClause.isTypeOnly, node.importClause.name, this.context.factory.updateNamedImports(node.importClause.namedBindings, filteredElements))
506
- : undefined, node.moduleSpecifier, node.assertClause);
507
- }
508
- return node;
509
- }
510
- // Replace "export ... from ..." places that no longer export anything
511
- // with an "import from ...", so side effects are preserved.
512
- if (ts.isExportDeclaration(node) && node.moduleSpecifier) {
513
- const symbol = this.typeChecker.getSymbolAtLocation(node.moduleSpecifier);
514
- const moduleExports = symbol &&
515
- this.typeChecker
516
- .getExportsOfModule(symbol)
517
- ?.filter((sym) => !sym.declarations?.some((decl) => this.isDeprecated(decl)));
518
- if ((node.exportClause == null || ts.isNamespaceExport(node.exportClause)) && moduleExports?.length === 0) {
519
- return this.context.factory.createImportDeclaration(undefined /* modifiers */, undefined /* importClause */, node.moduleSpecifier);
520
- }
521
- if (node.exportClause != null && moduleExports) {
522
- const namedExports = node.exportClause;
523
- const exportedNames = new Set(moduleExports.map((sym) => sym.name));
524
- const filteredElements = namedExports.elements?.filter((elt) => exportedNames.has(elt.name.text));
525
- if (filteredElements?.length !== namedExports.elements?.length) {
526
- return this.context.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, this.context.factory.updateNamedExports(namedExports, filteredElements), node.moduleSpecifier, node.assertClause);
527
- }
528
- }
529
- }
530
- return DeprecationRemovalTransformer.IGNORE_CHILDREN.has(node.kind) ? node : this.visitEachChild(node);
531
- }
532
- isDeprecated(node) {
533
- const original = ts.getOriginalNode(node);
534
- return (this.nodesToRemove.has(original) && ts.getJSDocTags(original).some((tag) => tag.tagName.text === 'deprecated'));
535
- }
536
- }
537
- function setTypeMembers(x) {
538
- if ((0, spec_1.isUnionTypeReference)(x)) {
539
- return x.union.types;
540
- }
541
- else {
542
- return x.intersection.types;
543
- }
544
- }
545
- //# sourceMappingURL=deprecated-remover.js.map