-
Notifications
You must be signed in to change notification settings - Fork 113
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
proposal: prependLeft(), prependRight(), appendLeft(), appendRight() #109
Comments
Agreed that more control over these types of inserts would be nice. The decaffeinate project extensively uses magic-string, and many of the bugs I've worked through have been due to subtleties where code snippets are inserted in the wrong order due to the details of how magic-string works. (Not that magic-string is necessarily at fault; it's a hard problem, especially in situations where you need to do many inserts at the same point.) Here are the rules I've learned to follow when working on decaffeinate:
I've thought a bit about how the magic-string API might be changed to alleviate these problems. One thought I've had is that you could provide a Another nice feature would be a way to programmatically examine the fragments that have been inserted at a particular point, and insert and slice between any of those fragments. But really, while these features would make more advanced use cases possible, it seems like the type of code that uses them may end up not being very pleasant or robust. One interpretation is that magic-string, and any tool with its strategy, naturally gets pretty fragile when doing complicated enough work, and it may be better at larger scales to use a tool like recast where you work on an AST rather than doing string modifications. The decaffeinate project is relatively stable at this point with the practices I mentioned above, so it's unlikely to have to move away from magic-string or to use more advanced features if they were introduced. |
I agree with the proposal to add those methods. I had a feeling something like this might be necessary, but I wanted to see how far we could get with I'm hoping that using these new methods in Bublé would fix #111 – it sounds like maybe the erroneously removed code is a casualty of using I can see how priority-based system such as @alangpierce describes might in theory be necessary. For now though I think we should stick with |
magic-string 0.17.0 deprecated insertLeft and insertRight in favor of more precise prepend/append names. See this issue for a discussion on that: Rich-Harris/magic-string#109 To silence that warning, this commit moves the code to the new API, which was pretty easy because there are only a few call sites. I also cleaned up that part of `NodePatcher`, which didn't make a lot of sense. Now, instead of `NodePatcher.insert` calling `NodePatcher.insertRight` calling `MagicString.insertLeft` and `NodePatcher.insertLeft` being unused, we just have `NodePatcher.insert` calling `MagicString.appendLeft`. At least for now, I think it's best to always use the same insert method so that inserts go in order. I also changed insertRight to appendRight in one case because it probably makes more sense anyway and the tests pass.
magic-string 0.17.0 deprecated insertLeft and insertRight in favor of more precise prepend/append names. See this issue for a discussion on that: Rich-Harris/magic-string#109 To silence that warning, this commit moves the code to the new API, which was pretty easy because there are only a few call sites. I also cleaned up that part of `NodePatcher`, which didn't make a lot of sense. Now, instead of `NodePatcher.insert` calling `NodePatcher.insertRight` calling `MagicString.insertLeft` and `NodePatcher.insertLeft` being unused, we just have `NodePatcher.insert` calling `MagicString.appendLeft`. At least for now, I think it's best to always use the same insert method so that inserts go in order. I also changed insertRight to appendRight in one case because it probably makes more sense anyway and the tests pass.
Motivation
There is a gap in the magic-string API that
insertLeft()
andinsertRight()
cannot accommodate.Proposal
New methods:
s.prependLeft( index, content )
s.appendRight( index, content )
Synonyms for existing methods for naming consistency:
s.appendLeft( index, content )
s.prependRight( index, content )
Illustrative example:
The text was updated successfully, but these errors were encountered: