@astryxdesign/cli 0.7.2-canary.c9f2659 → 0.7.2-canary.cbb7fa3

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 (72) hide show
  1. package/api/component/_adapter.d.mts +41 -1
  2. package/api/component/_adapter.mjs +93 -7
  3. package/api/component/component.mjs +22 -5
  4. package/api/component/component.test.mjs +420 -167
  5. package/api/component/integration-export-name.test.mjs +644 -0
  6. package/api/component/list/list.mjs +42 -4
  7. package/api/gap-report/gap-report.mjs +67 -4
  8. package/api/integration/add-contribution.component-names.test.mjs +66 -0
  9. package/api/integration/add-contribution.mjs +7 -3
  10. package/api/integration/pack-check.mjs +61 -107
  11. package/api/integration/pack-check.test.mjs +359 -0
  12. package/api/search/search.d.mts +9 -0
  13. package/api/search/search.mjs +108 -16
  14. package/api/search/search.test.mjs +36 -0
  15. package/api/swizzle/copy/copy.mjs +28 -14
  16. package/api/template/template.test.mjs +120 -3
  17. package/api/theme/build/build.family.test.mjs +16 -0
  18. package/api/theme/build/build.mjs +77 -18
  19. package/api/theme/themeBuild.doc.mjs +3 -3
  20. package/assets/codemods/transforms/next/__tests__/migrate-list-edge-compensation-to-edge.test.mjs +120 -0
  21. package/assets/codemods/transforms/next/index.mjs +11 -1
  22. package/assets/codemods/transforms/next/migrate-list-edge-compensation-to-edge.mjs +198 -0
  23. package/assets/docs/tree/add-a-component.doc.mjs +10 -0
  24. package/assets/docs/tree/checks.doc.mjs +1 -1
  25. package/assets/docs/tree/component-family.doc.mjs +5 -1
  26. package/assets/docs/tree/component-imports.doc.mjs +8 -0
  27. package/assets/docs/tree/single-component.doc.mjs +1 -1
  28. package/assets/docs/tree/troubleshooting.doc.mjs +9 -1
  29. package/assets/docs/tree/versioning.doc.mjs +1 -0
  30. package/assets/docs/working-with-ai.doc.mjs +43 -4
  31. package/assets/templates/blocks/components/Drawer/DrawerForm.doc.mjs +24 -0
  32. package/assets/templates/blocks/components/Drawer/DrawerForm.tsx +62 -0
  33. package/assets/templates/blocks/components/Drawer/DrawerScrim.doc.mjs +22 -0
  34. package/assets/templates/blocks/components/Drawer/DrawerScrim.tsx +68 -0
  35. package/assets/templates/blocks/components/Drawer/DrawerShowcase.doc.mjs +22 -0
  36. package/assets/templates/blocks/components/Drawer/DrawerShowcase.tsx +41 -0
  37. package/assets/templates/blocks/components/Drawer/DrawerStart.doc.mjs +21 -0
  38. package/assets/templates/blocks/components/Drawer/DrawerStart.tsx +41 -0
  39. package/assets/templates/blocks/components/Drawer/DrawerWidths.doc.mjs +22 -0
  40. package/assets/templates/blocks/components/Drawer/DrawerWidths.tsx +54 -0
  41. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderShowcase.doc.mjs +15 -0
  42. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderShowcase.tsx +73 -0
  43. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderSubtitleEndContent.doc.mjs +22 -0
  44. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderSubtitleEndContent.tsx +49 -0
  45. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderWithCloseButton.doc.mjs +21 -0
  46. package/assets/templates/blocks/components/DrawerHeader/DrawerHeaderWithCloseButton.tsx +42 -0
  47. package/assets/templates/pages/detail-page/page.tsx +3 -3
  48. package/assets/templates/pages/table-inbox/page.tsx +10 -8
  49. package/authoring/doctypes/base/type.ts +6 -0
  50. package/authoring/doctypes/component/component.doc.mjs +4 -4
  51. package/authoring/doctypes/component/type.ts +6 -3
  52. package/clients/cli/commands/build-theme.path-safety.test.mjs +84 -0
  53. package/clients/cli/commands/build-theme.registry.test.mjs +5 -2
  54. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  55. package/clients/cli/lib/component-format.mjs +5 -1
  56. package/foundation/discovery/component-discovery.d.mts +172 -10
  57. package/foundation/discovery/component-discovery.export-names.test.mjs +760 -0
  58. package/foundation/discovery/component-discovery.mjs +863 -55
  59. package/foundation/discovery/component-discovery.test.mjs +96 -0
  60. package/foundation/discovery/component-replacement.mjs +18 -0
  61. package/foundation/discovery/component-replacement.test.mjs +40 -0
  62. package/foundation/discovery/module-exports.d.mts +27 -0
  63. package/foundation/discovery/module-exports.mjs +356 -31
  64. package/foundation/discovery/module-exports.test.mjs +172 -0
  65. package/foundation/discovery/template-adapter.mjs +74 -5
  66. package/foundation/doc-compiler/doc-loads.test.mjs +5 -1
  67. package/foundation/doc-compiler/inputs.mjs +23 -0
  68. package/foundation/doc-compiler/inputs.test.mjs +0 -1
  69. package/foundation/integrations/contribution-inventory.mjs +6 -1
  70. package/foundation/xle/registry.mjs +4 -0
  71. package/foundation/xle/registry.test.mjs +28 -0
  72. package/package.json +10 -10
@@ -16,6 +16,7 @@ import {fileURLToPath} from 'node:url';
16
16
  import {
17
17
  CORE_PACKAGE,
18
18
  discoverComponents,
19
+ familyMember,
19
20
  findComponentReadme,
20
21
  findComponentSource,
21
22
  resolveImportPath,
@@ -121,6 +122,91 @@ describe('discoverComponents (real core)', () => {
121
122
  expect(discoverComponents(core)).toEqual({});
122
123
  });
123
124
 
125
+ it('lists HStack, VStack and Heading, whose docs live in their family folders', () => {
126
+ // Each has its own folder and import path; Stack's doc documents HStack and
127
+ // VStack as members, and Heading's own doc sits in the Text folder.
128
+ const comps = discoverComponents(CORE);
129
+ expect(comps.Layout).toEqual(expect.arrayContaining(['HStack', 'Stack', 'VStack']));
130
+ expect(comps.Heading).toEqual(['Heading']);
131
+ expect(findComponentReadme(CORE, 'VStack')).toMatch(/[\\/]Stack[\\/]Stack\.doc\.mjs$/);
132
+ expect(findComponentReadme(CORE, 'HStack')).toMatch(/[\\/]Stack[\\/]Stack\.doc\.mjs$/);
133
+ expect(findComponentReadme(CORE, 'Heading')).toMatch(/[\\/]Text[\\/]Heading\.doc\.mjs$/);
134
+ expect(resolveImportPath(CORE, 'VStack')).toBe('@astryxdesign/core/VStack');
135
+ expect(resolveImportPath(CORE, 'Heading')).toBe('@astryxdesign/core/Heading');
136
+ }, SLOW);
137
+
138
+ it('lists a component folder with no doc when its family doc lists it as a member', () => {
139
+ const core = mkTmp('as-cd-family-');
140
+ const write = (rel, text) => {
141
+ fs.mkdirSync(path.dirname(path.join(core, 'src', rel)), {recursive: true});
142
+ fs.writeFileSync(path.join(core, 'src', rel), text);
143
+ };
144
+ write('Stack/Stack.tsx', 'x');
145
+ write('VStack/VStack.tsx', "import {Stack} from '../Stack/Stack';\n");
146
+ write('Prose/Prose.tsx', "import {Stack} from '../Stack/Stack';\n");
147
+ // Listed by Stack's doc, but builds on nothing in Stack's folder.
148
+ write('Orphan/Orphan.tsx', "import {x} from '../utils';\n");
149
+ write(
150
+ 'Stack/Stack.doc.mjs',
151
+ 'export const docs = {\n' +
152
+ " name: 'Stack',\n" +
153
+ " group: 'Layout',\n" +
154
+ " relatedComponents: ['Prose'],\n" +
155
+ " usage: {description: 'Pair with Prose.', examples: [{code: \"<Prose label='Prose' />\"}]},\n" +
156
+ " components: [{name: 'Stack'}, {name: 'VStack', description: 'Vertical.'}, {name: 'Orphan'}],\n" +
157
+ '};\n',
158
+ );
159
+ expect(discoverComponents(core)).toEqual({Layout: ['Stack', 'VStack']});
160
+ expect(findComponentReadme(core, 'VStack')).toBe(
161
+ path.join(core, 'src', 'Stack', 'Stack.doc.mjs'),
162
+ );
163
+ // Named only in relatedComponents, prose, and an example: not documented.
164
+ expect(findComponentReadme(core, 'Prose')).toBeNull();
165
+ // A family doc is read only from the folders the component imports.
166
+ expect(findComponentReadme(core, 'Orphan')).toBeNull();
167
+ });
168
+
169
+ it('lists a component folder with no doc when its own doc sits in a family folder', () => {
170
+ const core = mkTmp('as-cd-sibling-');
171
+ for (const dir of ['Text', 'Heading']) {
172
+ fs.mkdirSync(path.join(core, 'src', dir), {recursive: true});
173
+ }
174
+ fs.writeFileSync(path.join(core, 'src', 'Text', 'Text.tsx'), 'x');
175
+ fs.writeFileSync(path.join(core, 'src', 'Heading', 'Heading.tsx'), 'x');
176
+ fs.writeFileSync(
177
+ path.join(core, 'src', 'Text', 'Text.doc.mjs'),
178
+ "export const docs = {name: 'Text', components: [{name: 'Heading'}]};\n",
179
+ );
180
+ fs.writeFileSync(
181
+ path.join(core, 'src', 'Text', 'Heading.doc.mjs'),
182
+ "export const docs = {name: 'Heading'};\n",
183
+ );
184
+ expect(discoverComponents(core)).toEqual({Heading: ['Heading'], Text: ['Text']});
185
+ expect(findComponentReadme(core, 'Heading')).toBe(
186
+ path.join(core, 'src', 'Text', 'Heading.doc.mjs'),
187
+ );
188
+ });
189
+
190
+ it('keeps a member its family hides, and a context folder, unlisted', () => {
191
+ const core = mkTmp('as-cd-family-hidden-');
192
+ for (const dir of ['Stack', 'VStack', 'SizeContext']) {
193
+ fs.mkdirSync(path.join(core, 'src', dir), {recursive: true});
194
+ fs.writeFileSync(
195
+ path.join(core, 'src', dir, `${dir}.tsx`),
196
+ "import {Stack} from '../Stack/Stack';\n",
197
+ );
198
+ }
199
+ fs.writeFileSync(
200
+ path.join(core, 'src', 'Stack', 'Stack.doc.mjs'),
201
+ 'export const docs = {\n' +
202
+ " name: 'Stack',\n" +
203
+ " hiddenComponents: ['VStack'],\n" +
204
+ " components: [{name: 'Stack'}, {name: 'VStack'}, {name: 'SizeContext'}],\n" +
205
+ '};\n',
206
+ );
207
+ expect(discoverComponents(core)).toEqual({Stack: ['Stack']});
208
+ });
209
+
124
210
  it('returns {} for an empty src/ dir', () => {
125
211
  const core = mkTmp('as-cd-emptysrc-');
126
212
  fs.mkdirSync(path.join(core, 'src'));
@@ -133,6 +219,16 @@ describe('discoverComponents (real core)', () => {
133
219
  });
134
220
  });
135
221
 
222
+ describe('familyMember', () => {
223
+ it("returns a family doc's entry for a member, and null for a component's own doc", () => {
224
+ const stack = {name: 'Stack', components: [{name: 'Stack'}, {name: 'VStack', description: 'v'}]};
225
+ expect(familyMember(stack, 'VStack')).toEqual({name: 'VStack', description: 'v'});
226
+ expect(familyMember(stack, 'Stack')).toBeNull();
227
+ expect(familyMember({name: 'Button'}, 'Button')).toBeNull();
228
+ expect(familyMember(stack, 'HStack')).toBeNull();
229
+ });
230
+ });
231
+
136
232
  describe('findComponentReadme / findComponentSource / resolveImportPath', () => {
137
233
  it('finds the doc + source for a top-level component', () => {
138
234
  expect(findComponentReadme(CORE, 'Button')).toMatch(/Button\.doc\.mjs$/);
@@ -161,6 +161,24 @@ export async function resolveComponentReplacements(
161
161
  typeof name === 'string' ? byTarget.get(name) : undefined,
162
162
  };
163
163
 
164
+ // A first read under file names alone settles the common case, no
165
+ // declaration anywhere, without reading the names docs document.
166
+ let declares = false;
167
+ for (const integration of loadedIntegrations) {
168
+ if (declares || !integration?.components || integration.__loadError)
169
+ continue;
170
+ try {
171
+ const {components} = await discoverValidIntegrationComponents(
172
+ integration,
173
+ {documentedNames: false},
174
+ );
175
+ declares = components.some(record => record.replaces !== undefined);
176
+ } catch {
177
+ // Read again below, where a root that cannot be read is skipped.
178
+ }
179
+ }
180
+ if (!declares) return result;
181
+
164
182
  /** @type {Declaration[]} */
165
183
  const declarations = [];
166
184
  /** @type {Array<{name: string, package: string}>} */
@@ -259,6 +259,46 @@ describe('resolveComponentReplacements', () => {
259
259
  ]);
260
260
  });
261
261
 
262
+ it('applies a replacement under the export a doc names when its file is named otherwise', async () => {
263
+ // Nav.doc.mjs documents AcmeSideNav, the export of a Nav.tsx that exports
264
+ // nothing named Nav.
265
+ const acme = integrationWithDocSource(
266
+ '@acme/nav',
267
+ 'Nav',
268
+ BASE_DOC.replace("name: 'AcmeNav'", "name: 'AcmeSideNav'"),
269
+ );
270
+ fs.writeFileSync(
271
+ path.join(acme.components, 'Nav.tsx'),
272
+ 'export function AcmeSideNav() { return null; }\n',
273
+ );
274
+ const result = await resolveComponentReplacements(CORE_DIR, [acme]);
275
+ expect(result.findings).toEqual([]);
276
+ expect(result.forTarget('SideNav')).toMatchObject({
277
+ name: 'AcmeSideNav',
278
+ package: '@acme/nav',
279
+ });
280
+ });
281
+
282
+ it("counts a doc's replaces once when the doc lists its own name as a member", async () => {
283
+ // Chip.doc.mjs documents AcmeChip beside a Chip.tsx that exports both, and
284
+ // lists itself first, as the component-family guide asks.
285
+ const acme = integrationWithDocSource(
286
+ '@acme/chips',
287
+ 'Chip',
288
+ `export default {type: 'component', name: 'AcmeChip', replaces: 'Badge', usage: {description: 'Acme chip.'}, components: [{name: 'AcmeChip', description: 'A chip.', props: []}]};\n`,
289
+ );
290
+ fs.writeFileSync(
291
+ path.join(acme.components, 'Chip.tsx'),
292
+ 'export function Chip() { return null; }\nexport function AcmeChip() { return null; }\n',
293
+ );
294
+ const result = await resolveComponentReplacements(CORE_DIR, [acme]);
295
+ expect(result.findings).toEqual([]);
296
+ expect(result.forTarget('Badge')).toMatchObject({
297
+ name: 'Chip',
298
+ package: '@acme/chips',
299
+ });
300
+ });
301
+
262
302
  it('lets a replacement keep the Core name it replaces', async () => {
263
303
  const acme = integration(
264
304
  '@acme/nav',
@@ -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
+ }