fix: count string-level appends and prepends in isEmpty - #345
Merged
Conversation
isEmpty() only walked the chunks, so it ignored the string-level intro and outro that plain prepend() and append() write to. A source whose only output came from append() or prepend() was reported as empty even though toString() returned that content. isEmpty() now checks intro and outro as well, the same way toString(), generateMap() and lastChar() already do, while still disregarding whitespace.
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.
isEmpty()is documented, both in the README and in its own JSDoc, as returning true when "the resulting source is empty (disregarding white space)". The resulting source is whattoString()produces.However,
isEmpty()only walked the chunks. It never looked at the string-levelintroandoutrothat plainprepend()andappend()write to (unlikeprependLeft/prependRight/appendLeft/appendRight, which write to chunk intro/outro and were already covered). So a source whose only output came fromappend()orprepend()was reported as empty even thoughtoString()returned that content.This is different from
length(), which is documented and tested to deliberately exclude edge inserts.isEmpty()has no such contract: its existing test only ever prepends and appends whitespace, so it never locked the old behaviour, and every other output-facing method (toString(),generateMap(),lastChar()) already accounts forintroandoutro.The fix adds the two missing checks, mirroring the
toString()order (intro first, outro last) and reusing the same whitespace-disregarding idiom already used for chunks and inBundle#isEmpty.Bundle#isEmpty()delegates to this method per source, so it inherits the fix as well.I added a test that fails on the old code and passes now, plus a guard test confirming that whitespace-only appends and prepends are still treated as empty. The suite goes from 262 to 264 passing;
tscandeslintare clean.