From 925087b12bcb9ef772835e8874c4d73f6ba161ac Mon Sep 17 00:00:00 2001 From: derdossi <12495454+derdossi@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:51:27 +0100 Subject: [PATCH] fix: apply the trim quantifier to the whole charType --- src/Bundle.ts | 4 ++-- src/MagicString.ts | 4 ++-- tests/Bundle/trim.test.ts | 11 +++++++++++ tests/MagicString/trim.test.ts | 5 +++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Bundle.ts b/src/Bundle.ts index 36290c10..4b4047f1 100644 --- a/src/Bundle.ts +++ b/src/Bundle.ts @@ -480,7 +480,7 @@ export class Bundle { * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start. */ trimStart(charType?: string): this { - const rx = new RegExp(`^${charType || '\\s'}+`) + const rx = new RegExp(`^(?:${charType || '\\s'})+`) this.intro = this.intro.replace(rx, '') if (!this.intro) { @@ -509,7 +509,7 @@ export class Bundle { * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end. */ trimEnd(charType?: string): this { - const rx = new RegExp(`${charType || '\\s'}+$`) + const rx = new RegExp(`(?:${charType || '\\s'})+$`) for (let i = this.sources.length - 1; i >= 0; i -= 1) { const source = this.sources[i] diff --git a/src/MagicString.ts b/src/MagicString.ts index 5aa887de..012c710d 100644 --- a/src/MagicString.ts +++ b/src/MagicString.ts @@ -1261,7 +1261,7 @@ export class MagicString { /** @internal */ trimEndAborted(charType?: string): boolean { - const rx = new RegExp(`${charType || '\\s'}+$`) + const rx = new RegExp(`(?:${charType || '\\s'})+$`) this.outro = this.outro.replace(rx, '') if (this.outro.length) @@ -1306,7 +1306,7 @@ export class MagicString { /** @internal */ trimStartAborted(charType?: string): boolean { - const rx = new RegExp(`^${charType || '\\s'}+`) + const rx = new RegExp(`^(?:${charType || '\\s'})+`) this.intro = this.intro.replace(rx, '') if (this.intro.length) diff --git a/tests/Bundle/trim.test.ts b/tests/Bundle/trim.test.ts index 28056e0c..cf3440bd 100644 --- a/tests/Bundle/trim.test.ts +++ b/tests/Bundle/trim.test.ts @@ -133,5 +133,16 @@ describe('bundle', () => { assert.equal(b1.toString(), 'a') assert.equal(b2.toString(), 'a \n') }) + + it('should apply the quantifier to the whole charType', () => { + const b = new Bundle() + + b.prepend('xy') + b.addSource({ content: new MagicString('a') }) + b.addSource({ content: new MagicString(''), separator: 'yx' }) + + b.trim('x|y') + assert.equal(b.toString(), 'a') + }) }) }) diff --git a/tests/MagicString/trim.test.ts b/tests/MagicString/trim.test.ts index 5de091d3..7e9ccae5 100644 --- a/tests/MagicString/trim.test.ts +++ b/tests/MagicString/trim.test.ts @@ -158,6 +158,11 @@ describe('magicString', () => { s.appendRight(4, 'test') assert.strictEqual(s.trim().toString(), 'test') }) + + it('should apply the quantifier to the whole charType', () => { + assert.equal(new MagicString('xyaxy').trimStart('x|y').toString(), 'axy') + assert.equal(new MagicString('axx').trimEnd('x|y').toString(), 'a') + }) }) describe('trimLines', () => {