Commit 4ca943e
Fix require() inside functions being treated as imports
When extracting imports from JavaScript/TypeScript code, dynamic
require() calls inside function bodies were incorrectly being
extracted as module-level imports. This caused invalid code to be
sent to the AI service for optimization.
For example, the code:
```
function foo() {
const mod = require('module') as typeof import('module');
}
```
Would have `require('module') as typeof import('module')` extracted
as an import statement, which when added to the top of the code
would create invalid syntax.
The fix tracks when we enter function/method bodies during tree
traversal and only treats require() calls as imports when they
are at module level.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>1 parent 5020757 commit 4ca943e
2 files changed
Lines changed: 54 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
454 | 454 | | |
455 | 455 | | |
456 | 456 | | |
457 | | - | |
458 | | - | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
459 | 478 | | |
460 | 479 | | |
461 | 480 | | |
462 | 481 | | |
463 | 482 | | |
464 | | - | |
465 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
466 | 486 | | |
467 | 487 | | |
468 | 488 | | |
469 | 489 | | |
470 | 490 | | |
471 | 491 | | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
472 | 495 | | |
473 | | - | |
| 496 | + | |
474 | 497 | | |
475 | 498 | | |
476 | 499 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
354 | 380 | | |
355 | 381 | | |
356 | 382 | | |
| |||
0 commit comments