Skip to content

Commit

Permalink
fix capturing group for matchAll (#10136)
Browse files Browse the repository at this point in the history
* fix capturing group for matchAll

* update test case

* add core-js/features/string/match-all to test
  • Loading branch information
tanhauhau authored and jridgewell committed Jul 26, 2019
1 parent f160522 commit 6a9d253
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -1891,16 +1891,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"
}

0 comments on commit 6a9d253

Please sign in to comment.