-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Fixing issue with double dollars in replace string #5840
Conversation
behaves like javascript itself
Hey--don't have time right now to fully think through/test this, but a couple of notes:
|
BTW, here's a useful reference for the JS behavior: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace - under "Specifying a string as a parameter". (This made me think...Instead of doing our own replacement function, would it make more sense to just use the actual JS replace function, calling it on the previously matched string, passing it the regexp and the user's substitution string? That way we wouldn't have to write all this logic to simulate the JS behavior--we would just get it for free. The precede/follow substitutions wouldn't work, but we haven't implemented those anyway.) |
I don't know how you want to pass matches to the js replace function, but I won't say anything against. |
Sorry, not the right commit name ;) It should be |
Just added unit tests, so this PR is completely (in my view). |
@njx Even if we would use another method to replace with subexpressions, the unit tests could be used anyway. |
@@ -117,6 +117,12 @@ define(function (require, exports, module) { | |||
} | |||
} | |||
|
|||
function parseDollars(replaceWith, match) { | |||
replaceWith = replaceWith.replace(/(\$+)(\d{1,2})/g, function (whole, dollars, index) { return dollars.length % 2 === 1 ? dollars.substr(1) + ((match.hasOwnProperty("result") ? match.result[parseInt(index, 10)] : match[parseInt(index, 10)]) || "") : dollars + index; }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be cleaner and easier to understand if you just pass in match.result
from the replaceAll case instead of having to deal with both cases here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the "else" case could just be whole
instead of dollars + index
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After those fixes, it might still be okay for this to be on a single line, but it might be nicer to just break it out into separate lines anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it would be good if there were several lines, but I dunno where to break and how to indent.
Finished re-review. Looks good, just a few nits. It seems like it would be good to add at least one Replace All case (I probably should have done this when I added the original one)...although that's more complex since you'd have to mock clicking the button in the Replace All panel (or add an API to directly invoke the replace all function). If you feel like looking into that, that would be great, but if not I think it's fine to just do some basic manual testing around it since it's pretty clear now that the behavior in all the edge cases will be consistent. Longer term, when we rewrite the find/replace stuff (which hopefully won't be too long from now), we'll be refactoroing out the actual editing code from the UI, so these can become true unit tests instead of interacting with UI elements. |
BTW, the other idea I mentioned (about just using |
Just pushed fixes for these nits. I will try to add one or more test(s) for Replace All, so don't merge yet. |
Just pushed some unit tests for "Replace All". Just say if these are too much 😀 We should add some other tests for this, but that isn't my job. |
}); | ||
// we need to escape (\$) these chars in the regexp to avoid to get these parsed as | ||
// normal regexp anchors | ||
replaceWith = replaceWith.replace(/\$\$/g, "$$"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, my comment here was about the "$$" in the replacement string being confusing (because it looks like you're replacing "$$" with "$$"--but the replacement "$$" is really just an escaped "$"), not the backslashes in the regexp. Like I mentioned before, I don't think you need the double-dollar in the replacement string since it won't be interpreted as a metacharacter there if it doesn't have anything after it. But this isn't a big deal.
Thanks for all the additional unit tests! But I'm getting one failure in the new "$0" test--it doesn't seem to be matching the expected text. Are you seeing the same failure? |
No, for me every test is working. Which error message do you get? |
Hmmm, it's working fine for me now--not sure what was wrong. Thanks for working on this! Merging. |
Fixing issue with double dollars in replace string
Thank you. You should add unit tests for normal "Replace All" cases. |
@njx I would also like to add the functionality of |
Or maybe we shouldn't include |
It would make sense to add |
Okay, I don't know what to use them for, too. |
@njx Would you mind adding some more, general unit tests for |
We have an open card for adding real unit tests for Find/Replace, so that should get done soon. I wouldn't worry about it for this PR as long as your own cases are tested. |
I picked this idea from #5772 (comment).
This should be exactly the same dollar-signs-in-replace-with-string behaviour as JavaScript itself haves.
I did a bit of testing and everything worked fine (for example
$1
,$$1
,$$$1
,$
,$$
).FindReplace tests passing, too.