Rich-Harris / magic-string

Manipulate strings like a wizard

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[bug]

Coober-Ding opened this issue · comments

example code:

const MagicString = require('../dist/magic-string.umd.js')
const SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;

const s = '0123456789';
const ms = new MagicString(s);

//remove '67' from s
const ns = ms.remove(6,8).toString();

var map = ms.generateMap({hires: true});
var smc = new SourceMapConsumer(map);

console.log(`${ns} = ${s}`);
for (var i = 0;i < ns.length; i++) {
    loc = smc.originalPositionFor({ line: 1, column: i });
    console.log(`${ns[i]} = ${s[loc.column]}`);
}

console:

01234589 = 0123456789
0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
8 = 6 //is this right???
9 = 9

/src/utils/Mappings.js
row:50
this.pending = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
row:19
this.rawSegments.push(this.pending);

@mourner Is this issue still present?

Thought I'll have a look. Could not reproduce in node and browser

env: node
magic-string: 0.25.3
source-map: 0.7.3

Code used

// Downloaded this from CDN
const MagicString = require('./magic-string.umd');
const SourceMapConsumer = require( 'source-map' ).SourceMapConsumer;

const consume = async () => {
  const s = '0123456789';
  const ms = new MagicString(s);
  
  const ns = ms.remove(6, 8).toString();
  
  var map = ms.generateMap({hires: true});
  var smc = await new SourceMapConsumer(map);
  
  console.log(`${ns} = ${s}`);
  for (var i = 0;i < ns.length; i++) {
      loc = smc.originalPositionFor({ line: 1, column: i });
      console.log(`${ns[i]} = ${s[loc.column]}`);
  }  
}

consume().then(() => console.log("Done"));

env: browser
Firefox Quantum 68.0 (64-bit)
source-map@0.7.3
magic-string@0.25.3

Code used:

<script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
<script>
    sourceMap.SourceMapConsumer.initialize({
        "lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm"
    });
</script>
<script src='https://unpkg.com/magic-string/dist/magic-string.umd.js'></script>
<script>
  const consume = async () => {
    const s = '0123456789';
    const ms = new MagicString(s);
    
    const ns = ms.remove(6, 8).toString();
    
    var map = ms.generateMap({hires: true});
    var smc = await new sourceMap.SourceMapConsumer(map);
    
    console.log(`${ns} = ${s}`);
    for (var i = 0;i < ns.length; i++) {
        loc = smc.originalPositionFor({ line: 1, column: i });
        console.log(`${ns[i]} = ${s[loc.column]}`);
    }  
  }

  consume();
</script>
commented

This bug still exists on master branch a312519.

Fixed by PR #159.