1
0

object-literal-methods.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Object-literal method extraction (general AST rule).
  3. *
  4. * The extractor pulls function-valued properties out of an object literal that
  5. * is the value of an exported const — either DIRECTLY
  6. * (`export const actions = { foo: () => {} }`) or RETURNED by an initializer
  7. * call (`export const useStore = create((set, get) => ({ foo: () => {} }))`,
  8. * incl. middleware wrappers). This makes store actions (Zustand/Redux/Pinia/
  9. * MobX/handler maps) real nodes, so `codegraph_node`/`callers` on them resolve
  10. * instead of returning "not found" and forcing the agent to Read the store.
  11. *
  12. * Keyed purely on AST shape — no library names in the implementation — so any
  13. * same-shaped store is covered. Resolution then falls out of the existing
  14. * exact-name matcher: every call form (`const {foo}=useStore.getState(); foo()`,
  15. * `useStore.getState().foo()`, in-store `get().foo()`) reduces to a bare `foo`
  16. * call that resolves to the action node once it exists.
  17. */
  18. import { describe, it, expect, beforeAll, afterEach } from 'vitest';
  19. import * as fs from 'fs';
  20. import * as path from 'path';
  21. import * as os from 'os';
  22. import { CodeGraph } from '../src';
  23. import { extractFromSource } from '../src/extraction';
  24. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  25. beforeAll(async () => {
  26. await initGrammars();
  27. await loadAllGrammars();
  28. });
  29. describe('object-literal method extraction', () => {
  30. it('extracts Zustand store actions (object returned by create()) as function nodes', () => {
  31. const code = `
  32. import { create } from 'zustand'
  33. interface Store {
  34. count: number
  35. fetchUser(): Promise<void>
  36. switchOrganization(id: string): Promise<void>
  37. reset(): void
  38. }
  39. export const useStore = create<Store>((set, get) => ({
  40. count: 0,
  41. fetchUser: async () => { await get().reset() },
  42. switchOrganization: async (id: string) => { set({ count: 1 }) },
  43. reset: () => set({ count: 0 }),
  44. }))
  45. `;
  46. const result = extractFromSource('store.ts', code);
  47. const fnNames = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
  48. expect(fnNames).toContain('fetchUser');
  49. expect(fnNames).toContain('switchOrganization');
  50. expect(fnNames).toContain('reset');
  51. // Each action's body was walked: fetchUser references its sibling `reset`,
  52. // so an in-store calls edge will resolve once the pipeline runs.
  53. const fetchUser = result.nodes.find((n) => n.name === 'fetchUser')!;
  54. const fetchUserRefs = result.unresolvedReferences.filter((r) => r.fromNodeId === fetchUser.id);
  55. expect(fetchUserRefs.map((r) => r.referenceName)).toContain('reset');
  56. // The action's body wasn't mis-attributed to the file scope (the reason we
  57. // skip the generic body-visit for the store-factory call).
  58. const fileNode = result.nodes.find((n) => n.kind === 'file')!;
  59. const fileRefs = result.unresolvedReferences.filter((r) => r.fromNodeId === fileNode.id);
  60. expect(fileRefs.map((r) => r.referenceName)).not.toContain('reset');
  61. });
  62. it('extracts actions through a middleware wrapper (create(persist(...)))', () => {
  63. const code = `
  64. import { create } from 'zustand'
  65. import { persist } from 'zustand/middleware'
  66. export const useCounter = create(
  67. persist(
  68. (set, get) => ({
  69. value: 0,
  70. increment: () => set({ value: get().value + 1 }),
  71. }),
  72. { name: 'counter' }
  73. )
  74. )
  75. `;
  76. const result = extractFromSource('counter.ts', code);
  77. const fnNames = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
  78. expect(fnNames).toContain('increment');
  79. });
  80. it('extracts actions when the initializer returns via a block (=> { return {...} })', () => {
  81. const code = `
  82. import { create } from 'zustand'
  83. export const useThing = create((set) => {
  84. const initial = 0
  85. return {
  86. value: initial,
  87. bump: () => set({ value: 1 }),
  88. }
  89. })
  90. `;
  91. const result = extractFromSource('thing.ts', code);
  92. const fnNames = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
  93. expect(fnNames).toContain('bump');
  94. });
  95. it('does NOT extract methods from a non-exported call-wrapped object (noise gate)', () => {
  96. const code = `
  97. function wrap(f: any) { return f }
  98. const local = wrap(() => ({ shouldNotExtract: () => {} }))
  99. `;
  100. const result = extractFromSource('inline.ts', code);
  101. const names = result.nodes.map((n) => n.name);
  102. expect(names).not.toContain('shouldNotExtract');
  103. });
  104. it('still extracts the existing direct-object shape (export const actions = {...})', () => {
  105. const code = `
  106. export const actions = {
  107. load: async () => { helper() },
  108. }
  109. function helper() {}
  110. `;
  111. const result = extractFromSource('actions.ts', code);
  112. const fnNames = result.nodes.filter((n) => n.kind === 'function').map((n) => n.name);
  113. expect(fnNames).toContain('load');
  114. });
  115. });
  116. describe('object-literal method resolution (end-to-end)', () => {
  117. let tmpDir: string | undefined;
  118. afterEach(() => {
  119. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  120. tmpDir = undefined;
  121. });
  122. it('resolves callers of store actions across files (destructured + chained getState())', async () => {
  123. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-store-'));
  124. fs.writeFileSync(path.join(tmpDir, 'package.json'), '{"name":"t","dependencies":{"zustand":"^4"}}\n');
  125. fs.writeFileSync(
  126. path.join(tmpDir, 'store.ts'),
  127. `import { create } from 'zustand'\n` +
  128. `interface S { fetchUser(): Promise<void>; reset(): void }\n` +
  129. `export const useStore = create<S>((set, get) => ({\n` +
  130. ` fetchUser: async () => { get().reset() },\n` +
  131. ` reset: () => set({}),\n` +
  132. `}))\n`
  133. );
  134. fs.writeFileSync(
  135. path.join(tmpDir, 'caller.ts'),
  136. `import { useStore } from './store'\n` +
  137. `export async function loginFlow() {\n` +
  138. ` const { fetchUser } = useStore.getState()\n` +
  139. ` await fetchUser()\n` +
  140. `}\n` +
  141. `export function hardReset() {\n` +
  142. ` useStore.getState().reset()\n` +
  143. `}\n`
  144. );
  145. const cg = CodeGraph.initSync(tmpDir);
  146. await cg.indexAll();
  147. const fns = cg.getNodesByKind('function');
  148. const fetchUser = fns.find((n) => n.name === 'fetchUser' && n.filePath.endsWith('store.ts'));
  149. const reset = fns.find((n) => n.name === 'reset' && n.filePath.endsWith('store.ts'));
  150. expect(fetchUser).toBeDefined();
  151. expect(reset).toBeDefined();
  152. // Destructured-then-bare call: loginFlow -> fetchUser
  153. const fetchUserCallers = cg.getCallers(fetchUser!.id).map((c) => c.node.name);
  154. expect(fetchUserCallers).toContain('loginFlow');
  155. // Chained getState() call: hardReset -> reset, AND in-store sibling: fetchUser -> reset
  156. const resetCallers = cg.getCallers(reset!.id).map((c) => c.node.name);
  157. expect(resetCallers).toContain('hardReset');
  158. expect(resetCallers).toContain('fetchUser');
  159. cg.close();
  160. });
  161. });