Skip to content

Commit 280bc3e

Browse files
authored
fix: respect indent exclusions on moved edits and CRLF bundle intros (#377)
1 parent c14d303 commit 280bc3e

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎src/Bundle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ export class Bundle {
436436
if (this.intro) {
437437
this.intro
438438
= indentStr
439-
+ this.intro.replace(/^[^\n]/gm, (match, index) => {
439+
+ this.intro.replace(/^[^\r\n]/gm, (match, index) => {
440440
return index > 0 ? indentStr + match : match
441441
})
442442
}

‎src/MagicString.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,13 @@ export class MagicString {
514514
chunk.intro = indentPiece(chunk.intro)
515515

516516
if (chunk.edited) {
517-
if (!isExcluded[charIndex]) {
517+
if (!isExcluded[chunk.start]) {
518518
chunk.content = indentPiece(chunk.content)
519519
}
520+
else if (chunk.content) {
521+
// excluded content is left as is, but still decides whether what follows starts a line
522+
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n'
523+
}
520524
}
521525
else if (options.exclude) {
522526
charIndex = chunk.start

‎tests/Bundle/indent.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,13 @@ describe('bundle', () => {
107107
const b = new Bundle()
108108
assert.strictEqual(b.indent(''), b)
109109
})
110+
111+
it('should not indent the carriage return of a blank CRLF line in the intro', () => {
112+
const b = new Bundle({ intro: 'a\r\n\r\nb\n' })
113+
b.addSource(new MagicString('x'))
114+
115+
b.indent(' ')
116+
assert.equal(b.toString(), ' a\r\n\r\n b\n x')
117+
})
110118
})
111119
})

‎tests/MagicString/indent.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ describe('magicString', () => {
9696
assert.equal(s.toString(), ' abc\n>>def')
9797
})
9898

99+
it('should not indent an excluded edited chunk that was moved', () => {
100+
const s = new MagicString('abc\ndef')
101+
s.overwrite(4, 7, 'X\nY')
102+
s.move(4, 7, 0)
103+
104+
s.indent(' ', { exclude: [[4, 7]] })
105+
assert.equal(s.toString(), 'X\nYabc\n')
106+
})
107+
99108
it('should not indent excluded edited content', () => {
100109
const s = new MagicString('abc\ndef\nghi')
101110
s.overwrite(4, 7, 'XXX')

0 commit comments

Comments
 (0)