Skip to content

fix(writer): splice hand-written TypeScript imports during overwrite#80

Merged
gjtorikian merged 2 commits into
mainfrom
fix-writer-ts-import-splicing
May 19, 2026
Merged

fix(writer): splice hand-written TypeScript imports during overwrite#80
gjtorikian merged 2 commits into
mainfrom
fix-writer-ts-import-splicing

Conversation

@gjtorikian
Copy link
Copy Markdown
Collaborator

Summary

  • spliceExtraImports only understood Python imports (import X and from X import Y). When a TypeScript file with hand-written @oagen-ignore-start/end regions was overwritten via overwriteWithPreservedRegions, the hand-written named/type-only imports those regions depended on were dropped from the import block — landing in the output only because top-level ignore blocks get appended to the end of the file. Compiles, but reads as garbage and trips eslint import/first.
  • Adds collectTsImports (named, type-only, default, namespace, side-effect — single-line and multi-line { … }) and wires it into spliceExtraImports alongside the Python collectors. Disjoint syntaxes; safe to run in parallel.
  • Widens the lastImportIdx loop to recognize TS import lines so spliced imports land in the import block rather than wherever the loop happened to stop.
  • Tightens the Python isSimpleImport regex to exclude lines containing from, so TS default imports (import X from 'module') flow through the new TS handler instead of being added verbatim.

Test plan

  • npm test — 1419/1419 pass; new preserves hand-written TypeScript imports during overwrite case in writer-ignore.test.ts
  • npm run typecheck — clean
  • End-to-end against workos-node's Webhooks class: hand-written named imports ({ deserializeEvent } from '../common/serializers', { Event, EventResponse } from '../common/interfaces', { SignatureProvider } from '../common/crypto/signature-provider') referenced from inside an @oagen-ignore-start/end block now land at the top of the file alongside the generated imports.

🤖 Generated with Claude Code

`spliceExtraImports` understood Python `import X` and `from X import Y`
but not TypeScript `import { X } from 'module'`, `import type { X }
from 'module'`, `import X from 'module'`, `import * as X from 'module'`,
or `import 'module'`. When a TypeScript file with hand-written
`@oagen-ignore-start/end` regions was overwritten via
`overwriteWithPreservedRegions`, the hand-written imports those regions
depended on were dropped — landing in the output only because top-level
ignore blocks get appended to the end of the file by the placement
logic (which compiles but reads as garbage and trips eslint
`import/first`).

Add `collectTsImports` covering all five TS import forms (including
multi-line `{ … }` named imports) and wire it into `spliceExtraImports`
alongside the existing Python collectors. Also widen the
`lastImportIdx` loop to recognize TS import lines so the spliced
imports land in the import block rather than wherever the loop happened
to stop.

The existing Python-style detector is unchanged in semantics — its
`isSimpleImport` regex is tightened to exclude lines containing `from`
so TS default imports (`import X from 'module'`) flow through the
new TS handler instead of being added verbatim.

Verified end-to-end against a hand-written `Webhooks` class with
hand-written named imports referenced from inside an
`@oagen-ignore-start/end` block: imports now land at the top of the
file alongside the generated imports.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
#81)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@gjtorikian gjtorikian merged commit 0072235 into main May 19, 2026
5 checks passed
@gjtorikian gjtorikian deleted the fix-writer-ts-import-splicing branch May 19, 2026 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant