Repository navigation
fix(js,ts): an import reaches what its module hands on, not a namesake or the first export - #2754
colbymchenry wants to merge 1 commit into
Conversation
…e or the first export
A module that exports an import of its own declares nothing under the
exported name: outline's `server/presenters/index.ts` is `import
presentUser from './user'; … export { presentUser, … }`, excalidraw's
`index.tsx` is `import Footer from "./components/footer/FooterCenter";
… export { Footer }`, and an `index.ts` beside a component is `import
Avatar from './Avatar'; export default Avatar`. findExportedSymbol found
no node there, so the import fell to a name match, which took any
namesake in the project. A default written as an expression had only the
first-exported-function guess behind it: `export default new Logger()`
sent outline's server logging calls to the browser app's Logger, and
`export default traceFunction({…})(accountProvisioner)` linked
`provisionFirstCollection`.
import-resolver.ts:
- statedDefaultExport also reads `new X()` (a class the file declares),
what a wrapper call hands on (handedOn: a capitalized argument or the
last of a curried chain, or a function written there, through nested
wrappers; only a function, class or component of the file answers),
and `export { X as default }`. A default naming a binding the file
imports is that import, followed by esmDefaultExport with the walk's
visited set and depth.
- A local export clause naming an import (`export { X }`, `export { X
as Y }`, CommonJS `module.exports = { X }`) is followed too
(forwardedExport; clauseImports is filled beside clauseAliases), in
the named and the namespace-member branches.
- A default the statement leaves unnamed (`createStore(reducer)`) keeps
the first-export guess, and a cast (`export default X as T`) is still
not read.
Replaces #2451, whose JSX half #2563, #2558, #2738 and #2420 cover.
Regression test import-forwarding.test.ts: 6 cases, 5 fail on main (the
sixth guards `createStore(reducer)` and a call chained past `new X()`).
A/B on 16 repos, fresh init, each arm's kernel built from its own
sources: 9 byte-identical (5 controls); outline 448 retargeted, 348
added, 0 removed; react-native 71/7/0; excalidraw 4/2/0; mastodon
1/3/6; community-app 1/4/1; refine-examples 0/21/0; redux-realworld
0/9/0. Every changed edge triaged against the source.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Where the module is in the project, the new forwarding is right: on excalidraw ( Everything below was verified on head 1. A factory called with a class is read as handing that class on. // client.ts
class Config { get(k: string) { return k; } }
declare function makeClient(c: typeof Config): { get(k: string): Promise<string> };
export default makeClient(Config);// use.ts
import client from './client';
export async function run4f() { return client.get('x'); }
2. After
The default-export reopen only looks at importers of the edited file and skips a barrel whose new default resolves nowhere; the named-forward case has no reopen pass. 3. On the rebase. Merging into main conflicts in What holds. CommonJS destructured-require forwarding, aliased imports, default-plus-named and a local clause winning over Checked against the fixtures in graph-assay, which I maintain: the pr-2754 fixture and the pr-2800 fixture both read BROKEN on the merge base and on main, and FIXED on this head and on my hand merge into main. No other fixture verdict changed. Rerun: |
Replaces #2451, re-done as a fresh, small change on current main (553d5d1). It keeps only what main still lacks.
What #2451 still added on main
#2451 had two halves.
The JSX half is covered on main. #2563 (
exportedComponent), #2558 (barrel forwarding), #2738 (wrapper hops), #2420 (own declarations), #2442 (tag positions), #2475 (package gate) and #2476 (generic tags) cover it. 11 of #2451's 14 cases pass on main as they are. Of the 3 that fail:export default memo(function SandpackWithHTMLOutput() {…}), where main renders nothing.codegenNativeComponentheld in a const, where main renders the const.The general import resolver (
findExportedSymbol) still gets these wrong on main. Each gives a wrong edge, not a missing one:import makeAvatar from './makeAvatar'; export default makeAvatar(index forwarding a default)import presentUser from './user'; export { presentUser }(barrel forwarding an import)server/mcp/tools/*)presenters/user.tsclass Logger {…}; export default new Logger()Logger::info(outline: 355 server calls →app/utils/Logger.ts)export default traceFunction({…})(accountProvisioner)provisionFirstCollection)accountProvisionerexport { createThing as default }createThingWhat changes (
src/resolution/import-resolver.tsonly)export defaultstatement is read further.statedDefaultExport/readDefaultExportnow also read:new X(), as the class the file declares;handedOn), through nested wrappers (withTranslation()(withRouter(Login))):observer(Login),React.memo(Row, areEqual)) or ends a curried chain (connect(mapState)(Login),traceFunction({…})(fn));memo(function X() {…}));export { X as default }.visitedset and depth (esmDefaultExport→importedDeclaration).export { X },export { X as Y }and CommonJSmodule.exports = { X }(forwardedExport;clauseImportsis filled besideclauseAliases), in both the named and the namespace-member branches.Decisions, noted here instead of asking:
createStore(reducer)is a store (a peer's catch on fix(react): a JSX tag renders what the file imports under that name #2451). Only a function, class or component of the file answers, socreateContext(DefaultTheme)doesn't hand on the theme constant.new X()and wrapper arguments resolve only inside the file. An imported class or wrapped import isn't followed: sync's tracking of re-exported bindings (ImportReader.exportedAs) covers plain bindings, not calls. Plain bindings (export default X,export { X },{ X as default }) are exactly what sync already tracks.export default create()). fix(react): a JSX tag renders what the file imports under that name #2451 dropped the guess, and that was its topcoder loss:const reducer = require('…/meta'); reducer.factory()was right only through the guess.export default LogBox as ILogBox) is still not read. That was fix(react): a JSX tag renders what the file imports under that name #2451's LogBox loss: 40 react-native calls moved fromILogBox.installto aletvariable.jsxChildreads modules throughexportedComponent, and no synthesized edge (jsx-render or any other pass) changed in any A/B repo.Validation
Tests. The regression test
__tests__/import-forwarding.test.tshas 6 cases, and 5 fail on main. The sixth is a guard:createStore(reducer), andnew Client()with.configure()chained on the next line, must not be read. The cases are:import * as;{ Marker as default }of an import;new Logger()in two apps;{ createThing as default }.The related suites pass:
default-export-declaration,require-default,sync-default-export-change,sync-name-matched-imports,sync-jsx-import-forwarding.A/B. Each repo got a fresh
codegraph init -ywith each arm's dist and kernel built from its own sources (main in a detached worktree at 553d5d1), dumped withscripts/dump-graph.mjs. Edges are paired by (source, kind, line, col, ref), or (source, via) for JSX:Triage. Every changed edge was checked against the source.
Logger.*355 →server/logging/Logger.ts, all fromserver/andplugins/*/server, none of which import the app logger. Presenters 72 →server/presenters/*throughpresenters/index.ts.accountProvisioner21 → the wrapped function. Added edges coverpresentPolicies→presentPolicy(214), thetraceFunctioncommands (documentImporter,teamProvisioner, …),new Metrics(),HEADER_HEIGHTthroughcomponents/Header.tsx, andobserver(DocumentNew).ReactNativeElement/ReadOnlyNode(71). Their module default isreplaceConstructorWithoutSuper(X) as typeof X, and main had a first-export guess. Added edges cover 4extendsof those classes, andDeviceInfo.js/NativeExceptionsManager.jsforwarding defaults.index.tsx'simport Footer from "./components/footer/FooterCenter"; export { Footer }now reachesFooterCenter, where main had the internalFooter. The same goes forLiveCollaborationTriggerandcanvases/index.tsx.component={X}references now reach the component insideconnect(…)(withRouter(X)),themr(…)(X)and the like.components/map/index.ts(import Map from './map'; … export { MapMarker, Map, AdvancedMarker }) and mern-dashboard-client'spages/index.tsnow lead to their own app's files.ColumnSettings×4 andUploadButton, community-app'sJoinCommunity), where the import names a class default that afunction_refdoesn't bind. One was mastodon's first-export guess (textForScreenReaderforwithOptionalRouter(injectIntl(Status))).function_refs to theHEADER_HEIGHTconstant that now park under the name instead ofmodule:Header(importBindingTail: the module now provides the name). They are still failed, and no edge changes.#2451's three loss patterns don't recur.
require()member calls are unchanged.Cost: none measurable. Interleaved
initruns, alternating which arm goes first, medians: outline (5 main / 4 fix runs) total 15.61 s → 15.60 s, resolution phase 9.27 s → 9.37 s (main's own runs spread 9.17–9.50 s); react-native (3 / 3) total 45.98 s → 45.59 s, resolution 31.58 s → 30.59 s. The reading runs once per module (memoized per file); a forward costs one cached import-mapping lookup. One outline run aborted in the parse phase with libuv'sUV_HANDLE_CLOSINGassertion, the Windows Node abort of #2617, and was rerun.Full suite:
CODEGRAPH_KERNEL_EXPECT=1 npx vitest run --maxWorkers=4 --minWorkers=1with the kernel built from this branch on Windows 11: test files 690 passed, 6 skipped, 0 failed (696); tests 8232 passed, 82 skipped, 0 failed (8314).Not covered (follow-ups)
Sync, an existing gap this PR shares. When the module a barrel forwards from changes its default, or a barrel's named re-export changes, a sync keeps the old target where a fresh index has the new one. Main already does this for
export … fromre-exports. Probed sync against fresh on main:export { default } from './a'witha's default moved: diverges.export { default as named } from './a'→'./b': diverges.export { default as named } from './a'witha's default moved: diverges.With this PR the forwarded-import shapes behave the same way, except that a barrel changing the import it default-exports re-links correctly (that case is in the test). I'll take this up as a follow-up for both kinds of forwarding.
The JSX-only
export default memo(function X() {…}). react.dev renders nothing there, on main and here (silent, not wrong).🤖 Generated with Claude Code