babel: RegExp groups broken on `v7.13.x` versions

Bug Report

Current behavior The latest v7.13.x version seem to break RegExp(...).exec(...).groups functionality. It is undefined for a correctly built RegExp when using these versions, but works correctly when setting:

{
  "devDependencies": {
      "@babel/cli": "7.12.17",
      "@babel/core": "7.12.17",
      "@babel/plugin-transform-runtime": "7.12.17",
      "@babel/preset-env": "7.12.17",
      "@babel/preset-typescript": "7.12.17",
  },
  "resolutions": {
      "@babel/cli": "7.12.17",
      "@babel/core": "7.12.17",
      "@babel/plugin-transform-runtime": "7.12.17",
      "@babel/preset-env": "7.12.17",
      "@babel/preset-typescript": "7.12.17",
   }
 }

Input Code

RegExp(/*...*/).exec(/*...*/).groups

Expected behavior

.groups should be defined for correct regexes

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
const test = process.env.BABEL_ENV === 'test';
const useESModules = test ? {} : { useESModules: false };

module.exports = {
  sourceType: 'module',
  presets: ['@babel/preset-typescript', '@babel/preset-react', '@babel/preset-env'],
  plugins: [['@babel/plugin-transform-runtime', { ...useESModules }], 'lodash'],
};

Environment

  • Babel version(s): v7.13.x
  • Node version: v15.9.0
  • OS: macOS Big Sur
  • Monorepo: no
  • How you are using Babel: Webpack / babel-jest

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 43 (15 by maintainers)

Most upvoted comments

Adding the logs so you know I’m not making this up

All these conditions seem so random that it’s impossible to be so creative to make this up πŸ˜›

Got it, sending you a repo

Regarding the polyfill, may be worth having some documentation or warning about that since it looks like it does try to polyfill it and then fails silently

Thanks I see the bug πŸ‘

@nicolo-ribaudo this looks like something non deterministic since I can get it to pass or fail tests by adding console.log("#") before a try/catch block in which the regex runs.

When trying to create a demo project for this the issue happened on the older version as well.

Could you please try running

const userAgentRegex = /(?<type>(Chrome))\/(?<version>\d+)(.|\s)/;

const getBrowserInformation = (userAgent) => {
  try {
    const groups = RegExp(userAgentRegex).exec(userAgent)?.groups;
    return groups;
  } catch {
    return null;
  }
};

const browserInformation = getBrowserInformation(
  "Chrome/88.0.4324.182"
);

In a jest test using babel-jest? It seems to return undefined as browserInformation even though it should return {type: "Chrome", version: "88"} (and does in the browser / with some versions in some projects)