erlang-arity-resolution.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * Erlang arity-aware resolution (#1610).
  3. *
  4. * Arity is part of a function's identity: `f/1` and `f/2` are unrelated
  5. * definitions. Extraction gives each arity its own node (`mod::f/1`) and
  6. * stamps refs with the call-site arity; resolution must land each ref on the
  7. * def of exactly that arity — the everyday `header/2 -> header/3` delegation
  8. * must be a real edge, never a self-loop — and refuse to guess a sibling
  9. * arity when the named one doesn't exist.
  10. */
  11. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  12. import * as fs from 'node:fs';
  13. import * as path from 'node:path';
  14. import * as os from 'node:os';
  15. import { CodeGraph } from '../src';
  16. describe('erlang arity-aware resolution', () => {
  17. let dir: string;
  18. beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'erlang-arity-')); });
  19. afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); });
  20. async function callEdges(d: string): Promise<Array<{ sq: string; tq: string }>> {
  21. const cg = await CodeGraph.init(d, { silent: true });
  22. await cg.indexAll();
  23. const db = (cg as any).db.db;
  24. const rows = db
  25. .prepare(
  26. `SELECT s.qualified_name sq, t.qualified_name tq
  27. FROM edges e JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
  28. WHERE e.kind IN ('calls','references') AND s.kind = 'function'`
  29. )
  30. .all();
  31. cg.destroy();
  32. return rows;
  33. }
  34. it('resolves the f/N -> f/N+1 delegation to a real edge, not a self-loop', async () => {
  35. fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
  36. fs.writeFileSync(
  37. path.join(dir, 'src', 'deleg.erl'),
  38. `-module(deleg).
  39. -export([header/2]).
  40. header(Name, Req) ->
  41. header(Name, Req, undefined).
  42. -spec header(binary(), map(), any()) -> any().
  43. header(Name, Headers, Default) ->
  44. maps:get(Name, Headers, Default).
  45. `
  46. );
  47. const edges = await callEdges(dir);
  48. expect(edges).toContainEqual({ sq: 'deleg::header/2', tq: 'deleg::header/3' });
  49. // No self-loop in either direction.
  50. expect(edges.some((e) => e.sq === e.tq && e.sq.startsWith('deleg::header'))).toBe(false);
  51. });
  52. it('resolves remote calls to the called arity and refuses a sibling arity', async () => {
  53. fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
  54. fs.writeFileSync(
  55. path.join(dir, 'src', 'store.erl'),
  56. `-module(store).
  57. -export([get/1, get/2]).
  58. get(K) -> get(K, undefined).
  59. get(K, Default) -> {K, Default}.
  60. `
  61. );
  62. fs.writeFileSync(
  63. path.join(dir, 'src', 'client.erl'),
  64. `-module(client).
  65. -export([fetch/1, broken/1]).
  66. fetch(K) ->
  67. store:get(K, nil).
  68. broken(K) ->
  69. store:get(K, nil, extra).
  70. `
  71. );
  72. const edges = await callEdges(dir);
  73. expect(edges).toContainEqual({ sq: 'client::fetch/1', tq: 'store::get/2' });
  74. // store:get/3 doesn't exist — the ref must resolve to NOTHING, not /1 or /2.
  75. expect(edges.some((e) => e.sq === 'client::broken/1' && e.tq.startsWith('store::get'))).toBe(false);
  76. });
  77. it('resolves an arity-less dynamic MFA ref only when exactly one arity exists', async () => {
  78. fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
  79. fs.writeFileSync(
  80. path.join(dir, 'src', 'single.erl'),
  81. `-module(single).
  82. -export([work/1]).
  83. work(X) -> X.
  84. `
  85. );
  86. fs.writeFileSync(
  87. path.join(dir, 'src', 'multi.erl'),
  88. `-module(multi).
  89. -export([job/1, job/2]).
  90. job(X) -> X.
  91. job(X, Y) -> {X, Y}.
  92. `
  93. );
  94. fs.writeFileSync(
  95. path.join(dir, 'src', 'spawner.erl'),
  96. `-module(spawner).
  97. -export([go/1]).
  98. go(Args) ->
  99. erlang:spawn(single, work, Args),
  100. erlang:spawn(multi, job, Args).
  101. `
  102. );
  103. const edges = await callEdges(dir);
  104. // `Args` is dynamic, so both refs are arity-less. single:work has exactly
  105. // one arity — it resolves; multi:job has two — silent beats wrong.
  106. expect(edges).toContainEqual({ sq: 'spawner::go/1', tq: 'single::work/1' });
  107. expect(edges.some((e) => e.sq === 'spawner::go/1' && e.tq.startsWith('multi::job'))).toBe(false);
  108. });
  109. it('lands `fun mod:f/1` references on the written arity', async () => {
  110. fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
  111. fs.writeFileSync(
  112. path.join(dir, 'src', 'lib_m.erl'),
  113. `-module(lib_m).
  114. -export([bump/1, bump/2]).
  115. bump(X) -> X + 1.
  116. bump(X, N) -> X + N.
  117. `
  118. );
  119. fs.writeFileSync(
  120. path.join(dir, 'src', 'user_m.erl'),
  121. `-module(user_m).
  122. -export([run/1]).
  123. run(L) ->
  124. lists:map(fun lib_m:bump/1, L).
  125. `
  126. );
  127. const edges = await callEdges(dir);
  128. expect(edges).toContainEqual({ sq: 'user_m::run/1', tq: 'lib_m::bump/1' });
  129. expect(edges.some((e) => e.sq === 'user_m::run/1' && e.tq === 'lib_m::bump/2')).toBe(false);
  130. });
  131. });