Implement runtime support
DmitrySoshnikov opened this issue · 3 comments
NOTE: this is mostly for spec-compatibility; practically regexes can be used without runtime
As the comments says, a transform can accept includeRuntime
option (which is currently disabled).
If the option is passed, regexes are wrapped into RegExpTree
wrapper, and provide extra functionality at runtime (e.g. accessing captured named groups on matched result).
The runtime implementation already exists, we need to pull it into a separate package, which can be auto-required by the transform.
const re = new RegExp(`
# A regular expression for date.
(?<year>\\d{4})- # year part of a date
(?<month>\\d{2})- # month part of a date
(?<day>\\d{2}) # day part of a date
`, 'x');
const result = re.exec('2017-04-17');
console.log(result.groups.year); // 2017
Is translated into:
// Auto-require `regexp-tree-runtime`, it should be in the dependencies.
const RegExpTree = require('regexp-tree-runtime');
...
const re = new RegExpTree(/(\d{4})-(\d{2})-(\d{2})/, {
flags: 'x',
source: <original-source>,
groups: {
year: 1,
month: 2,
day: 3,
},
});
const result = re.exec('2017-04-17');
console.log(result.groups.year); // 2017
Is this still the latest on this issue? Named capture groups would be a game changer for me, and I can try to find time to work on this if it's still the preferred strategy.
@mike-marcacci, yes, would be great to add this, and I agree, actual usage of the groups is a game changer. We can discuss any strategy here, those described above are just an example, I'm happy to consider any alternatives you have. And of course I'll appreciate a PR.
CC @nicolo-ribaudo who was trying to add a PR to Babel, but the babel/babel#7105 PR itself seems stuck there forever, so we can add it here.
Yeah sorry, currently I'm working on decorators which are a more requested feature. I will revisit that PR after them.