Do not transform -0

Reviewed By: rubennorte

Differential Revision: D8095511

fbshipit-source-id: 8f75cdf04cce58691b59b2c9dcac620150706ab3
This commit is contained in:
Miguel Jimenez Esun 2018-05-22 08:12:17 -07:00 committed by Facebook Github Bot
parent adce40d38d
commit f0a0e92f5f
2 changed files with 15 additions and 1 deletions

View File

@ -259,4 +259,16 @@ describe('constant expressions', () => {
expect(fold('arbitrary.js', code)).toEqual('var x=2;{z();}');
});
it('does not mess up -0', () => {
const code = `
var plusZero = +0;
var zero = 0;
var minusZero = -0;
`;
expect(fold('arbitrary.js', code)).toEqual(
'var plusZero=0;var zero=0;var minusZero=-0;',
);
});
});

View File

@ -63,7 +63,9 @@ function constantFoldingPlugin(context: {types: BabelTypes}) {
const result = path.evaluate();
if (result.confident) {
path.replaceWith(t.valueToNode(result.value));
if (!Object.is(result.value, -0)) {
path.replaceWith(t.valueToNode(result.value));
}
}
},
};