leafo / scssphp

SCSS compiler written in PHP
MIT License
1.34k stars 214 forks source link

SourceMapGenerator / Formatter: Invalid offset for first element and elements with column = 0 #618

Closed fabian-mcfly closed 5 years ago

fabian-mcfly commented 5 years ago

https://github.com/leafo/scssphp/blob/5caf261a0c618eeb123df944597df3ca7bdf0913/src/Formatter.php#L259

In src/Formatter.php, you call

$this->sourceMapGenerator->addMapping()

and subtract 1 from

$this->currentBlock->sourceColumn

This will result in a negative column for mapped elements where the column really is 0.

While this hasn't been an issue yet, and the generated source maps will still work in Chrome and Firefox below v64, Mozilla has implemented a source map validator. And this results in a non-working source map in Firefox 64 an above.

This is a generated Source Mapping: AAAD,aAAD;ACCA;AAAA;aAAA;AAAA;AA8BA;AAAA;oBAAA;AAAA;AAIA;AAAA,YAAA;AAAA;AAKA;AAAA;uBAAA;AAAA;AAaA;AAAA,wBAAA

The right Source Mapping should look like this: AAAA;AAAA;aAAA;AAAA;AA8BA;AAAA;oBAAA;AAAA;AAIA;AAAA,YAAA;AAAA;AAKA;AAAA;uBAAA;AAAA;AAaA;AAAA,wBAAA

You can test the Source Map on http://murzwin.com/base64vlq.html and convert both encoded values. The wrong mapping decoded: 0) [0,0,0,-1], [13,0,0,-1] but you shouldn't get a negative offset (-1).

Solution: Change line 259 to

$this->currentBlock->sourceColumn > 0 ? $this->currentBlock->sourceColumn - 1 : 0, //columns from parser are off by one
nxtpl commented 5 years ago

Amazing! Lost so much time trying to figure out the sourcemap problem in Firefox! This fix should be definitely appended to the main code. THANKS!!!