Repository navigation
Fix format producing undefined edits at 200 indent characters - #152
Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
Conversation
newLinesAndIndent() looked up cachedBreakLinesWithSpaces[...][amountOfSpaces] when amountOfSpaces was equal to the cache length, which is one past the last entry. The edit content was then undefined, and applying the edits fails. This happens with deeply nested documents, for example at nesting level 100 with two-space indentation.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the indentation-cache boundary fix.
No open issues 🎉
What changed in this PR
Fixes an off-by-one error in the formatter’s indentation cache that could produce edits with undefined content at exactly 200 indentation characters.
Changes:
- Uses the fallback when indentation reaches or exceeds the cache length.
- Adds a regression test for 250 nested arrays using both spaces and tabs.
| File | Description |
|---|---|
src/test/format.test.ts |
Tests deeply nested formatting at and beyond the cache boundary. |
src/impl/format.ts |
Changes the cache boundary check from > to >=. |
Review effort: Balanced
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.
Fixes microsoft/vscode-json-languageservice#262
format()can return edits whosecontentisundefined. Anything that applies them then throws, which is theCannot read properties of undefined (reading 'length')inapplyEditsfrom the issue.The cause is an off-by-one in
newLinesAndIndent(). The cached line break strings have 200 entries (indexes 0 to 199), but the fallback torepeat()only kicked in whenamountOfSpaces > length. With exactly 200 indent characters the lookup read index 200 and gotundefined. That is nesting level 100 withtabSize: 2, level 50 withtabSize: 4, or level 200 with tabs. I changed the check to>=.I added a formatter test that formats 250 nested arrays with spaces and with tabs. It fails without the change and passes with it.
What I ran:
npm test(compile, eslint, node tests): 89 tests pass.tmp.jsonfile attached to the issue throughformat(). Before the change, withtabSize: 2and spaces, 307 of the 11404 edits hadundefinedcontent. After the change none do fortabSize2 or 4 with spaces or for tabs, and the formatted output parses to the same value as the input.