Skip to content
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

@babel/plugin-proposal-duplicate-named-capturing-groups-regex: support dynamically created regular expressions #16502

Open
1 task
rauschma opened this issue May 16, 2024 · 4 comments

Comments

@rauschma
Copy link

rauschma commented May 16, 2024

💻

  • Would you like to work on this feature?

What problem are you trying to solve?

The plugin works for RegExp literals but not for regular expressions created via new RegExp().

Describe the solution you'd like

FWIW: I created the following (quick and very dirty) workaround: I replace new RegExp with new RegExpX – which invokes the class shown below. The solution I’d like would be cleaner than that.

class RegExpX {
  constructor(str, flags) {
    const RE = /(?<!\\)\((\?<(?<name>[^<>]+)>)?/g;
    let parenCounter = 0;
    const groupNameToGroupNumbers = new Map();
    str = str.replaceAll(
      RE,
      (all, ...args) => {
        const groups = args.pop();
        parenCounter++;
        if (groups.name) {
          let groupNumbers = groupNameToGroupNumbers.get(groups.name);
          if (groupNumbers === undefined) {
            groupNumbers = [];
            groupNameToGroupNumbers.set(groups.name, groupNumbers);
          }
          groupNumbers.push(parenCounter);
          // It’s in the Map, only its group number matters now
          return `(`; // avoid any name clashes
        }
        return all;
      }
    );
    // Override what the constructor would normally return:
    return _wrapRegExp(
      new RegExp(str, flags),
      Object.fromEntries(groupNameToGroupNumbers)
    );
  }
}

Describe alternatives you've considered

None

Documentation, Adoption, Migration Strategy

No response

@babel-bot
Copy link
Collaborator

Hey @rauschma! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@liuxingbaoyu
Copy link
Member

zloirock/core-js@913625f
Have you enabled corejs? It seems they already support it.

@rauschma
Copy link
Author

Thanks! I tried both of the following import statements:

import 'core-js/actual/regexp/constructor.js';
import 'core-js/actual/index.js';

But both failed as follows:

SyntaxError: Invalid regular expression:
/^(?<year>[0-9]{4})-(?<month>[0-9]{2})|(?<month>[0-9]{2})\/(?<year>[0-9]{4})$/:
Duplicate capture group name

I can create an issue for core-js, but it may make sense to document that the Babel plugin only handles RegExp literals.

@M-MkM

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants