@astryxdesign/cli 0.7.3-canary.cde6233 → 0.7.3-canary.d421aed

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 (47) hide show
  1. package/api/build/_adapter.mjs +37 -25
  2. package/api/build/blocks/blocks.d.mts +68 -20
  3. package/api/build/blocks/blocks.mjs +472 -196
  4. package/api/build/blocks/blocks.test.mjs +185 -168
  5. package/api/build/blocks/blocks.weights.json +1 -1
  6. package/api/build/build.type.mjs +1 -1
  7. package/api/build/kit/answer.d.mts +4 -1
  8. package/api/build/kit/answer.mjs +11 -3
  9. package/api/build/kit/signals.d.mts +11 -0
  10. package/api/build/kit/signals.mjs +8 -7
  11. package/api/build/tree.mjs +2 -1
  12. package/api/build/tree.test.mjs +27 -27
  13. package/api/component/_adapter.mjs +39 -5
  14. package/api/discover/_adapter.d.mts +1 -2
  15. package/api/discover/_adapter.mjs +6 -8
  16. package/api/discover/discover.mjs +0 -1
  17. package/api/docs/_adapter.mjs +4 -0
  18. package/api/doctor/_adapter.d.mts +24 -10
  19. package/api/doctor/_adapter.mjs +28 -25
  20. package/api/doctor/doctor.d.mts +7 -4
  21. package/api/doctor/doctor.mjs +52 -14
  22. package/api/doctor/doctor.test.mjs +58 -0
  23. package/api/integration/validate-integration-gaps.test.mjs +3 -0
  24. package/api/integration/validateIntegration.doc.mjs +1 -1
  25. package/api/template/template.mjs +5 -1
  26. package/assets/docs/tree/checks.doc.mjs +1 -1
  27. package/clients/cli/commands/setup-nudge.test.mjs +17 -0
  28. package/clients/cli/index.mjs +25 -8
  29. package/foundation/agent-docs/agent-doc-state.d.mts +12 -0
  30. package/foundation/agent-docs/agent-doc-state.mjs +24 -2
  31. package/foundation/agent-docs/agent-docs.mjs +5 -5
  32. package/foundation/agent-docs/computer-agent-text.mjs +16 -1
  33. package/foundation/agent-docs/computer-agent-text.test.mjs +23 -2
  34. package/foundation/integrations/public-imports.d.mts +37 -0
  35. package/foundation/integrations/public-imports.mjs +839 -46
  36. package/foundation/integrations/public-imports.test.mjs +971 -46
  37. package/foundation/release/global-state.d.mts +8 -4
  38. package/foundation/release/global-state.mjs +4 -2
  39. package/foundation/release/notices.d.mts +62 -8
  40. package/foundation/release/notices.mjs +136 -25
  41. package/foundation/release/notices.test.mjs +21 -0
  42. package/foundation/text/string-utils.d.mts +2 -1
  43. package/foundation/text/string-utils.mjs +16 -5
  44. package/foundation/text/suggest-order.d.mts +72 -0
  45. package/foundation/text/suggest-order.mjs +363 -0
  46. package/foundation/text/suggest-order.test.mjs +259 -0
  47. package/package.json +9 -9
@@ -32,10 +32,14 @@ import {CLI_ROOT} from '../fs/paths.mjs';
32
32
  const CODES = new Set([
33
33
  'component_import_not_exported',
34
34
  'component_import_other_package',
35
+ 'component_import_missing_name',
35
36
  'example_import_not_exported',
37
+ 'example_import_missing_name',
36
38
  'template_import_not_exported',
37
39
  'template_import_outside_root',
38
40
  'template_private_import',
41
+ 'template_import_missing_name',
42
+ 'template_no_default_export',
39
43
  ]);
40
44
 
41
45
  let dir;
@@ -284,7 +288,7 @@ describe('public imports: component doc import', () => {
284
288
  });
285
289
  write(
286
290
  'templates/account-card.tsx',
287
- "import {X} from '../components/AcmeWidget';\nexport function A() {\n return <X />;\n}\n",
291
+ "import {X} from '../components/AcmeWidget';\nexport default function A() {\n return <X />;\n}\n",
288
292
  );
289
293
  const issues = await found();
290
294
  expect(issues.length).toBeGreaterThan(2);
@@ -364,6 +368,58 @@ describe('public imports: component doc import', () => {
364
368
  });
365
369
 
366
370
  describe('public imports: component examples', () => {
371
+ it('flags a name the package does not export', async () => {
372
+ write(
373
+ 'components/AcmeWidget.doc.mjs',
374
+ DOC(
375
+ undefined,
376
+ "import {AcmeGadget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeGadget />",
377
+ ),
378
+ );
379
+ expect(await codes()).toEqual(['example_import_missing_name']);
380
+ const issue = (await found()).find(
381
+ i => i.code === 'example_import_missing_name',
382
+ );
383
+ expect(issue?.message).toContain(
384
+ 'import a name that module exports ("AcmeWidget" for this doc)',
385
+ );
386
+ });
387
+
388
+ it("points a missing-name example at the doc's import only when that module exports the missing name", async () => {
389
+ pkg(p => {
390
+ p.exports['./other'] = './lib/other.ts';
391
+ });
392
+ write('lib/other.ts', 'export const Unrelated = 1;\n');
393
+ write(
394
+ 'components/AcmeWidget.tsx',
395
+ 'export function AcmeWidget() {\n return null;\n}\nexport function AcmeGadget() {\n return null;\n}\n',
396
+ );
397
+ write(
398
+ 'components/AcmeWidget.doc.mjs',
399
+ DOC(
400
+ undefined,
401
+ "import {AcmeGadget} from '@acme/widgets/other';\\n<AcmeGadget />",
402
+ ),
403
+ );
404
+ const issue = (await found()).find(
405
+ i => i.code === 'example_import_missing_name',
406
+ );
407
+ expect(issue?.message).not.toContain('for this doc');
408
+ expect(issue?.message).toContain(
409
+ "import from '@acme/widgets/components/AcmeWidget', as the doc's import does",
410
+ );
411
+ // Without the name there, the doc's import is no fix.
412
+ write(
413
+ 'components/AcmeWidget.tsx',
414
+ 'export function AcmeWidget() {\n return null;\n}\n',
415
+ );
416
+ const generic = (await found()).find(
417
+ i => i.code === 'example_import_missing_name',
418
+ );
419
+ expect(generic?.message).toContain('Fix: Export the name from it.');
420
+ expect(generic?.message).not.toContain("as the doc's import does");
421
+ });
422
+
367
423
  it('flags a subpath exports does not declare', async () => {
368
424
  write(
369
425
  'components/AcmeWidget.doc.mjs',
@@ -375,6 +431,22 @@ describe('public imports: component examples', () => {
375
431
  expect(await codes()).toEqual(['example_import_not_exported']);
376
432
  });
377
433
 
434
+ it('points an undeclared example import at the doc import when that is declared', async () => {
435
+ write(
436
+ 'components/AcmeWidget.doc.mjs',
437
+ DOC(
438
+ undefined,
439
+ "import {AcmeWidget} from '@acme/widgets/src/AcmeWidget';\\n<AcmeWidget />",
440
+ ),
441
+ );
442
+ const issue = (await found()).find(
443
+ i => i.code === 'example_import_not_exported',
444
+ );
445
+ expect(issue?.message).toContain(
446
+ "Fix: import from '@acme/widgets/components/AcmeWidget' in the example, as the doc's import does.",
447
+ );
448
+ });
449
+
378
450
  it('never points an example back at the same undeclared doc import', async () => {
379
451
  pkg(p => delete p.exports['./components/AcmeWidget']);
380
452
  const issue = (await found()).find(
@@ -409,6 +481,61 @@ describe('public imports: component examples', () => {
409
481
  );
410
482
  });
411
483
 
484
+ it('never points a side-effect import of an undeclared subpath at another subpath', async () => {
485
+ write('styles.css', '.acme {}\n');
486
+ write(
487
+ 'components/AcmeWidget.doc.mjs',
488
+ DOC(undefined, "import '@acme/widgets/styles.css';\\n<AcmeWidget />"),
489
+ );
490
+ const issue = (await found()).find(
491
+ i => i.code === 'example_import_not_exported',
492
+ );
493
+ expect(issue?.message).not.toContain('in the example');
494
+ expect(issue?.message).toContain(
495
+ 'add "./styles.css": "./styles.css" to package.json "exports"',
496
+ );
497
+ });
498
+
499
+ it('points an example at the subpath the source is already exported under', async () => {
500
+ pkg(p => {
501
+ delete p.exports['./components/AcmeWidget'];
502
+ p.exports['./widget'] = './components/AcmeWidget.tsx';
503
+ });
504
+ write(
505
+ 'components/AcmeWidget.doc.mjs',
506
+ DOC(
507
+ '@acme/widgets/nope',
508
+ "import {AcmeWidget} from '@acme/widgets/nope';\\n<AcmeWidget />",
509
+ ),
510
+ );
511
+ const issue = (await found()).find(
512
+ i => i.code === 'example_import_not_exported',
513
+ );
514
+ expect(issue?.message).toContain(
515
+ "Fix: import from '@acme/widgets/widget' in the example (already exported).",
516
+ );
517
+ });
518
+
519
+ it('leaves a CommonJS target unjudged for names', async () => {
520
+ write(
521
+ 'components/AcmeWidget.doc.mjs',
522
+ DOC(
523
+ undefined,
524
+ "import {AcmeGadget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeGadget />",
525
+ ),
526
+ );
527
+ write(
528
+ 'components/AcmeWidget.tsx',
529
+ 'module.exports = {AcmeWidget: () => null, AcmeGadget: () => null};\n',
530
+ );
531
+ expect(await codes()).toEqual([]);
532
+ pkg(p => {
533
+ p.exports['./components/AcmeWidget'] = './components/AcmeWidget.cjs';
534
+ });
535
+ write('components/AcmeWidget.cjs', 'export const Other = 1;\n');
536
+ expect(await codes()).toEqual([]);
537
+ });
538
+
412
539
  it('accepts aliased, type-only and other-package imports', async () => {
413
540
  write(
414
541
  'components/AcmeWidget.doc.mjs',
@@ -460,6 +587,60 @@ describe('public imports: template sources', () => {
460
587
  expect(await codes()).toEqual([]);
461
588
  });
462
589
 
590
+ it('flags a name its own package does not export', async () => {
591
+ write(
592
+ 'templates/account-card.tsx',
593
+ 'import {AcmeGadget} from \'@acme/widgets/components/AcmeWidget\';\nexport default function A() {\n return <AcmeGadget label="x" />;\n}\n',
594
+ );
595
+ expect(await codes()).toEqual(['template_import_missing_name']);
596
+ });
597
+
598
+ it('leaves a built CommonJS dist file reached through exports unjudged for names', async () => {
599
+ pkg(p => {
600
+ p.exports['./components/AcmeWidget'] = {
601
+ types: './dist/AcmeWidget.d.ts',
602
+ default: './dist/AcmeWidget.js',
603
+ };
604
+ });
605
+ write(
606
+ 'dist/AcmeWidget.js',
607
+ "'use strict';\nObject.defineProperty(exports, '__esModule', {value: true});\nexports.AcmeWidget = void 0;\nconst AcmeWidget = () => null;\nexports.AcmeWidget = AcmeWidget;\n",
608
+ );
609
+ write(
610
+ 'templates/account-card.tsx',
611
+ 'import {AcmeGadget} from \'@acme/widgets/components/AcmeWidget\';\nexport default function A() {\n return <AcmeGadget label="x" />;\n}\n',
612
+ );
613
+ write(
614
+ 'components/AcmeWidget.doc.mjs',
615
+ DOC(
616
+ undefined,
617
+ "import {AcmeGadget} from '@acme/widgets/components/AcmeWidget';\\n<AcmeGadget />",
618
+ ),
619
+ );
620
+ expect(await codes()).toEqual([]);
621
+ });
622
+
623
+ it('leaves a module that also assigns exports.* unjudged for names', async () => {
624
+ write(
625
+ 'components/AcmeWidget.tsx',
626
+ 'export function AcmeWidget() {\n return null;\n}\nexports.AcmeGadget = AcmeWidget;\n',
627
+ );
628
+ write(
629
+ 'templates/account-card.tsx',
630
+ 'import {AcmeGadget} from \'@acme/widgets/components/AcmeWidget\';\nexport default function A() {\n return <AcmeGadget label="x" />;\n}\n',
631
+ );
632
+ expect(await codes()).toEqual([]);
633
+ });
634
+
635
+ it('leaves a CommonJS target of a template import unjudged for names', async () => {
636
+ write('components/AcmeWidget.tsx', 'exports.AcmeWidget = () => null;\n');
637
+ write(
638
+ 'templates/account-card.tsx',
639
+ 'import {AcmeGadget} from \'@acme/widgets/components/AcmeWidget\';\nexport default function A() {\n return <AcmeGadget label="x" />;\n}\n',
640
+ );
641
+ expect(await codes()).toEqual([]);
642
+ });
643
+
463
644
  it('flags a template whose public import exports does not declare', async () => {
464
645
  pkg(p => delete p.exports['./templates/account-card']);
465
646
  const issue = (await found()).find(
@@ -471,6 +652,38 @@ describe('public imports: template sources', () => {
471
652
  });
472
653
  });
473
654
 
655
+ describe('template default export', () => {
656
+ it('lists a template source with no default export, naming the export to add', async () => {
657
+ write(
658
+ 'templates/account-card.tsx',
659
+ 'export function AccountCard() {\n return <div>Account</div>;\n}\n',
660
+ );
661
+ const issues = await found();
662
+ expect(issues.map(i => i.code)).toEqual(['template_no_default_export']);
663
+ expect(issues[0].message).toContain('export default AccountCard;');
664
+ });
665
+
666
+ it('suggests a value, never a type, as the default export', async () => {
667
+ write(
668
+ 'templates/account-card.tsx',
669
+ 'export interface AccountCardProps {\n title: string;\n}\nexport type AccountCardSize = "sm";\nexport function AccountCard() {\n return <div>Account</div>;\n}\n',
670
+ );
671
+ const issue = (await found()).find(
672
+ i => i.code === 'template_no_default_export',
673
+ );
674
+ expect(issue?.message).toContain('export default AccountCard;');
675
+ expect(issue?.message).not.toContain('AccountCardProps;');
676
+ });
677
+
678
+ it('accepts a re-exported or type-annotated default', async () => {
679
+ write(
680
+ 'templates/account-card.tsx',
681
+ 'function AccountCard() {\n return <div>Account</div>;\n}\nexport {AccountCard as default};\n',
682
+ );
683
+ expect(await codes()).toEqual([]);
684
+ });
685
+ });
686
+
474
687
  describe('subpathDeclaration', () => {
475
688
  it('reads each way exports can declare a subpath', () => {
476
689
  expect(subpathDeclaration(undefined, './a').state).toBe('no_exports');
@@ -504,6 +717,43 @@ describe('derived component imports', () => {
504
717
  };
505
718
  `;
506
719
 
720
+ it('warns when the derived root import does not export the component, naming the subpath that does', async () => {
721
+ write('components/AcmeWidget.doc.mjs', NO_IMPORT);
722
+ write('index.js', 'export const Other = 1;\n');
723
+ pkg(p => {
724
+ p.exports['.'] = './index.js';
725
+ });
726
+ const issues = (await found()).filter(
727
+ i => i.code === 'component_import_missing_name',
728
+ );
729
+ expect(issues).toHaveLength(1);
730
+ expect(issues[0].message).toContain(
731
+ 'has no import, so apps are told to import from "@acme/widgets"',
732
+ );
733
+ expect(issues[0].message).toContain(
734
+ "set import: '@acme/widgets/components/AcmeWidget' in the doc",
735
+ );
736
+ });
737
+
738
+ it('names the wildcard subpath the component is already exported under', async () => {
739
+ write('index.js', 'export const Other = 1;\n');
740
+ pkg(p => {
741
+ p.exports = {
742
+ '.': './index.js',
743
+ './components/*': './components/*.tsx',
744
+ './templates/account-card': './templates/account-card.tsx',
745
+ };
746
+ });
747
+ write('components/AcmeWidget.doc.mjs', DOC('@acme/widgets'));
748
+ const issue = (await found()).find(
749
+ i => i.code === 'component_import_missing_name',
750
+ );
751
+ expect(issue?.message).toContain(
752
+ "set import: '@acme/widgets/components/AcmeWidget' in the doc (that subpath exports it)",
753
+ );
754
+ expect(issue?.message).not.toContain('add "./components/AcmeWidget"');
755
+ });
756
+
507
757
  it('warns when the derived root import is not declared', async () => {
508
758
  write('components/AcmeWidget.doc.mjs', NO_IMPORT);
509
759
  const issue = (await found()).find(
@@ -513,6 +763,51 @@ describe('derived component imports', () => {
513
763
  'has no import, so apps are told to import from "@acme/widgets"',
514
764
  );
515
765
  });
766
+
767
+ it('warns when a written import does not export the component', async () => {
768
+ write('index.js', 'export const Other = 1;\n');
769
+ pkg(p => {
770
+ p.exports['.'] = './index.js';
771
+ });
772
+ write('components/AcmeWidget.doc.mjs', DOC('@acme/widgets'));
773
+ expect(await codes()).toContain('component_import_missing_name');
774
+ });
775
+
776
+ it('warns when a written import does not export the component, even while the package augments a module', async () => {
777
+ write('index.js', 'export const Other = 1;\n');
778
+ write(
779
+ 'types/augment.d.ts',
780
+ "declare module '@acme/widgets' {\n interface AcmeTone {}\n}\nexport {};\n",
781
+ );
782
+ pkg(p => {
783
+ p.exports['.'] = './index.js';
784
+ });
785
+ write('components/AcmeWidget.doc.mjs', DOC('@acme/widgets'));
786
+ expect(await codes()).toContain('component_import_missing_name');
787
+ });
788
+
789
+ it('lists a derived import whose target is not built as not checked', async () => {
790
+ write('components/AcmeWidget.doc.mjs', NO_IMPORT);
791
+ pkg(p => {
792
+ p.exports['.'] = {types: './dist/index.d.ts', import: './dist/index.js'};
793
+ });
794
+ const result = await validateIntegration(undefined, {cwd: dir});
795
+ expect(
796
+ (result.data.notes ?? []).filter(i =>
797
+ i.code.startsWith('component_import'),
798
+ ),
799
+ ).toEqual([]);
800
+ expect(result.data.publicImports?.unchecked).toEqual([
801
+ expect.objectContaining({
802
+ file: 'components/AcmeWidget.doc.mjs',
803
+ specifier: '@acme/widgets',
804
+ reason: expect.stringContaining('build output'),
805
+ }),
806
+ ]);
807
+ const check = await integrationCheck({cwd: dir, strict: true});
808
+ expect(check.data.unchecked).toBe(1);
809
+ expect(check.data.failed).toBe(false);
810
+ });
516
811
  });
517
812
 
518
813
  describe('fixes for two components in one folder', () => {
@@ -547,6 +842,44 @@ describe('fixes for two components in one folder', () => {
547
842
  );
548
843
  for (const fix of fixes) expect(fix).not.toContain('".":');
549
844
  });
845
+
846
+ it('gives each a per-component entry when the derived folder subpath lacks it', async () => {
847
+ write('components/index.js', 'export const Other = 1;\n');
848
+ pkg(p => {
849
+ p.exports['./components'] = './components/index.js';
850
+ p.exports['.'] = './components/index.js';
851
+ });
852
+ const fixes = (await found())
853
+ .filter(i => i.code === 'component_import_missing_name')
854
+ .map(i => i.message.split('Fix: ')[1]);
855
+ expect(fixes).toHaveLength(2);
856
+ expect(fixes[0]).toContain(
857
+ `add "./components/AcmeAlpha": "./components/AcmeAlpha.tsx" to package.json "exports" and set import: '@acme/widgets/components/AcmeAlpha' in the doc`,
858
+ );
859
+ expect(fixes[1]).toContain(
860
+ '"./components/AcmeBeta": "./components/AcmeBeta.tsx"',
861
+ );
862
+ for (const fix of fixes) expect(fix).not.toContain('"./components":');
863
+ });
864
+
865
+ it('never offers a per-component entry when the source exports the component only as default', async () => {
866
+ write('components/index.js', 'export const Other = 1;\n');
867
+ write(
868
+ 'components/AcmeAlpha.tsx',
869
+ 'export default function AcmeAlpha() {\n return null;\n}\n',
870
+ );
871
+ pkg(p => {
872
+ p.exports['./components'] = './components/index.js';
873
+ p.exports['.'] = './components/index.js';
874
+ });
875
+ const fix = (await found())
876
+ .filter(i => i.code === 'component_import_missing_name')
877
+ .map(i => i.message.split('Fix: ')[1])
878
+ .find(text => text.includes('"AcmeAlpha"'));
879
+ expect(fix).toBe(
880
+ 'export "AcmeAlpha" from the module "@acme/widgets/components" loads.',
881
+ );
882
+ });
550
883
  });
551
884
 
552
885
  describe('type-only imports', () => {
@@ -698,29 +1031,6 @@ describe('an empty exports map', () => {
698
1031
  });
699
1032
  });
700
1033
 
701
- describe('a fresh scaffold passes the checks', () => {
702
- for (const exportsMap of [undefined, {'.': './index.js'}]) {
703
- it(`with ${exportsMap ? 'an' : 'no'} exports map`, async () => {
704
- fs.rmSync(dir, {recursive: true, force: true});
705
- fs.mkdirSync(dir);
706
- write(
707
- 'package.json',
708
- JSON.stringify({
709
- name: '@acme/fresh',
710
- version: '1.0.0',
711
- type: 'module',
712
- ...(exportsMap ? {exports: exportsMap} : {}),
713
- }),
714
- );
715
- write('index.js', 'export {};\n');
716
- await integrationAddComponent('AcmeThing', {cwd: dir});
717
- await integrationAddTemplate('acme-page', {cwd: dir});
718
- const check = await integrationCheck({cwd: dir, strict: true});
719
- expect(check.data.failures).toEqual([]);
720
- });
721
- }
722
- });
723
-
724
1034
  describe('imports listed as not checked', () => {
725
1035
  it('lists a template import of Core when Core is not installed', () => {
726
1036
  const out = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-no-core-'));
@@ -775,30 +1085,225 @@ describe('imports listed as not checked', () => {
775
1085
  fs.rmSync(out, {recursive: true, force: true});
776
1086
  }
777
1087
  }, 60_000);
1088
+
1089
+ it('lists template and example imports whose names cannot be judged', async () => {
1090
+ pkg(p => {
1091
+ p.exports['./built'] = './dist/built.js';
1092
+ p.exports['./cjs'] = './cjs.cjs';
1093
+ });
1094
+ write('cjs.cjs', 'module.exports = {A: 1};\n');
1095
+ write(
1096
+ 'templates/account-card.tsx',
1097
+ "import {Built} from '@acme/widgets/built';\nimport {A} from '@acme/widgets/cjs';\nimport {AcmeWidget} from '@acme/widgets/components/AcmeWidget';\nexport default function C() {\n return <AcmeWidget label=\"x\" />;\n}\n",
1098
+ );
1099
+ const result = await validateIntegration(undefined, {cwd: dir});
1100
+ expect(result.data.issues).toEqual([]);
1101
+ expect(
1102
+ result.data.publicImports?.unchecked.map(u => [u.file, u.specifier]),
1103
+ ).toEqual([
1104
+ ['templates/account-card.tsx', '@acme/widgets/built'],
1105
+ ['templates/account-card.tsx', '@acme/widgets/cjs'],
1106
+ ]);
1107
+ const check = await integrationCheck({cwd: dir, strict: true});
1108
+ expect(check.data.unchecked).toBe(2);
1109
+ expect(check.data.failed).toBe(false);
1110
+ });
778
1111
  });
779
1112
 
780
- describe('which Core and which package', () => {
781
- it("lists Core imports as not checked when the found core is a workspace's own package", async () => {
782
- // An npm-workspaces monorepo: its own packages/core is @acme/core, and
783
- // the integration lives in packages/widgets.
784
- const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-workspace-'));
785
- try {
786
- const put = (/** @type {string} */ rel, /** @type {string} */ text) => {
787
- fs.mkdirSync(path.dirname(path.join(root, rel)), {recursive: true});
788
- fs.writeFileSync(path.join(root, rel), text);
789
- };
790
- put(
791
- 'package.json',
792
- JSON.stringify({
793
- name: 'acme-monorepo',
794
- private: true,
795
- workspaces: ['packages/*'],
796
- }),
797
- );
798
- put(
799
- 'packages/core/package.json',
800
- JSON.stringify({
801
- name: '@acme/core',
1113
+ describe('a root import in a package with no exports', () => {
1114
+ it('says the package has no exports map', async () => {
1115
+ pkg(p => delete p.exports);
1116
+ write(
1117
+ 'templates/account-card.tsx',
1118
+ 'import {AcmeWidget} from \'@acme/widgets\';\nexport default function C() {\n return <AcmeWidget label="x" />;\n}\n',
1119
+ );
1120
+ const result = await validateIntegration(undefined, {cwd: dir});
1121
+ const entry = result.data.publicImports?.unchecked.find(
1122
+ u => u.file === 'templates/account-card.tsx',
1123
+ );
1124
+ expect(entry?.reason).toContain('package.json has no "exports"');
1125
+ expect(entry?.reason).not.toContain('build output');
1126
+ });
1127
+ });
1128
+
1129
+ describe('a fresh scaffold passes the checks', () => {
1130
+ for (const exportsMap of [undefined, {'.': './index.js'}]) {
1131
+ it(`with ${exportsMap ? 'an' : 'no'} exports map`, async () => {
1132
+ fs.rmSync(dir, {recursive: true, force: true});
1133
+ fs.mkdirSync(dir);
1134
+ write(
1135
+ 'package.json',
1136
+ JSON.stringify({
1137
+ name: '@acme/fresh',
1138
+ version: '1.0.0',
1139
+ type: 'module',
1140
+ ...(exportsMap ? {exports: exportsMap} : {}),
1141
+ }),
1142
+ );
1143
+ write('index.js', 'export {};\n');
1144
+ await integrationAddComponent('AcmeThing', {cwd: dir});
1145
+ await integrationAddTemplate('acme-page', {cwd: dir});
1146
+ const check = await integrationCheck({cwd: dir, strict: true});
1147
+ expect(check.data.failures).toEqual([]);
1148
+ });
1149
+ }
1150
+ });
1151
+
1152
+ describe('export * cycles and repeated checks', () => {
1153
+ beforeEach(() => {
1154
+ pkg(p => {
1155
+ p.exports['./a'] = './lib/a.ts';
1156
+ p.exports['./b'] = './lib/b.ts';
1157
+ });
1158
+ write('lib/a.ts', "export const A = 1;\nexport * from './b';\n");
1159
+ write('lib/b.ts', "export * from './a';\nexport const B = 2;\n");
1160
+ });
1161
+
1162
+ it('reads every name through an export * cycle, from either side', async () => {
1163
+ write(
1164
+ 'templates/account-card.tsx',
1165
+ "import {A, B} from '@acme/widgets/a';\nimport {A as A2, B as B2} from '@acme/widgets/b';\nimport {AcmeWidget} from '@acme/widgets/components/AcmeWidget';\nexport default function C() {\n return <AcmeWidget label={String(A + B + A2 + B2)} />;\n}\n",
1166
+ );
1167
+ expect(await codes()).toEqual([]);
1168
+ });
1169
+
1170
+ it('rereads a re-exported module on a second check in the same process', async () => {
1171
+ write('lib/b.ts', 'export const B = 2;\n');
1172
+ write('lib/c.ts', "export * from './b';\n");
1173
+ pkg(p => {
1174
+ p.exports['./c'] = './lib/c.ts';
1175
+ });
1176
+ write(
1177
+ 'templates/account-card.tsx',
1178
+ "import {C} from '@acme/widgets/c';\nexport default function T() {\n return <div>{C}</div>;\n}\n",
1179
+ );
1180
+ expect(await codes()).toEqual(['template_import_missing_name']);
1181
+ // Fix only the re-exported module: c.ts itself does not change.
1182
+ write('lib/b.ts', 'export const B = 2;\nexport const C = 3;\n');
1183
+ expect(await codes()).toEqual([]);
1184
+ });
1185
+ });
1186
+
1187
+ describe('export import declarations', () => {
1188
+ it('counts `export import Alias = Inner.Value` as an exported name', async () => {
1189
+ pkg(p => {
1190
+ p.exports['./ns'] = './lib/ns.ts';
1191
+ });
1192
+ write(
1193
+ 'lib/ns.ts',
1194
+ 'namespace Inner {\n export const Value = 1;\n}\nexport const Other = 2;\nexport import Alias = Inner.Value;\n',
1195
+ );
1196
+ write(
1197
+ 'templates/account-card.tsx',
1198
+ "import {Alias} from '@acme/widgets/ns';\nexport default function C() {\n return <div>{Alias}</div>;\n}\n",
1199
+ );
1200
+ expect(await codes()).toEqual([]);
1201
+ });
1202
+
1203
+ it('leaves a module with a type-only export import unjudged', async () => {
1204
+ pkg(p => {
1205
+ p.exports['./ns'] = './lib/ns.ts';
1206
+ });
1207
+ write(
1208
+ 'lib/ns.ts',
1209
+ "export const Kept = 1;\nexport import type Alias = require('./other');\n",
1210
+ );
1211
+ write(
1212
+ 'templates/account-card.tsx',
1213
+ "import {Other} from '@acme/widgets/ns';\nexport default function C() {\n return <div>{Other}</div>;\n}\n",
1214
+ );
1215
+ const result = await validateIntegration(undefined, {cwd: dir});
1216
+ expect(
1217
+ (result.data.notes ?? []).filter(
1218
+ i => i.code === 'template_import_missing_name',
1219
+ ),
1220
+ ).toEqual([]);
1221
+ expect(
1222
+ result.data.publicImports?.unchecked.map(u => u.specifier),
1223
+ ).toContain('@acme/widgets/ns');
1224
+ });
1225
+ });
1226
+
1227
+ describe('types from a built package', () => {
1228
+ it('reads a type re-exported through export * in a declaration file', async () => {
1229
+ pkg(p => {
1230
+ p.exports['./widget'] = {
1231
+ types: './dist/widget.d.ts',
1232
+ import: './dist/widget.js',
1233
+ };
1234
+ });
1235
+ write(
1236
+ 'dist/widget.js',
1237
+ "export * from './types.js';\nexport const AcmeWidget = () => null;\n",
1238
+ );
1239
+ write(
1240
+ 'dist/widget.d.ts',
1241
+ "export * from './types.js';\nexport declare const AcmeWidget: () => null;\n",
1242
+ );
1243
+ write('dist/types.js', 'export {};\n');
1244
+ write(
1245
+ 'dist/types.d.ts',
1246
+ 'export interface AcmeWidgetProps {\n label: string;\n}\n',
1247
+ );
1248
+ write(
1249
+ 'templates/account-card.tsx',
1250
+ "import {AcmeWidget, AcmeWidgetProps} from '@acme/widgets/widget';\nexport default function C(props: AcmeWidgetProps) {\n return <AcmeWidget {...props} />;\n}\n",
1251
+ );
1252
+ expect(await codes()).toEqual([]);
1253
+ });
1254
+
1255
+ it('reads the declaration file beside a .js export target', async () => {
1256
+ pkg(p => {
1257
+ p.exports['./built'] = './dist/built.js';
1258
+ });
1259
+ write('dist/built.js', 'export const AcmeThing = 1;\n');
1260
+ write(
1261
+ 'dist/built.d.ts',
1262
+ 'export declare const AcmeThing: number;\nexport type AcmeThingSize = "sm" | "md";\n',
1263
+ );
1264
+ write(
1265
+ 'templates/account-card.tsx',
1266
+ "import {AcmeThing, AcmeThingSize} from '@acme/widgets/built';\nexport default function C() {\n const size: AcmeThingSize = 'sm';\n return <div>{AcmeThing}{size}</div>;\n}\n",
1267
+ );
1268
+ expect(await codes()).toEqual([]);
1269
+ });
1270
+
1271
+ it('still warns on a name neither the module nor its declarations export', async () => {
1272
+ pkg(p => {
1273
+ p.exports['./built'] = './dist/built.js';
1274
+ });
1275
+ write('dist/built.js', 'export const AcmeThing = 1;\n');
1276
+ write('dist/built.d.ts', 'export declare const AcmeThing: number;\n');
1277
+ write(
1278
+ 'templates/account-card.tsx',
1279
+ "import {AcmeOther} from '@acme/widgets/built';\nexport default function C() {\n return <div>{AcmeOther}</div>;\n}\n",
1280
+ );
1281
+ expect(await codes()).toEqual(['template_import_missing_name']);
1282
+ });
1283
+ });
1284
+
1285
+ describe('which Core and which package', () => {
1286
+ it("lists Core imports as not checked when the found core is a workspace's own package", async () => {
1287
+ // An npm-workspaces monorepo: its own packages/core is @acme/core, and
1288
+ // the integration lives in packages/widgets.
1289
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-workspace-'));
1290
+ try {
1291
+ const put = (/** @type {string} */ rel, /** @type {string} */ text) => {
1292
+ fs.mkdirSync(path.dirname(path.join(root, rel)), {recursive: true});
1293
+ fs.writeFileSync(path.join(root, rel), text);
1294
+ };
1295
+ put(
1296
+ 'package.json',
1297
+ JSON.stringify({
1298
+ name: 'acme-monorepo',
1299
+ private: true,
1300
+ workspaces: ['packages/*'],
1301
+ }),
1302
+ );
1303
+ put(
1304
+ 'packages/core/package.json',
1305
+ JSON.stringify({
1306
+ name: '@acme/core',
802
1307
  version: '1.0.0',
803
1308
  exports: {'.': './index.js'},
804
1309
  }),
@@ -866,3 +1371,423 @@ describe('which Core and which package', () => {
866
1371
  ).toEqual([]);
867
1372
  });
868
1373
  });
1374
+
1375
+ describe('what the export-list reader does not model is not checked', () => {
1376
+ const template = (/** @type {string} */ names, /** @type {string} */ from) =>
1377
+ `import {${names}} from '${from}';\nexport default function C() {\n return <div />;\n}\n`;
1378
+
1379
+ it('lists a name as not checked when a declared types target is not built yet', async () => {
1380
+ pkg(p => {
1381
+ p.exports['./widget'] = {
1382
+ types: './dist/widget.d.ts',
1383
+ import: './src/widget.ts',
1384
+ };
1385
+ });
1386
+ write('src/widget.ts', 'export const AcmeThing = 1;\n');
1387
+ write(
1388
+ 'templates/account-card.tsx',
1389
+ template('AcmeThing, AcmeThingProps', '@acme/widgets/widget'),
1390
+ );
1391
+ const result = await validateIntegration(undefined, {cwd: dir});
1392
+ expect(await codes()).toEqual([]);
1393
+ expect(result.data.publicImports?.unchecked).toEqual([
1394
+ expect.objectContaining({
1395
+ specifier: '@acme/widgets/widget',
1396
+ reason: expect.stringContaining('does not exist yet'),
1397
+ }),
1398
+ ]);
1399
+ });
1400
+
1401
+ it('resolves a .js specifier in TypeScript source to the .ts file beside it', async () => {
1402
+ pkg(p => {
1403
+ p.exports['./widget'] = './src/widget.ts';
1404
+ });
1405
+ write('src/widget.ts', "export * from './inner.js';\n");
1406
+ write('src/inner.ts', 'export const Inner = 1;\nexport const Newer = 2;\n');
1407
+ // A stale build of inner beside its source, without the newer export.
1408
+ write('src/inner.js', 'export const Inner = 1;\n');
1409
+ write(
1410
+ 'templates/account-card.tsx',
1411
+ template('Newer', '@acme/widgets/widget'),
1412
+ );
1413
+ expect(await codes()).toEqual([]);
1414
+ });
1415
+
1416
+ it('reads the declaration file beside a module an export * reaches', async () => {
1417
+ pkg(p => {
1418
+ p.exports['./widget'] = './dist/index.js';
1419
+ });
1420
+ write('dist/index.js', "export * from './inner.js';\n");
1421
+ write('dist/inner.js', 'export const Inner = 1;\n');
1422
+ write(
1423
+ 'dist/inner.d.ts',
1424
+ 'export declare const Inner: number;\nexport interface InnerProps {\n size: string;\n}\n',
1425
+ );
1426
+ write(
1427
+ 'templates/account-card.tsx',
1428
+ template('Inner, InnerProps', '@acme/widgets/widget'),
1429
+ );
1430
+ expect(await codes()).toEqual([]);
1431
+ });
1432
+
1433
+ it('does not judge a name a JavaScript module without declarations lacks, since it may be a type', async () => {
1434
+ pkg(p => {
1435
+ p.exports['./widget'] = './dist/widget.js';
1436
+ });
1437
+ write('dist/widget.js', 'export const AcmeThing = 1;\n');
1438
+ write(
1439
+ 'templates/account-card.tsx',
1440
+ template('AcmeThing, AcmeThingProps', '@acme/widgets/widget'),
1441
+ );
1442
+ const result = await validateIntegration(undefined, {cwd: dir});
1443
+ expect(await codes()).toEqual([]);
1444
+ expect(result.data.publicImports?.unchecked.map(u => u.specifier)).toEqual([
1445
+ '@acme/widgets/widget',
1446
+ ]);
1447
+ });
1448
+ });
1449
+
1450
+ describe('declaration files follow TypeScript', () => {
1451
+ const template = (/** @type {string} */ names) =>
1452
+ `import {${names}} from '@acme/widgets/widget';\nexport default function C() {\n return <div />;\n}\n`;
1453
+
1454
+ beforeEach(() => {
1455
+ pkg(p => {
1456
+ p.exports['./widget'] = './dist/widget.js';
1457
+ });
1458
+ write('dist/widget.js', 'export const AcmeWidget = () => null;\n');
1459
+ });
1460
+
1461
+ it('counts every top-level declaration of a declaration-file module as exported', async () => {
1462
+ write(
1463
+ 'dist/widget.d.ts',
1464
+ 'interface AcmeWidgetProps {\n label: string;\n}\nexport declare const AcmeWidget: (p: AcmeWidgetProps) => null;\n',
1465
+ );
1466
+ write(
1467
+ 'templates/account-card.tsx',
1468
+ template('AcmeWidget, AcmeWidgetProps'),
1469
+ );
1470
+ expect(await codes()).toEqual([]);
1471
+ });
1472
+
1473
+ it('exports only what is listed once the declaration file has an export list', async () => {
1474
+ write(
1475
+ 'dist/widget.d.ts',
1476
+ 'interface AcmeWidgetProps {\n label: string;\n}\ndeclare const AcmeWidget: (p: AcmeWidgetProps) => null;\nexport {AcmeWidget};\n',
1477
+ );
1478
+ write(
1479
+ 'templates/account-card.tsx',
1480
+ template('AcmeWidget, AcmeWidgetProps'),
1481
+ );
1482
+ expect(await codes()).toEqual(['template_import_missing_name']);
1483
+ });
1484
+
1485
+ // A `declare module './widget.js'` block in a file widget.d.ts loads adds
1486
+ // names widget.d.ts itself does not list.
1487
+ const AUGMENT =
1488
+ "import './widget.js';\ndeclare module './widget.js' {\n interface AcmeWidgetSize {\n value: string;\n }\n}\nexport {};\n";
1489
+ const LISTED =
1490
+ 'declare const AcmeWidget: () => null;\nexport {AcmeWidget};\n';
1491
+
1492
+ for (const [form, head] of [
1493
+ ['a side-effect import', "import './augment.js';\n"],
1494
+ ['a reference directive', '/// <reference path="./augment.d.ts" />\n'],
1495
+ ]) {
1496
+ it(`does not report a name added by augmentation in a file loaded through ${form}`, async () => {
1497
+ write('dist/augment.d.ts', AUGMENT);
1498
+ write('dist/widget.d.ts', `${head}${LISTED}`);
1499
+ write(
1500
+ 'templates/account-card.tsx',
1501
+ template('AcmeWidget, AcmeWidgetSize'),
1502
+ );
1503
+ const result = await validateIntegration(undefined, {cwd: dir});
1504
+ expect(await codes()).toEqual([]);
1505
+ expect(
1506
+ result.data.publicImports?.unchecked.map(u => u.specifier),
1507
+ ).toEqual(['@acme/widgets/widget']);
1508
+ });
1509
+ }
1510
+
1511
+ it('still reports a missing name when no file in the package augments a module', async () => {
1512
+ write('dist/widget.d.ts', LISTED);
1513
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1514
+ expect(await codes()).toEqual(['template_import_missing_name']);
1515
+ });
1516
+
1517
+ it.each([
1518
+ [
1519
+ 'a `declare module` example in a JSDoc comment',
1520
+ "/**\n * @example\n * declare module './widget.js' {\n * interface AcmeWidgetSize {}\n * }\n */\nexport type AcmeTone = 'a';\n",
1521
+ ],
1522
+ [
1523
+ 'a wildcard `declare module`',
1524
+ "declare module '*.doc.mjs' {\n const doc: unknown;\n export default doc;\n}\n",
1525
+ ],
1526
+ [
1527
+ 'a `declare global` block',
1528
+ 'declare global {\n interface AcmeGlobal {}\n}\nexport {};\n',
1529
+ ],
1530
+ [
1531
+ "an augmentation of another package's module",
1532
+ "declare module '@astryxdesign/core' {\n interface AcmeTone {}\n}\nexport {};\n",
1533
+ ],
1534
+ ])('still reports a missing name beside %s', async (_label, text) => {
1535
+ write('dist/augment.d.ts', text);
1536
+ write('dist/widget.d.ts', LISTED);
1537
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1538
+ expect(await codes()).toEqual(['template_import_missing_name']);
1539
+ });
1540
+
1541
+ it("lists a name as not checked when a file augments the package's own name", async () => {
1542
+ write(
1543
+ 'types/augment.d.ts',
1544
+ "declare module '@acme/widgets/widget' {\n interface AcmeWidgetSize {}\n}\nexport {};\n",
1545
+ );
1546
+ write('dist/widget.d.ts', LISTED);
1547
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1548
+ const result = await validateIntegration(undefined, {cwd: dir});
1549
+ expect(await codes()).toEqual([]);
1550
+ expect(
1551
+ result.data.publicImports?.unchecked.map(u => u.reason).join('\n'),
1552
+ ).toContain('"types/augment.d.ts"');
1553
+ });
1554
+
1555
+ it('lists a name as not checked whenever any file in the package augments a module, even one nothing loads', async () => {
1556
+ write('dist/augment.d.ts', AUGMENT);
1557
+ write('dist/widget.d.ts', LISTED);
1558
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1559
+ const result = await validateIntegration(undefined, {cwd: dir});
1560
+ expect(await codes()).toEqual([]);
1561
+ expect(result.data.publicImports?.unchecked).toEqual([
1562
+ expect.objectContaining({
1563
+ specifier: '@acme/widgets/widget',
1564
+ reason: expect.stringContaining('"dist/augment.d.ts"'),
1565
+ }),
1566
+ ]);
1567
+ });
1568
+
1569
+ it('lists a name as not checked when the module re-exports from the augmenting file', async () => {
1570
+ write('dist/augment.d.ts', AUGMENT);
1571
+ write(
1572
+ 'dist/widget.d.ts',
1573
+ `${LISTED}export {AcmeWidgetTone} from './augment.js';\n`,
1574
+ );
1575
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1576
+ const result = await validateIntegration(undefined, {cwd: dir});
1577
+ expect(await codes()).toEqual([]);
1578
+ expect(
1579
+ result.data.publicImports?.unchecked.map(u => u.reason).join('\n'),
1580
+ ).toContain('"dist/augment.d.ts"');
1581
+ });
1582
+
1583
+ it('lists a name as not checked when the augmentation is reached through another declaration file', async () => {
1584
+ write('dist/augment.d.ts', AUGMENT);
1585
+ write(
1586
+ 'dist/types.d.ts',
1587
+ "import './augment.js';\nexport type AcmeTone = 'a';\n",
1588
+ );
1589
+ write(
1590
+ 'dist/widget.d.ts',
1591
+ "import type {AcmeTone} from './types.js';\ndeclare const AcmeWidget: (t: AcmeTone) => null;\nexport {AcmeWidget};\n",
1592
+ );
1593
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1594
+ const result = await validateIntegration(undefined, {cwd: dir});
1595
+ expect(await codes()).toEqual([]);
1596
+ expect(
1597
+ result.data.publicImports?.unchecked.map(u => u.reason).join('\n'),
1598
+ ).toContain('"dist/augment.d.ts"');
1599
+ });
1600
+
1601
+ it('lists a name as not checked when TypeScript source augments the module it exports', async () => {
1602
+ pkg(p => {
1603
+ p.exports['./widget'] = './src/widget.ts';
1604
+ });
1605
+ write(
1606
+ 'src/widget.ts',
1607
+ "import './augment.js';\nexport const AcmeWidget = () => null;\n",
1608
+ );
1609
+ write(
1610
+ 'src/augment.ts',
1611
+ "declare module './widget.js' {\n export interface AcmeWidgetSize {\n value: string;\n }\n}\nexport {};\n",
1612
+ );
1613
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1614
+ const result = await validateIntegration(undefined, {cwd: dir});
1615
+ expect(await codes()).toEqual([]);
1616
+ expect(
1617
+ result.data.publicImports?.unchecked.map(u => u.reason).join('\n'),
1618
+ ).toContain('"src/augment.ts"');
1619
+ });
1620
+
1621
+ it('reads a `.ts` file with an angle-bracket assertion for its augmentation', async () => {
1622
+ write(
1623
+ 'src/augment.ts',
1624
+ "declare const value: unknown;\nconst s = <string>value;\ndeclare module './widget.js' {\n interface AcmeWidgetSize {}\n}\nexport {s};\n",
1625
+ );
1626
+ write('dist/widget.d.ts', LISTED);
1627
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1628
+ const result = await validateIntegration(undefined, {cwd: dir});
1629
+ expect(await codes()).toEqual([]);
1630
+ expect(result.data.publicImports?.unchecked).toEqual([
1631
+ expect.objectContaining({
1632
+ specifier: '@acme/widgets/widget',
1633
+ reason: expect.stringContaining('"src/augment.ts"'),
1634
+ }),
1635
+ ]);
1636
+ });
1637
+
1638
+ it('counts a file with `declare module` that does not parse as augmenting', async () => {
1639
+ write(
1640
+ 'src/augment.ts',
1641
+ "declare module './widget.js' {\n interface AcmeWidgetSize {\n",
1642
+ );
1643
+ write('dist/widget.d.ts', LISTED);
1644
+ write('templates/account-card.tsx', template('AcmeWidget, AcmeWidgetSize'));
1645
+ const result = await validateIntegration(undefined, {cwd: dir});
1646
+ expect(await codes()).toEqual([]);
1647
+ expect(
1648
+ result.data.publicImports?.unchecked.map(u => u.reason).join('\n'),
1649
+ ).toContain('"src/augment.ts"');
1650
+ });
1651
+
1652
+ it('leaves a declaration file with no import or export (a global script) unchecked', async () => {
1653
+ write(
1654
+ 'dist/widget.d.ts',
1655
+ 'interface AcmeWidgetProps {\n label: string;\n}\ndeclare const AcmeWidget: (p: AcmeWidgetProps) => null;\n',
1656
+ );
1657
+ write(
1658
+ 'templates/account-card.tsx',
1659
+ template('AcmeWidget, AcmeWidgetProps'),
1660
+ );
1661
+ const result = await validateIntegration(undefined, {cwd: dir});
1662
+ expect(await codes()).toEqual([]);
1663
+ expect(result.data.publicImports?.unchecked.map(u => u.specifier)).toEqual([
1664
+ '@acme/widgets/widget',
1665
+ ]);
1666
+ });
1667
+ });
1668
+
1669
+ describe('declaration files: default exports', () => {
1670
+ const template = (/** @type {string} */ names) =>
1671
+ `import {${names}} from '@acme/widgets/widget';\nexport default function C() {\n return <div />;\n}\n`;
1672
+ const HEAD =
1673
+ 'interface AcmeWidgetProps {\n label: string;\n}\nexport declare const AcmeWidget: (p: AcmeWidgetProps) => null;\n';
1674
+
1675
+ beforeEach(() => {
1676
+ pkg(p => {
1677
+ p.exports['./widget'] = './dist/widget.js';
1678
+ });
1679
+ write('dist/widget.js', 'export const AcmeWidget = () => null;\n');
1680
+ write(
1681
+ 'templates/account-card.tsx',
1682
+ template('AcmeWidget, AcmeWidgetProps'),
1683
+ );
1684
+ });
1685
+
1686
+ for (const [form, tail] of [
1687
+ ['export default function', 'export default function f(): void;\n'],
1688
+ ['export default class', 'export default class K {}\n'],
1689
+ ['export default interface', 'export default interface I {\n a: 1;\n}\n'],
1690
+ ]) {
1691
+ it(`keeps implicit exports after ${form}`, async () => {
1692
+ write('dist/widget.d.ts', HEAD + tail);
1693
+ expect(await codes()).toEqual([]);
1694
+ });
1695
+ }
1696
+
1697
+ for (const [form, tail] of [
1698
+ [
1699
+ 'export default <expression>',
1700
+ 'declare const d: number;\nexport default d;\n',
1701
+ ],
1702
+ ['export {} list', 'export {};\n'],
1703
+ ['export … from', "export {X} from './x';\n"],
1704
+ ['export *', "export * from './x';\n"],
1705
+ ]) {
1706
+ it(`ends implicit exports at ${form}`, async () => {
1707
+ write('dist/widget.d.ts', HEAD + tail);
1708
+ write('dist/x.d.ts', 'export declare const X: number;\n');
1709
+ expect(await codes()).toEqual(['template_import_missing_name']);
1710
+ });
1711
+ }
1712
+ });
1713
+
1714
+ describe('the default-export fix names a value', () => {
1715
+ it('prefers an exported function or class declaration', async () => {
1716
+ write(
1717
+ 'templates/account-card.tsx',
1718
+ 'export const ACCOUNT_LIMIT = 3;\nexport const Size = "sm";\nexport class AccountCard {}\n',
1719
+ );
1720
+ const [issue] = await found();
1721
+ expect(issue?.message).toContain('export default AccountCard;');
1722
+ });
1723
+
1724
+ it('skips ALL-CAPS names and falls back to the generic fix with none left', async () => {
1725
+ write(
1726
+ 'templates/account-card.tsx',
1727
+ 'export const ACCOUNT_LIMIT = 3;\nexport const API = 1;\n',
1728
+ );
1729
+ const [issue] = await found();
1730
+ expect(issue?.message).not.toContain('export default ACCOUNT_LIMIT');
1731
+ expect(issue?.message).toContain('`export default function …`');
1732
+ });
1733
+
1734
+ it('names no hook, helper or type-only name, and falls back to the generic fix', async () => {
1735
+ write(
1736
+ 'templates/account-card.tsx',
1737
+ 'export function useAccount() {\n return null;\n}\nexport function formatBalance(n: number) {\n return String(n);\n}\nexport const ACCOUNT_LIMIT = 3;\nexport type AccountCardProps = {title: string};\nexport interface AccountCardSize {\n value: string;\n}\n',
1738
+ );
1739
+ const [issue] = await found();
1740
+ expect(issue?.code).toBe('template_no_default_export');
1741
+ expect(issue?.message).not.toContain('export default useAccount;');
1742
+ expect(issue?.message).not.toContain('formatBalance');
1743
+ expect(issue?.message).not.toContain('export default AccountCard');
1744
+ expect(issue?.message).toContain('`export default function …`');
1745
+ });
1746
+
1747
+ it('suggests a PascalCase const when no function or class fits', async () => {
1748
+ write(
1749
+ 'templates/account-card.tsx',
1750
+ 'export function useAccount() {\n return null;\n}\nexport const AccountCard = () => null;\n',
1751
+ );
1752
+ const [issue] = await found();
1753
+ expect(issue?.message).toContain('export default AccountCard;');
1754
+ });
1755
+
1756
+ it('names no public import when package.json has no name', async () => {
1757
+ pkg(p => delete p.name);
1758
+ write(
1759
+ 'templates/account-card.tsx',
1760
+ 'export function AccountCard() {\n return null;\n}\n',
1761
+ );
1762
+ const result = await validateLocalIntegration(dir);
1763
+ const note = (result.notes ?? []).find(
1764
+ n => n.code === 'template_no_default_export',
1765
+ );
1766
+ expect(note?.message).toContain(
1767
+ 'anything that renders the template gets nothing',
1768
+ );
1769
+ expect(note?.message).not.toContain('null/');
1770
+ });
1771
+ });
1772
+
1773
+ describe('a doc name that differs from its file name', () => {
1774
+ it('leaves the export-name judgment to the doc-name check', async () => {
1775
+ write(
1776
+ 'components/AcmeWidget.doc.mjs',
1777
+ "export default {type: 'component', name: 'AcmeWidgets', import: '@acme/widgets/components/AcmeWidget', usage: {description: 'A widget.'}, props: []};\n",
1778
+ );
1779
+ write(
1780
+ 'components/AcmeWidget.tsx',
1781
+ "export {AcmeWidgets} from '@acme/ui';\n",
1782
+ );
1783
+ const result = await validateIntegration(undefined, {cwd: dir});
1784
+ expect(
1785
+ (await codes()).filter(code => code === 'component_import_missing_name'),
1786
+ ).toEqual([]);
1787
+ expect(
1788
+ (result.data.publicImports?.unchecked ?? []).filter(
1789
+ u => u.file === 'components/AcmeWidget.doc.mjs',
1790
+ ),
1791
+ ).toEqual([]);
1792
+ });
1793
+ });