Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "simplify trim methods" #53

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 58 additions & 23 deletions src/MagicString.js
Expand Up @@ -181,11 +181,11 @@ MagicString.prototype = {
},

// get current location of character in original string
locate () {
locate ( character ) {
throw new Error( 'magicString.locate is deprecated' );
},

locateOrigin () {
locateOrigin ( character ) {
throw new Error( 'magicString.locateOrigin is deprecated' );
},

Expand Down Expand Up @@ -290,6 +290,8 @@ MagicString.prototype = {

let result = '';

// TODO handle moves

for ( let i = 0; i < this.chunks.length; i += 1 ) {
const chunk = this.chunks[i];

Expand Down Expand Up @@ -352,18 +354,36 @@ MagicString.prototype = {
this.outro = this.outro.replace( rx, '' );
if ( this.outro.length ) return this;

do {
let lastChunk = this.chunks[ this.chunks.length - 1 ];
if ( rx.test( lastChunk.content ) ) {
lastChunk.edit( lastChunk.content.replace( rx, '' ) );
}
let charIndex = this.original.length;
let i = this.chunks.length;

if ( lastChunk.content.length || this.chunks.length === 1 ) {
break;
} else {
this.chunks.pop();
while ( i-- ) {
const chunk = this.chunks[i];

if ( charIndex > chunk.end ) {
const slice = this.original.slice( chunk.end, charIndex );

const match = rx.exec( slice );
if ( match ) {
this.chunk( charIndex - match[0].length, charIndex, '' );
}

if ( !match || match[0].length < slice.length ) {
// there is non-whitespace after the chunk
return this;
}
}
} while ( true );

chunk.content = chunk.content.replace( rx, '' );
if ( chunk.content ) return this;

charIndex = chunk.start;
}

const slice = this.original.slice( 0, charIndex );

const match = rx.exec( slice );
if ( match ) this.chunk( charIndex - match[0].length, charIndex, '' );

return this;
},
Expand All @@ -374,19 +394,34 @@ MagicString.prototype = {
this.intro = this.intro.replace( rx, '' );
if ( this.intro.length ) return this;

do {
let firstChunk = this.chunks[0];
if ( rx.test( firstChunk.content ) ) {
firstChunk.edit( firstChunk.content.replace( rx, '' ) );
}
let charIndex = 0;

if ( firstChunk.content.length || this.chunks.length === 1 ) {
break;
} else {
this.chunks.shift();
for ( let i = 0; i < this.chunks.length; i += 1 ) {
const chunk = this.chunks[i];

if ( charIndex < chunk.start ) {
const slice = this.original.slice( charIndex, chunk.start );

const match = rx.exec( slice );
if ( match ) this.chunk( charIndex, charIndex + match[0].length, '' );

if ( !match || match[0].length < slice.length ) {
// there is non-whitespace before the chunk
return this;
}
}
} while ( true );

chunk.content = chunk.content.replace( rx, '' );
if ( chunk.content ) return this;

charIndex = chunk.end;
}

const slice = this.original.slice( charIndex, this.original.length );

const match = rx.exec( slice );
if ( match ) this.chunk( charIndex, charIndex + match[0].length, '' );

return this;
}
};
}