Simplify interopRequireWildcard helper - #18255
Merged
nicolo-ribaudo merged 7 commits intoOct 3, 2026
Merged
Conversation
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62225 |
for (const in interopRequireWildcardinteropRequireWildcard helper
|
commit: |
Member
Author
|
Both CI failures are also on |
The `nodeInterop` flag does not affect the shape of the object that is cached, so there is no need of two separate caches.
nicolo-ribaudo
force-pushed
the
iinteropreqwild-const-to-var
branch
from
October 1, 2026 17:30
f93e0da to
8a3896f
Compare
JLHwung
reviewed
Oct 2, 2026
| return obj; | ||
| } | ||
| // Temporary variable for output size | ||
| var _; |
Contributor
There was a problem hiding this comment.
Could we somehow keep the temporary variable for golfing purpose?
Member
Author
There was a problem hiding this comment.
I don't think it does much. Before it was used for the cache + defineProp, now it's not needed anymore for the cache. defineProp is effectively what _ was, just with a better name (that gets minified away anyway).
liuxingbaoyu
approved these changes
Oct 2, 2026
liuxingbaoyu
left a comment
Member
There was a problem hiding this comment.
Can we use a global variable here?
/* @minVersion 7.14.0 */
var cache: WeakMap<any, any> | undefined;
export default function _interopRequireWildcard(
obj: any,
nodeInterop: boolean,
) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (!cache && typeof WeakMap === "function") {
cache = new WeakMap();
}
// Temporary variable for output size
var defineProp = Object.defineProperty as
typeof Object.defineProperty | undefined;
var newObj: Record<string, any> = { __proto__: null, default: obj };
var desc: PropertyDescriptor | undefined;
var key: string;
if (Object(obj) !== obj) {
return newObj;
}
if (cache) {
if (cache.has(obj)) return cache.get(obj);
cache.set(obj, newObj);
}
for (key in obj) {
if (key !== "default" && {}.hasOwnProperty.call(obj, key)) {
desc = defineProp && Object.getOwnPropertyDescriptor(obj, key);
if (desc && (desc.get || desc.set)) {
defineProp!(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
return newObj;
}
Member
Author
|
For some reason global variables in helpers scare me, but I think we fixed years ago the cases in which they'd be a problem. I'll try :) |
JLHwung
approved these changes
Oct 3, 2026
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.
Fixes #1, Fixes #2The first commit is because I am using the CJS transform targeting an old SpiderMonkey version that almost supports
let/const, but apparently not when they are in the head of afor-inloop and the binding has the same name of the parameter.It's a version from more than 10 years ago, so it's probably not worth having a bugfix plugin just for that. However, this change means that I can keep going without having to enable the block-scoping plugin :) Also, it seems like our other helpers for es6 features already use
var.The second commit and third commits are simplifications I noticed while looking at this helper.