An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): the export reader follows an alias of an imported binding (#10471)
Cindy Zhangadb756608f5c
diff --git a/.changeset/cli-export-alias-origin.md b/.changeset/cli-export-alias-origin.mdnew file mode 100644index 0000000000..45b67a34d5--- /dev/null+++ b/.changeset/cli-export-alias-origin.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[experimental] `integration check` and `experimental icon new-set` follow `export const X = Y` to where `Y` is declared: an icon-set theme map or icon export written that way is now checked and recorded.++@cixzhangdiff --git a/packages/cli/foundation/discovery/module-exports.mjs b/packages/cli/foundation/discovery/module-exports.mjsindex 1cc294a403..aeb31591e0 100644--- a/packages/cli/foundation/discovery/module-exports.mjs+++ b/packages/cli/foundation/discovery/module-exports.mjs@@ -707,7 +707,12 @@ function exportOriginsOf(file, read) {for (const node of declarations) {for (const name of declaredNames(node.declaration, types)) {named.add(name);- if (!origins.has(name)) origins.set(name, {file, name});+ if (!origins.has(name)) {+ origins.set(+ name,+ declaredOrigin(ast, file, node.declaration, name, types),+ );+ }}}for (const [exportName, entries] of specifiers) {@@ -1211,7 +1216,15 @@ function exportOrigin(file, exportName, seen, read, types = false) {const {declarations, specifiers} = namedExportIndex(ast.program, types, read);for (const node of declarations) {if (declarationExports(node.declaration, exportName, types)) {- return {file, name: exportName};+ return declaredOrigin(+ ast,+ file,+ node.declaration,+ exportName,+ types,+ seen,+ read,+ );}}for (const {node, specifier} of specifiers.get(exportName) ?? []) {@@ -1690,6 +1703,48 @@ export function elementPropOrigins(file, {sources, name, prop, unwrap}) {return origins;}+/**+ * Where a declared export is defined. An `export const X = Y` whose value is+ * another binding is that binding, so an alias of an imported value has the+ * imported value's origin.+ * @param {any} ast+ * @param {string} file+ * @param {any} declaration+ * @param {string} name+ * @param {boolean} types+ * @param {Set<string>} [seen]+ * @param {(node: any, type: string) => readonly any[]} [read]+ * @returns {{file: string, name: string}}+ */+function declaredOrigin(+ ast,+ file,+ declaration,+ name,+ types,+ seen = new Set(),+ read = nodesOfType,+) {+ if (+ declaration?.type === 'VariableDeclaration' &&+ declaration.kind === 'const'+ ) {+ for (const declarator of declaration.declarations) {+ if (+ declarator.id?.type === 'Identifier' &&+ declarator.id.name === name &&+ declarator.init?.type === 'Identifier'+ ) {+ const init = declarator.init.name;+ return (+ bindingOrigin(ast, file, init, seen, types, read) ?? {file, name}+ );+ }+ }+ }+ return {file, name};+}+/*** Where a top-level binding of a parsed module comes from: an import from a* local module is followed to its origin; anything else is declared here.diff --git a/packages/cli/foundation/discovery/module-exports.test.mjs b/packages/cli/foundation/discovery/module-exports.test.mjsindex fbe94514d8..61e6697e42 100644--- a/packages/cli/foundation/discovery/module-exports.test.mjs+++ b/packages/cli/foundation/discovery/module-exports.test.mjs@@ -401,6 +401,19 @@ describe('moduleExportOrigins', () => {}});+ it('follows an alias of an imported binding to where that binding is declared', () => {+ const write = (name, text) => fs.writeFileSync(path.join(dir, name), text);+ write('inner.ts', 'export const Inner = {};\n');+ write(+ 'alias.ts',+ "import {Inner as Provider} from './inner';\nexport const Public = Provider;\n",+ );+ const alias = path.join(dir, 'alias.ts');+ const inner = {file: path.join(dir, 'inner.ts'), name: 'Inner'};+ expect(moduleExportOrigin(alias, 'Public')).toEqual(inner);+ expect(moduleExportOrigins(alias).get('Public')).toEqual(inner);+ });+it('agrees through an export * cycle', () => {const write = (name, text) => fs.writeFileSync(path.join(dir, name), text);write('c.ts', 'export const w = 1;\nexport const z = 2;\n');