Skip to content

Commit e3fc1ff

Browse files
Copilotmrlubos
authored andcommitted
fix: symbolOnce now correctly handles multiple symbols from same external source
1 parent 454202c commit e3fc1ff

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

packages/codegen-core/src/__tests__/symbols.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,22 @@ describe('SymbolRegistry', () => {
106106
const result = r.query(meta({ a: { b: { c: 123 } } }));
107107
expect(result).toEqual([a]);
108108
});
109+
110+
it('query() returns all symbols sharing the same meta but with different names', () => {
111+
const r = new SymbolRegistry();
112+
113+
const sharedMeta = meta({ category: 'external', resource: '@shared/types' });
114+
115+
const a = r.register({ meta: sharedMeta, name: 'CustomNumber' });
116+
const b = r.register({ meta: sharedMeta, name: 'FlakeIdString' });
117+
118+
const results = r.query(sharedMeta);
119+
expect(results).toHaveLength(2);
120+
expect(results).toContain(a);
121+
expect(results).toContain(b);
122+
123+
// filtering by name correctly identifies each symbol independently
124+
expect(results.find((s) => s.name === 'CustomNumber')).toBe(a);
125+
expect(results.find((s) => s.name === 'FlakeIdString')).toBe(b);
126+
});
109127
});

packages/shared/src/plugins/shared/utils/instance.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
399399

400400
/**
401401
* Registers a symbol only if it does not already exist based on the provided
402-
* metadata. This prevents duplicate symbols from being created in the project.
402+
* name and metadata. This prevents duplicate symbols from being created in
403+
* the project.
403404
*/
404405
symbolOnce(name: SymbolIn['name'], symbol?: Omit<SymbolIn, 'name'>): Symbol {
405406
const meta = {
@@ -409,7 +410,7 @@ export class PluginInstance<T extends Plugin.Types = Plugin.Types> {
409410
meta.category = 'external';
410411
meta.resource = symbol.external;
411412
}
412-
const existing = this.querySymbol(meta);
413+
const existing = this.gen.symbols.query(meta).find((s) => s.name === name);
413414
if (existing) return existing;
414415
return this.symbol(name, { ...symbol, meta });
415416
}

0 commit comments

Comments
 (0)