docspack 0.3.0 → 0.4.0

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/src/verify.ts CHANGED
@@ -196,8 +196,12 @@ async function verifyPackage(
196
196
  ): Promise<VerifiedPackage> {
197
197
  const empty = { id, checked: 0, matched: 0, unrecognised: 0, findings: [] };
198
198
 
199
- const declaredLibraries = await documentedLibraries(packageDir, manifest);
200
- if (declaredLibraries.length === 0) {
199
+ const packageLibraries = await documentedLibraries(packageDir, manifest);
200
+ // A chunk may name its own libraries, which is how a monorepo says that one chunk describes
201
+ // `@acme/api@1.2.0` and its neighbour describes `@acme/cli@3.0.0`. Those are what that chunk
202
+ // is checked against; the package's list covers every chunk that names none.
203
+ const scopes = manifest.chunks.map((chunk) => chunk.documents ?? packageLibraries);
204
+ if (packageLibraries.length === 0 && scopes.every((scope) => scope.length === 0)) {
201
205
  return {
202
206
  ...empty,
203
207
  status: "skipped",
@@ -205,39 +209,52 @@ async function verifyPackage(
205
209
  };
206
210
  }
207
211
 
208
- const documents = declaredLibraries.map(formatDocumentedLibrary);
209
- // An entity is fine if any documented library declares it: a monorepo documents eighteen
210
- // packages from one surface, and a name belongs to whichever of them exports it.
211
- const names = new Set<string>();
212
- const missing: string[] = [];
213
- for (const library of declaredLibraries) {
212
+ const documents = [
213
+ ...new Set([...packageLibraries, ...scopes.flat()].map(formatDocumentedLibrary)),
214
+ ];
215
+
216
+ // One resolution per library, however many chunks name it: reading a type surface is the
217
+ // expensive part, and a monorepo pack names the same eighteen libraries throughout.
218
+ const surfaces = new Map<string, ReadonlySet<string>>();
219
+ const resolve = async (library: DocumentedLibrary): Promise<ReadonlySet<string>> => {
220
+ const cached = surfaces.get(library.name);
221
+ if (cached !== undefined) return cached;
222
+
214
223
  const libraryDir =
215
224
  (await resolvePackageDir(library.name, packageDir)) ??
216
225
  (await resolvePackageDir(library.name, cwd));
217
226
  const surface = libraryDir === undefined ? undefined : await readExportSurface(libraryDir);
218
- if (surface === undefined || surface.names.size === 0) {
219
- missing.push(library.name);
220
- continue;
221
- }
222
- for (const name of surface.names) names.add(name);
223
- }
224
-
225
- if (names.size === 0) {
226
- return {
227
- ...empty,
228
- status: "skipped",
229
- documents,
230
- reason: `${missing.join(", ")} ${missing.length === 1 ? "is" : "are"} not installed, or ship no type declarations`,
231
- };
232
- }
227
+ const names = surface?.names ?? new Set<string>();
228
+ surfaces.set(library.name, names);
229
+ return names;
230
+ };
233
231
 
234
- const declared = new Set([...names].map(normalize));
235
232
  const findings: DriftFinding[] = [];
233
+ const missing = new Set<string>();
236
234
  let checked = 0;
237
235
  let matched = 0;
238
236
  let unrecognised = 0;
237
+ let readAny = false;
238
+
239
+ for (const [index, chunk] of manifest.chunks.entries()) {
240
+ const scope = scopes[index] ?? packageLibraries;
241
+ if (scope.length === 0) continue;
242
+
243
+ // An entity is fine if any library in scope declares it: a monorepo documents eighteen
244
+ // packages from one surface, and a name belongs to whichever of them exports it.
245
+ const names = new Set<string>();
246
+ for (const library of scope) {
247
+ const surface = await resolve(library);
248
+ if (surface.size === 0) {
249
+ missing.add(library.name);
250
+ continue;
251
+ }
252
+ for (const name of surface) names.add(name);
253
+ }
254
+ if (names.size === 0) continue;
255
+ readAny = true;
239
256
 
240
- for (const chunk of manifest.chunks) {
257
+ const declared = new Set([...names].map(normalize));
241
258
  for (const entity of chunk.entities) {
242
259
  const symbol = checkableSymbol(entity);
243
260
  if (symbol === undefined) continue;
@@ -266,11 +283,24 @@ async function verifyPackage(
266
283
  }
267
284
  }
268
285
 
286
+ if (!readAny) {
287
+ const unread = [...missing];
288
+ return {
289
+ ...empty,
290
+ status: "skipped",
291
+ documents,
292
+ reason:
293
+ unread.length === 0
294
+ ? "has no chunk naming a library to check against"
295
+ : `${unread.join(", ")} ${unread.length === 1 ? "is" : "are"} not installed, or ship no type declarations`,
296
+ };
297
+ }
298
+
269
299
  return {
270
300
  id,
271
301
  status: "verified",
272
302
  documents,
273
- ...(missing.length === 0 ? {} : { unchecked: missing }),
303
+ ...(missing.size === 0 ? {} : { unchecked: [...missing] }),
274
304
  checked,
275
305
  matched,
276
306
  unrecognised,