Skip to content

fix(octane): let a type-only import share a name with a value declaration - #2265

Merged
trueadm merged 1 commit into
mainfrom
fix/type-only-import-bindings
Oct 28, 2026
Merged

trueadm merged 1 commit into
mainfrom
fix/type-only-import-bindings

Conversation

@trueadm

@trueadm trueadm commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • A .tsrx module with a type-only import and a value declaration of the same name no longer fails with Identifier 'Toc' has already been declared (TS2300). TypeScript accepts this, and the identical .tsx already compiled:
    import type { Toc } from './types';
    export function Toc({ headings }: { headings: Toc }) @{ <nav>{headings.length as string}</nav> }
    react.dev's src/components/Layout/Toc.tsx and TanStack Query's examples/react/react-router/src/routes/contact.tsx use this shape. They were the only two compile failures when the codemod prototype (feat(codemod): add @octanejs/codemod, a React to Octane/TSRX migration CLI #2264) migrated 575 third-party modules.

Why

Both parsers that read .tsrx bind the names a type-only import introduces as lexical values:

  • @tsrx/oxc 0.23.0: tsrx-core-compat/facade.js scopeDiagnostics ignores importKind. The Node compiler parses .tsrx with it first. .tsx/.ts take the ordinary lane, which skips this check, which is why they compiled.
  • @sveltejs/acorn-typescript 1.0.13: acorn's own specifier parsers, and import { type X }, declare BIND_LEXICAL. @tsrx/core's parseModule uses it, and so do the slot-hook, Vite, and bundler passes, Octane's browser parser, and volar. Volar recovered but still reported the error, so octane-tsc and editors flagged these files too.

Changes

  • patches/@[email protected]: inside import type …, and for import { type X }, a binding goes through the existing Babel-style BIND_FLAGS_TS_IMPORT import stack, which was previously unreachable. That branch raises the same recoverable message as before and marks a local export defined. This backports the type-only part of test: run the parser over the TypeScript compiler corpus sveltejs/acorn-typescript#110, which is merged on main but unreleased through 1.0.13.
  • patches/@[email protected]: the same rule in the native facade, via a DECLARED_TYPE_IMPORT scope bit.
  • Resulting semantics, identical across the native, @tsrx/core, browser and volar parsers (checked over 20 cases):
    • Accepted: a type-only import (named, inline type, default, namespace, import type A = require()) beside a same-named const/let/var/function/class, and export { T }/export type { T } before or after the import.
    • Still rejected, with unchanged messages: duplicate type-only imports, a value import beside a same-named local value, duplicate value imports, and undefined exports.
    • Left to the type checker: a value import plus a type-only import of the same name. TS2300 is still reported, and the emitted JS is valid because the type import is erased.
  • pnpm-workspace.yaml: both patchedDependencies entries are documented with scope, guard test, and drop condition.
  • pnpm sync restamped lockfileSha256 in 85 audit/react-parity.json files and overlayLockfileHash in the evals manifest. Nothing else in those files changed.
  • No changeset: the patches don't ship in the published tarball, and the package change is test-only.

Validation

  • pnpm format:check: not run repo-wide; pnpm format:files:check on every hand-edited file passes
  • pnpm typecheck: not run. pnpm typecheck:files on the two test files reports only errors that predate this diff, at lines it doesn't touch.
  • pnpm test: not run locally (machine load rule); CI covers it
  • targeted tests: vitest run packages/octane/tests/compiler/parser.test.ts packages/octane/tests/compiler/volar.test.ts --project octane, 88/88 pass
    • parser.test.ts "type-only import bindings": native, editor, and volar parsing; three compile modes, where a plain acorn parse of the output proves no type-only import survives as a value import; plus the rejections that must stay.
    • volar.test.ts "leaves conflicts with a type-only import to the type checker": the repro type-checks clean; value use of a type-only import is still TS1361; a mixed duplicate is TS2300.
    • Against the unpatched dependencies, 8 of the 12 new tests fail with the original TS2300. The other 4 are guards that pass either way: the three rejections, and an export written before its type-only import. That case fails if the patch drops maybeExportDefined.
  • perf-review: not applicable. Nothing under packages/octane/src and no compiled output changed. The parser change adds one integer compare per declared binding.
  • concise-code: two small patch hunks per parser. I dropped a redundant || isInTypeOnlyImportExport condition, which the declareName hook already covers. I rejected backporting all of Bi-directional React bridge: React packages on Octane out of the box, and Octane inside React #110, which would also let import { X } beside const X parse; Octane keeps value imports in its output, so that would emit a duplicate declaration.

Risk / follow-ups

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.


Note

Medium Risk
Changes scope binding for every .tsrx parse path (native, acorn, Vite/Volar), but behavior is narrow, regression-tested, and limited to workspace patches—not shipped runtime code.

Overview
Fixes false Identifier '…' has already been declared errors when a .tsrx module combines a type-only import with a value of the same name (patterns TypeScript already accepts, e.g. import type { Toc } next to export function Toc).

Dependency patches (repo-only, not in the published Octane tarball):

Tests: New type-only import bindings coverage in parser.test.ts (native, browser, Volar, compile + acorn parse of output) and a Volar integration case in volar.test.ts that parsing succeeds while TypeScript still reports value misuse (TS1361) and mixed duplicate imports (TS2300).

Mechanical: pnpm-workspace.yaml documents both patches; pnpm-lock.yaml registers them; 85 audit/react-parity.json files only refresh lockfileSha256 after sync.

Reviewed by Cursor Bugbot for commit 4ee18d4. Bugbot is set up for automated code reviews on this repo. Configure here.

…tion

A .tsrx module with `import type { Toc } from './types'` beside
`export function Toc` failed with "Identifier 'Toc' has already been
declared" (TS2300), which TypeScript accepts and the identical .tsx compiles.
react.dev's Toc.tsx and TanStack Query's contact.tsx both use this shape.

Both parsers bound the names a type-only import introduces as lexical
values: the @tsrx/oxc compat facade's scope check (Node's first parser) and
@sveltejs/acorn-typescript (@tsrx/core, the browser parser, and volar).
Patch both so a type-only import clashes only with another type-only import
of the name. The type checker still reports value use and mixed duplicates,
and the emitted module erases the import.

The acorn-typescript change backports the type-only part of
sveltejs/acorn-typescript#110, merged on main but unreleased through 1.0.13.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 28, 2026
@trueadm
trueadm marked this pull request as ready for review October 28, 2026 17:42
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 4ee18d4 Commit Preview URL

Branch Preview URL
Oct 28 2026, 05:43 PM

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No byte growth, no added work, and no timing changes beyond ±3%.

Compares 19d2825a0, the merge commit's first parent with 48f7c01d8 on the same runner. Any increase in a js-framework work counter fails this check; byte changes are a report. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 7.567 7.992 +4.7% −11.9% … +20.1% ⚪ within noise
octane-tsrx replace 18.342 16.342 −1.8% −12.0% … +6.5% ⚪ within noise
octane-tsrx add 4.892 5.192 −7.1% −13.6% … +5.4% ⚪ within noise
octane-tsrx update 0.155 0.161 −0.0% −5.4% … +8.5% ⚪ within noise
octane-tsrx select 0.012 0.011 −3.9% −11.2% … +9.1% ⚪ within noise
octane-tsrx swap 0.036 0.036 +2.1% −4.8% … +6.5% ⚪ within noise
octane-tsrx remove 0.060 0.056 −2.2% −12.5% … +13.3% ⚪ within noise
octane-tsrx runlots 57.117 56.383 +0.6% −7.4% … +12.2% ⚪ within noise
octane-tsrx select_lots 0.013 0.012 −4.2% −11.7% … +1.4% ⚪ within noise
octane-tsrx clear 113.683 112.742 +1.7% −11.3% … +18.2% ⚪ within noise
octane-jsx run 8.983 8.667 −3.4% −13.4% … +5.7% ⚪ within noise
octane-jsx replace 14.825 15.808 +1.5% −4.8% … +14.3% ⚪ within noise
octane-jsx add 6.692 5.742 −5.9% −14.4% … +4.4% ⚪ within noise
octane-jsx update 0.299 0.257 −5.2% −22.5% … +7.1% ⚪ within noise
octane-jsx select 0.183 0.160 −4.8% −21.9% … +5.3% ⚪ within noise
octane-jsx swap 0.263 0.251 −8.0% −16.9% … +3.5% ⚪ within noise
octane-jsx remove 0.228 0.193 −6.1% −13.8% … +10.3% ⚪ within noise
octane-jsx runlots 40.383 42.583 −2.3% −12.0% … +5.4% ⚪ within noise
octane-jsx select_lots 1.248 1.363 +7.4% +3.0% … +14.9% ⚪ within noise
octane-jsx clear 68.183 64.792 −4.4% −14.2% … +4.5% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 111 measured values.

Public-import reachability bundles (bytes)

No changes across 141 measured values.

@trueadm
trueadm merged commit fc5f37e into main Oct 28, 2026
56 checks passed
@trueadm
trueadm deleted the fix/type-only-import-bindings branch October 28, 2026 18:04
trueadm added a commit that referenced this pull request Oct 28, 2026
trueadm added a commit that referenced this pull request Oct 28, 2026
…n CLI (#2264)

* feat(codemod): add @octanejs/codemod, a React to Octane/TSRX migration CLI

Add `octane-codemod`, which rewrites React + TSX/JSX modules as Octane +
TSRX: React imports and React libraries move to `octane` and their bindings,
components become `@{ … }` templates with `@if`/`@for`/`@try` where that keeps
React's behavior, and template modules are renamed to `.tsrx` with every
import of them updated. Every migrated module is compiled before it is
written; `--strong` applies the compiler's fixes and keeps each module Strong
where the compiler accepts it.

Names resolve through a single-file TypeScript binding, contexts by
provenance across modules, literals and comments survive rewrites, and a
second run changes nothing. `@octanejs/cli` exports its binding catalog and
fix application for the codemod to reuse.

The Hacker News example gains a codemod lane: `codemod.mjs` respells `jsx/`
with React imports and migrates it, and the shared Playwright journeys run
against the result as a third project.

docs/octane-codemod-plan.md holds the implementation plan and the
third-party corpus results.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(codemod): reuse the CLI's project helpers and simplify the runner

Concise pass over the codemod before review:

- Find the project root and walk its sources with `findRoot` and
  `scanSourceFiles` from `@octanejs/cli` instead of a second walker; dot
  directories such as `.storybook/` are now skipped like other tooling.
- Share the compiler finding shape (`finding`, `thrown`) between the runner
  and the Strong pass.
- Report a module the compiler rejects in every mode as its compile error,
  not as a Strong blocker, with a regression test.
- Collapse duplicated helpers in the transform (binding names, line
  prefixes, whitespace-only children) and flatten `mapReactImport`.
- Count CLI errors from one findings list; `--no-verify` uses `parseArgs`
  `allowNegative`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(changeset): note the CLI root exports the codemod uses

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codemod): address review findings on imports, providers, and Strong fallback

- Map `import('react')` and other dynamic imports of React modules to their
  Octane module, as static imports already were.
- Rewrite `.Provider` on the context's tag name, so self-closing providers
  migrate too; this replaces the element-level provider rewrite.
- Remove the `"use strong"` the codemod added wherever it sits in the
  prologue (after `"use client"` too) when a module falls back to
  compatibility mode.
- `test:e2e:dev` in the Hacker News example generates the codemod app first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codemod): insert Strong directives on their own line and skip out/

- The prologue insertion point only ever falls at a line start (after a
  shebang, blank lines, whole-line comments, and directives), so the Strong
  fallback always finds the directive line it added, including when a comment
  shares the first line with code.
- Project scans skip `out/` build output again, now in the CLI's shared
  walker, so `octane analyze` skips it too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(codemod): list @octanejs/codemod as Node tooling source-publication debt

`@octanejs/codemod` is a Node CLI like `@octanejs/cli`: it validates its
shipped source with Node's types and publishes `.js` without sibling
declarations. List it beside the CLI under both rules, tracked by #2266,
which covers shipping declarations and scoping the Node-types rule for every
Node tooling package.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(codemod): record a clean corpus now that #2265 fixed TS2300

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codemod): migrate default-imported contexts and import() types

- Only a default import of React or Octane stands for the whole module; any
  other module's default import is its `default` export, so a context
  imported with `import Theme from './theme'` is recognized through the
  project's context index and its `.Provider` migrates.
- `import('react').X` in a type position maps its module, and a React type
  name Octane spells differently, the same way static imports do; renamed
  relative modules are followed there too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(codemod): rewrite inside a remapped import() type

A remapped specifier copied the rest of its node verbatim, so in
`import('./box').Box<import('react').ReactNode>` the nested React import
was never migrated. The rest of the node is now emitted, which visits its
type arguments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(codemod): record the js-framework-benchmark results

Migrated react-hooks is correct and beats React on every op, but trails
the hand-written octane-tsrx target 1.2-169x and ships React-sized
bytes. Record the numbers, the Strong blockers, react-classes, and the
three rule candidates they point to: text-hole casts, render(App), and
row inlining held on the keyed-selection question.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(codemod): spell typed text holes as text bindings

A child hole whose type proves it a string becomes `{x as string}`; a
number or bigint becomes `{String(x)}` (a template literal where a local
shadows `String`). Types come from one program over the project under
its tsconfig, so imported types resolve, falling back to the module's
own program, which now loads the ES5 library so array and string members
have types. As with the compiler's typed-text proof, the rule runs only
on TypeScript modules in a project with `strictNullChecks`: otherwise a
`string` may hold null, which React renders as nothing.

The js-framework run measured the casts at about 20% off creates. The
corpus casts 219 holes with no new compile errors and Strong unchanged.

The plan now records the keyed-selection finding: the inlined row's slow
selection was its `String()` call, a compiler gap fixed in #2268, not
the useReducer state; the un-inlined row's cost is memo's comparator, so
dropping a provably safe comparator replaces row inlining as the
candidate rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(codemod): make every JSX module TSRX and fix the weather/chat defects

Every module with JSX (`.jsx`, `.tsx`, or a `.js` file with JSX) now
becomes `.tsrx`, entries and helpers without a component included, so no
`.tsx` module is left outside Strong mode. A module that was JavaScript
gains `// @ts-nocheck`, so it stays as unchecked as it was. On the corpus
this takes `.tsrx` modules from 414 to 488 and Strong from 316 to 378,
with no compile errors.

Validating on the weather and chat benchmark apps found three defects:

- a `.jsx` entry kept JSX that Octane's Vite plugin does not compile, so
  the build failed (fixed by the TSRX change); root-absolute
  `index.html` script paths now resolve from the page's directory;
- a key on every arm of a conditional row stayed on the arms, leaving
  an unkeyed `@for` of runtime descriptors (+12.8 kB, ~2x streaming);
  a shared key now moves to the header, and differing keys keep `.map()`;
- finding lines named the new file with the old file's lines, and
  Strong blockers kept the directive's lines; notes now name the
  original file, and blockers are shifted to the written one.

The Hacker News server loads a TSRX `entry-server` when the lane has one.
The plan records both apps' results and the compiler follow-ups.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant