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.

conditional operator not supported

See original GitHub issue

Great plugin. Thanks for writing. Are conditionals supposed to be supported?

This sample input

const countJumps = (list, index, step) => {
  const value = list.get(index);

  if (value === undefined) return step;

  return countJumps(list.set(index, value + 1), index + value, step + 1);
};

const countJumpsTernary = (list, index, step) => {
  const value = list.get(index);

  return value === undefined
    ? step
    : countJumps(list.set(index, value + 1), index + value, step + 1);
};

Produces this output

const countJumps = (list, index, step) => {
  var _repeat = true;

  var _list, _index, _step;

  while (_repeat) {
    _repeat = false;

    const value = list.get(index);

    if (value === undefined) return step;

    _list = list.set(index, value + 1);
    _index = index + value;
    _step = step + 1;
    list = _list;
    index = _index;
    step = _step;
    _repeat = true;
    continue;
  }
};

const countJumpsTernary = (list, index, step) => {
  const value = list.get(index);

  return value === undefined ? step : countJumps(list.set(index, value + 1), index + value, step + 1);
};

Is this the expected behavior? I’m using a fresh install of babel with "tailcall-optimization" as my only plugin.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
acthpcommented, Dec 18, 2017

This is awesome! Thanks!

I just tested with this:

function fact(n, acc = 1) {
	return n > 1 ? fact(n - 1, n * acc) : acc;
}

and with some nested ternary expressions, which have all worked perfectly.

I notice that if I write it as an arrow function, it doesn’t get converted:

var fact = (n, acc = 1) => n > 1 ? fact(n - 1, n * acc) : acc;

output:

var fact = function fact(n) {
  var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  return n > 1 ? fact(n - 1, n * acc) : acc;
};

while if I write an arrow function with if/else, it does get converted:

var fact = (n, acc = 1) => {
	if (n > 1) {
		return fact(n - 1, n * acc);
	}
	return acc;
};

output:

var fact = function fact(n) {
	var acc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
	var _repeat = true;

	var _n, _acc;

	while (_repeat) {
		_repeat = false;

		if (n > 1) {
			_n = n - 1;
			_acc = n * acc;
			n = _n;
			acc = _acc;
			_repeat = true;
			continue;
		}
		return acc;
	}
};

Maybe an ordering issue in the transform?

0reactions
krzkaczorcommented, Dec 18, 2017

Okay 1.0.12 is out. Please reopen this issue if it didn’t solve your problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - ternary operator not working - Stack Overflow
The statements in the ternary operator need to be non-void. They need to return something. System.out.println(direction == 0 ? 'L' : 'R');.
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 >
Nullish coalescing operator '??' - The Modern JavaScript Tutorial
The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first “defined” value of the two.
Read more >
Null-conditional operator `??` not allowed in generics unless ...
I encountered a curiosity. Given the following code: public int TheFunction (T a, T b) { T c; c = (a != null)...
Read more >
Member access operators and expressions - Microsoft Learn
Null-conditional operators ?. and ?[] · If a evaluates to null , the result of a?.x or a?[x] is null . · If...
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