ts-chained-receiver.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 PingService { ping(): string { return "service"; } }\n' +
  44. 'export class Runner {\n' +
  45. ' constructor(private svc: PingService) {}\n' +
  46. ' run(): string { return this.svc.ping(); }\n' +
  47. '}\n' +
  48. 'export class AnonymousRunner {\n' +
  49. ' constructor(private svc: { ping(): string }) {}\n' +
  50. ' run(): string { return this.svc.ping(); }\n' +
  51. '}\n'
  52. );
  53. cg = await CodeGraph.init(dir, { index: true });
  54. cg.resolveReferences();
  55. });
  56. afterAll(() => {
  57. cg.destroy();
  58. try {
  59. fs.rmSync(dir, { recursive: true, force: true });
  60. } catch {
  61. // Windows can still hold the SQLite handle for a moment; the OS temp dir is swept anyway.
  62. }
  63. });
  64. const fn = (name: string, file: string) =>
  65. cg.getNodesByKind('function').find((n) => n.name === name && n.filePath === file)!;
  66. const method = (qn: string) => cg.getNodesByKind('method').find((n) => n.qualifiedName === qn)!;
  67. const callTargets = (id: string) =>
  68. cg
  69. .getOutgoingEdges(id)
  70. .filter((e) => e.kind === 'calls')
  71. .map((e) => e.target);
  72. describe('TS/JS call through a host-global chain (#1707)', () => {
  73. it('does not make a storage wrapper call itself through chrome.storage.local.get', () => {
  74. const get = fn('get', 'storage.ts');
  75. expect(get).toBeDefined();
  76. expect(callTargets(get.id)).not.toContain(get.id);
  77. });
  78. it('does not bind document.body.querySelector to a same-named project function', () => {
  79. expect(callTargets(fn('findRow', 'dom.ts').id)).not.toContain(
  80. fn('querySelector', 'dom.ts').id
  81. );
  82. });
  83. it('keeps a chain rooted at a project value — window.MyNs.m() and this.<field>.m()', () => {
  84. const ping = fn('ping', 'service.ts').id;
  85. expect(callTargets(fn('viaGlobal', 'service.ts').id)).toContain(ping);
  86. expect(callTargets(method('Runner::run').id)).toEqual([method('PingService::ping').id]);
  87. });
  88. it('does not guess a same-named project target for an anonymous field type (#1496)', () => {
  89. // Neither the top-level ping nor PingService::ping establishes what svc is.
  90. expect(callTargets(method('AnonymousRunner::run').id)).toEqual([]);
  91. });
  92. });