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
Add @babel/plugin-transform-named-capturing-groups-regex #7105
Conversation
@@ -0,0 +1 @@ | |||
/(?<\u{41}>)\k<\u{41}>/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DmitrySoshnikov I think that regexp-tree
should throw on this regex, since unicode escapes are only alloed when there is the u
flag?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, @Golmote added generic support for Unicode to parser (DmitrySoshnikov/regexp-tree#114); we should also check the group names, if they are allowed only with u
flag (since by itself \u{41}
can normally be used without u
flag -- "u
repeated 41 times").
If you're positive from spec, that it should throw, we should open an issue. Thanks for catching it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure identity escapes don't throw without the u
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Golmote, right, however this one is specifically for a capturing group name, like in (?<\u{41}>)
. I'll need to double-check the spec, but seems, yes, it should throw, and be an invalid name for a group if no u
flag is passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I read too fast 😳
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/9800/ |
7920760
to
f09188a
Compare
"regexp", | ||
"regular expressions" | ||
], | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-named-capturing-groups-regex.git", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove the .git
as the end here?
The tests are failing because |
Thanks for the notice! Just an FYI, I removed |
891eaf7
to
8c8bfa7
Compare
Proposal merged - tc39/ecma262#1027 (comment) |
}, Object.create(null)); | ||
} | ||
|
||
wrap(RegExp.prototype, "exec", function(f, str) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is effectful, I understand that it's hard (impossible?) to implement this feature without actually messing with built in prototypes - but when that is the case I feel like it should not be a part of babel-helpers, but rather babel-polyfill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zloirock Does corejs polyfill support for [String.replace]
and overwritten exec
? If it does, we can remove thi "compat" helper from Babel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolo-ribaudo core-js
supports @@replace
, but does not overwrite exec
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't explain myself correctly. Every RegExp method (or string method which uses regexs) should use exec
internally. e.g.
class MyRegExp extends RegExp {
exec() {
return ["foo", "bar"];
}
}
"abc".match(new MyRegExp("b")); // ["foo", "bar"]
If it doesn't work in core-js
, I could open a PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
core-js
provides a naive realisation of support RegExp
-related well-known symbols and use original methods like String.prototype.match
or String.prototype.replace
. It's done for reducing slowdown in wrappers. However, now it should not cause too many problems, so feel free to add a PR with adding this logic.
helpers.wrapRegExp = defineHelper(` | ||
var kGroups = Symbol("@@babel/groups"); | ||
|
||
function BabelRegExp(re, groups) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be wrapped in an iife? all those assignments at the moment will prevent tree-shaking if BabelRegExp
class stays unused. Im not sure if we can annotate helper as #__PURE__
at the moment though (I think comments in the helpers are ignored)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could probably also use a redefining helper technique here to contain this whole declaration within a single top level node
it seems though that it would actually be cleaner if we could transpile helpers with user settings - this could be written as an actual class
Is this PR ready to me merged? |
8c8bfa7
to
5036877
Compare
Can this be merged @nicolo-ribaudo ? |
304b365
to
1a2f8cb
Compare
Is this ready for merge? |
if (result) result.groups = buildGroups(result, this); | ||
return result; | ||
}; | ||
BabelRegExp.prototype[Symbol.replace] = function(str, substitution) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that I will move this whole method to core-js (I already moved the first part)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably it is better to leave it there, otherwise even es6+ browsers require core-js
1a2f8cb
to
2ce3df0
Compare
de260aa
to
4bd414a
Compare
Any updates on this PR y'all? |
4bd414a
to
492ce95
Compare
export default function({ types: t }, options) { | ||
const { runtime = true } = options; | ||
if (typeof runtime !== "boolean") { | ||
throw new Error(".runtime must be boolean"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Is the period necessary?
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-named-capturing-groups-regex", | ||
"bugs": "https://github.com/babel/babel/issues", | ||
"dependencies": { | ||
"regexp-tree": "^0.0.78" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we bump to latest (85)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's use the latest version.
This still needs zloirock/core-js#435 to be published, yea? /cc: @zloirock |
Yeah, It hasn't been released yet. |
@existentialism we need a backport of zloirock/core-js#453 to |
Oh right, I will backport it. |
core-js was just updated, I'll update this PR soon. |
492ce95
to
c0014fe
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍, I'll follow this up with preset-env support!
Thanks @zloirock for the support as well!
@nicolo-ribaudo, what's the status on this? You can also update to the latest |
00dd8cd
to
ce8ff40
Compare
Wanted to cross-reference DmitrySoshnikov/regexp-tree#170. regexp-tree brings in a bunch of large CLI dependencies including an old version of yargs, which significantly increases the install size of @babel/preset-env (due to #9345). It would be great to have these moved to a separate CLI package, if possible. |
@billyjanitsch, yes, we'll be separating the CLI (and potentially other modules) into its own npm package. |
Note: as of |
regexp-tree
When the
runtime
flag is on, this plugin adds a new helper which wraps the nativeRegExp
class to provide groups support. People nees to use a polyfill (I implemented it in core-js) for browsers that don't support overwriting regexp methods.