@astryxdesign/cli 0.7.3-canary.2468b8a → 0.7.3-canary.296edaf
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/README.md +63 -63
- package/api/component/_adapter.mjs +39 -5
- package/api/discover/_adapter.d.mts +1 -2
- package/api/discover/_adapter.mjs +6 -8
- package/api/discover/discover.mjs +0 -1
- package/api/docs/_adapter.mjs +4 -0
- package/api/doctor/_adapter.d.mts +24 -10
- package/api/doctor/_adapter.mjs +28 -25
- package/api/doctor/doctor.d.mts +7 -4
- package/api/doctor/doctor.mjs +52 -14
- package/api/doctor/doctor.test.mjs +58 -0
- package/api/integration/validate-integration-gaps.test.mjs +3 -0
- package/api/integration/validate-integration.mjs +33 -7
- package/api/integration/validate-integration.type.d.mts +3 -1
- package/api/integration/validate-integration.type.mjs +3 -1
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/search/_entries.d.mts +11 -0
- package/api/search/_entries.mjs +35 -17
- package/api/search/_entries.test.mjs +48 -0
- package/api/template/template.mjs +5 -1
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/docs/tree/versioning.doc.mjs +4 -0
- package/clients/cli/commands/setup-nudge.test.mjs +17 -0
- package/clients/cli/index.mjs +25 -8
- package/foundation/agent-docs/agent-doc-state.d.mts +12 -0
- package/foundation/agent-docs/agent-doc-state.mjs +24 -2
- package/foundation/agent-docs/agent-docs.mjs +5 -5
- package/foundation/agent-docs/computer-agent-text.mjs +16 -1
- package/foundation/agent-docs/computer-agent-text.test.mjs +23 -2
- package/foundation/integrations/cli-peer-version.d.mts +36 -0
- package/foundation/integrations/cli-peer-version.mjs +132 -0
- package/foundation/integrations/cli-peer-version.test.mjs +354 -0
- package/foundation/integrations/public-imports.d.mts +37 -0
- package/foundation/integrations/public-imports.mjs +839 -46
- package/foundation/integrations/public-imports.test.mjs +971 -46
- package/foundation/release/global-state.d.mts +8 -4
- package/foundation/release/global-state.mjs +4 -2
- package/foundation/release/notices.d.mts +62 -8
- package/foundation/release/notices.mjs +136 -25
- package/foundation/release/notices.test.mjs +21 -0
- package/foundation/response/response-types.doc.mjs +1 -1
- package/foundation/text/string-utils.d.mts +2 -1
- package/foundation/text/string-utils.mjs +16 -5
- package/foundation/text/suggest-order.d.mts +72 -0
- package/foundation/text/suggest-order.mjs +363 -0
- package/foundation/text/suggest-order.test.mjs +259 -0
- package/package.json +9 -9
|
@@ -6,21 +6,26 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @input A package directory and its loaded integration record.
|
|
8
8
|
* @output Notes, listed on validate's result and never counted (they fail
|
|
9
|
-
* `--strict` from the next minor), for: a component's import (the doc's
|
|
10
|
-
* the CLI derives) or a template's public import whose
|
|
11
|
-
* package.json `exports` does not declare (or that names another
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
9
|
+
* `--strict` from the next minor), for: a component's import (the doc's
|
|
10
|
+
* `import`, or the one the CLI derives) or a template's public import whose
|
|
11
|
+
* subpath package.json `exports` does not declare (or that names another
|
|
12
|
+
* package), or whose module does not export the component; a component
|
|
13
|
+
* example that imports an undeclared subpath or a name its package does not
|
|
14
|
+
* export; a template source with no default export, or one that imports a
|
|
15
|
+
* relative path outside the templates root, an undeclared subpath of its own
|
|
16
|
+
* package or of Core, or a name its package does not export. Imports it
|
|
17
|
+
* cannot judge are listed in a report.
|
|
16
18
|
* @position foundation/integrations, beside the other on-disk contribution
|
|
17
19
|
* validators; run by `astryx doctor integration validate` (and so by
|
|
18
20
|
* `integration check`) through api/integration/validate-integration.mjs.
|
|
19
21
|
*
|
|
20
22
|
* An `exports` target may point at build output that does not exist yet, so
|
|
21
|
-
* only the declared subpath is judged here, never the target file
|
|
22
|
-
*
|
|
23
|
-
*
|
|
23
|
+
* only the declared subpath is judged here, never the target file. A name is
|
|
24
|
+
* judged only when a declared target exists on disk and its export list can be
|
|
25
|
+
* read completely, and (for a name other than a doc's component) no
|
|
26
|
+
* TypeScript file in the package has a `declare module` block for one of its
|
|
27
|
+
* own modules (which can add names to it); anything less is left to
|
|
28
|
+
* `integration verify`.
|
|
24
29
|
*/
|
|
25
30
|
|
|
26
31
|
import * as fs from 'node:fs';
|
|
@@ -115,15 +120,23 @@ const PARSE_PLUGINS = /** @type {any} */ ([
|
|
|
115
120
|
'exportDefaultFrom',
|
|
116
121
|
]);
|
|
117
122
|
|
|
118
|
-
/**
|
|
119
|
-
|
|
123
|
+
/** The plugins without `jsx`: `.ts` files allow `<T>value` assertions. */
|
|
124
|
+
const TS_PARSE_PLUGINS = PARSE_PLUGINS.filter(
|
|
125
|
+
(/** @type {unknown} */ plugin) => plugin !== 'jsx',
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @param {string} text
|
|
130
|
+
* @param {{jsx?: boolean}} [options] `jsx: false` parses TypeScript only
|
|
131
|
+
*/
|
|
132
|
+
function parseModuleText(text, {jsx = true} = {}) {
|
|
120
133
|
return parse(text, {
|
|
121
134
|
sourceType: 'module',
|
|
122
135
|
allowImportExportEverywhere: true,
|
|
123
136
|
allowReturnOutsideFunction: true,
|
|
124
137
|
allowAwaitOutsideFunction: true,
|
|
125
138
|
errorRecovery: true,
|
|
126
|
-
plugins: PARSE_PLUGINS,
|
|
139
|
+
plugins: jsx ? PARSE_PLUGINS : TS_PARSE_PLUGINS,
|
|
127
140
|
});
|
|
128
141
|
}
|
|
129
142
|
|
|
@@ -215,6 +228,27 @@ export function readImports(text) {
|
|
|
215
228
|
|
|
216
229
|
/** @typedef {import('./exports-subpaths.mjs').SubpathDeclaration} SubpathDeclaration */
|
|
217
230
|
|
|
231
|
+
/**
|
|
232
|
+
* The files on disk a declared subpath can load, in condition order of the
|
|
233
|
+
* package.json (any condition); empty when none exists yet (build output).
|
|
234
|
+
* @param {string} packageDir
|
|
235
|
+
* @param {SubpathDeclaration} declaration
|
|
236
|
+
* @returns {string[]}
|
|
237
|
+
*/
|
|
238
|
+
function existingTargets(packageDir, declaration) {
|
|
239
|
+
if (declaration.state !== 'declared') return [];
|
|
240
|
+
const root = path.resolve(packageDir);
|
|
241
|
+
return allTargets(declaration.value)
|
|
242
|
+
.filter(target => target.startsWith('./'))
|
|
243
|
+
.map(target =>
|
|
244
|
+
path.resolve(root, target.replaceAll('*', declaration.wildcard ?? '')),
|
|
245
|
+
)
|
|
246
|
+
.filter(
|
|
247
|
+
file =>
|
|
248
|
+
(file === root || file.startsWith(root + path.sep)) && isFile(file),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
218
252
|
/**
|
|
219
253
|
* How a package declares one subpath. A package with no `exports` keeps its
|
|
220
254
|
* legacy deep paths: a subpath that names a file on disk (with or without a
|
|
@@ -345,6 +379,534 @@ function exportsFix(exportsField, state, subpath, target) {
|
|
|
345
379
|
return `add ${entry} to package.json "exports" (or the built file it compiles to).`;
|
|
346
380
|
}
|
|
347
381
|
|
|
382
|
+
// ── Module export names ─────────────────────────────────────────────
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Export lists read during one public-import check, by file. Scoped to that
|
|
386
|
+
* check, so a later check in the same process rereads every module.
|
|
387
|
+
* @typedef {Map<string, {names: Set<string>, complete: boolean, types: Set<string>, typed: boolean}>} ExportNameCache
|
|
388
|
+
*/
|
|
389
|
+
|
|
390
|
+
const DECLARATION_RE = /\.d\.[cm]?ts$/u;
|
|
391
|
+
const DECLARATION_EXTENSIONS = ['.d.ts', '.d.mts', '.d.cts'];
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* The declaration file beside a built module (`x.js` → `x.d.ts`), if any.
|
|
395
|
+
* @param {string} file
|
|
396
|
+
* @returns {string | null}
|
|
397
|
+
*/
|
|
398
|
+
function declarationTwin(file) {
|
|
399
|
+
const m = /\.([cm]?)jsx?$/u.exec(file);
|
|
400
|
+
if (!m) return null;
|
|
401
|
+
const stem = file.slice(0, m.index);
|
|
402
|
+
return (
|
|
403
|
+
[`.d.${m[1]}ts`, '.d.ts'].map(ext => `${stem}${ext}`).find(isFile) ?? null
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** @param {string} specifier @param {string} fromFile */
|
|
408
|
+
function resolveRelative(specifier, fromFile) {
|
|
409
|
+
const base = path.resolve(path.dirname(fromFile), specifier);
|
|
410
|
+
const stripped = base.replace(/\.(?:m?js|jsx|cjs)$/u, '');
|
|
411
|
+
// TypeScript source resolves `./x.js` to `./x.ts` (or .tsx, .d.ts) first.
|
|
412
|
+
if (TYPED_RE.test(fromFile) && !DECLARATION_RE.test(fromFile)) {
|
|
413
|
+
const source = [
|
|
414
|
+
...['.ts', '.tsx', '.mts', '.cts'].map(ext => `${stripped}${ext}`),
|
|
415
|
+
...DECLARATION_EXTENSIONS.map(ext => `${stripped}${ext}`),
|
|
416
|
+
].find(isFile);
|
|
417
|
+
if (source && stripped !== base) return source;
|
|
418
|
+
}
|
|
419
|
+
// A declaration file's `export * from './x.js'` names `./x.d.ts`.
|
|
420
|
+
if (DECLARATION_RE.test(fromFile)) {
|
|
421
|
+
const twin = [
|
|
422
|
+
...DECLARATION_EXTENSIONS.map(ext => `${stripped}${ext}`),
|
|
423
|
+
...DECLARATION_EXTENSIONS.map(ext => path.join(base, `index${ext}`)),
|
|
424
|
+
].find(isFile);
|
|
425
|
+
if (twin) return twin;
|
|
426
|
+
}
|
|
427
|
+
const candidates = [
|
|
428
|
+
base,
|
|
429
|
+
...MODULE_EXTENSIONS.map(ext => `${base}${ext}`),
|
|
430
|
+
...MODULE_EXTENSIONS.map(ext => `${stripped}${ext}`),
|
|
431
|
+
...MODULE_EXTENSIONS.map(ext => path.join(base, `index${ext}`)),
|
|
432
|
+
];
|
|
433
|
+
return candidates.find(isFile) ?? null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Every name (runtime or type) a module exports, and whether the list is
|
|
438
|
+
* complete: an `export *` from another package, `export =`, or a module that
|
|
439
|
+
* does not parse leaves it incomplete, and then no name is judged missing.
|
|
440
|
+
* @param {string} file
|
|
441
|
+
* @param {ExportNameCache} [cache] export lists already read in this check
|
|
442
|
+
* @returns {{names: Set<string>, complete: boolean, types: Set<string>, typed: boolean}}
|
|
443
|
+
* `types`: the names exported only as types (an interface, a type alias,
|
|
444
|
+
* `export type`); `typed`: the list includes every type the module
|
|
445
|
+
* exports (TypeScript source, or a JavaScript module whose declaration
|
|
446
|
+
* file was read in full), so a name absent from it is absent as a type too
|
|
447
|
+
*/
|
|
448
|
+
export function moduleExportNames(file, cache = new Map()) {
|
|
449
|
+
const {names, complete, types, typed} = readExportNames(file, cache, []);
|
|
450
|
+
return {names, complete, types, typed};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** TypeScript source or a declaration file: its exports include its types. */
|
|
454
|
+
const TYPED_RE = /\.(?:[cm]?tsx?)$/u;
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* One module's export list, following local `export *` declarations. `stack`
|
|
458
|
+
* holds the modules whose lists are being read: an `export *` that reaches one
|
|
459
|
+
* of them closes a cycle, and a list read through a cycle the module does not
|
|
460
|
+
* close is not cached, since it lacks what the module on the stack exports.
|
|
461
|
+
* @param {string} file
|
|
462
|
+
* @param {ExportNameCache} cache
|
|
463
|
+
* @param {string[]} stack
|
|
464
|
+
* @returns {{names: Set<string>, complete: boolean, types: Set<string>, typed: boolean, cycleTo: Set<string>}}
|
|
465
|
+
*/
|
|
466
|
+
function readExportNames(file, cache, stack) {
|
|
467
|
+
if (stack.includes(file)) {
|
|
468
|
+
return {
|
|
469
|
+
names: new Set(),
|
|
470
|
+
complete: true,
|
|
471
|
+
types: new Set(),
|
|
472
|
+
typed: true,
|
|
473
|
+
cycleTo: new Set([file]),
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
const cached = cache.get(file);
|
|
477
|
+
if (cached) return {...cached, cycleTo: new Set()};
|
|
478
|
+
const own = readOwnExportNames(file, cache, stack);
|
|
479
|
+
const {names, types, cycleTo} = own;
|
|
480
|
+
let {complete, typed} = own;
|
|
481
|
+
// A JavaScript module's declaration file lists its types, at every level
|
|
482
|
+
// an `export *` reaches.
|
|
483
|
+
const twin = declarationTwin(file);
|
|
484
|
+
if (twin && !stack.includes(twin)) {
|
|
485
|
+
const declared = readExportNames(twin, cache, [...stack, file]);
|
|
486
|
+
for (const name of declared.names) names.add(name);
|
|
487
|
+
for (const name of declared.types) types.add(name);
|
|
488
|
+
for (const each of declared.cycleTo) cycleTo.add(each);
|
|
489
|
+
if (declared.complete && declared.typed) {
|
|
490
|
+
complete = true;
|
|
491
|
+
typed = true;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
cycleTo.delete(file);
|
|
495
|
+
const value = {names, complete, types, typed};
|
|
496
|
+
if (cycleTo.size === 0) cache.set(file, value);
|
|
497
|
+
return {...value, cycleTo};
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* The exports one module itself declares, following local `export *` (each
|
|
502
|
+
* through {@link readExportNames}); its declaration file is merged by the
|
|
503
|
+
* caller.
|
|
504
|
+
* @param {string} file
|
|
505
|
+
* @param {ExportNameCache} cache
|
|
506
|
+
* @param {string[]} stack
|
|
507
|
+
* @returns {{names: Set<string>, complete: boolean, types: Set<string>, typed: boolean, cycleTo: Set<string>}}
|
|
508
|
+
*/
|
|
509
|
+
function readOwnExportNames(file, cache, stack) {
|
|
510
|
+
/** @type {Set<string>} */
|
|
511
|
+
const names = new Set();
|
|
512
|
+
/** @type {Set<string>} */
|
|
513
|
+
const types = new Set();
|
|
514
|
+
/** @type {Set<string>} */
|
|
515
|
+
const cycleTo = new Set();
|
|
516
|
+
let typed = TYPED_RE.test(file);
|
|
517
|
+
/** @param {boolean} complete */
|
|
518
|
+
const done = complete => ({names, complete, types, typed, cycleTo});
|
|
519
|
+
// A CommonJS module's exports cannot be read from declarations: leave it
|
|
520
|
+
// unjudged rather than read it as exporting nothing. That is any .cjs/.cts
|
|
521
|
+
// file, any file (built `dist/*.js` included) with no ES module import or
|
|
522
|
+
// export, and any file that also assigns `module.exports` or `exports.*`.
|
|
523
|
+
if (/\.c[jt]s$/u.test(file)) return done(false);
|
|
524
|
+
let complete = true;
|
|
525
|
+
let ast;
|
|
526
|
+
let text;
|
|
527
|
+
try {
|
|
528
|
+
text = fs.readFileSync(file, 'utf-8');
|
|
529
|
+
ast = parseModuleText(text);
|
|
530
|
+
if (ast.errors?.length) complete = false;
|
|
531
|
+
} catch {
|
|
532
|
+
return done(false);
|
|
533
|
+
}
|
|
534
|
+
const esm = ast.program.body.some(
|
|
535
|
+
(/** @type {any} */ node) =>
|
|
536
|
+
node.type === 'ImportDeclaration' ||
|
|
537
|
+
node.type === 'ExportNamedDeclaration' ||
|
|
538
|
+
node.type === 'ExportDefaultDeclaration' ||
|
|
539
|
+
node.type === 'ExportAllDeclaration' ||
|
|
540
|
+
node.type === 'TSExportAssignment',
|
|
541
|
+
);
|
|
542
|
+
// A declaration file with no import or export is a global script: no
|
|
543
|
+
// module import can read it (TypeScript: "is not a module"). Not judged.
|
|
544
|
+
if (!esm) return done(false);
|
|
545
|
+
if (/\b(?:module\.exports|exports\.[\w$]+)\s*=/u.test(text)) {
|
|
546
|
+
return done(false);
|
|
547
|
+
}
|
|
548
|
+
// A declaration file that is a module exports every top-level declaration,
|
|
549
|
+
// marked or not, unless it has an export list (`export {}`, `export {…}
|
|
550
|
+
// from`), `export *`, `export =`, or a default export of an expression
|
|
551
|
+
// (TypeScript's rule for ambient modules). A default-exported declaration
|
|
552
|
+
// (`export default function`, `class`, `interface`) does not end it.
|
|
553
|
+
if (DECLARATION_RE.test(file)) {
|
|
554
|
+
const implicit = !ast.program.body.some(
|
|
555
|
+
(/** @type {any} */ node) =>
|
|
556
|
+
(node.type === 'ExportNamedDeclaration' && !node.declaration) ||
|
|
557
|
+
(node.type === 'ExportDefaultDeclaration' &&
|
|
558
|
+
![
|
|
559
|
+
'FunctionDeclaration',
|
|
560
|
+
'TSDeclareFunction',
|
|
561
|
+
'ClassDeclaration',
|
|
562
|
+
'TSInterfaceDeclaration',
|
|
563
|
+
].includes(node.declaration?.type)) ||
|
|
564
|
+
node.type === 'TSExportAssignment' ||
|
|
565
|
+
node.type === 'ExportAllDeclaration',
|
|
566
|
+
);
|
|
567
|
+
if (implicit) {
|
|
568
|
+
for (const node of ast.program.body) {
|
|
569
|
+
const d = /** @type {any} */ (
|
|
570
|
+
node.type === 'ExportNamedDeclaration' ? node.declaration : node
|
|
571
|
+
);
|
|
572
|
+
if (!d) continue;
|
|
573
|
+
if (d.type === 'VariableDeclaration') {
|
|
574
|
+
for (const v of d.declarations ?? []) {
|
|
575
|
+
if (v.id?.type === 'Identifier') names.add(v.id.name);
|
|
576
|
+
else complete = false;
|
|
577
|
+
}
|
|
578
|
+
} else if (
|
|
579
|
+
d.type === 'TSInterfaceDeclaration' ||
|
|
580
|
+
d.type === 'TSTypeAliasDeclaration'
|
|
581
|
+
) {
|
|
582
|
+
names.add(d.id.name);
|
|
583
|
+
types.add(d.id.name);
|
|
584
|
+
} else if (
|
|
585
|
+
[
|
|
586
|
+
'FunctionDeclaration',
|
|
587
|
+
'TSDeclareFunction',
|
|
588
|
+
'ClassDeclaration',
|
|
589
|
+
'TSEnumDeclaration',
|
|
590
|
+
'TSModuleDeclaration',
|
|
591
|
+
].includes(d.type) &&
|
|
592
|
+
d.id?.type === 'Identifier'
|
|
593
|
+
) {
|
|
594
|
+
names.add(d.id.name);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
for (const node of ast.program.body) {
|
|
600
|
+
if (node.type === 'ExportDefaultDeclaration') names.add('default');
|
|
601
|
+
else if (node.type === 'TSExportAssignment') complete = false;
|
|
602
|
+
else if (
|
|
603
|
+
node.type === 'TSImportEqualsDeclaration' &&
|
|
604
|
+
/** @type {any} */ (node).isExport
|
|
605
|
+
) {
|
|
606
|
+
// `export import Alias = Inner.Value` exports Alias. Whether a
|
|
607
|
+
// type-only one names a value cannot be read, so the list is no longer
|
|
608
|
+
// complete.
|
|
609
|
+
names.add(/** @type {any} */ (node).id.name);
|
|
610
|
+
if (/** @type {any} */ (node).importKind === 'type') {
|
|
611
|
+
types.add(/** @type {any} */ (node).id.name);
|
|
612
|
+
complete = false;
|
|
613
|
+
}
|
|
614
|
+
} else if (node.type === 'ExportNamedDeclaration') {
|
|
615
|
+
const d = /** @type {any} */ (node.declaration);
|
|
616
|
+
const typeOnly = /** @type {any} */ (node).exportKind === 'type';
|
|
617
|
+
if (d) {
|
|
618
|
+
if (d.id?.name) {
|
|
619
|
+
names.add(d.id.name);
|
|
620
|
+
if (
|
|
621
|
+
typeOnly ||
|
|
622
|
+
d.type === 'TSInterfaceDeclaration' ||
|
|
623
|
+
d.type === 'TSTypeAliasDeclaration'
|
|
624
|
+
) {
|
|
625
|
+
types.add(d.id.name);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
for (const v of d.declarations ?? []) {
|
|
629
|
+
if (v.id?.type === 'Identifier') names.add(v.id.name);
|
|
630
|
+
else complete = false;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
for (const sp of /** @type {any[]} */ (node.specifiers ?? [])) {
|
|
634
|
+
const exported = sp.exported?.name ?? sp.exported?.value;
|
|
635
|
+
if (!exported) continue;
|
|
636
|
+
names.add(exported);
|
|
637
|
+
if (typeOnly || sp.exportKind === 'type') types.add(exported);
|
|
638
|
+
}
|
|
639
|
+
} else if (node.type === 'ExportAllDeclaration') {
|
|
640
|
+
const n = /** @type {any} */ (node);
|
|
641
|
+
if (n.exported) {
|
|
642
|
+
names.add(n.exported.name ?? n.exported.value);
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
645
|
+
const source = String(n.source.value);
|
|
646
|
+
const target = source.startsWith('.')
|
|
647
|
+
? resolveRelative(source, file)
|
|
648
|
+
: null;
|
|
649
|
+
if (!target) {
|
|
650
|
+
complete = false;
|
|
651
|
+
continue;
|
|
652
|
+
}
|
|
653
|
+
const inner = readExportNames(target, cache, [...stack, file]);
|
|
654
|
+
for (const name of inner.names) if (name !== 'default') names.add(name);
|
|
655
|
+
for (const name of inner.types) types.add(name);
|
|
656
|
+
if (!inner.complete) complete = false;
|
|
657
|
+
if (!inner.typed) typed = false;
|
|
658
|
+
for (const each of inner.cycleTo) cycleTo.add(each);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return done(complete);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Every file a declared subpath names, and whether each is on disk.
|
|
666
|
+
* @param {string} packageDir
|
|
667
|
+
* @param {SubpathDeclaration} declaration
|
|
668
|
+
* @returns {{files: string[], allPresent: boolean}}
|
|
669
|
+
*/
|
|
670
|
+
function declaredTargets(packageDir, declaration) {
|
|
671
|
+
if (declaration.state !== 'declared') return {files: [], allPresent: false};
|
|
672
|
+
const root = path.resolve(packageDir);
|
|
673
|
+
const all = allTargets(declaration.value)
|
|
674
|
+
.filter(target => target.startsWith('./'))
|
|
675
|
+
.map(target =>
|
|
676
|
+
path.resolve(root, target.replaceAll('*', declaration.wildcard ?? '')),
|
|
677
|
+
)
|
|
678
|
+
.filter(file => file === root || file.startsWith(root + path.sep));
|
|
679
|
+
const files = all.filter(isFile);
|
|
680
|
+
return {files, allPresent: files.length === all.length};
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* The names an import statement asks for that the module provably lacks.
|
|
685
|
+
* A name is missing only when every file the subpath names is on disk and
|
|
686
|
+
* read in full, and at least one of those lists includes its types: any
|
|
687
|
+
* construct the reader does not model leaves the import unchecked.
|
|
688
|
+
* @param {ImportStatement} statement
|
|
689
|
+
* @param {string[]} files candidate targets on disk
|
|
690
|
+
* @param {ExportNameCache} cache
|
|
691
|
+
* @param {boolean} [allPresent] every target the subpath names is on disk
|
|
692
|
+
* @param {boolean} [values] every name is known to be a value (a component),
|
|
693
|
+
* so a list without the module's types can still judge it
|
|
694
|
+
* @returns {string[] | null} null when it cannot be judged
|
|
695
|
+
*/
|
|
696
|
+
function missingNames(
|
|
697
|
+
statement,
|
|
698
|
+
files,
|
|
699
|
+
cache,
|
|
700
|
+
allPresent = true,
|
|
701
|
+
values = false,
|
|
702
|
+
) {
|
|
703
|
+
// A type-only name may live only in a declaration file: not judged.
|
|
704
|
+
const wanted = statement.names
|
|
705
|
+
.filter(binding => !binding.typeOnly)
|
|
706
|
+
.map(binding => binding.imported);
|
|
707
|
+
if (wanted.length === 0) return [];
|
|
708
|
+
if (files.length === 0) return null;
|
|
709
|
+
// Any condition's file (source or built) that has every name settles it.
|
|
710
|
+
let incomplete = false;
|
|
711
|
+
let typed = false;
|
|
712
|
+
/** @type {Set<string>} */
|
|
713
|
+
const seen = new Set();
|
|
714
|
+
for (const file of files) {
|
|
715
|
+
const list = moduleExportNames(file, cache);
|
|
716
|
+
if (wanted.every(name => list.names.has(name))) return [];
|
|
717
|
+
if (!list.complete) incomplete = true;
|
|
718
|
+
if (list.typed) typed = true;
|
|
719
|
+
for (const name of list.names) seen.add(name);
|
|
720
|
+
}
|
|
721
|
+
// A target not built yet, a list cut short, or one without the module's
|
|
722
|
+
// types (a name written without `type` may be a type) is not judged.
|
|
723
|
+
if (incomplete || (!typed && !values) || !allPresent) return null;
|
|
724
|
+
return wanted.filter(name => !seen.has(name));
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const AUGMENTATION_HINT_RE = /\bdeclare\s+module\b/u;
|
|
728
|
+
const TYPESCRIPT_FILE_RE = /\.(?:d\.[cm]?ts|[cm]?ts|tsx)$/u;
|
|
729
|
+
const AUGMENTATION_WALK_LIMIT = 5000;
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Whether a TypeScript file has a real `declare module '<spec>'` block for a
|
|
733
|
+
* module of this package: a relative spec, or the package's own name or a
|
|
734
|
+
* subpath of it. Comments, `declare global`, wildcard specs (`'*.css'`) and
|
|
735
|
+
* other packages' names do not count. A `.tsx` file is parsed with JSX, any
|
|
736
|
+
* other TypeScript file without it (so `<T>value` assertions parse); a file
|
|
737
|
+
* with `declare module` in its text that still fails to parse counts as
|
|
738
|
+
* augmenting.
|
|
739
|
+
* @param {Context} ctx
|
|
740
|
+
* @param {string} text
|
|
741
|
+
* @param {string} file
|
|
742
|
+
*/
|
|
743
|
+
function augmentsOwnModule(ctx, text, file) {
|
|
744
|
+
if (!AUGMENTATION_HINT_RE.test(text)) return false;
|
|
745
|
+
let body;
|
|
746
|
+
try {
|
|
747
|
+
body = parseModuleText(text, {jsx: file.endsWith('.tsx')}).program.body;
|
|
748
|
+
} catch {
|
|
749
|
+
return true;
|
|
750
|
+
}
|
|
751
|
+
return body.some(node => {
|
|
752
|
+
const d = node.type === 'ExportNamedDeclaration' ? node.declaration : node;
|
|
753
|
+
if (d?.type !== 'TSModuleDeclaration' || d.id?.type !== 'StringLiteral')
|
|
754
|
+
return false;
|
|
755
|
+
const spec = String(d.id.value);
|
|
756
|
+
if (spec.includes('*')) return false;
|
|
757
|
+
if (spec.startsWith('./') || spec.startsWith('../')) return true;
|
|
758
|
+
return (
|
|
759
|
+
ctx.name != null && (spec === ctx.name || spec.startsWith(`${ctx.name}/`))
|
|
760
|
+
);
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* The first TypeScript file in the package (outside `node_modules` and dot
|
|
766
|
+
* folders) with a `declare module '…'` block for a module of the package,
|
|
767
|
+
* read once per check. Such a block can add names to that module, so while
|
|
768
|
+
* one exists no name is judged missing: what TypeScript merges is not
|
|
769
|
+
* modelled. A package too large to walk counts as augmented.
|
|
770
|
+
* @param {Context} ctx
|
|
771
|
+
* @returns {string | null} the reason names were not checked, or null
|
|
772
|
+
*/
|
|
773
|
+
function augmentationReason(ctx) {
|
|
774
|
+
if (ctx.augmentation !== undefined) return ctx.augmentation;
|
|
775
|
+
const root = path.resolve(ctx.packageDir);
|
|
776
|
+
/** @type {string[]} */
|
|
777
|
+
const queue = [root];
|
|
778
|
+
let seen = 0;
|
|
779
|
+
/** @type {string | null} */
|
|
780
|
+
let reason = null;
|
|
781
|
+
walk: while (queue.length > 0) {
|
|
782
|
+
const current = /** @type {string} */ (queue.shift());
|
|
783
|
+
let entries;
|
|
784
|
+
try {
|
|
785
|
+
entries = fs.readdirSync(current, {withFileTypes: true});
|
|
786
|
+
} catch {
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
entries.sort((x, y) => x.name.localeCompare(y.name));
|
|
790
|
+
for (const entry of entries) {
|
|
791
|
+
if (++seen > AUGMENTATION_WALK_LIMIT) {
|
|
792
|
+
reason = `the package has more than ${AUGMENTATION_WALK_LIMIT} files to search for a \`declare module\` block, which can add names to its modules, so the names it imports were not checked`;
|
|
793
|
+
break walk;
|
|
794
|
+
}
|
|
795
|
+
const full = path.join(current, entry.name);
|
|
796
|
+
if (entry.isDirectory()) {
|
|
797
|
+
if (entry.name !== 'node_modules' && !entry.name.startsWith('.'))
|
|
798
|
+
queue.push(full);
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
if (!entry.isFile() || !TYPESCRIPT_FILE_RE.test(entry.name)) continue;
|
|
802
|
+
let text;
|
|
803
|
+
try {
|
|
804
|
+
text = fs.readFileSync(full, 'utf-8');
|
|
805
|
+
} catch {
|
|
806
|
+
continue;
|
|
807
|
+
}
|
|
808
|
+
if (augmentsOwnModule(ctx, text, entry.name)) {
|
|
809
|
+
reason = `"${rel(root, full)}" has a \`declare module\` block for a module of the package, which can add names to it, so the names it imports were not checked`;
|
|
810
|
+
break walk;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
ctx.augmentation = reason;
|
|
815
|
+
return reason;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Why an import's names could not be judged.
|
|
820
|
+
* @param {string[]} files
|
|
821
|
+
* @param {boolean} [allPresent]
|
|
822
|
+
*/
|
|
823
|
+
function uncheckedReason(files, allPresent = true) {
|
|
824
|
+
if (files.length === 0 || !allPresent) {
|
|
825
|
+
return 'a file its package.json "exports" entry names does not exist yet (build output), so the names it imports were not checked';
|
|
826
|
+
}
|
|
827
|
+
return "its module's export list cannot be read in full (CommonJS, an `export *` from another package, a parse error, or JavaScript with no declaration file for its types), so the names it imports were not checked";
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Whether `name` reads as a component: PascalCase, not ALL CAPS.
|
|
832
|
+
* @param {string} name
|
|
833
|
+
*/
|
|
834
|
+
function isComponentName(name) {
|
|
835
|
+
return /^[A-Z]/u.test(name) && /[a-z]/u.test(name);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* The export a template's default export should be: an exported function or
|
|
840
|
+
* class first, else an exported `const`, each only when its name is
|
|
841
|
+
* PascalCase (not ALL CAPS) and not only a type; null when there is none to
|
|
842
|
+
* suggest (a hook such as `useAccount`, a helper such as `formatBalance`, a
|
|
843
|
+
* constant such as `ACCOUNT_LIMIT`), and then the fix stays generic.
|
|
844
|
+
* @param {string} text the template source
|
|
845
|
+
* @param {{names: Set<string>, types: Set<string>}} exported
|
|
846
|
+
* @returns {string | null}
|
|
847
|
+
*/
|
|
848
|
+
function defaultExportCandidate(text, exported) {
|
|
849
|
+
let body;
|
|
850
|
+
try {
|
|
851
|
+
body = parseModuleText(text).program.body;
|
|
852
|
+
} catch {
|
|
853
|
+
return null;
|
|
854
|
+
}
|
|
855
|
+
/** @type {Map<string, 'callable' | 'const'>} top-level bindings by kind */
|
|
856
|
+
const local = new Map();
|
|
857
|
+
/** @param {any} d */
|
|
858
|
+
const record = d => {
|
|
859
|
+
if (
|
|
860
|
+
(d?.type === 'FunctionDeclaration' || d?.type === 'ClassDeclaration') &&
|
|
861
|
+
d.id?.name
|
|
862
|
+
) {
|
|
863
|
+
local.set(d.id.name, 'callable');
|
|
864
|
+
} else if (d?.type === 'VariableDeclaration' && d.kind === 'const') {
|
|
865
|
+
for (const v of d.declarations ?? []) {
|
|
866
|
+
if (v.id?.type === 'Identifier') local.set(v.id.name, 'const');
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
};
|
|
870
|
+
for (const node of body) {
|
|
871
|
+
record(node.type === 'ExportNamedDeclaration' ? node.declaration : node);
|
|
872
|
+
}
|
|
873
|
+
/** @type {{name: string, kind: 'callable' | 'const'}[]} */
|
|
874
|
+
const candidates = [];
|
|
875
|
+
for (const node of body) {
|
|
876
|
+
if (node.type !== 'ExportNamedDeclaration') continue;
|
|
877
|
+
const n = /** @type {any} */ (node);
|
|
878
|
+
if (n.exportKind === 'type') continue;
|
|
879
|
+
const d = n.declaration;
|
|
880
|
+
/** @type {string[]} */
|
|
881
|
+
const names = d
|
|
882
|
+
? d.id?.name
|
|
883
|
+
? [d.id.name]
|
|
884
|
+
: (d.declarations ?? [])
|
|
885
|
+
.map((/** @type {any} */ v) => v.id?.name)
|
|
886
|
+
.filter(Boolean)
|
|
887
|
+
: n.source
|
|
888
|
+
? []
|
|
889
|
+
: (n.specifiers ?? [])
|
|
890
|
+
.filter((/** @type {any} */ sp) => sp.exportKind !== 'type')
|
|
891
|
+
.filter(
|
|
892
|
+
(/** @type {any} */ sp) =>
|
|
893
|
+
(sp.exported?.name ?? sp.exported?.value) === sp.local?.name,
|
|
894
|
+
)
|
|
895
|
+
.map((/** @type {any} */ sp) => sp.local.name);
|
|
896
|
+
for (const name of names) {
|
|
897
|
+
const kind = local.get(name);
|
|
898
|
+
if (kind) candidates.push({name, kind});
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
const fits = (/** @type {{name: string}} */ c) =>
|
|
902
|
+
isComponentName(c.name) && !exported.types.has(c.name);
|
|
903
|
+
return (
|
|
904
|
+
candidates.find(c => c.kind === 'callable' && fits(c))?.name ??
|
|
905
|
+
candidates.find(c => c.kind === 'const' && fits(c))?.name ??
|
|
906
|
+
null
|
|
907
|
+
);
|
|
908
|
+
}
|
|
909
|
+
|
|
348
910
|
/**
|
|
349
911
|
* Whether `name` is a bare npm package name (an optional `@scope/` and a
|
|
350
912
|
* name), the only form an import can name the package by.
|
|
@@ -385,6 +947,9 @@ function listed(code, message) {
|
|
|
385
947
|
* @property {string | null} coreDir
|
|
386
948
|
* @property {any} corePkg
|
|
387
949
|
* @property {PublicImportUnchecked[]} unchecked what could not be judged
|
|
950
|
+
* @property {ExportNameCache} exportNames export lists read in this check
|
|
951
|
+
* @property {string | null} [augmentation] why no name is judged missing
|
|
952
|
+
* (a module augmentation in the package); null when none, unset until read
|
|
388
953
|
* @property {string[]} files the package.json `files` entries, normalized
|
|
389
954
|
* @property {string[]} roots the integration's contribution roots, absolute
|
|
390
955
|
*/
|
|
@@ -578,15 +1143,15 @@ function typesOnlyImport(ctx, statement, subpath, declaration) {
|
|
|
578
1143
|
}
|
|
579
1144
|
|
|
580
1145
|
/**
|
|
581
|
-
*
|
|
582
|
-
*
|
|
583
|
-
* checked.
|
|
1146
|
+
* How an import of the package's own name is judged: the declaration when
|
|
1147
|
+
* `exports` does not declare its subpath, the names its module provably
|
|
1148
|
+
* lacks, or the reason it was not checked.
|
|
584
1149
|
* @param {Context} ctx
|
|
585
1150
|
* @param {ImportStatement} statement
|
|
586
1151
|
* @param {string} subpath
|
|
587
|
-
* @returns {{undeclared: SubpathDeclaration | null,
|
|
1152
|
+
* @returns {{undeclared: SubpathDeclaration | null, missing: string[] | null, reason: string | null}}
|
|
588
1153
|
*/
|
|
589
|
-
function
|
|
1154
|
+
function judgeOwnImport(ctx, statement, subpath) {
|
|
590
1155
|
const declaration = declarationIn(ctx.exportsField, ctx.packageDir, subpath);
|
|
591
1156
|
// A subpath an ES module import reaches only through `types` (any other
|
|
592
1157
|
// condition beside it, such as `require`, loads nothing for it) is judged as
|
|
@@ -595,10 +1160,54 @@ function undeclaredOwnImport(ctx, statement, subpath) {
|
|
|
595
1160
|
declaration.state === 'not_importable' &&
|
|
596
1161
|
typesTargets(declaration.value, declaration.wildcard ?? '').length > 0
|
|
597
1162
|
) {
|
|
598
|
-
const
|
|
599
|
-
return
|
|
1163
|
+
const reason = typesOnlyImport(ctx, statement, subpath, declaration);
|
|
1164
|
+
return {undeclared: null, missing: [], reason};
|
|
1165
|
+
}
|
|
1166
|
+
if (declaration.state !== 'declared')
|
|
1167
|
+
return {undeclared: declaration, missing: null, reason: null};
|
|
1168
|
+
if (subpath === '.' && declaration.value == null) {
|
|
1169
|
+
return {
|
|
1170
|
+
undeclared: null,
|
|
1171
|
+
missing: null,
|
|
1172
|
+
reason:
|
|
1173
|
+
'package.json has no "exports", and the root module "main" names is not read, so the names it imports were not checked',
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
const {files, allPresent} = declaredTargets(ctx.packageDir, declaration);
|
|
1177
|
+
const missing = missingNames(statement, files, ctx.exportNames, allPresent);
|
|
1178
|
+
const augmented = missing?.length ? augmentationReason(ctx) : null;
|
|
1179
|
+
if (augmented) return {undeclared: null, missing: null, reason: augmented};
|
|
1180
|
+
return {
|
|
1181
|
+
undeclared: null,
|
|
1182
|
+
missing,
|
|
1183
|
+
reason: missing == null ? uncheckedReason(files, allPresent) : null,
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* A declared subpath of the package whose module, read in full, exports
|
|
1189
|
+
* `name`: the one the component's source is exported under first.
|
|
1190
|
+
* @param {Context} ctx
|
|
1191
|
+
* @param {string} name
|
|
1192
|
+
* @param {string | null} preferred
|
|
1193
|
+
* @returns {string | null}
|
|
1194
|
+
*/
|
|
1195
|
+
function subpathExporting(ctx, name, preferred) {
|
|
1196
|
+
if (!isRecord(ctx.exportsField)) return null;
|
|
1197
|
+
/** @param {string} subpath */
|
|
1198
|
+
const exportsName = subpath =>
|
|
1199
|
+
existingTargets(
|
|
1200
|
+
ctx.packageDir,
|
|
1201
|
+
declarationIn(ctx.exportsField, ctx.packageDir, subpath),
|
|
1202
|
+
).some(file => moduleExportNames(file, ctx.exportNames).names.has(name));
|
|
1203
|
+
// The source's own subpath, which may come from a wildcard key, first.
|
|
1204
|
+
if (preferred && exportsName(preferred)) return preferred;
|
|
1205
|
+
for (const key of Object.keys(ctx.exportsField)) {
|
|
1206
|
+
if (!key.startsWith('.') || key.includes('*') || key === preferred)
|
|
1207
|
+
continue;
|
|
1208
|
+
if (exportsName(key)) return key;
|
|
600
1209
|
}
|
|
601
|
-
return
|
|
1210
|
+
return null;
|
|
602
1211
|
}
|
|
603
1212
|
|
|
604
1213
|
/**
|
|
@@ -688,7 +1297,66 @@ async function componentIssues(ctx, integration) {
|
|
|
688
1297
|
specifier,
|
|
689
1298
|
reason: unbuiltReason(split.subpath),
|
|
690
1299
|
});
|
|
691
|
-
} else if (
|
|
1300
|
+
} else if (
|
|
1301
|
+
declaration.state === 'declared' &&
|
|
1302
|
+
split.subpath === '.' &&
|
|
1303
|
+
declaration.value == null
|
|
1304
|
+
) {
|
|
1305
|
+
// A package with no exports resolves its root through main: not read.
|
|
1306
|
+
ctx.unchecked.push({
|
|
1307
|
+
file: shownDoc,
|
|
1308
|
+
specifier,
|
|
1309
|
+
reason:
|
|
1310
|
+
'package.json has no "exports", and the root module "main" names is not read, so the component names were not checked',
|
|
1311
|
+
});
|
|
1312
|
+
} else if (declaration.state === 'declared') {
|
|
1313
|
+
// A doc whose `name` differs from the name it is listed under is
|
|
1314
|
+
// judged by the doc-name check (`doc_name_mismatch`), never here.
|
|
1315
|
+
const judged = list.filter(
|
|
1316
|
+
r => typeof doc.name !== 'string' || doc.name === r.name,
|
|
1317
|
+
);
|
|
1318
|
+
const {files, allPresent} = declaredTargets(
|
|
1319
|
+
ctx.packageDir,
|
|
1320
|
+
declaration,
|
|
1321
|
+
);
|
|
1322
|
+
const statement = {
|
|
1323
|
+
source: specifier,
|
|
1324
|
+
names: judged.map(r => ({imported: r.name, typeOnly: false})),
|
|
1325
|
+
namespace: false,
|
|
1326
|
+
};
|
|
1327
|
+
// A component is a run-time export, which no augmentation adds, so
|
|
1328
|
+
// a missing one is reported whatever the package declares.
|
|
1329
|
+
const missing =
|
|
1330
|
+
judged.length === 0
|
|
1331
|
+
? []
|
|
1332
|
+
: missingNames(statement, files, ctx.exportNames, allPresent, true);
|
|
1333
|
+
if (missing == null) {
|
|
1334
|
+
ctx.unchecked.push({
|
|
1335
|
+
file: shownDoc,
|
|
1336
|
+
specifier,
|
|
1337
|
+
reason: uncheckedReason(files, allPresent),
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
for (const name of missing ?? []) {
|
|
1341
|
+
const there = subpathExporting(ctx, name, declared);
|
|
1342
|
+
// The per-component entry helps only when the component's source
|
|
1343
|
+
// exports the name itself and the doc does not already use it.
|
|
1344
|
+
const ownEntry =
|
|
1345
|
+
perComponent != null &&
|
|
1346
|
+
source != null &&
|
|
1347
|
+
entryKey !== split.subpath &&
|
|
1348
|
+
moduleExportNames(source, ctx.exportNames).names.has(name);
|
|
1349
|
+
const fix = there
|
|
1350
|
+
? `set import: '${ctx.name}${there === '.' ? '' : there.slice(1)}' in the doc (that subpath exports it), or export "${name}" from the module "${specifier}" loads.`
|
|
1351
|
+
: `export "${name}" from the module "${specifier}" loads${ownEntry ? `, or ${perComponent}` : ''}.`;
|
|
1352
|
+
issues.push(
|
|
1353
|
+
listed(
|
|
1354
|
+
'component_import_missing_name',
|
|
1355
|
+
`Component doc "${shownDoc}" ${gives}, but that module does not export "${name}", so apps that follow the doc get nothing. Fix: ${fix}`,
|
|
1356
|
+
),
|
|
1357
|
+
);
|
|
1358
|
+
}
|
|
1359
|
+
} else {
|
|
692
1360
|
const already =
|
|
693
1361
|
declared && declared !== split.subpath
|
|
694
1362
|
? `set import: '${ctx.name}${declared.slice(1)}' (already exported), or `
|
|
@@ -703,40 +1371,133 @@ async function componentIssues(ctx, integration) {
|
|
|
703
1371
|
}
|
|
704
1372
|
|
|
705
1373
|
/**
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
1374
|
+
* Whether the module at a declared subpath exports every value name the
|
|
1375
|
+
* statement imports (and it imports at least one).
|
|
1376
|
+
* @param {ImportStatement} statement
|
|
1377
|
+
* @param {string} candidate
|
|
1378
|
+
*/
|
|
1379
|
+
const providesAll = (statement, candidate) => {
|
|
1380
|
+
if (!statement.names.some(binding => !binding.typeOnly)) return false;
|
|
1381
|
+
const declaration = declarationIn(
|
|
1382
|
+
ctx.exportsField,
|
|
1383
|
+
ctx.packageDir,
|
|
1384
|
+
candidate,
|
|
1385
|
+
);
|
|
1386
|
+
if (declaration.state !== 'declared' || declaration.value == null)
|
|
1387
|
+
return false;
|
|
1388
|
+
const {files, allPresent} = declaredTargets(ctx.packageDir, declaration);
|
|
1389
|
+
const missing = missingNames(
|
|
1390
|
+
statement,
|
|
1391
|
+
files,
|
|
1392
|
+
ctx.exportNames,
|
|
1393
|
+
allPresent,
|
|
1394
|
+
);
|
|
1395
|
+
return Array.isArray(missing) && missing.length === 0;
|
|
1396
|
+
};
|
|
1397
|
+
|
|
1398
|
+
/**
|
|
1399
|
+
* Where an example with an undeclared import should import from: the
|
|
1400
|
+
* doc's own import, when it differs from the example's and is declared;
|
|
1401
|
+
* else the subpath the component's source is already exported under;
|
|
1402
|
+
* else the exact `exports` entry to add for the example's subpath.
|
|
1403
|
+
* Another subpath is offered only when the statement imports at least one
|
|
1404
|
+
* value name and its module provably exports every one of them.
|
|
710
1405
|
* @param {ImportStatement} statement
|
|
711
1406
|
* @param {string} subpath
|
|
712
1407
|
* @param {SubpathDeclaration} undeclared
|
|
713
1408
|
*/
|
|
714
1409
|
const exampleFix = (statement, subpath, undeclared) => {
|
|
1410
|
+
/** @param {string} candidate */
|
|
1411
|
+
const provides = candidate => providesAll(statement, candidate);
|
|
715
1412
|
const values = statement.names
|
|
716
1413
|
.filter(binding => !binding.typeOnly)
|
|
717
1414
|
.map(binding => binding.imported);
|
|
1415
|
+
// A statement that imports no value name (a side-effect, namespace or
|
|
1416
|
+
// all-type import) is never sent to another subpath: no names prove it.
|
|
1417
|
+
const own = authored ? splitSpecifier(authored) : null;
|
|
1418
|
+
if (
|
|
1419
|
+
values.length > 0 &&
|
|
1420
|
+
authored &&
|
|
1421
|
+
authored !== statement.source &&
|
|
1422
|
+
own?.pkg === ctx.name &&
|
|
1423
|
+
provides(own.subpath)
|
|
1424
|
+
) {
|
|
1425
|
+
return `import from '${authored}' in the example, as the doc's import does.`;
|
|
1426
|
+
}
|
|
1427
|
+
if (
|
|
1428
|
+
values.length > 0 &&
|
|
1429
|
+
declared &&
|
|
1430
|
+
declared !== subpath &&
|
|
1431
|
+
provides(declared)
|
|
1432
|
+
) {
|
|
1433
|
+
return `import from '${ctx.name}${declared.slice(1)}' in the example (already exported).`;
|
|
1434
|
+
}
|
|
1435
|
+
// The entry for the example's own subpath: the file there, or the
|
|
1436
|
+
// component's source when it exports every name, else a placeholder.
|
|
718
1437
|
const atSubpath = [
|
|
719
1438
|
'',
|
|
720
1439
|
...MODULE_EXTENSIONS,
|
|
721
1440
|
...MODULE_EXTENSIONS.map(ext => `/index${ext}`),
|
|
722
1441
|
]
|
|
723
|
-
.map(ext => path.resolve(ctx.packageDir,
|
|
1442
|
+
.map(ext => path.resolve(ctx.packageDir, subpath.slice(2) + ext))
|
|
724
1443
|
.find(
|
|
725
1444
|
file =>
|
|
726
1445
|
file.startsWith(path.resolve(ctx.packageDir) + path.sep) &&
|
|
727
1446
|
isFile(file),
|
|
728
1447
|
);
|
|
729
|
-
const
|
|
730
|
-
|
|
731
|
-
|
|
1448
|
+
const sourceHasAll =
|
|
1449
|
+
values.length > 0 &&
|
|
1450
|
+
source != null &&
|
|
1451
|
+
values.every(name =>
|
|
1452
|
+
moduleExportNames(source, ctx.exportNames).names.has(name),
|
|
1453
|
+
);
|
|
732
1454
|
const target = atSubpath
|
|
733
1455
|
? `./${rel(ctx.packageDir, atSubpath)}`
|
|
734
|
-
:
|
|
1456
|
+
: sourceHasAll && sourceTarget
|
|
735
1457
|
? fixTarget(subpath, sourceTarget)
|
|
736
|
-
:
|
|
1458
|
+
: values.length > 0
|
|
1459
|
+
? `<the file that exports ${values.join(', ')}>`
|
|
1460
|
+
: '<the file it loads>';
|
|
737
1461
|
return exportsFix(ctx.exportsField, undeclared.state, subpath, target);
|
|
738
1462
|
};
|
|
739
1463
|
|
|
1464
|
+
/**
|
|
1465
|
+
* The fix for an example that imports names its module lacks: import
|
|
1466
|
+
* this doc's components that module does export, else the doc's own
|
|
1467
|
+
* import when it differs, else export the names from it.
|
|
1468
|
+
* @param {ImportStatement} statement
|
|
1469
|
+
* @param {string} subpath
|
|
1470
|
+
* @param {string[]} missing
|
|
1471
|
+
*/
|
|
1472
|
+
const missingNameFix = (statement, subpath, missing) => {
|
|
1473
|
+
const declaration = declarationIn(
|
|
1474
|
+
ctx.exportsField,
|
|
1475
|
+
ctx.packageDir,
|
|
1476
|
+
subpath,
|
|
1477
|
+
);
|
|
1478
|
+
/** @type {Set<string>} */
|
|
1479
|
+
const there = new Set();
|
|
1480
|
+
for (const file of existingTargets(ctx.packageDir, declaration)) {
|
|
1481
|
+
for (const name of moduleExportNames(file, ctx.exportNames).names)
|
|
1482
|
+
there.add(name);
|
|
1483
|
+
}
|
|
1484
|
+
const docNames = list.map(r => r.name).filter(name => there.has(name));
|
|
1485
|
+
const exportIt = `export ${missing.length === 1 ? 'the name' : 'the names'} from it`;
|
|
1486
|
+
if (docNames.length > 0) {
|
|
1487
|
+
return `import a name that module exports (${docNames.map(n => `"${n}"`).join(', ')} for this doc), or ${exportIt}.`;
|
|
1488
|
+
}
|
|
1489
|
+
const own = authored ? splitSpecifier(authored) : null;
|
|
1490
|
+
if (
|
|
1491
|
+
authored &&
|
|
1492
|
+
authored !== statement.source &&
|
|
1493
|
+
own?.pkg === ctx.name &&
|
|
1494
|
+
providesAll(statement, own.subpath)
|
|
1495
|
+
) {
|
|
1496
|
+
return `import from '${authored}', as the doc's import does, or ${exportIt}.`;
|
|
1497
|
+
}
|
|
1498
|
+
return `${exportIt[0].toUpperCase()}${exportIt.slice(1)}.`;
|
|
1499
|
+
};
|
|
1500
|
+
|
|
740
1501
|
const examples = Array.isArray(doc.examples) ? doc.examples : [];
|
|
741
1502
|
for (const [index, example] of examples.entries()) {
|
|
742
1503
|
if (!isRecord(example) || typeof example.code !== 'string') continue;
|
|
@@ -747,21 +1508,19 @@ async function componentIssues(ctx, integration) {
|
|
|
747
1508
|
for (const statement of readImports(example.code)) {
|
|
748
1509
|
const parts = splitSpecifier(statement.source);
|
|
749
1510
|
if (!parts || parts.pkg !== ctx.name) continue;
|
|
750
|
-
const {undeclared,
|
|
1511
|
+
const {undeclared, missing, reason} = judgeOwnImport(
|
|
751
1512
|
ctx,
|
|
752
1513
|
statement,
|
|
753
1514
|
parts.subpath,
|
|
754
1515
|
);
|
|
755
|
-
if (
|
|
1516
|
+
if (reason) {
|
|
756
1517
|
ctx.unchecked.push({
|
|
757
1518
|
file: shownDoc,
|
|
758
1519
|
specifier: statement.source,
|
|
759
|
-
reason: `example ${label}: ${
|
|
1520
|
+
reason: `example ${label}: ${reason}`,
|
|
760
1521
|
});
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
unbuiltOutput(ctx, parts.subpath, undeclared)
|
|
764
|
-
) {
|
|
1522
|
+
}
|
|
1523
|
+
if (undeclared && unbuiltOutput(ctx, parts.subpath, undeclared)) {
|
|
765
1524
|
ctx.unchecked.push({
|
|
766
1525
|
file: shownDoc,
|
|
767
1526
|
specifier: statement.source,
|
|
@@ -774,6 +1533,13 @@ async function componentIssues(ctx, integration) {
|
|
|
774
1533
|
`Component doc "${shownDoc}" example ${label} imports "${statement.source}", but ${whyUndeclared(undeclared, parts.subpath)}, so the example fails when an app copies it. Fix: ${exampleFix(statement, parts.subpath, undeclared)}`,
|
|
775
1534
|
),
|
|
776
1535
|
);
|
|
1536
|
+
} else if (missing && missing.length > 0) {
|
|
1537
|
+
issues.push(
|
|
1538
|
+
listed(
|
|
1539
|
+
'example_import_missing_name',
|
|
1540
|
+
`Component doc "${shownDoc}" example ${label} imports ${missing.map(n => `"${n}"`).join(', ')} from "${statement.source}", which does not export ${missing.length === 1 ? 'it' : 'them'}, so the example fails when an app copies it. Fix: ${missingNameFix(statement, parts.subpath, missing)}`,
|
|
1541
|
+
),
|
|
1542
|
+
);
|
|
777
1543
|
}
|
|
778
1544
|
}
|
|
779
1545
|
}
|
|
@@ -824,6 +1590,27 @@ async function templateIssues(ctx, integration) {
|
|
|
824
1590
|
} catch {
|
|
825
1591
|
text = '';
|
|
826
1592
|
}
|
|
1593
|
+
// `astryx template` copies the source as written, but the package's
|
|
1594
|
+
// public import, the gallery preview and a page that renders the
|
|
1595
|
+
// template import its default export.
|
|
1596
|
+
const exported = text ? moduleExportNames(file, ctx.exportNames) : null;
|
|
1597
|
+
if (exported?.complete && !exported.names.has('default')) {
|
|
1598
|
+
const named = defaultExportCandidate(text, exported);
|
|
1599
|
+
const fix = named
|
|
1600
|
+
? `add \`export default ${named};\` to ${shown}, or make ${named} the default export`
|
|
1601
|
+
: `export the template's component as the default export of ${shown} (\`export default function …\`)`;
|
|
1602
|
+
// With no package name there is no public import to name.
|
|
1603
|
+
const what =
|
|
1604
|
+
ctx.name == null
|
|
1605
|
+
? 'anything that renders the template gets nothing'
|
|
1606
|
+
: `its public import "${ctx.name}${subpath.slice(1)}" and anything that renders the template get nothing`;
|
|
1607
|
+
issues.push(
|
|
1608
|
+
listed(
|
|
1609
|
+
'template_no_default_export',
|
|
1610
|
+
`Template "${id}" (${shown}) has no default export, so ${what}. Fix: ${fix}.`,
|
|
1611
|
+
),
|
|
1612
|
+
);
|
|
1613
|
+
}
|
|
827
1614
|
for (const statement of readImports(text)) {
|
|
828
1615
|
const spec = statement.source;
|
|
829
1616
|
if (spec.startsWith('.')) {
|
|
@@ -841,17 +1628,15 @@ async function templateIssues(ctx, integration) {
|
|
|
841
1628
|
const parts = splitSpecifier(spec);
|
|
842
1629
|
if (!parts) continue;
|
|
843
1630
|
if (ctx.name != null && parts.pkg === ctx.name) {
|
|
844
|
-
const {undeclared,
|
|
1631
|
+
const {undeclared, missing, reason} = judgeOwnImport(
|
|
845
1632
|
ctx,
|
|
846
1633
|
statement,
|
|
847
1634
|
parts.subpath,
|
|
848
1635
|
);
|
|
849
|
-
if (
|
|
850
|
-
ctx.unchecked.push({file: shown, specifier: spec, reason
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
unbuiltOutput(ctx, parts.subpath, undeclared)
|
|
854
|
-
) {
|
|
1636
|
+
if (reason) {
|
|
1637
|
+
ctx.unchecked.push({file: shown, specifier: spec, reason});
|
|
1638
|
+
}
|
|
1639
|
+
if (undeclared && unbuiltOutput(ctx, parts.subpath, undeclared)) {
|
|
855
1640
|
ctx.unchecked.push({
|
|
856
1641
|
file: shown,
|
|
857
1642
|
specifier: spec,
|
|
@@ -864,6 +1649,13 @@ async function templateIssues(ctx, integration) {
|
|
|
864
1649
|
`Template "${id}" (${shown}) imports "${spec}", but ${whyUndeclared(undeclared, parts.subpath)}, so the template breaks in an app. Fix: import from a subpath package.json "exports" declares, or add one for it.`,
|
|
865
1650
|
),
|
|
866
1651
|
);
|
|
1652
|
+
} else if (missing && missing.length > 0) {
|
|
1653
|
+
issues.push(
|
|
1654
|
+
listed(
|
|
1655
|
+
'template_import_missing_name',
|
|
1656
|
+
`Template "${id}" (${shown}) imports ${missing.map(n => `"${n}"`).join(', ')} from "${spec}", which does not export ${missing.length === 1 ? 'it' : 'them'}, so the template breaks in an app. Fix: import a name that module exports, or export the name from it.`,
|
|
1657
|
+
),
|
|
1658
|
+
);
|
|
867
1659
|
}
|
|
868
1660
|
} else if (parts.pkg === CORE && !ctx.corePkg) {
|
|
869
1661
|
ctx.unchecked.push({
|
|
@@ -952,6 +1744,7 @@ export async function publicImportIssues(packageDir, integration) {
|
|
|
952
1744
|
coreDir: isCore ? foundCore : null,
|
|
953
1745
|
corePkg: isCore ? foundCorePkg : null,
|
|
954
1746
|
unchecked: report.unchecked,
|
|
1747
|
+
exportNames: new Map(),
|
|
955
1748
|
files: Array.isArray(pkg.files)
|
|
956
1749
|
? pkg.files
|
|
957
1750
|
.filter(entry => typeof entry === 'string')
|