jsii 6.0.25 → 6.0.26-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.
@@ -15,6 +15,8 @@ export declare class Assembler implements Emitter {
15
15
  private readonly mainFile;
16
16
  private readonly tscRootDir?;
17
17
  private readonly tscOutDir?;
18
+ /** Set when package.json's `types` is outside of tsc's outDir, to explain a missing `mainFile`. */
19
+ private readonly typesOutsideOutDir?;
18
20
  private readonly compressAssembly?;
19
21
  private readonly usedFeatures;
20
22
  private readonly _typeChecker;
@@ -24,6 +26,8 @@ export declare class Assembler implements Emitter {
24
26
  private readonly _packageInfoCache;
25
27
  /** Map of Symbol to namespace export Symbol */
26
28
  private readonly _submoduleMap;
29
+ /** Symbols reachable from the entry point's exports, including those of submodules and namespaces. */
30
+ private _entryPointExports?;
27
31
  /**
28
32
  * Submodule information
29
33
  *
@@ -153,6 +157,16 @@ export declare class Assembler implements Emitter {
153
157
  * partially-ignored merged declaration stays a visible part of the public API.
154
158
  */
155
159
  private _isIgnored;
160
+ /**
161
+ * Collects the symbols exported from the entry point, following re-exports into submodules and namespaces.
162
+ */
163
+ private _collectEntryPointExports;
164
+ /**
165
+ * Whether a type is declared in this package's sources but is not reachable from the entry point. Such a type is
166
+ * hidden from jsii consumers just like a type declared without `export`, which it must have to be imported from
167
+ * another file.
168
+ */
169
+ private _isUnexportedLocalType;
156
170
  /**
157
171
  * @returns true if this member is internal and should be omitted from the type manifest
158
172
  */
package/lib/assembler.js CHANGED
@@ -84,6 +84,8 @@ class Assembler {
84
84
  mainFile;
85
85
  tscRootDir;
86
86
  tscOutDir;
87
+ /** Set when package.json's `types` is outside of tsc's outDir, to explain a missing `mainFile`. */
88
+ typesOutsideOutDir;
87
89
  compressAssembly;
88
90
  usedFeatures = new Set();
89
91
  _typeChecker;
@@ -93,6 +95,8 @@ class Assembler {
93
95
  _packageInfoCache = new Map();
94
96
  /** Map of Symbol to namespace export Symbol */
95
97
  _submoduleMap = new Map();
98
+ /** Symbols reachable from the entry point's exports, including those of submodules and namespaces. */
99
+ _entryPointExports;
96
100
  /**
97
101
  * Submodule information
98
102
  *
@@ -136,6 +140,7 @@ class Assembler {
136
140
  this.tscOutDir = (0, helpers_1.normalizeConfigPath)(projectInfo.projectRoot, program.getCompilerOptions().outDir);
137
141
  if (this.tscOutDir != null) {
138
142
  mainFile = path.relative(this.tscOutDir, mainFile);
143
+ const typesOutsideOutDir = isOutsideOfDirectory(mainFile);
139
144
  // rootDir may be set explicitly or not. If not, inferRootDir replicates
140
145
  // tsc's behavior of using the longest prefix of all built source files.
141
146
  // rootDir might also be absolute (just like outDir), so we normalize it
@@ -146,6 +151,15 @@ class Assembler {
146
151
  if (this.tscRootDir != null) {
147
152
  mainFile = path.join(this.tscRootDir, mainFile);
148
153
  }
154
+ if (typesOutsideOutDir) {
155
+ // tsc writes declarations into outDir, so `types` can't be re-rooted from it, and the entry point is usually
156
+ // looked up in the wrong place. Remember where tsc puts the declarations, to report that if it is not found.
157
+ const fromRootDir = path.relative(this.tscRootDir ?? '.', dts);
158
+ this.typesOutsideOutDir = {
159
+ types: dts,
160
+ suggestion: isOutsideOfDirectory(fromRootDir) ? undefined : path.join(this.tscOutDir, fromRootDir),
161
+ };
162
+ }
149
163
  }
150
164
  this.mainFile = path.resolve(projectInfo.projectRoot, mainFile);
151
165
  this.runtimeTypeInfoInjector = new runtime_info_1.RuntimeTypeInfoInjector(projectInfo.version);
@@ -159,6 +173,9 @@ class Assembler {
159
173
  * @return the result of the assembly emission.
160
174
  */
161
175
  emit() {
176
+ if (this.projectInfo.name.includes('.')) {
177
+ this._diagnostics.push(jsii_diagnostic_1.JsiiDiagnostic.JSII_0009_PACKAGE_NAME_CONTAINS_DOT.createDetached(this.projectInfo.name));
178
+ }
162
179
  if (!this.projectInfo.description) {
163
180
  this._diagnostics.push(jsii_diagnostic_1.JsiiDiagnostic.JSII_0001_PKG_MISSING_DESCRIPTION.createDetached());
164
181
  }
@@ -171,7 +188,10 @@ class Assembler {
171
188
  }
172
189
  const docs = _loadDocs.call(this);
173
190
  const sourceFile = this.program.getSourceFile(this.mainFile);
174
- if (sourceFile == null) {
191
+ if (sourceFile == null && this.typesOutsideOutDir != null) {
192
+ this._diagnostics.push(jsii_diagnostic_1.JsiiDiagnostic.JSII_0008_TYPES_OUTSIDE_OUT_DIR.createDetached(this.typesOutsideOutDir.types, this.tscOutDir, this.typesOutsideOutDir.suggestion));
193
+ }
194
+ else if (sourceFile == null) {
175
195
  this._diagnostics.push(jsii_diagnostic_1.JsiiDiagnostic.JSII_0004_COULD_NOT_FIND_ENTRYPOINT.createDetached(this.mainFile));
176
196
  }
177
197
  else {
@@ -181,6 +201,7 @@ class Assembler {
181
201
  }
182
202
  const symbol = this._typeChecker.getSymbolAtLocation(sourceFile);
183
203
  if (symbol) {
204
+ this._entryPointExports = this._collectEntryPointExports(symbol);
184
205
  const moduleExports = this._typeChecker.getExportsOfModule(symbol);
185
206
  moduleExports.map((item) => this._registerNamespaces(item, this.projectInfo.projectRoot));
186
207
  for (const node of moduleExports) {
@@ -380,12 +401,12 @@ class Assembler {
380
401
  if (typeof ref !== 'string') {
381
402
  ref = ref.fqn;
382
403
  }
383
- const [assm] = ref.split('.');
384
404
  let type;
385
- if (assm === this.projectInfo.name) {
405
+ if (ref.startsWith(`${this.projectInfo.name}.`)) {
386
406
  type = this._types.get(ref);
387
407
  }
388
408
  else {
409
+ const [assm] = ref.split('.');
389
410
  const assembly = this.projectInfo.dependencyClosure.find((dep) => dep.name === assm);
390
411
  type = assembly?.types?.[ref];
391
412
  // since we are exposing a type of this assembly in this module's public API,
@@ -907,7 +928,9 @@ class Assembler {
907
928
  const processBaseTypes = (types) => {
908
929
  for (const iface of types) {
909
930
  // base is private/internal, so we continue recursively with it's own bases
910
- if (this._isPrivateOrInternal(iface.symbol) || isInternalSymbol(iface.symbol)) {
931
+ if (this._isPrivateOrInternal(iface.symbol) ||
932
+ isInternalSymbol(iface.symbol) ||
933
+ this._isUnexportedLocalType(iface.symbol)) {
911
934
  erasedBases.push(iface);
912
935
  if (!isInternalSymbol(iface.symbol)) {
913
936
  const bases = iface.getBaseTypes();
@@ -983,7 +1006,7 @@ class Assembler {
983
1006
  // base classes ("extends foo")
984
1007
  // Crawl up the inheritance tree if the current base type is not exported, so we identify the type(s) to be
985
1008
  // erased, and identify the closest exported base class, should there be one.
986
- while (base?.symbol && this._isPrivateOrInternal(base.symbol)) {
1009
+ while (base?.symbol && (this._isPrivateOrInternal(base.symbol) || this._isUnexportedLocalType(base.symbol))) {
987
1010
  LOG.debug(`Base class of ${chalk_1.default.green(jsiiType.fqn)} named ${chalk_1.default.green(base.symbol.name)} is not exported, erasing it...`);
988
1011
  erasedBases.push(base);
989
1012
  base = (base.getBaseTypes() ?? [])[0];
@@ -1239,6 +1262,42 @@ class Assembler {
1239
1262
  return ((symbol.declarations?.length ?? 0) > 0 &&
1240
1263
  symbol.declarations.every((decl) => directives_1.Directives.of(decl, (diag) => this._diagnostics.push(diag)).ignore != null));
1241
1264
  }
1265
+ /**
1266
+ * Collects the symbols exported from the entry point, following re-exports into submodules and namespaces.
1267
+ */
1268
+ _collectEntryPointExports(entryPoint) {
1269
+ const result = new Set();
1270
+ const visit = (moduleSymbol) => {
1271
+ for (const exported of this._typeChecker.getExportsOfModule(moduleSymbol)) {
1272
+ const target = (exported.flags & ts.SymbolFlags.Alias) !== 0 ? this._typeChecker.getAliasedSymbol(exported) : exported;
1273
+ if (result.has(target)) {
1274
+ continue;
1275
+ }
1276
+ result.add(target);
1277
+ if ((target.flags & ts.SymbolFlags.Module) !== 0) {
1278
+ visit(target);
1279
+ }
1280
+ }
1281
+ };
1282
+ visit(entryPoint);
1283
+ return result;
1284
+ }
1285
+ /**
1286
+ * Whether a type is declared in this package's sources but is not reachable from the entry point. Such a type is
1287
+ * hidden from jsii consumers just like a type declared without `export`, which it must have to be imported from
1288
+ * another file.
1289
+ */
1290
+ _isUnexportedLocalType(symbol) {
1291
+ if (this._entryPointExports == null || this._entryPointExports.has(symbol)) {
1292
+ return false;
1293
+ }
1294
+ const declarations = symbol.declarations ?? [];
1295
+ return (declarations.length > 0 &&
1296
+ declarations.every((decl) => {
1297
+ const sourceFile = decl.getSourceFile();
1298
+ return !sourceFile.isDeclarationFile && !this.program.isSourceFileFromExternalLibrary(sourceFile);
1299
+ }));
1300
+ }
1242
1301
  /**
1243
1302
  * @returns true if this member is internal and should be omitted from the type manifest
1244
1303
  */
@@ -1733,7 +1792,8 @@ class Assembler {
1733
1792
  this._diagnostics.push(jsii_diagnostic_1.JsiiDiagnostic.JSII_1001_TYPE_HAS_NO_SYMBOL.create(declaration));
1734
1793
  return { type: spec.CANONICAL_ANY };
1735
1794
  }
1736
- if (type.symbol.name === 'Array') {
1795
+ // Built-in Array/ReadonlyArray (including `readonly T[]`), by type identity.
1796
+ if (type.symbol.name === 'Array' || this._typeChecker.isArrayType(type)) {
1737
1797
  return { type: _arrayType.call(this) };
1738
1798
  }
1739
1799
  if (type.symbol.name === '__type' && type.symbol.members) {
@@ -2482,6 +2542,10 @@ class EmitContext {
2482
2542
  return new EmitContext(this.namespace, undefined);
2483
2543
  }
2484
2544
  }
2545
+ /** Whether a path that is relative to some directory points outside of that directory. */
2546
+ function isOutsideOfDirectory(relativePath) {
2547
+ return relativePath === '..' || relativePath.startsWith(`..${path.sep}`) || path.isAbsolute(relativePath);
2548
+ }
2485
2549
  function inferRootDir(program) {
2486
2550
  const directories = program
2487
2551
  .getRootFileNames()