Skip to content

Commit d489a75

Browse files
authored
fix: count string-level appends and prepends in isEmpty (#345)
1 parent 5473bfb commit d489a75

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/MagicString.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,9 @@ export default class MagicString {
10431043
* Returns true if the resulting source is empty (disregarding white space).
10441044
*/
10451045
isEmpty(): boolean {
1046+
// mirrors toString(): the string-level intro and outro bookend the chunks
1047+
if (this.intro.length && this.intro.trim())
1048+
return false
10461049
let chunk: Chunk | null = this.firstChunk
10471050
while (chunk) {
10481051
if (
@@ -1054,6 +1057,8 @@ export default class MagicString {
10541057
}
10551058
chunk = chunk.next
10561059
}
1060+
if (this.outro.length && this.outro.trim())
1061+
return false
10571062
return true
10581063
}
10591064

test/MagicString.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,22 @@ describe('magicString', () => {
18821882

18831883
assert.equal(s.isEmpty(), true)
18841884
})
1885+
1886+
it('should count content appended or prepended to the string', () => {
1887+
assert.equal(new MagicString('').append('X').isEmpty(), false)
1888+
assert.equal(new MagicString('').prepend('Y').isEmpty(), false)
1889+
1890+
const s = new MagicString('abc')
1891+
s.remove(0, 3)
1892+
s.append('!')
1893+
assert.equal(s.toString(), '!')
1894+
assert.equal(s.isEmpty(), false)
1895+
})
1896+
1897+
it('should still disregard whitespace appended or prepended to the string', () => {
1898+
const s = new MagicString('').prepend(' ').append(' ')
1899+
assert.equal(s.isEmpty(), true)
1900+
})
18851901
})
18861902

18871903
describe('length', () => {

0 commit comments

Comments
 (0)