d3/d3-array

d3.cumsum fails to transpile with babel6 + preset-react

TobbeLino opened this issue · 6 comments

When trying to transpile "d3-array" with babel 6 + babel-preset-react, babel fails with a syntax error on "cumsum.js"

How to repeat:
Run:
npm install d3-array
npm install babel-cli
npm install babel-preset-react
node node_modules\babel-cli\bin\babel.js --presets babel-preset-react .\node_modules\d3-array\src\cumsum.js

Result:

SyntaxError: ./node_modules/d3-array/src/cumsum.js: Only '=' operator can be used for specifying default value. (4:15)
  2 |   var sum = 0, index = 0;
  3 |   return Float64Array.from(values, valueof === undefined
> 4 |     ? v => (sum += +v || 0)
    |                ^
  5 |     : v => (sum += +valueof(v, index++, values) || 0));
  6 | }

Omitting "--presets babel-preset-react" and transpiling with e.g. preset-env works:
npm install babel-preset-env
node node_modules\babel-cli\bin\babel.js --presets babel-preset-env .\node_modules\d3-array\src\cumsum.js

Results in:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = cumsum;
function cumsum(values, valueof) {
  var sum = 0,
      index = 0;
  return Float64Array.from(values, valueof === undefined ? function (v) {
    return sum += +v || 0;
  } : function (v) {
    return sum += +valueof(v, index++, values) || 0;
  });
}

Sorry, but this isn’t a help forum for Babel. You’ll need to find an alternative forum if you want help using Babel. Good luck!

For anyone else that stumbles upon this babel/babel#11038

temporary solution but works by tweaking the input :

link to original post https://github.com/FormidableLabs/victory-native/issues/653#issuecomment-859398327

function cumsum(values, valueof) {
let fn = (v) => (sum += +valueof(v, index++, values) || 0);
if (valueof === undefined)
fn = (v) => (sum += +v || 0);
var sum = 0, index = 0;
return Float64Array.from(values, fn);
}

1rjun commented

Please downgrade the d3 version to "d3": "5.15.0".

Unlocking briefly to mention that this bug (originally reported in January 2018 babel/babel#7235) has now been fixed in Babel by babel/babel#13581.