babel: `typescript` preset: incompatible with `+=` operator inside arrow functions, nested in ternary

Bug Report

  • I would like to work on a fix! I have close to zero experience with Babel’s internals though. I would not be offended, if you think you can fix it yourself quicklier.

Current Behavior The typescript preset is incompatible with d3-array/cumsum.js. The issue seems to only occur for arrow functions using the += operator, nested inside a ternary. When typescript is disabled, the code compiled without any issues.

Input Code

  • Original broken d3-array/cumsum.js: REPL
  • Minimal reproduction: REPL
0 ? v => (sum += v) : v => 0;

/repl.ts: Only ‘=’ operator can be used for specifying default value. (1:13)

1 | 0 ? v => (sum += v) : v => 0;

Expected behavior/code Babel should compile the above code with typescript enabled.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 24
  • Comments: 40 (18 by maintainers)

Commits related to this issue

Most upvoted comments

@pelly You need to check your yarn.lock and manually merge the duplicate elements that you’ve probably got after yarn add d3-array@2.3.3:

-"d3-array@1.2.0 - 2":
-  version "2.5.1"
-  resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.5.1.tgz#cc785e1c4b560a34b8c77af9e6709bdf3f2ee117"
-  integrity sha512-cKvAlQZUKhXInw5mosJMtAYsY3dDYwTess/WOFUQTGcr8xV04SZMJs6n6QznsqZC5vJTkvZuCgsH9fo981ysPA==
-
-d3-array@2.3.3:
+d3-array@2.3.3, "d3-array@1.2.0 - 2":
   version "2.3.3"
   resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.3.3.tgz#e90c39fbaedccedf59fc30473092f99a0e14efa2"
   integrity sha512-syv3wp0U5aB6toP2zb2OdBkhTy1MWDsCAaYk6OXJZv+G4u7bSWEmYgxLoFyc88RQUhZYGCebW9a9UD1gFi5+MQ==

A safer approach would be to add a static resolution to package.json:

{
  "resolutions": {
    "d3-array": "2.3.3"
  }
}

and re-run yarn.

I had same problem. I’m wondering if ignoring cumsum.js could be a workaround.

// babel.config.js
module.exports = function () {
  return {
    ignore: [new RegExp("d3-array/src/cumsum.js")],
    // other configs...
  }
};

Notes:

Released in @babel/parser 7.14.9

My 2 cts observation: it seems the minimal reproduction in REPL can be fixed by changing

0 ? v => (sum += v) : v => 0;

to

0 ? v => (sum += v) : (v => 0);

Similarly, the code in d3-array compiles if we wrap the second arrow function with parentheses:

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

Adding a resolution to 2.3.3 gave us other compilation errors. For anybody else stumbling on this, we’ve added this in our package.json: "d3-array": "file:./lib/d3-array", then copied the content from the d3-array folder in node_modules to our local folder and changed cumsum.js to this:

export default 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)
        }
  )
}

I am still seeing the same error with babel/parser 7.15.0

I opened https://github.com/babel/babel/issues/13644 since it’s effectively a different bug, so that we don’t spam people interested in the TS one.

Ok, it looks like we fixed it for TypeScript but we have the same bug when using Flow (which is the one used by React Native on .js files)

This also affects d3-array + jest + ts combo.