alias-binding-resolution.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Calls through an alias binding.
  3. *
  4. * A name bound to nothing but another symbol — `export const alias = fn`,
  5. * `export { fn as alias }`, `export const api = { run: fn }`, or a same-file
  6. * `const local = fn` — used to resolve to the BINDING, one hop short of the
  7. * function. The edge existed, so nothing looked broken, but `callers fn` omitted
  8. * every caller that went through the alias and reported a confident zero while
  9. * `callers alias` found them.
  10. *
  11. * Specifiers here are extensionless so these cases stand independently of
  12. * `.js`-specifier resolution.
  13. */
  14. import { describe, it, expect, afterEach } from 'vitest';
  15. import * as fs from 'fs';
  16. import * as path from 'path';
  17. import * as os from 'os';
  18. import CodeGraph from '../src/index';
  19. describe('calls through an alias binding reach the aliased symbol', () => {
  20. let cg: CodeGraph;
  21. let dir: string;
  22. afterEach(() => {
  23. if (cg) cg.destroy();
  24. if (dir && fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  25. });
  26. const index = async (files: Record<string, string>): Promise<void> => {
  27. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-alias-'));
  28. for (const [name, content] of Object.entries(files)) {
  29. fs.writeFileSync(path.join(dir, name), content);
  30. }
  31. cg = CodeGraph.initSync(dir, { config: { include: ['**/*.ts'], exclude: [] } });
  32. await cg.indexAll();
  33. };
  34. const callersOf = (name: string): string[] => {
  35. const target = cg.getNodesByKind('function').find((n) => n.name === name);
  36. expect(target, `fixture symbol ${name} was not indexed`).toBeDefined();
  37. return cg.getCallers(target!.id).map((c) => c.node.name);
  38. };
  39. it('follows `export const alias = fn`', async () => {
  40. await index({
  41. 'impl.ts': 'export function realImpl(): number { return 1; }\nexport const aliasName = realImpl;\n',
  42. 'consumer.ts': "import { aliasName } from './impl';\nexport function consumerFn(): number { return aliasName(); }\n",
  43. });
  44. expect(callersOf('realImpl')).toContain('consumerFn');
  45. });
  46. it('follows a local `export { fn as alias }` clause', async () => {
  47. // The declaration carries no `export` keyword, so extraction does not flag
  48. // it exported — the export index must still bind the renamed export to it.
  49. await index({
  50. 'impl.ts': 'function realImpl(): number { return 1; }\nexport { realImpl as aliasName };\n',
  51. 'consumer.ts': "import { aliasName } from './impl';\nexport function consumerFn(): number { return aliasName(); }\n",
  52. });
  53. expect(callersOf('realImpl')).toContain('consumerFn');
  54. });
  55. it('follows a function reference held in an object-literal property', async () => {
  56. await index({
  57. 'impl.ts': 'export function realImpl(): number { return 1; }\nexport const api = { run: realImpl };\n',
  58. 'consumer.ts': "import { api } from './impl';\nexport function consumerFn(): number { return api.run(); }\n",
  59. });
  60. expect(callersOf('realImpl')).toContain('consumerFn');
  61. });
  62. it('follows a same-file alias binding', async () => {
  63. await index({
  64. 'impl.ts':
  65. 'function realImpl(): number { return 1; }\n' +
  66. 'const localAlias = realImpl;\n' +
  67. 'export function consumerFn(): number { return localAlias(); }\n',
  68. });
  69. expect(callersOf('realImpl')).toContain('consumerFn');
  70. });
  71. it('leaves a genuine wrapper pointing at the wrapper, not the wrapped function', async () => {
  72. // `wrapper` is a real function, not an alias: the call site calls IT.
  73. await index({
  74. 'impl.ts':
  75. 'export function realImpl(): number { return 1; }\n' +
  76. 'export const wrapper = (): number => realImpl();\n',
  77. 'consumer.ts': "import { wrapper } from './impl';\nexport function consumerFn(): number { return wrapper(); }\n",
  78. });
  79. expect(callersOf('realImpl')).not.toContain('consumerFn');
  80. });
  81. it('does not hop when the aliased name is ambiguous across files', async () => {
  82. // Two same-named callables and no same-file declaration to prefer: a hop
  83. // would have to guess, and a wrong edge is worse than a missing one.
  84. await index({
  85. 'one.ts': 'export function shared(): number { return 1; }\n',
  86. 'two.ts': 'export function shared(): number { return 2; }\n',
  87. 'alias.ts': "import { shared } from './one';\nexport const aliasName = shared;\n",
  88. 'consumer.ts': "import { aliasName } from './alias';\nexport function consumerFn(): number { return aliasName(); }\n",
  89. });
  90. const sharedNodes = cg.getNodesByKind('function').filter((n) => n.name === 'shared');
  91. expect(sharedNodes).toHaveLength(2);
  92. for (const node of sharedNodes) {
  93. expect(cg.getCallers(node.id).map((c) => c.node.name)).not.toContain('consumerFn');
  94. }
  95. });
  96. });