Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): integration verify reads exports with the reader discovery uses (#7660)

Joey58279bc7d37b
diff --git a/.changeset/cli-verify-folder-reexport.md b/.changeset/cli-verify-folder-reexport.md
new file mode 100644
index 0000000000..1d84d9cf2b
--- /dev/null
+++ b/.changeset/cli-verify-folder-reexport.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `integration verify` reads a package's exports with the same reader discovery uses, so the two agree: a component re-exported through a folder's index file passes, and a type-only re-export no longer counts as the component.
+
+@josephfarina
diff --git a/packages/cli/api/integration/pack-check.mjs b/packages/cli/api/integration/pack-check.mjs
index b43456d0aa..986b2f6fa2 100644
--- a/packages/cli/api/integration/pack-check.mjs
+++ b/packages/cli/api/integration/pack-check.mjs
@@ -24,7 +24,6 @@ import * as path from 'node:path';
import * as os from 'node:os';
import {fileURLToPath} from 'node:url';
import {spawnSync} from 'node:child_process';
-import jscodeshift from 'jscodeshift';
import {assertWithin} from '../../foundation/fs/path-safety.mjs';
import {resolvePackageDir} from '../../foundation/integrations/integrations.mjs';
import {
@@ -41,6 +40,7 @@ import {
} from '../../foundation/discovery/docs-discovery.mjs';
import {discoverIntegrationThemes} from '../../foundation/discovery/theme-discovery.mjs';
import {inspectPackageExport} from '../../foundation/discovery/package-exports.mjs';
+import {moduleExportsName} from '../../foundation/discovery/module-exports.mjs';
import {
ThemeImportError,
resolveThemeImports,
@@ -307,98 +307,6 @@ function extractTarball(tgzPath, extractDir) {
}
}
-const MODULE_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.tsx', '.jsx'];
-const moduleParser = jscodeshift.withParser('tsx');
-
-/** @param {unknown} specifier @param {string} fromFile */
-function resolveLocalModule(specifier, fromFile) {
- if (typeof specifier !== 'string' || !specifier.startsWith('.')) return null;
- const base = path.resolve(path.dirname(fromFile), specifier);
- const candidates = [
- base,
- ...MODULE_EXTENSIONS.map(extension => `${base}${extension}`),
- ...MODULE_EXTENSIONS.map(extension => path.join(base, `index${extension}`)),
- ];
- return candidates.find(candidate => fs.existsSync(candidate)) ?? null;
-}
-
-/** @param {any} declaration @param {string} exportName */
-function declarationExports(declaration, exportName) {
- if (!declaration) return false;
- if (['FunctionDeclaration', 'ClassDeclaration'].includes(declaration.type)) {
- return declaration.id?.name === exportName;
- }
- if (declaration.type !== 'VariableDeclaration') return false;
- return declaration.declarations.some(
- (/** @type {any} */ item) =>
- item.id?.type === 'Identifier' && item.id.name === exportName,
- );
-}
-
-/**
- * Check one resolved module's static export surface without executing package
- * code. Local ESM re-exports are followed recursively.
- *
- * @param {string} file
- * @param {string} exportName
- * @param {Set<string>} [seen]
- */
-function moduleExportsName(file, exportName, seen = new Set()) {
- const identity = `${file}\0${exportName}`;
- if (seen.has(identity) || !fs.existsSync(file)) return false;
- seen.add(identity);
-
- let root;
- try {
- root = moduleParser(fs.readFileSync(file, 'utf-8'));
- } catch {
- return false;
- }
-
- let found = false;
- root
- .find(moduleParser.ExportNamedDeclaration)
- .forEach((/** @type {any} */ exportPath) => {
- if (found) return;
- const node = exportPath.node;
- if (declarationExports(node.declaration, exportName)) {
- found = true;
- return;
- }
- for (const specifier of node.specifiers ?? []) {
- const exported = specifier.exported?.name ?? specifier.exported?.value;
- if (exported !== exportName) continue;
- if (!node.source) {
- found = true;
- return;
- }
- const target = resolveLocalModule(node.source.value, file);
- const imported =
- specifier.local?.name ?? specifier.local?.value ?? exportName;
- if (target && moduleExportsName(target, imported, seen)) {
- found = true;
- return;
- }
- }
- });
- if (found) return true;
- if (
- exportName === 'default' &&
- root.find(moduleParser.ExportDefaultDeclaration).size() > 0
- ) {
- return true;
- }
-
- root
- .find(moduleParser.ExportAllDeclaration)
- .forEach((/** @type {any} */ exportPath) => {
- if (found) return;
- const target = resolveLocalModule(exportPath.node.source?.value, file);
- if (target && moduleExportsName(target, exportName, seen)) found = true;
- });
- return found;
-}
-
/**
* Whether any of these doc files has a section that sets `id`.
* @param {string[]} files
diff --git a/packages/cli/api/integration/pack-check.test.mjs b/packages/cli/api/integration/pack-check.test.mjs
index 61b5e3f010..f16546150a 100644
--- a/packages/cli/api/integration/pack-check.test.mjs
+++ b/packages/cli/api/integration/pack-check.test.mjs
@@ -811,6 +811,86 @@ describe('integrationPackCheck', () => {
);
});
+ it('does not count a type-only re-export as the component', async () => {
+ fs.writeFileSync(
+ path.join(tmpDir, 'package.json'),
+ `${JSON.stringify({
+ name: '@acme/widgets',
+ version: '1.0.0',
+ files: ['astryx.integration.mjs', 'src'],
+ exports: {'.': './src/index.ts'},
+ })}\n`,
+ );
+ fs.writeFileSync(
+ path.join(tmpDir, 'astryx.integration.mjs'),
+ "export default {components: './src'};\n",
+ );
+ fs.mkdirSync(path.join(tmpDir, 'src'));
+ fs.writeFileSync(
+ path.join(tmpDir, 'src', 'index.ts'),
+ "export type {AcmeWidget} from './AcmeWidget';\n",
+ );
+ fs.writeFileSync(
+ path.join(tmpDir, 'src', 'AcmeWidget.tsx'),
+ 'export function AcmeWidget() { return null; }\n',
+ );
+ fs.writeFileSync(
+ path.join(tmpDir, 'src', 'AcmeWidget.doc.mjs'),
+ "export default {type: 'component', name: 'AcmeWidget', import: '@acme/widgets', description: 'Widget.', props: []};\n",
+ );
+
+ const result = await integrationPackCheck({cwd: tmpDir});
+
+ expect(result.data.issues).toContainEqual(
+ expect.objectContaining({code: 'component_export_missing'}),
+ );
+ });
+
+ it('follows a re-export of a folder to its index file', async () => {
+ // `./primitives` names a folder: TypeScript and bundlers load its index file.
+ fs.writeFileSync(
+ path.join(tmpDir, 'package.json'),
+ `${JSON.stringify({
+ name: '@acme/widgets',
+ version: '1.0.0',
+ files: ['astryx.integration.mjs', 'src'],
+ exports: {'.': './src/index.ts'},
+ })}\n`,
+ );
+ fs.writeFileSync(
+ path.join(tmpDir, 'astryx.integration.mjs'),
+ "export default {components: './src'};\n",
+ );
+ const primitives = path.join(tmpDir, 'src', 'primitives');
+ fs.mkdirSync(primitives, {recursive: true});
+ fs.writeFileSync(
+ path.join(tmpDir, 'src', 'index.ts'),
+ "export * from './primitives';\n",
+ );
+ fs.writeFileSync(
+ path.join(primitives, 'index.ts'),
+ "export * from './AcmeWidget';\n",
+ );
+ fs.writeFileSync(
+ path.join(primitives, 'AcmeWidget.tsx'),
+ 'export function AcmeWidget() { return null; }\n',
+ );
+ fs.writeFileSync(
+ path.join(primitives, 'AcmeWidget.doc.mjs'),
+ "export default {type: 'component', name: 'AcmeWidget', import: '@acme/widgets', description: 'Widget.', props: []};\n",
+ );
+
+ const result = await integrationPackCheck({cwd: tmpDir});
+
+ expect(result.data.issues).not.toContainEqual(
+ expect.objectContaining({code: 'component_export_missing'}),
+ );
+ expect(
+ result.data.packable,
+ JSON.stringify(result.data.issues, null, 2),
+ ).toBe(true);
+ });
+
it('fails when packed template source is hidden by package exports', async () => {
writePackage({
manifest: "export default {templates: './templates'};\n",