|
1 | 1 | import { describe, it, jest, expect } from "@jest/globals"; |
2 | | -import { precookFunction } from "./precookFunction.js"; |
| 2 | +import { clearFunctionASTCache, precookFunction } from "./precookFunction.js"; |
| 3 | +import { cook } from "./cook.js"; |
3 | 4 |
|
4 | 5 | const consoleWarn = jest |
5 | 6 | .spyOn(console, "warn") |
6 | 7 | .mockImplementation(() => void 0); |
7 | 8 |
|
8 | 9 | describe("precookFunction", () => { |
| 10 | + beforeEach(() => { |
| 11 | + clearFunctionASTCache(); |
| 12 | + }); |
| 13 | + |
9 | 14 | it.each<[string, string, string[]]>([ |
10 | 15 | [ |
11 | 16 | "lexical variables in block statement", |
@@ -206,7 +211,7 @@ describe("precookFunction", () => { |
206 | 211 | `, |
207 | 212 | ["c", "b", "d"], |
208 | 213 | ], |
209 | | - ])("%s", (desc, source, result) => { |
| 214 | + ])("%s", (_desc, source, result) => { |
210 | 215 | expect(Array.from(precookFunction(source).attemptToVisitGlobals)).toEqual( |
211 | 216 | result |
212 | 217 | ); |
@@ -263,6 +268,26 @@ describe("precookFunction", () => { |
263 | 268 | } |
264 | 269 | ); |
265 | 270 |
|
| 271 | + it("should isolate regexp", () => { |
| 272 | + const source = `function test() { |
| 273 | + const r = /\\w/g; |
| 274 | + r.exec("abc"); |
| 275 | + return r.lastIndex; |
| 276 | + }`; |
| 277 | + const fn = { |
| 278 | + name: "test", |
| 279 | + source, |
| 280 | + }; |
| 281 | + const attempt1 = precookFunction(source, { cacheKey: fn }); |
| 282 | + const fn1 = cook(attempt1.function, source) as Function; |
| 283 | + expect(fn1()).toBe(1); |
| 284 | + |
| 285 | + const attempt2 = precookFunction(source, { cacheKey: fn }); |
| 286 | + const fn2 = cook(attempt2.function, source) as Function; |
| 287 | + // The second RegExp is using the cached one |
| 288 | + expect(fn2()).toBe(1); |
| 289 | + }); |
| 290 | + |
266 | 291 | it("should warn unsupported type", () => { |
267 | 292 | const { attemptToVisitGlobals } = precookFunction( |
268 | 293 | "function test() { this }" |
|
0 commit comments