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

incorrect insert order for s.insertRight(s.original.length, str) #110

Closed
kzc opened this issue Nov 19, 2016 · 1 comment
Closed

incorrect insert order for s.insertRight(s.original.length, str) #110

kzc opened this issue Nov 19, 2016 · 1 comment

Comments

@kzc
Copy link
Contributor

kzc commented Nov 19, 2016

Observed when researching #109:

version: magic-string@0.16.0

$ cat test_insertRight.js

var magic = require('magic-string');

var s = new magic('0123456789');

var index = 0;
s.insertRight(index, 'a');
s.insertRight(index, 'b');
s.insertRight(index, 'c');

index = 5;
s.insertRight(index, 'd');
s.insertRight(index, 'e');
s.insertRight(index, 'f');

index = 10;
s.insertRight(index, 'g');
s.insertRight(index, 'h');
s.insertRight(index, 'i');

console.log(s.toString());

Produces incorrect result:

$ node ./test_insertRight.js

cba01234fed56789ghi

Should be:

cba01234fed56789ihg

Fix:

--- a/src/MagicString.js
+++ b/src/MagicString.js
@@ -234,7 +234,7 @@ MagicString.prototype = {
                if ( chunk ) {
                        chunk.prepend( content );
                } else {
-                       this.outro += content;
+                       this.outro = content + this.outro;
                }
 
                if ( DEBUG ) this.stats.timeEnd( 'insertRight' );

This would make the behavior consistent with chunk.prepend( content );.

The fix probably should result in a major version change as code in the wild may rely on this inconsistent behavior.

@Rich-Harris
Copy link
Owner

Thanks – fixed in 0.17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants