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

Implement runtime support #3

Open
DmitrySoshnikov opened this issue Apr 17, 2017 · 3 comments
Open

Implement runtime support #3

DmitrySoshnikov opened this issue Apr 17, 2017 · 3 comments

Comments

@DmitrySoshnikov
Copy link
Owner

DmitrySoshnikov commented Apr 17, 2017

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
@mike-marcacci
Copy link

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.

@DmitrySoshnikov
Copy link
Owner Author

@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.

@nicolo-ribaudo
Copy link

Yeah sorry, currently I'm working on decorators which are a more requested feature. I will revisit that PR after them.

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