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
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
- Local DataTable support (#691) - Use babel plugins to disable codesplitting in `.js` files (Doesn't affect our own codesplitting in`.ts(x)` files) - Convert babel config to js instead of json to fix... — committed to datapane/datapane by Buroni 3 years ago
- Hard-code d3-array 2.3.3 for Babel compat Summary: Got this in my dep bump: https://www.internalfb.com/intern/sandcastle/job/13510799320314417/ Babel is having some trouble with a certain syntax in ... — committed to facebook/flipper by passy 3 years ago
@pelly You need to check your
yarn.lock
and manually merge the duplicate elements that you’ve probably got afteryarn add d3-array@2.3.3
:A safer approach would be to add a static resolution to
package.json
:and re-run
yarn
.I had same problem. I’m wondering if ignoring cumsum.js could be a workaround.
Notes:
Released in
@babel/parser
7.14.9My 2 cts observation: it seems the minimal reproduction in REPL can be fixed by changing
to
Similarly, the code in d3-array compiles if we wrap the second arrow function with parentheses:
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 changedcumsum.js
to this: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.