ts-chained-receiver.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * A TS/JS member call reached through a host namespace — `chrome.storage.local
  3. * .get(k)`, `document.body.querySelector(s)` — ends in a platform API. Emitting
  4. * the bare method name for it let every such call exact-match whatever project
  5. * symbol shared the name, so a storage wrapper's `get` called itself (#1707).
  6. * Those are dropped, as are untyped identifier chains (#1566). The existing
  7. * `window.MyNs.run()` and `this.<field>.m()` paths remain outside that guard.
  8. */
  9. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  10. import * as fs from 'fs';
  11. import * as os from 'os';
  12. import * as path from 'path';
  13. import { CodeGraph } from '../src';
  14. let dir: string;
  15. let cg: CodeGraph;
  16. beforeAll(async () => {
  17. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-1707-'));
  18. const w = (rel: string, body: string) => fs.writeFileSync(path.join(dir, rel), body);
  19. w(
  20. 'storage.ts',
  21. 'declare const chrome: any;\n' +
  22. 'export const DraftHubStorage = {\n' +
  23. ' async get(key: string): Promise<unknown> {\n' +
  24. ' const result = await chrome.storage.local.get([key]);\n' +
  25. ' return result[key];\n' +
  26. ' },\n' +
  27. '};\n'
  28. );
  29. w(
  30. 'dom.ts',
  31. 'export function querySelector(sel: string): string { return sel; }\n' +
  32. 'export function findRow(): unknown {\n' +
  33. ' return document.body.querySelector("tr");\n' +
  34. '}\n'
  35. );
  36. w(
  37. 'service.ts',
  38. 'declare const window: any;\n' +
  39. 'export function ping(): string { return "pong"; }\n' +
  40. 'export function viaGlobal(): string {\n' +
  41. ' return window.MyNs.ping();\n' +
  42. '}\n' +
  43. 'export class Runner {\n' +
  44. ' constructor(private svc: { ping(): string }) {}\n' +
  45. ' run(): string { return this.svc.ping(); }\n' +
  46. '}\n'
  47. );
  48. cg = await CodeGraph.init(dir, { index: true });
  49. cg.resolveReferences();
  50. });
  51. afterAll(() => {
  52. cg.destroy();
  53. try {
  54. fs.rmSync(dir, { recursive: true, force: true });
  55. } catch {
  56. // Windows can still hold the SQLite handle for a moment; the OS temp dir is swept anyway.
  57. }
  58. });
  59. const fn = (name: string, file: string) =>
  60. cg.getNodesByKind('function').find((n) => n.name === name && n.filePath === file)!;
  61. const method = (qn: string) => cg.getNodesByKind('method').find((n) => n.qualifiedName === qn)!;
  62. const callTargets = (id: string) =>
  63. cg
  64. .getOutgoingEdges(id)
  65. .filter((e) => e.kind === 'calls')
  66. .map((e) => e.target);
  67. describe('TS/JS call through a host-global chain (#1707)', () => {
  68. it('does not make a storage wrapper call itself through chrome.storage.local.get', () => {
  69. const get = fn('get', 'storage.ts');
  70. expect(get).toBeDefined();
  71. expect(callTargets(get.id)).not.toContain(get.id);
  72. });
  73. it('does not bind document.body.querySelector to a same-named project function', () => {
  74. expect(callTargets(fn('findRow', 'dom.ts').id)).not.toContain(
  75. fn('querySelector', 'dom.ts').id
  76. );
  77. });
  78. it('keeps a chain rooted at a project value — window.MyNs.m() and this.<field>.m()', () => {
  79. const ping = fn('ping', 'service.ts').id;
  80. expect(callTargets(fn('viaGlobal', 'service.ts').id)).toContain(ping);
  81. expect(callTargets(method('Runner::run').id)).toContain(ping);
  82. });
  83. });