Skip to content

Simplify interopRequireWildcard helper - #18255

Merged
nicolo-ribaudo merged 7 commits into
babel:mainfrom
nicolo-ribaudo:iinteropreqwild-const-to-var
Oct 3, 2026
Merged

nicolo-ribaudo merged 7 commits into
babel:mainfrom
nicolo-ribaudo:iinteropreqwild-const-to-var

Conversation

@nicolo-ribaudo

@nicolo-ribaudo nicolo-ribaudo commented Sep 28, 2026 •

Copy link
Copy Markdown
Member
Q 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

The 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 a for-in loop 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.

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 馃彔 A type of pull request used for our changelog categories label Sep 28, 2026
@babel-bot

babel-bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62225

@nicolo-ribaudo nicolo-ribaudo changed the title Avoid for (const in interopRequireWildcard Simplify interopRequireWildcard helper Sep 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 8369097

@nicolo-ribaudo

Copy link
Copy Markdown
Member Author

Both CI failures are also on main

@nicolo-ribaudo
nicolo-ribaudo force-pushed the iinteropreqwild-const-to-var branch from f93e0da to 8a3896f Compare October 1, 2026 17:30
return obj;
}
// Temporary variable for output size
var _;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we somehow keep the temporary variable for golfing purpose?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 liuxingbaoyu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

@nicolo-ribaudo

Copy link
Copy Markdown
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 :)

@liuxingbaoyu liuxingbaoyu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@nicolo-ribaudo
nicolo-ribaudo merged commit b4ff3e7 into babel:main Oct 3, 2026
57 checks passed
@nicolo-ribaudo
nicolo-ribaudo deleted the iinteropreqwild-const-to-var branch October 3, 2026 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Internal 馃彔 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants