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
10 changes: 8 additions & 2 deletions src/MagicString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,10 @@ export default class MagicString {
chunk = chunk.previous
} while (chunk)

return false
// like Chunk#trimEnd, carry on into the intro once everything after it has
// been trimmed away, since the intro is then the end of the string
this.intro = this.intro.replace(rx, '')
return this.intro.length > 0
}

/**
Expand Down Expand Up @@ -1288,7 +1291,10 @@ export default class MagicString {
chunk = chunk.next
} while (chunk)

return false
// like Chunk#trimStart, carry on into the outro once everything before it
// has been trimmed away, since the outro is then the start of the string
this.outro = this.outro.replace(rx, '')
return this.outro.length > 0
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/Bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,26 @@ describe('bundle', () => {
b.trimEnd()
assert.equal(b.toString(), 'abc ;')
})

it('should stop trimStart at a blank source that still has appended content', () => {
const b = new Bundle()

b.addSource({ content: new MagicString(' ').append('X') })
b.addSource({ content: new MagicString(' Y') })

b.trimStart()
assert.equal(b.toString(), 'X\n Y')
})

it('should stop trimEnd at a blank source that still has prepended content', () => {
const b = new Bundle()

b.addSource({ content: new MagicString('Y ') })
b.addSource({ content: new MagicString(' ').prepend('X') })

b.trimEnd()
assert.equal(b.toString(), 'Y \nX')
})
})

describe('toString', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/MagicString.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,20 @@ describe('magicString', () => {
assert.equal(s.toString(), 'x abc')
})

it('should carry trimStart into the global outro when nothing comes before it', () => {
const s = new MagicString(' ')
s.append(' x ')
s.trimStart()
assert.equal(s.toString(), 'x ')
})

it('should carry trimEnd into the global intro when nothing comes after it', () => {
const s = new MagicString(' ')
s.prepend(' x ')
s.trimEnd()
assert.equal(s.toString(), ' x')
})

it('should trim original content', () => {
assert.equal(new MagicString(' abcdefghijkl ').trim().toString(), 'abcdefghijkl')
assert.equal(new MagicString(' abcdefghijkl').trim().toString(), 'abcdefghijkl')
Expand Down