Repository navigation
Pass the exports object as this to exported functions that read sibling exports - #1610
Merged
Merged
Conversation
…ling exports
retry 0.13.1's `exports.timeouts` reads `this.createTimeout`, and
`exports.operation` calls it through the exports object; async-retry wraps
every request in an operation. A call through the exports object passes
that object as `this`, which compiles to a static table, so the call was
fenced ("calling 'exports.timeouts' through the module's exports object
when the function may read 'this'").
When every `this` in an exported function expression names another
export the module writes (`this.X` with `exports.X = …`), the exports
object is never replaced, and the module calls the function only through
`exports.f(…)`, a package-file stage now respells `this.X` as `exports.X`:
the member Node reads for those calls. Only a caller that detaches the
function or rebinds its `this` would observe a difference.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…/npm-exports-this
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # tests/harness/npm-cases.ts # tests/harness/npm-static.test.ts
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…t shadow exports - A sibling read as a value through `this.f` inside the module (`var g = this.b; g()`) now counts as a detached use, as `exports.f` already did, so `b` keeps its `this`. - An exported function that declares its own `exports` binding (a parameter, a `var`, an arrow parameter or its own name) keeps its text, and so does a module that declares `exports` at module scope: the respelled reads would name that binding instead of the exports object.
This branch was successfully deployed
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.
Pass the exports object as
thisto exported functions that read sibling exportsStacked on #1558.
Summary
Some CommonJS modules export plain functions that reach their sibling exports through
this:Node runs these with the exports object as
this, because callers writeretry.operation(...). scriptc compiled the function bodies without a receiver, so the firstthis.createTimeoutfailed. retry 0.13.1 is the most common example.A new package-file stage,
npm-static-exports-this.ts, runs before the interop-helper stage. Inside a top-levelexports.f = function () {…}, it respellsthis.Xasexports.X. It does so only when all of these hold:thisin the function, including inside arrows, is a read ofthis.<name>where<name>is one of the module's exports;exports =and nomodule.exports =);exports.fis only stored or called asexports.f(...), never passed around as a value.Under those rules,
thisandexportsare the same object for every call a consumer can make through the namespace, so the respelling preserves behavior. Any other shape keeps its text and its current diagnostics.Evidence
npm-static-exports-this.test.tsrun rewritten modules against their originals under Node. They also pin the shapes that must keep their text: athisvalue escaping, a non-export member, a replaced exports object, andexports.fused as a value.2597-exports-this: a generic package whose exported functions call each other throughthis, including from an arrow function. Its output is byte-identical to Node's.2598-retry-timeouts: the unmodified retry 0.13.1 package (MIT) as a fixture. It callsretry.timeouts(...)andretry.operation(...). Its output is byte-identical to Node's; before this change it failed at load.--corpus=all --timing-runs=3: 3433 of 3434 programs are identical. The one difference,fs-extra-package, is only the fs-extra shim's absolute checkout path.pnpm lint,pnpm format:checkandtest:ts7 --baselines-onlypass. The only baseline changes are new entries for the new cases.package-shimssuites, plus the frontend unit tests, pass 1153 of 1154. The one failure,node24-url-authority, is caused by the sandbox's Node 24.15 and also fails on the base.self-hosting-native-driver.test.tspasses (1092 s).🤖 Generated with Claude Code