repl-instrument.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseReplCode,
  4. collectTopLevelBindingNames,
  5. instrumentForContextPersistence,
  6. rewriteDynamicImports,
  7. rewriteDynamicImportsForRepl,
  8. } from "../src/repl/instrument.js";
  9. /** 解析 + 收集顶层绑定名的便捷断言:parse 成功后取名字数组。 */
  10. function names(code: string): string[] {
  11. const parsed = parseReplCode(code);
  12. if ("parseError" in parsed) {
  13. throw parsed.parseError;
  14. }
  15. return collectTopLevelBindingNames(parsed.ast);
  16. }
  17. /** 解析 + instrument 的便捷断言。 */
  18. function instrument(code: string): string {
  19. const parsed = parseReplCode(code);
  20. if ("parseError" in parsed) {
  21. throw parsed.parseError;
  22. }
  23. return instrumentForContextPersistence(code, parsed.ast);
  24. }
  25. describe("collectTopLevelBindingNames", () => {
  26. it("const single", () => {
  27. expect(names("const a = 1;")).toEqual(["a"]);
  28. });
  29. it("let multiple declarators", () => {
  30. expect(names("let a, b;")).toEqual(["a", "b"]);
  31. });
  32. it("var single", () => {
  33. expect(names("var c = 1;")).toEqual(["c"]);
  34. });
  35. it("object pattern", () => {
  36. expect(names("const { x, y } = o;")).toEqual(["x", "y"]);
  37. });
  38. it("object pattern with rename and rest", () => {
  39. expect(names("const { x: xx, ...rest } = o;")).toEqual(["xx", "rest"]);
  40. });
  41. it("nested array pattern", () => {
  42. expect(names("const [p, [q]] = arr;")).toEqual(["p", "q"]);
  43. });
  44. it("array pattern with rest", () => {
  45. expect(names("const [a, ...r] = arr;")).toEqual(["a", "r"]);
  46. });
  47. it("array pattern with elision (holes)", () => {
  48. expect(names("const [, b] = arr;")).toEqual(["b"]);
  49. });
  50. it("assignment pattern (default value)", () => {
  51. expect(names("const { x = 1 } = o;")).toEqual(["x"]);
  52. });
  53. it("function declaration", () => {
  54. expect(names("function f(){}")).toEqual(["f"]);
  55. });
  56. it("class declaration", () => {
  57. expect(names("class C {}")).toEqual(["C"]);
  58. });
  59. it("does NOT collect block-scoped declarations", () => {
  60. expect(names("{ const z = 1; }")).toEqual([]);
  61. });
  62. it("does NOT collect declarations inside function bodies", () => {
  63. expect(names("function f(){ const inner = 1; }")).toEqual(["f"]);
  64. });
  65. it("dedupes repeated names", () => {
  66. expect(names("var a = 1; var a = 2;")).toEqual(["a"]);
  67. });
  68. it("mixes declarations and other statements", () => {
  69. expect(names("const a = 1; console.log(a); function f(){}")).toEqual(["a", "f"]);
  70. });
  71. });
  72. describe("instrumentForContextPersistence", () => {
  73. it("appends globalThis assignment after a const", () => {
  74. const out = instrument("const a = 1;");
  75. expect(out).toContain("const a = 1;");
  76. expect(out).toContain("globalThis.a=a;");
  77. });
  78. it("appends one assignment per declared name", () => {
  79. const out = instrument("let a, b;");
  80. expect(out).toContain("globalThis.a=a;");
  81. expect(out).toContain("globalThis.b=b;");
  82. });
  83. it("handles destructuring names", () => {
  84. const out = instrument("const { x: xx, ...rest } = o;");
  85. expect(out).toContain("globalThis.xx=xx;");
  86. expect(out).toContain("globalThis.rest=rest;");
  87. });
  88. it("instruments function and class declarations", () => {
  89. const out = instrument("function f(){} class C {}");
  90. expect(out).toContain("globalThis.f=f;");
  91. expect(out).toContain("globalThis.C=C;");
  92. });
  93. it("preserves ordering with interleaved plain statements", () => {
  94. const out = instrument("const a = 1; foo(); const b = 2;");
  95. const idxA = out.indexOf("globalThis.a=a;");
  96. const idxFoo = out.indexOf("foo()");
  97. const idxB = out.indexOf("globalThis.b=b;");
  98. expect(idxA).toBeGreaterThan(-1);
  99. expect(idxFoo).toBeGreaterThan(idxA);
  100. expect(idxB).toBeGreaterThan(idxFoo);
  101. });
  102. it("returns the last top-level expression statement as completion value", () => {
  103. // Bugfix 回归:末尾表达式语句要转成 return,让外层 async-IIFE 回传其值(REPL 回显)。
  104. const out = instrument("foo(); bar();");
  105. expect(out).toBe("foo(); return (bar());");
  106. });
  107. it("wraps a trailing await expression as returned completion value", () => {
  108. const out = instrument("await tab.snapshot();");
  109. expect(out).toBe("return (await tab.snapshot());");
  110. });
  111. it("returns a parenthesized object without inserting return inside its parens", () => {
  112. expect(instrument("({ value: 1 });")).toBe("return (({ value: 1 }));");
  113. });
  114. it("does NOT return when the last statement is a declaration", () => {
  115. const out = instrument("const tab = await open();");
  116. expect(out).not.toContain("return (");
  117. expect(out).toContain("globalThis.tab=tab;");
  118. });
  119. it("does NOT inject for block-scoped declarations", () => {
  120. const out = instrument("{ const z = 1; }");
  121. expect(out).not.toContain("globalThis.z");
  122. });
  123. });
  124. describe("parseReplCode", () => {
  125. it("returns ast for valid code", () => {
  126. const parsed = parseReplCode("const x = 1;");
  127. expect("ast" in parsed).toBe(true);
  128. });
  129. it("returns parseError for invalid code", () => {
  130. const parsed = parseReplCode("const x = (");
  131. expect("parseError" in parsed).toBe(true);
  132. if ("parseError" in parsed) {
  133. expect(parsed.parseError).toBeInstanceOf(Error);
  134. }
  135. });
  136. });
  137. describe("rewriteDynamicImports", () => {
  138. it("rewrites import expressions without touching strings or import.meta", () => {
  139. const code = 'const word = "import(ignored)"; const mod = await import("node:path");';
  140. const parsed = parseReplCode(code);
  141. if ("parseError" in parsed) throw parsed.parseError;
  142. const rewritten = rewriteDynamicImports(code, parsed.ast);
  143. expect(rewritten).toContain('"import(ignored)"');
  144. expect(rewritten).toContain('await importModule("node:path")');
  145. });
  146. it("rewrites import when the REPL cell also contains a top-level return", () => {
  147. expect(
  148. rewriteDynamicImportsForRepl('const mod = await import("node:path"); return mod.sep;'),
  149. ).toContain('await importModule("node:path")');
  150. });
  151. });