hhvm / hack-codegen

Library to programatically generate Hack code and write it to signed files
https://hhvm.github.io/hack-codegen/
MIT License
341 stars 93 forks source link

Unable to codegen regex strings #104

Closed Kilenaitor closed 6 years ago

Kilenaitor commented 6 years ago

This is more of an issue with the HackBuilder render exporter, I think, but I can't figure out a way to render regex patterns without them being escaped.


For sake of example, say I have this string:

'/^\/?(\?.*)?$/'

The HackBuilderKeys renderer has four options:

Lambda and classname are right out since they don't apply, but neither literal nor export output the correct result for regex strings.

literal outputs:

/^\/?(\?.*)?$/

without any string wrapping (as expected and documented).

export outputs:

'/^\\/?(\\?.*)?$/'

after escaping all of the slashes in the string (which breaks the regex pattern).


Root cause of this is because export uses var_export to render the string pattern as you can see here which escapes strings.

I think best bet is to just have a specific case for a regex pattern since var_export works for most (if not all other) cases.

fredemmott commented 6 years ago

yeah, this should get a built-in.

For now, you can make a custom renderer (or lambda) that does something along the lines of literal('re'.export($regexp)