fix capturing group for matchAll (#10136)

* fix capturing group for matchAll

* update test case

* add core-js/features/string/match-all to test
This commit is contained in:
Tan Li Hau 2019-07-27 06:53:22 +08:00 committed by Justin Ridgewell
parent f160522ab0
commit 6a9d2538e0
5 changed files with 40 additions and 5 deletions

View File

@ -1891,16 +1891,17 @@ helpers.wrapRegExp = helper("7.2.6")`
export default function _wrapRegExp(re, groups) { export default function _wrapRegExp(re, groups) {
_wrapRegExp = function(re, groups) { _wrapRegExp = function(re, groups) {
return new BabelRegExp(re, groups); return new BabelRegExp(re, undefined, groups);
}; };
var _RegExp = wrapNativeSuper(RegExp); var _RegExp = wrapNativeSuper(RegExp);
var _super = RegExp.prototype; var _super = RegExp.prototype;
var _groups = new WeakMap(); var _groups = new WeakMap();
function BabelRegExp(re, groups) { function BabelRegExp(re, flags, groups) {
var _this = _RegExp.call(this, re); var _this = _RegExp.call(this, re, flags);
_groups.set(_this, groups); // if the regex is recreated with 'g' flag
_groups.set(_this, groups || _groups.get(re));
return _this; return _this;
} }
inherits(BabelRegExp, _RegExp); inherits(BabelRegExp, _RegExp);

View File

@ -21,6 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"@babel/core": "^7.4.5", "@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"
} }
} }

View File

@ -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",
});

View File

@ -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",
});

View File

@ -0,0 +1,3 @@
{
"minNodeVersion": "12.0.0"
}