question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sucrase can't transpile d3-array

See original GitHub issue

Over here at @toucantoco we’re using Sucrase and we’re quite happy with it, thanks for maintaining it! However after a deps update we can’t run the tests anymore because it looks like Sucrase can’t properly transpile this file

As you can see in this Sucrase playground by checking and unchecking this typescript option, Sucrase fails when TS is on

However this is valid TS, and as you can see in this TS playground, this properly transpiles to JS, and if noImplicitAny is set to false there is no error

I don’t know why this happens when running our tests since @sucrase/jest-plugin precises that a .js will not be transpiles with the typescript option

Anyway just wanted to give you a heads up on this, I have no idea how to fix it yet but I can try if you guys are too busy

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
alangpiercecommented, Jan 1, 2022

Thanks for reporting, I’m getting back to Sucrase maintenance now and this is definitely a tricky one! Working on it now and I’m not totally sure how to fix, but I figure writing down some thoughts may help.

Babel ran into the same bug and fixed it here: https://github.com/babel/babel/pull/13581/files . Unfortunately, that fix is a rare situation where the Babel parser inspects its partially-created AST and uses it to make parsing decisions. Sucrase intentionally avoids creating an AST, so there isn’t an obvious equivalent of the fix for Sucrase.

In general, arrow functions are among the hardest things to parse in JS, and TS makes them even more complicated. If you’re reading the code token-by-token and see (a = 1, b = 2), there’s no way to know if you’re dealing with two variable assignments separated by the comma operator, or if this is the start of an arrow function with two parameters with default values. It’s only when seeing the next token after the ) that you can distinguish those two situations; for example, if you see a => token, then this must actually be an arrow function. Babel’s strategy is take the incorrectly-parsed comma operator AST and rewrite it so that it instead represents arrow parameters. Sucrase doesn’t have an AST but still needs to correctly indicate IdentifierRole for the params, so it does a restore-from-snapshot to before the ( and re-parses the expression as an arrow function. In both cases, the relevant function is parseParenAndDistinguishExpression. TypeScript syntax needs extra care because : after a parenthesized list might indicate the return type of the arrow function.

In the example failing code, Sucrase (and Babel before the bug fix) is seeing (PARAMETERS) : a => 0, which is a valid arrow expression where a is the return type. Babel’s fix is to inspect the AST for PARAMETERS, see that a += 0 isn’t actually a valid parameter declaration, and bail out of the arrow parsing case. Sucrase might need to detect that distinction up-front or have a fallback when the arrow case later doesn’t parse correctly. I’ll also take a look at how the TypeScript parser handles it; it might take an approach that’s easier to incorporate into Sucrase.

0reactions
ninofiliucommented, Jan 12, 2022

Allowing the Sucrase Jest plugin not to use the Flow transform on .js files would allow Sucrase Jest plugin users to bypass the issue

I have the bandwith to suggest a PR allowing Sucrase Jest plugin users to configure it as such

{
  "transform": {
    // uses default transforms
    "\\.js$": ["@sucrase/jest-plugin"],
    // uses custom transforms
    "\\.jsx$": ["@sucrase/jest-plugin", { "transforms": ["jsx", "imports", "jest"] }]
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Transpile src? · Issue #87 · d3/d3-array - GitHub
As of v2.0.0 d3-array is exposing non-ES5 code as part of the ES modules ... and are not transpiling their node_modules (as is...
Read more >
Must use import to load ES Module" when using D3.js 7.0.0 ...
When trying to use D3 with Next.js, I cannot get past this error when using D3.js v7 ...
Read more >
d3-array - npm
Array manipulation, ordering, searching, summarizing, etc.. Latest version: 3.2.1, last published: 22 days ago. Start using d3-array in your ...
Read more >
Open Source Used In Intersight Mobile App 1.0.275 - Cisco
This document contains licenses and notices for open source software used in this product. With respect to the free/open source software listed in...
Read more >
Sucrase-isomaltase deficiency (Concept Id: C1283620) - NCBI
People with this condition cannot break down the sugars sucrose and maltose. Sucrose (a sugar found in fruits, and also known as table...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found