Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/MagicString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,9 @@ export default class MagicString {
* Returns true if the resulting source is empty (disregarding white space).
*/
isEmpty(): boolean {
// mirrors toString(): the string-level intro and outro bookend the chunks
if (this.intro.length && this.intro.trim())
return false
let chunk: Chunk | null = this.firstChunk
while (chunk) {
if (
Expand All @@ -1054,6 +1057,8 @@ export default class MagicString {
}
chunk = chunk.next
}
if (this.outro.length && this.outro.trim())
return false
return true
}

Expand Down
16 changes: 16 additions & 0 deletions test/MagicString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,22 @@ describe('magicString', () => {

assert.equal(s.isEmpty(), true)
})

it('should count content appended or prepended to the string', () => {
assert.equal(new MagicString('').append('X').isEmpty(), false)
assert.equal(new MagicString('').prepend('Y').isEmpty(), false)

const s = new MagicString('abc')
s.remove(0, 3)
s.append('!')
assert.equal(s.toString(), '!')
assert.equal(s.isEmpty(), false)
})

it('should still disregard whitespace appended or prepended to the string', () => {
const s = new MagicString('').prepend(' ').append(' ')
assert.equal(s.isEmpty(), true)
})
})

describe('length', () => {
Expand Down
Loading