fix(writer): splice hand-written TypeScript imports during overwrite#80
Merged
Conversation
`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>
2 tasks
#81) Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
spliceExtraImportsonly understood Python imports (import Xandfrom X import Y). When a TypeScript file with hand-written@oagen-ignore-start/endregions was overwritten viaoverwriteWithPreservedRegions, 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 eslintimport/first.collectTsImports(named, type-only, default, namespace, side-effect — single-line and multi-line{ … }) and wires it intospliceExtraImportsalongside the Python collectors. Disjoint syntaxes; safe to run in parallel.lastImportIdxloop to recognize TS import lines so spliced imports land in the import block rather than wherever the loop happened to stop.isSimpleImportregex to exclude lines containingfrom, 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; newpreserves hand-written TypeScript imports during overwritecase inwriter-ignore.test.tsnpm run typecheck— cleanworkos-node'sWebhooksclass: 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/endblock now land at the top of the file alongside the generated imports.🤖 Generated with Claude Code