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.

Fails to parse object division in ternary alternate: `weird ? true : {}/2`

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior Fails to parse two useless but valid pieces of JavaScript.

Input Code

Case 1:

weird ? true : {}/2;
Unterminated regular expression (1:18)

Case 2:

weird ? true : {}/2/g;
Unexpected token, expected ";" (1:17)

Note: When adding parentheses parsing succeeds:

weird ? true : ({}/2);

But Babel’s output is:

weird ? true : {} / 2;

Which means that Babel cannot parse its output.

Moving {}/2 to the consequent of the ternary parses:

weird ? {}/2 : true;

Expected behavior/code They should parse. For example Node.js, Firefox and TypeScript accept them.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc) Defaults.

Environment

  System:
    OS: Linux 5.3 Ubuntu 19.10 (Eoan Ermine)
  Binaries:
    Node: 13.11.0 - /home/linuxbrew/.linuxbrew/bin/node
    Yarn: 1.22.4 - /home/linuxbrew/.linuxbrew/bin/yarn
    npm: 6.13.7 - /home/linuxbrew/.linuxbrew/bin/npm
  npmPackages:
    @babel/parser: 7.9.4 => 7.9.4 
    eslint: 6.8.0 => 6.8.0 
    jest: 25.2.3 => 25.2.3 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
oliverdunkcommented, Jun 29, 2020

Just wanted to say that I haven’t forgotten about this one. https://github.com/babel/babel/pull/11404 has been taking my focus, but I hope to tackle this soon.

1reaction
oliverdunkcommented, Apr 8, 2020

Could I take this one? ✋

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ternary operator parse error (TS 2.5) #18204 - GitHub
The workaround is to not parenthesize the true branch of the ternary, because this makes the parser think that it could be looking...
Read more >
Alternative to nested ternary operator in JS - Stack Overflow
The question was how to express the above logic without nested ternary operators or an if/else chain. The option of a switch was...
Read more >
Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >
Programming FAQ — Python 3.11.1 documentation
How can my code discover the name of an object? What's up with the comma operator's precedence? Is there an equivalent of C's...
Read more >
Essentials - Julia Documentation
Compute the amount of memory, in bytes, used by all unique objects ... i = 0 0 julia> while true global i +=...
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