@astryxdesign/cli 0.7.2-canary.db3a2e7 → 0.7.2-canary.e26723d

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 (142) hide show
  1. package/README.md +7 -7
  2. package/api/build/help/help.mjs +1 -1
  3. package/api/component/_adapter.d.mts +41 -1
  4. package/api/component/_adapter.mjs +93 -7
  5. package/api/component/component.mjs +22 -5
  6. package/api/component/component.test.mjs +420 -167
  7. package/api/component/integration-export-name.test.mjs +644 -0
  8. package/api/component/list/list.mjs +42 -4
  9. package/api/docs/_adapter.mjs +207 -1
  10. package/api/docs/integrationDocs.test.mjs +124 -0
  11. package/api/doctor/theme-checks.mjs +1 -1
  12. package/api/gap-report/gap-report.mjs +67 -4
  13. package/api/init/run/run.mjs +2 -2
  14. package/api/integration/_adapter.d.mts +12 -0
  15. package/api/integration/_adapter.mjs +54 -0
  16. package/api/integration/add-contribution.component-names.test.mjs +66 -0
  17. package/api/integration/add-contribution.mjs +7 -3
  18. package/api/integration/authoring-checks.mjs +11 -2
  19. package/api/integration/authoring-checks.test.mjs +39 -0
  20. package/api/integration/authoring-checks.type.d.mts +9 -0
  21. package/api/integration/authoring-checks.type.mjs +14 -1
  22. package/api/integration/integrationComponentConflicts.doc.mjs +21 -5
  23. package/api/integration/pack-check.mjs +61 -107
  24. package/api/integration/pack-check.test.mjs +359 -0
  25. package/api/integration/validate-integration.test.mjs +11 -0
  26. package/api/search/search.d.mts +9 -0
  27. package/api/search/search.mjs +108 -16
  28. package/api/search/search.test.mjs +36 -0
  29. package/api/swizzle/copy/copy.mjs +28 -14
  30. package/api/template/template.test.mjs +120 -3
  31. package/api/theme/build/build.family.test.mjs +16 -0
  32. package/api/theme/build/build.mjs +81 -22
  33. package/api/theme/themeBuild.doc.mjs +3 -3
  34. package/assets/codemods/transforms/next/__tests__/migrate-list-edge-compensation-to-edge.test.mjs +120 -0
  35. package/assets/codemods/transforms/next/index.mjs +11 -1
  36. package/assets/codemods/transforms/next/migrate-list-edge-compensation-to-edge.mjs +198 -0
  37. package/assets/docs/README.md +10 -0
  38. package/assets/docs/author-a-theme.doc.mjs +1 -1
  39. package/assets/docs/getting-started.doc.mjs +3 -3
  40. package/assets/docs/theme.doc.dense.mjs +1 -1
  41. package/assets/docs/theme.doc.mjs +1 -1
  42. package/assets/docs/theme.doc.zh.mjs +1 -1
  43. package/assets/docs/tree/add-a-component.doc.mjs +10 -0
  44. package/assets/docs/tree/check-the-props.doc.mjs +145 -0
  45. package/assets/docs/tree/checks.doc.mjs +4 -4
  46. package/assets/docs/tree/component-family.doc.mjs +5 -1
  47. package/assets/docs/tree/component-imports.doc.mjs +8 -0
  48. package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
  49. package/assets/docs/tree/links.doc.mjs +2 -2
  50. package/assets/docs/tree/single-component.doc.mjs +3 -2
  51. package/assets/docs/tree/troubleshooting.doc.mjs +9 -1
  52. package/assets/docs/tree/type-scale.doc.mjs +3 -3
  53. package/assets/docs/tree/versioning.doc.mjs +1 -0
  54. package/assets/docs/tree/write-the-template-file.doc.mjs +2 -1
  55. package/assets/docs/use-a-theme.doc.mjs +2 -2
  56. package/assets/docs/working-with-ai.doc.mjs +43 -4
  57. package/assets/templates/blocks/components/Drawer/DrawerForm.doc.mjs +24 -0
  58. package/assets/templates/blocks/components/Drawer/DrawerForm.tsx +62 -0
  59. package/assets/templates/blocks/components/Drawer/DrawerScrim.doc.mjs +22 -0
  60. package/assets/templates/blocks/components/Drawer/DrawerScrim.tsx +68 -0
  61. package/assets/templates/blocks/components/Drawer/DrawerShowcase.doc.mjs +22 -0
  62. package/assets/templates/blocks/components/Drawer/DrawerShowcase.tsx +41 -0
  63. package/assets/templates/blocks/components/Drawer/DrawerStart.doc.mjs +21 -0
  64. package/assets/templates/blocks/components/Drawer/DrawerStart.tsx +41 -0
  65. package/assets/templates/blocks/components/Drawer/DrawerWidths.doc.mjs +22 -0
  66. package/assets/templates/blocks/components/Drawer/DrawerWidths.tsx +54 -0
  67. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderShowcase.doc.mjs +15 -0
  68. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderShowcase.tsx +73 -0
  69. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderSubtitleEndContent.doc.mjs +22 -0
  70. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderSubtitleEndContent.tsx +49 -0
  71. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderWithCloseButton.doc.mjs +21 -0
  72. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderWithCloseButton.tsx +42 -0
  73. package/assets/templates/pages/detail-page/page.tsx +3 -3
  74. package/assets/templates/pages/table-inbox/page.tsx +10 -8
  75. package/assets/theme.template.ts +1 -1
  76. package/authoring/doctypes/base/type.ts +6 -0
  77. package/authoring/doctypes/component/component.doc.mjs +4 -4
  78. package/authoring/doctypes/component/type.ts +6 -3
  79. package/authoring/doctypes/template/integration-template.doc.d.mts +11 -0
  80. package/authoring/doctypes/template/integration-template.doc.mjs +49 -0
  81. package/authoring/doctypes/template/parse.d.mts +6 -2
  82. package/authoring/doctypes/template/parse.mjs +3 -1
  83. package/authoring/doctypes/template/type.ts +22 -0
  84. package/authoring/index.d.ts +1 -0
  85. package/authoring/integration/integration.doc.mjs +8 -1
  86. package/authoring/integration/parse.test.mjs +10 -0
  87. package/authoring/integration/schema.d.mts +2 -0
  88. package/authoring/integration/schema.mjs +1 -0
  89. package/authoring/integration/type.ts +4 -0
  90. package/clients/cli/commands/build-theme.batch-report.test.mjs +3 -1
  91. package/clients/cli/commands/build-theme.mjs +1 -1
  92. package/clients/cli/commands/build-theme.path-safety.test.mjs +84 -0
  93. package/clients/cli/commands/build-theme.registry.test.mjs +5 -2
  94. package/clients/cli/commands/doctor-integration-components.doc.mjs +9 -3
  95. package/clients/cli/commands/doctor-integration.test.mjs +392 -0
  96. package/clients/cli/commands/doctor.mjs +74 -0
  97. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  98. package/clients/cli/lib/component-format.mjs +5 -1
  99. package/foundation/config/project.mjs +82 -0
  100. package/foundation/discovery/authoring-self-docs.mjs +1 -0
  101. package/foundation/discovery/component-discovery.d.mts +190 -10
  102. package/foundation/discovery/component-discovery.export-names.test.mjs +760 -0
  103. package/foundation/discovery/component-discovery.mjs +864 -56
  104. package/foundation/discovery/component-discovery.test.mjs +96 -0
  105. package/foundation/discovery/component-replacement.mjs +18 -0
  106. package/foundation/discovery/component-replacement.test.mjs +40 -0
  107. package/foundation/discovery/doc-types.d.mts +157 -0
  108. package/foundation/discovery/doc-types.mjs +1224 -0
  109. package/foundation/discovery/doc-types.test.mjs +652 -0
  110. package/foundation/discovery/docs-discovery.d.mts +81 -0
  111. package/foundation/discovery/docs-discovery.mjs +57 -0
  112. package/foundation/discovery/module-exports.d.mts +27 -0
  113. package/foundation/discovery/module-exports.mjs +356 -31
  114. package/foundation/discovery/module-exports.test.mjs +172 -0
  115. package/foundation/discovery/package-tsconfig.d.mts +14 -0
  116. package/foundation/discovery/package-tsconfig.mjs +228 -0
  117. package/foundation/discovery/package-tsconfig.test.mjs +210 -0
  118. package/foundation/discovery/project-typescript.d.mts +23 -0
  119. package/foundation/discovery/project-typescript.mjs +61 -0
  120. package/foundation/discovery/prop-drift.d.mts +220 -0
  121. package/foundation/discovery/prop-drift.mjs +1765 -0
  122. package/foundation/discovery/prop-drift.test.mjs +1197 -0
  123. package/foundation/discovery/template-adapter.mjs +74 -5
  124. package/foundation/discovery/type-match.d.mts +53 -0
  125. package/foundation/discovery/type-match.mjs +660 -0
  126. package/foundation/discovery/type-match.test.mjs +169 -0
  127. package/foundation/discovery/type-text.d.mts +68 -0
  128. package/foundation/discovery/type-text.mjs +658 -0
  129. package/foundation/discovery/type-text.test.mjs +196 -0
  130. package/foundation/discovery/unresolved-references.d.mts +62 -0
  131. package/foundation/discovery/unresolved-references.mjs +790 -0
  132. package/foundation/discovery/unresolved-references.test.mjs +567 -0
  133. package/foundation/doc-compiler/doc-loads.test.mjs +9 -1
  134. package/foundation/doc-compiler/inputs.mjs +23 -0
  135. package/foundation/doc-compiler/inputs.test.mjs +0 -1
  136. package/foundation/doc-compiler/links.d.mts +9 -5
  137. package/foundation/doc-compiler/links.mjs +4 -2
  138. package/foundation/integrations/contribution-inventory.mjs +6 -1
  139. package/foundation/response/response-types.doc.mjs +1 -1
  140. package/foundation/xle/registry.mjs +4 -0
  141. package/foundation/xle/registry.test.mjs +28 -0
  142. package/package.json +10 -10
@@ -132,6 +132,35 @@ export const GRAPH_BLOCK_TYPES: Set<string>;
132
132
  * load; a guide the tree places may set `placement` (spec:AST-046).
133
133
  */
134
134
  export const GRAPH_ONLY_FIELDS: string[];
135
+ /**
136
+ * A component a link can name: its owner's provider id, package, and name,
137
+ * and the doc file that describes it.
138
+ * @typedef {object} LinkableComponent
139
+ * @property {string} providerId
140
+ * @property {string} package
141
+ * @property {string} name
142
+ * @property {string | null} docPath
143
+ * @property {string} [replaces] the Core component whose name it answers to
144
+ * in unqualified lookups (spec:AST-035 FR11)
145
+ */
146
+ /**
147
+ * A page or block template a link can name, by the id `astryx template` takes.
148
+ * @typedef {object} LinkableTemplate
149
+ * @property {string} providerId
150
+ * @property {string} package
151
+ * @property {'page' | 'block'} type
152
+ * @property {string} id
153
+ * @property {string} name
154
+ * @property {string} [displayName]
155
+ * @property {string} [description]
156
+ * @property {string} [replaces] the Core template id it answers to in
157
+ * unqualified lookups
158
+ */
159
+ /**
160
+ * The component and template docs links can name (spec:AST-047 FR9). No docs
161
+ * tree holds them, so a catalog carries the records the project reads.
162
+ * @typedef {{components: LinkableComponent[], templates: LinkableTemplate[]}} LinkableContributions
163
+ */
135
164
  /**
136
165
  * Every topic a project can read, and the relationships between them.
137
166
  *
@@ -181,6 +210,18 @@ export class DocsCatalog {
181
210
  namespaces: import("../doc-compiler/tree.mjs").TreeNamespaceInput[];
182
211
  guides: import("../doc-compiler/tree.mjs").TreeDocInput[];
183
212
  }>;
213
+ /**
214
+ * Say where links find component, page, and block docs: the records
215
+ * `astryx component` and `astryx template` read, loaded on first use.
216
+ * @param {() => Promise<LinkableContributions>} load
217
+ */
218
+ setContributions(load: () => Promise<LinkableContributions>): void;
219
+ /**
220
+ * The component and template docs links can name, or null when nothing set
221
+ * them, so no link to one resolves.
222
+ * @returns {Promise<LinkableContributions | null>}
223
+ */
224
+ contributions(): Promise<LinkableContributions | null>;
184
225
  /**
185
226
  * Add one integration-contributed doc, honoring what it declares. Returns
186
227
  * the issue it caused, or null when it applied cleanly — the caller owns
@@ -290,4 +331,44 @@ export type DocsTopicEntry = {
290
331
  */
291
332
  tree?: boolean | undefined;
292
333
  };
334
+ /**
335
+ * A component a link can name: its owner's provider id, package, and name,
336
+ * and the doc file that describes it.
337
+ */
338
+ export type LinkableComponent = {
339
+ providerId: string;
340
+ package: string;
341
+ name: string;
342
+ docPath: string | null;
343
+ /**
344
+ * the Core component whose name it answers to
345
+ * in unqualified lookups (spec:AST-035 FR11)
346
+ */
347
+ replaces?: string | undefined;
348
+ };
349
+ /**
350
+ * A page or block template a link can name, by the id `astryx template` takes.
351
+ */
352
+ export type LinkableTemplate = {
353
+ providerId: string;
354
+ package: string;
355
+ type: "page" | "block";
356
+ id: string;
357
+ name: string;
358
+ displayName?: string | undefined;
359
+ description?: string | undefined;
360
+ /**
361
+ * the Core template id it answers to in
362
+ * unqualified lookups
363
+ */
364
+ replaces?: string | undefined;
365
+ };
366
+ /**
367
+ * The component and template docs links can name (spec:AST-047 FR9). No docs
368
+ * tree holds them, so a catalog carries the records the project reads.
369
+ */
370
+ export type LinkableContributions = {
371
+ components: LinkableComponent[];
372
+ templates: LinkableTemplate[];
373
+ };
293
374
  import { withSourceTitle } from './docs-section-key.mjs';
@@ -633,6 +633,38 @@ function findMergeTarget(sections, section) {
633
633
  return sections.findIndex(candidate => candidate.id === key);
634
634
  }
635
635
 
636
+ /**
637
+ * A component a link can name: its owner's provider id, package, and name,
638
+ * and the doc file that describes it.
639
+ * @typedef {object} LinkableComponent
640
+ * @property {string} providerId
641
+ * @property {string} package
642
+ * @property {string} name
643
+ * @property {string | null} docPath
644
+ * @property {string} [replaces] the Core component whose name it answers to
645
+ * in unqualified lookups (spec:AST-035 FR11)
646
+ */
647
+
648
+ /**
649
+ * A page or block template a link can name, by the id `astryx template` takes.
650
+ * @typedef {object} LinkableTemplate
651
+ * @property {string} providerId
652
+ * @property {string} package
653
+ * @property {'page' | 'block'} type
654
+ * @property {string} id
655
+ * @property {string} name
656
+ * @property {string} [displayName]
657
+ * @property {string} [description]
658
+ * @property {string} [replaces] the Core template id it answers to in
659
+ * unqualified lookups
660
+ */
661
+
662
+ /**
663
+ * The component and template docs links can name (spec:AST-047 FR9). No docs
664
+ * tree holds them, so a catalog carries the records the project reads.
665
+ * @typedef {{components: LinkableComponent[], templates: LinkableTemplate[]}} LinkableContributions
666
+ */
667
+
636
668
  /**
637
669
  * Every topic a project can read, and the relationships between them.
638
670
  *
@@ -644,6 +676,10 @@ function findMergeTarget(sections, section) {
644
676
  export class DocsCatalog {
645
677
  /** @type {Map<string, DocsTopicEntry>} */
646
678
  #topics = new Map();
679
+ /** @type {(() => Promise<LinkableContributions>) | null} */
680
+ #contributions = null;
681
+ /** @type {Promise<LinkableContributions> | null} */
682
+ #contributionsLoaded = null;
647
683
  /** @type {Map<string, string>} old topic name → the name that replaced it */
648
684
  #aliases = new Map();
649
685
  /** @type {Array<{namespaces: import('../doc-compiler/tree.mjs').TreeNamespaceInput[], guides: import('../doc-compiler/tree.mjs').TreeDocInput[]}>} */
@@ -687,6 +723,27 @@ export class DocsCatalog {
687
723
  return this.#treeInputs;
688
724
  }
689
725
 
726
+ /**
727
+ * Say where links find component, page, and block docs: the records
728
+ * `astryx component` and `astryx template` read, loaded on first use.
729
+ * @param {() => Promise<LinkableContributions>} load
730
+ */
731
+ setContributions(load) {
732
+ this.#contributions = load;
733
+ this.#contributionsLoaded = null;
734
+ }
735
+
736
+ /**
737
+ * The component and template docs links can name, or null when nothing set
738
+ * them, so no link to one resolves.
739
+ * @returns {Promise<LinkableContributions | null>}
740
+ */
741
+ contributions() {
742
+ if (!this.#contributions) return Promise.resolve(null);
743
+ this.#contributionsLoaded ??= this.#contributions();
744
+ return this.#contributionsLoaded;
745
+ }
746
+
690
747
  /**
691
748
  * Seed a catalog with the CLI's own topics.
692
749
  * @param {Record<string, string>} [builtins] topic name → absolute path
@@ -6,5 +6,32 @@
6
6
  * @param {string} file
7
7
  * @param {string} exportName
8
8
  * @param {Set<string>} [seen]
9
+ * @returns {boolean}
9
10
  */
10
11
  export function moduleExportsName(file: string, exportName: string, seen?: Set<string>): boolean;
12
+ /**
13
+ * The file that declares a module's runtime export, without executing it (the
14
+ * module, or where a local re-export leads); null when it cannot be proved.
15
+ * @param {string} file
16
+ * @param {string} exportName
17
+ * @param {Set<string>} [seen]
18
+ * @returns {string | null}
19
+ */
20
+ export function moduleExportSource(file: string, exportName: string, seen?: Set<string>): string | null;
21
+ /**
22
+ * The object a module exports, as plain data, read without running it: the
23
+ * first of `exportNames` it exports, an object literal in place or a top-level
24
+ * binding (under `satisfies` or `as` too). Only literal values are kept;
25
+ * anything computed, or overridable by a later spread, is left out.
26
+ *
27
+ * @param {string} file
28
+ * @param {readonly string[]} exportNames `default` or named exports, in order
29
+ * @returns {Record<string, unknown> | null}
30
+ */
31
+ export function staticExportObject(file: string, exportNames: readonly string[]): Record<string, unknown> | null;
32
+ /**
33
+ * Marks an object {@link staticExportObject} returns (or one inside it) whose
34
+ * source also has properties or spreads it could not read as literals. Not
35
+ * enumerable, so the object compares and serializes as its literal properties.
36
+ */
37
+ export const HAS_COMPUTED: unique symbol;
@@ -6,14 +6,16 @@
6
6
  * Proves a runtime export without executing package code. Local ESM re-exports
7
7
  * are followed recursively; unresolved or unparsable modules fail closed.
8
8
  *
9
- * @input A module path and runtime export name
10
- * @output Whether that export can be proved statically
9
+ * @input A module path and runtime export name, or a module path and the
10
+ * exports to try
11
+ * @output Whether that export can be proved statically and the file that
12
+ * declares it, or the exported object literal as plain data
11
13
  * @position packages/cli/foundation/discovery — shared package inspection
12
14
  */
13
15
 
14
16
  import * as fs from 'node:fs';
15
17
  import * as path from 'node:path';
16
- import jscodeshift from 'jscodeshift';
18
+ import {parse} from '@babel/parser';
17
19
 
18
20
  const MODULE_EXTENSIONS = [
19
21
  '.mjs',
@@ -25,7 +27,141 @@ const MODULE_EXTENSIONS = [
25
27
  '.tsx',
26
28
  '.jsx',
27
29
  ];
28
- const parser = jscodeshift.withParser('tsx');
30
+ // The options jscodeshift's tsx parser passes to Babel, so the same sources
31
+ // parse, without tokens or the recast layer: these reads only look at the tree.
32
+ /** @type {import('@babel/parser').ParserOptions} */
33
+ const PARSE_OPTIONS = /** @type {any} */ ({
34
+ sourceType: 'module',
35
+ allowImportExportEverywhere: true,
36
+ allowReturnOutsideFunction: true,
37
+ plugins: [
38
+ 'jsx',
39
+ 'asyncGenerators',
40
+ 'decoratorAutoAccessors',
41
+ 'bigInt',
42
+ 'classPrivateMethods',
43
+ 'classPrivateProperties',
44
+ 'classProperties',
45
+ 'decorators-legacy',
46
+ 'doExpressions',
47
+ 'dynamicImport',
48
+ 'exportDefaultFrom',
49
+ 'exportExtensions',
50
+ 'exportNamespaceFrom',
51
+ 'functionBind',
52
+ 'functionSent',
53
+ 'importAttributes',
54
+ 'importMeta',
55
+ 'nullishCoalescingOperator',
56
+ 'numericSeparator',
57
+ 'objectRestSpread',
58
+ 'optionalCatchBinding',
59
+ 'optionalChaining',
60
+ ['pipelineOperator', {proposal: 'minimal'}],
61
+ 'throwExpressions',
62
+ 'typescript',
63
+ ],
64
+ });
65
+
66
+ /**
67
+ * Modules parsed in this process, by path, reused while the file is unchanged:
68
+ * one command can ask about the same module more than once.
69
+ * @type {Map<string, {stamp: string, ast: any}>}
70
+ */
71
+ const parsedModules = new Map();
72
+
73
+ /**
74
+ * Module texts read in this process, by path, reused while the file is
75
+ * unchanged.
76
+ * @type {Map<string, {stamp: string, text: string}>}
77
+ */
78
+ const moduleTexts = new Map();
79
+
80
+ /**
81
+ * A file's identity for these caches: size and mtime, plus ctime and inode,
82
+ * which change when a file is rewritten even if npm stamps the same mtime on it.
83
+ * @param {string} file
84
+ */
85
+ function fileStamp(file) {
86
+ const {size, mtimeMs, ctimeMs, ino} = fs.statSync(file);
87
+ return `${size}:${mtimeMs}:${ctimeMs}:${ino}`;
88
+ }
89
+
90
+ /** @param {string} file */
91
+ function moduleText(file) {
92
+ const stamp = fileStamp(file);
93
+ const cached = moduleTexts.get(file);
94
+ if (cached?.stamp === stamp) return cached.text;
95
+ const text = fs.readFileSync(file, 'utf-8');
96
+ moduleTexts.set(file, {stamp, text});
97
+ return text;
98
+ }
99
+
100
+ /** @param {string} file */
101
+ function parseModule(file) {
102
+ const stamp = fileStamp(file);
103
+ const cached = parsedModules.get(file);
104
+ if (cached?.stamp === stamp) return cached.ast;
105
+ const ast = parse(moduleText(file), PARSE_OPTIONS);
106
+ parsedModules.set(file, {stamp, ast});
107
+ return ast;
108
+ }
109
+
110
+ // `export *`: the one way a module exports a name it never writes.
111
+ const EXPORT_ALL_RE = /\bexport\s*\*/;
112
+
113
+ /**
114
+ * Whether `text` has `word` with no identifier character on either side.
115
+ * @param {string} text
116
+ * @param {string} word
117
+ */
118
+ function writesWord(text, word) {
119
+ for (
120
+ let at = text.indexOf(word);
121
+ at !== -1;
122
+ at = text.indexOf(word, at + 1)
123
+ ) {
124
+ const before = text[at - 1] ?? '';
125
+ const after = text[at + word.length] ?? '';
126
+ if (!/[\w$]/.test(before) && !/[\w$]/.test(after)) return true;
127
+ }
128
+ return false;
129
+ }
130
+
131
+ const NOT_CHILDREN = new Set([
132
+ 'loc',
133
+ 'start',
134
+ 'end',
135
+ 'extra',
136
+ 'comments',
137
+ 'leadingComments',
138
+ 'trailingComments',
139
+ 'innerComments',
140
+ ]);
141
+
142
+ /**
143
+ * Every node of `type` in the tree, at any depth, in source order.
144
+ * @param {any} node
145
+ * @param {string} type
146
+ * @param {any[]} [found]
147
+ * @returns {any[]}
148
+ */
149
+ function nodesOfType(node, type, found = []) {
150
+ if (Array.isArray(node)) {
151
+ for (const child of node) nodesOfType(child, type, found);
152
+ return found;
153
+ }
154
+ if (node == null || typeof node !== 'object') return found;
155
+ if (node.type === type) found.push(node);
156
+ for (const key in node) {
157
+ if (NOT_CHILDREN.has(key)) continue;
158
+ const child = node[key];
159
+ if (child != null && typeof child === 'object') {
160
+ nodesOfType(child, type, found);
161
+ }
162
+ }
163
+ return found;
164
+ }
29
165
 
30
166
  /** @param {unknown} specifier @param {string} fromFile */
31
167
  function resolveLocalModule(specifier, fromFile) {
@@ -76,32 +212,56 @@ function runtimeDefault(declaration) {
76
212
  * @param {string} file
77
213
  * @param {string} exportName
78
214
  * @param {Set<string>} [seen]
215
+ * @returns {boolean}
79
216
  */
80
217
  export function moduleExportsName(file, exportName, seen = new Set()) {
218
+ return moduleExportSource(file, exportName, seen) != null;
219
+ }
220
+
221
+ /**
222
+ * The file that declares a module's runtime export, without executing it (the
223
+ * module, or where a local re-export leads); null when it cannot be proved.
224
+ * @param {string} file
225
+ * @param {string} exportName
226
+ * @param {Set<string>} [seen]
227
+ * @returns {string | null}
228
+ */
229
+ export function moduleExportSource(file, exportName, seen = new Set()) {
81
230
  const identity = `${file}\0${exportName}`;
82
- if (seen.has(identity)) return false;
231
+ if (seen.has(identity)) return null;
83
232
  seen.add(identity);
233
+ let text;
84
234
  try {
85
- if (!fs.statSync(file).isFile()) return false;
235
+ if (!fs.statSync(file).isFile()) return null;
236
+ text = moduleText(file);
86
237
  } catch {
87
- return false;
238
+ return null;
239
+ }
240
+ // A module whose text writes neither the export's name as a word nor
241
+ // `export *` cannot export it, so it is not parsed. Any `export *` text, even
242
+ // in a comment, leaves the answer to the parsed tree below.
243
+ if (
244
+ exportName !== 'default' &&
245
+ !writesWord(text, exportName) &&
246
+ !EXPORT_ALL_RE.test(text)
247
+ ) {
248
+ return null;
88
249
  }
89
250
 
90
- let root;
251
+ let ast;
91
252
  try {
92
- root = parser(fs.readFileSync(file, 'utf-8'));
253
+ ast = parseModule(file);
93
254
  } catch {
94
- return false;
255
+ return null;
95
256
  }
96
257
 
97
- let found = false;
98
- root
99
- .find(parser.ExportNamedDeclaration)
100
- .forEach((/** @type {any} */ exportPath) => {
101
- if (found || exportPath.node.exportKind === 'type') return;
102
- const node = exportPath.node;
258
+ /** @type {string | null} */
259
+ let found = null;
260
+ nodesOfType(ast.program, 'ExportNamedDeclaration').forEach(
261
+ (/** @type {any} */ node) => {
262
+ if (found || node.exportKind === 'type') return;
103
263
  if (declarationExports(node.declaration, exportName)) {
104
- found = true;
264
+ found = file;
105
265
  return;
106
266
  }
107
267
  for (const specifier of node.specifiers ?? []) {
@@ -114,33 +274,198 @@ export function moduleExportsName(file, exportName, seen = new Set()) {
114
274
  const exported = specifier.exported?.name ?? specifier.exported?.value;
115
275
  if (exported !== exportName) continue;
116
276
  if (!node.source) {
117
- found = true;
277
+ found = file;
118
278
  return;
119
279
  }
120
280
  const target = resolveLocalModule(node.source.value, file);
121
281
  const imported =
122
282
  specifier.local?.name ?? specifier.local?.value ?? exportName;
123
- if (target && moduleExportsName(target, imported, seen)) {
124
- found = true;
283
+ const declared = target
284
+ ? moduleExportSource(target, imported, seen)
285
+ : null;
286
+ if (declared) {
287
+ found = declared;
125
288
  return;
126
289
  }
127
290
  }
128
- });
129
- if (found) return true;
291
+ },
292
+ );
293
+ if (found) return found;
130
294
 
131
295
  if (exportName === 'default') {
132
- const defaults = root.find(parser.ExportDefaultDeclaration).nodes();
296
+ const defaults = nodesOfType(ast.program, 'ExportDefaultDeclaration');
133
297
  if (defaults.some((/** @type {any} */ node) => runtimeDefault(node.declaration))) {
134
- return true;
298
+ return file;
135
299
  }
136
300
  }
137
301
 
138
- root
139
- .find(parser.ExportAllDeclaration)
140
- .forEach((/** @type {any} */ exportPath) => {
141
- if (found || exportPath.node.exportKind === 'type') return;
142
- const target = resolveLocalModule(exportPath.node.source?.value, file);
143
- if (target && moduleExportsName(target, exportName, seen)) found = true;
144
- });
302
+ nodesOfType(ast.program, 'ExportAllDeclaration').forEach(
303
+ (/** @type {any} */ node) => {
304
+ if (found || node.exportKind === 'type') return;
305
+ const target = resolveLocalModule(node.source?.value, file);
306
+ if (target) found = moduleExportSource(target, exportName, seen);
307
+ },
308
+ );
145
309
  return found;
146
310
  }
311
+
312
+ /**
313
+ * Marks an object {@link staticExportObject} returns (or one inside it) whose
314
+ * source also has properties or spreads it could not read as literals. Not
315
+ * enumerable, so the object compares and serializes as its literal properties.
316
+ */
317
+ export const HAS_COMPUTED = Symbol('hasComputed');
318
+
319
+ const TS_WRAPPERS = new Set([
320
+ 'TSSatisfiesExpression',
321
+ 'TSAsExpression',
322
+ 'TSNonNullExpression',
323
+ 'ParenthesizedExpression',
324
+ ]);
325
+
326
+ /** @param {any} node */
327
+ function withoutTypeWrappers(node) {
328
+ let current = node;
329
+ while (current && TS_WRAPPERS.has(current.type)) current = current.expression;
330
+ return current;
331
+ }
332
+
333
+ /**
334
+ * The object a module exports, as plain data, read without running it: the
335
+ * first of `exportNames` it exports, an object literal in place or a top-level
336
+ * binding (under `satisfies` or `as` too). Only literal values are kept;
337
+ * anything computed, or overridable by a later spread, is left out.
338
+ *
339
+ * @param {string} file
340
+ * @param {readonly string[]} exportNames `default` or named exports, in order
341
+ * @returns {Record<string, unknown> | null}
342
+ */
343
+ export function staticExportObject(file, exportNames) {
344
+ /** @type {any[]} */
345
+ let body;
346
+ try {
347
+ body = parseModule(file).program.body;
348
+ } catch {
349
+ return null;
350
+ }
351
+
352
+ /** @type {Map<string, any>} top-level bindings to their initializers */
353
+ const bindings = new Map();
354
+ for (const statement of body) {
355
+ const declaration =
356
+ statement.type === 'ExportNamedDeclaration'
357
+ ? statement.declaration
358
+ : statement;
359
+ if (declaration?.type !== 'VariableDeclaration') continue;
360
+ for (const declarator of declaration.declarations) {
361
+ if (declarator.id?.type === 'Identifier' && declarator.init) {
362
+ bindings.set(declarator.id.name, declarator.init);
363
+ }
364
+ }
365
+ }
366
+
367
+ /** @param {string} name @returns {any} undefined when not exported */
368
+ const exported = name => {
369
+ for (const statement of body) {
370
+ if (name === 'default' && statement.type === 'ExportDefaultDeclaration') {
371
+ return statement.declaration;
372
+ }
373
+ if (
374
+ statement.type !== 'ExportNamedDeclaration' ||
375
+ statement.exportKind === 'type'
376
+ ) {
377
+ continue;
378
+ }
379
+ if (statement.declaration?.type === 'VariableDeclaration') {
380
+ const declarator = statement.declaration.declarations.find(
381
+ (/** @type {any} */ item) =>
382
+ item.id?.type === 'Identifier' && item.id.name === name,
383
+ );
384
+ if (declarator) return declarator.init ?? null;
385
+ }
386
+ if (statement.source) continue;
387
+ for (const specifier of statement.specifiers ?? []) {
388
+ const as = specifier.exported?.name ?? specifier.exported?.value;
389
+ if (specifier.type === 'ExportSpecifier' && as === name) {
390
+ return {type: 'Identifier', name: specifier.local?.name};
391
+ }
392
+ }
393
+ }
394
+ return undefined;
395
+ };
396
+
397
+ /**
398
+ * @param {any} node
399
+ * @param {Set<string>} following bindings being followed, against cycles
400
+ * @returns {unknown} undefined when the value is not a literal
401
+ */
402
+ const literal = (node, following) => {
403
+ const value = withoutTypeWrappers(node);
404
+ switch (value?.type) {
405
+ case 'StringLiteral':
406
+ case 'NumericLiteral':
407
+ case 'BooleanLiteral':
408
+ return value.value;
409
+ case 'NullLiteral':
410
+ return null;
411
+ case 'TemplateLiteral':
412
+ return value.expressions.length === 0
413
+ ? value.quasis[0]?.value.cooked
414
+ : undefined;
415
+ case 'Identifier': {
416
+ if (!bindings.has(value.name) || following.has(value.name)) {
417
+ return undefined;
418
+ }
419
+ const next = new Set(following).add(value.name);
420
+ return literal(bindings.get(value.name), next);
421
+ }
422
+ case 'ArrayExpression':
423
+ return value.elements
424
+ .map((/** @type {any} */ element) =>
425
+ element == null || element.type === 'SpreadElement'
426
+ ? undefined
427
+ : literal(element, following),
428
+ )
429
+ .filter((/** @type {unknown} */ item) => item !== undefined);
430
+ case 'ObjectExpression': {
431
+ /** @type {Record<string, unknown>} */
432
+ let object = {};
433
+ let computed = false;
434
+ for (const property of value.properties) {
435
+ if (property.type === 'SpreadElement') {
436
+ object = {};
437
+ computed = true;
438
+ continue;
439
+ }
440
+ const key = property.computed
441
+ ? undefined
442
+ : (property.key?.name ?? property.key?.value);
443
+ const item =
444
+ typeof key === 'string' && property.type === 'ObjectProperty'
445
+ ? literal(property.value, following)
446
+ : undefined;
447
+ if (item === undefined) computed = true;
448
+ if (typeof key !== 'string') continue;
449
+ if (item === undefined) delete object[key];
450
+ else object[key] = item;
451
+ }
452
+ if (computed) {
453
+ Object.defineProperty(object, HAS_COMPUTED, {value: true});
454
+ }
455
+ return object;
456
+ }
457
+ default:
458
+ return undefined;
459
+ }
460
+ };
461
+
462
+ for (const exportName of exportNames) {
463
+ const node = exported(exportName);
464
+ if (node === undefined) continue;
465
+ const value = literal(node, new Set());
466
+ return value != null && typeof value === 'object' && !Array.isArray(value)
467
+ ? /** @type {Record<string, unknown>} */ (value)
468
+ : null;
469
+ }
470
+ return null;
471
+ }