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

fix capturing group for matchAll #10136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -1886,16 +1886,17 @@ helpers.wrapRegExp = helper("7.2.6")`

export default function _wrapRegExp(re, groups) {
_wrapRegExp = function(re, groups) {
return new BabelRegExp(re, groups);
return new BabelRegExp(re, undefined, groups);
};

var _RegExp = wrapNativeSuper(RegExp);
var _super = RegExp.prototype;
var _groups = new WeakMap();

function BabelRegExp(re, groups) {
var _this = _RegExp.call(this, re);
_groups.set(_this, groups);
function BabelRegExp(re, flags, groups) {
var _this = _RegExp.call(this, re, flags);
// if the regex is recreated with 'g' flag
_groups.set(_this, groups || _groups.get(re));
return _this;
}
inherits(BabelRegExp, _RegExp);
Expand Down
Expand Up @@ -21,6 +21,7 @@
},
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/helper-plugin-test-runner": "^7.0.0"
"@babel/helper-plugin-test-runner": "^7.0.0",
"core-js-pure": "^3.0.0"
}
}
@@ -0,0 +1,16 @@
require('core-js/features/string/match-all.js');

const string = "Favorite GitHub repos: tc39/ecma262 v8/v8.dev";
const regex = /\b(?<owner>[a-z0-9]+)\/(?<repo>[a-z0-9\.]+)\b/g;

const matches = string.matchAll(regex);

expect(matches.next().value.groups).toEqual({
owner: "tc39",
repo: "ecma262",
});

expect(matches.next().value.groups).toEqual({
owner: "v8",
repo: "v8.dev",
});
@@ -0,0 +1,14 @@
const string = "Favorite GitHub repos: tc39/ecma262 v8/v8.dev";
const regex = /\b(?<owner>[a-z0-9]+)\/(?<repo>[a-z0-9\.]+)\b/g;

const matches = string.matchAll(regex);

expect(matches.next().value.groups).toEqual({
owner: "tc39",
repo: "ecma262",
});

expect(matches.next().value.groups).toEqual({
owner: "v8",
repo: "v8.dev",
});
@@ -0,0 +1,3 @@
{
"minNodeVersion": "12.0.0"
}