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

Named group is not transformed #7

Open
dotnetCarpenter opened this issue Jun 29, 2018 · 8 comments
Open

Named group is not transformed #7

dotnetCarpenter opened this issue Jun 29, 2018 · 8 comments

Comments

@dotnetCarpenter
Copy link

Code:

'use strict'

let testString = 'foo:napp2019'
const regex = /(?<p>\w+):(?<name>\w+)(?<year>\d+)/ui

let res = regex.exec(testString)

console.log(res.groups.year)
console.log(res.groups.p)
console.log(res.groups.name)

console.log(regex.test(testString))

Result:

> [email protected] test /Users/nappdev/projects/bugz/babel-plugin-transform-modern-regexp
> babel --plugins transform-modern-regexp -o compiled.js script.js && node compiled.js

/Users/nappdev/projects/bugz/babel-plugin-transform-modern-regexp/compiled.js:8
console.log(res.groups.year);
                       ^

TypeError: Cannot read property 'year' of undefined
    at Object.<anonymous> (/Users/nappdev/projects/bugz/babel-plugin-transform-modern-regexp/compiled.js:8:24)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
npm ERR! Test failed.  See above for more details.

An example project for this issue is available here: https://github.com/dotnetCarpenter/bug-babel-plugin-transform-modern-regexp

dotnetCarpenter added a commit to dotnetCarpenter/bug-babel-plugin-transform-modern-regexp that referenced this issue Jun 29, 2018
@dotnetCarpenter
Copy link
Author

node -v v8.11.3
yarn -v 1.7.0
babel -v 6.26.0 (babel-core 6.26.3)

@DmitrySoshnikov
Copy link
Owner

Yeah, it depends on the runtime support from #3. We should build the runtime in this repo, and also in the generic Babel plugin @nicolo-ribaudo is working on in babel/babel#7105.

@dotnetCarpenter
Copy link
Author

After reading babel/babel#7105, I understand that babel doesn't support changing regExp.exec, which there is a patch for in zloirock/core-js/#411.

In other words, babel-plugin-transform-modern-regexp doesn't work until #411 and #7105 land.

Please update the README.

@DmitrySoshnikov
Copy link
Owner

Yeah, it's in the README.

But we'll add the implementation to this plugin in parallel with the Babel plugin. A lightweight runtime already exists in the regexp-tree, it just needs to be published as the regexp-tree-runtime module (as written in #3), and added to the transform.

Alternatively we could embed the code of the runtime directly to a transforming file, but that would make the code bloated.

Eventually, both new RegExp('/ .. named groups ... /'), and / .. named groups ... / have to be translated into new RegExpTree( ... ), which will have wrappers for the exec, and other methods.

The class itself should be defined as extending RegExp (to support instanceof, etc).

I'll appreciate a PR for any of these parts in case, or I'll try to reach it at some point.

@dotnetCarpenter
Copy link
Author

dotnetCarpenter commented Jul 3, 2018 via email

@liudonghua123
Copy link

I have the similar issue, I write my regexp and tested it ok on https://regex101.com, and the group info also exist on the console of devtools of Chrome, but in my script it always lost the group info.
In the end, I found that my project used babel, and the babel-plugin-transform-named-capturing-groups-regex which is some similar to this plugin is used, and the regexp is not the original native one.
Hope it could be fixed soon.

@liudonghua123
Copy link

In my project, I could use yarn upgrade or ncu -u && yarn to update the dependences and then fixed this problem.

However I could not find the key packages to fix this problem because so many packages updated.

@DmitrySoshnikov
Copy link
Owner

Yes, for named capturing groups we've been working on plugin-transform-named-capturing-groups-regex to make it standard Babel transform. For now you can apply that transform on top as a workaround, and we can probably port it into this repo as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants