regexp-tree icon indicating copy to clipboard operation
regexp-tree copied to clipboard

Suggestion: source map in transformer

Open RReverser opened this issue 7 years ago • 5 comments

While source maps are not extremely useful for debugging on such a small data sets, they can be still handy for testing transformers and (re)using existing tools that can match transformed pieces of regexp with original ones.

Given that you already have API to generate transformed regexp, and you know original / final locations, it should be pretty easy to add source map support using https://www.npmjs.com/package/source-map.

RReverser avatar Jun 16 '17 23:06 RReverser

Yep, sounds like a good idea 👍

I'll probably reach it at some point later, and of course will appreciate a PR in case.

DmitrySoshnikov avatar Jun 17 '17 00:06 DmitrySoshnikov

@DmitrySoshnikov Started trying but noticed that currently AST lacks line/column locations, and my understanding is that regexps are not necessarily single-line... Would need to have such locations available in AST first if doing this in a non-hacky way.

RReverser avatar Jun 18 '17 11:06 RReverser

@RReverser, ah, yes, it's tracked in the #49. In general there should be no issue with adding it (except it might be breaking change for existing transforms, but might be fine).

The Token, and AST node locations in the parsing handlers already have all needed information (for line, column, and offset). Currently we get only offsets, here (and in other similar places), but can get all.

The only ugly handling in the parser is this function, which handles named grouped reference. With the Unicode names have to parse manually, and track locations. Should be doable there as well, or we can drop support of Unicode names in named group references (like \k\u{0d36} -- not sure if someone even use this on practice, see this note).

DmitrySoshnikov avatar Jun 18 '17 23:06 DmitrySoshnikov

I finally added line/column support, in https://github.com/DmitrySoshnikov/regexp-tree/commit/97daef91aec7a61fdd7a5a356e566ac65c3a834c 😄

DmitrySoshnikov avatar Jun 30 '17 09:06 DmitrySoshnikov

Great, thanks! Maybe we can merge all the breaking changes together into a new release?

RReverser avatar Jun 30 '17 12:06 RReverser