-
Notifications
You must be signed in to change notification settings - Fork 13k
Only bind module.exports if no local definition exists #25869
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only bind module.exports if no local definition exists #25869
Conversation
Note that this uses `lookupSymbolForNameWorker`, which is really a best-effort check since it only knows about symbols that it has already encountered. As a side-effect, even when `module` is bound as part of a `module.exports` reference, it only declares it once instead of one declaration per reference.
@sandersn can you also add a test with function exec() {
const module = new C(12);
return () => {
return module.exports; // should be fine because `module` is defined locally
}
} I don't think |
It is an error inside script files, but the binder sometimes creates a ModuleExports symbol because we doesn't know whether we have a commonjs module until after binding is done.
Nice test -- that exposed a crash that happens because we erroneously create a symbol with ModuleExports on it in a script file. Since script files, unlike module files, don't have a symbol, the checker is unable to create the synthetic type for The binder doesn't know ahead of time whether it is in a commonjs module or not -- that property is calculated lazily inside the binder, so I just added a check in the checker. |
>12 : 12 | ||
|
||
return () => { | ||
>() => { return module.exports; } : () => any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This return type is wrong. AFAIK, module.exports
here should be a number[]
.
Note that this, too, is a best-effort check since evidence of commonjs-ness may be found after a *reference* to module.exports. (A reference to module.exports alone is not enough evidence that a file is commonjs. It has to have an assignment to it.)
OK, I made the binder more conservative about creating a symbol for |
Note that this uses
lookupSymbolForNameWorker
, which is really abest-effort check since it only knows about symbols that it has already encountered.As a side-effect, even when
module
is bound as part of amodule.exports
reference, it only declares it once instead of one declaration per reference.Fixes user baseline regression in webpack as found in #25866.