Shopify / javascript

The home for all things JavaScript at Shopify.
MIT License
251 stars 38 forks source link

Fix erroneous range/slice translations #170

Closed GoodForOneFare closed 8 years ago

GoodForOneFare commented 8 years ago

Decaf is generating an unnecessary second slice parameter when translating CS ranges with negative arguments.

Simplified example

Coffeescript

foo[-bar..-1]

js2.coffee

foo.slice(-bar);

decaf

foo.slice(-bar, -1 + 1);

Problem

The js2.coffe translation produces the right result; decaf doesn't.

['a', 'b', 'c', 'd', 'e', 'f'].slice(-3); // == ['d', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f'].slice(-3, -1 + 1); // == []

Production code example

From app_messenger.coffee.

CoffeeScript

newUri.hostname()[-originalUri.hostname().length..-1] == originalUri.hostname()

js2.coffee

newUri.hostname().slice(-originalUri.hostname().length) === originalUri.hostname();

decaf

newUri.hostname().slice(-originalUri.hostname().length, -1 + 1)
GoodForOneFare commented 8 years ago

Similar example from ui_popover.coffee:

CoffeeScript

newStyles.base.transformOrigin.split(" ")[1..-1]

js2.coffee

newStyles.base.transformOrigin.split(" ").slice(1);

decaf

newStyles.base.transformOrigin.split(" ").slice(1, -1 + 1)
GoodForOneFare commented 8 years ago

Merged in fix from mainline and published to NPM.