feat: make JSON edit without reserialize/reformat - #28
Open
jorenbroekema wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Both
JSON.parse/stringifyandjqtools will parse JSON files by reserializing the whole tree.It's quite challenging to then respect the original formatting of the file when we stringify the changes back to the file.
I looked up how VSCode tackles this challenge for the
settings.jsonfile, when you make changes to your settings via the UI.They seem to be using jsonc-parser and use this trick to make robust edits without reserializing/reformatting the original document.
Alternative considered: regex string replace, but this seemed like a worse alternative in terms of maintainability and reliability/robustness. The downside of my proposal is introducing jsonc-parser as a dependency. However, it's an actively maintained package, backed by large organization (Microsoft), and has 0 dependencies itself and no vulnerabilities. Bundlesize of the entire thing is 6.5kb, I could check for the 2 methods we use assuming treeshaking, but it seems to not be as relevant for a devtools used only in pipelines/extensions/actions.