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

Extracting helper functions in generators #19

Closed
jedwards1211 opened this issue Oct 23, 2020 · 2 comments
Closed

Extracting helper functions in generators #19

jedwards1211 opened this issue Oct 23, 2020 · 2 comments

Comments

@jedwards1211
Copy link
Contributor

jedwards1211 commented Oct 23, 2020

I was looking into implementing Regex.Escape for JS/TS. Even though it's pretty simple (string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')) I think it would be better to write a regexEscape helper function at the top of the file and then call it.

I haven't noticed evidence that GenJs currently does this for anything else...I think a generic mechanism to extract helper functions would be useful in a lot of situations.

This would make the rest of the translation simple to write:

Write(regexEscapeMethodName + '(');
pattern.Accept(this, CiPriority.Statement);
Write(')');

The main challenges would be

  • Extracting a function needs to automatically add something to the function name if it would conflict with the user's own identifiers
  • Since generation relies on sequential Write calls there's not currently a way to go back and insert something at the top of the output

What do you think? If nothing else we could run all the code through a visitor to decide what to extract before writing anything.

@pfusik
Copy link
Collaborator

pfusik commented Oct 23, 2020

There's a mechanism for that. Search for GenJsMethod.

@jedwards1211
Copy link
Contributor Author

Oh okay, great!

@pfusik pfusik closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2022
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

2 participants