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.

Rule change proposal regarding `prefer-ternary`

See original GitHub issue

Ternaries are a useful syntactic construct for const assignments. They reduce a let/if/else mess to (hopefully) a single line. They can also be misused in various ways. The current implementation of the rule always enforces the use of a ternary if both branches of an if/else expression return the same type. This is unfortunately coincidentally true for Promises and Errors. Take these two examples:

if (condition) {
  await doSomething();
} else {
  await doSomeOtherThing();
}

The rule corrects this to:

await (condition ? doSomething() : doSomeOtherThing());
if (condition) {
  throw new ThisKindOfError();
} else {
  throw new ThatKindOfError();
}

The rule corrects this to:

throw (condition ? new ThisKindOfError() : new ThatKindOfError());

Both cases I would categorize as misuse of ternaries, since they do nothing more than deduplicate a await/throw and make the code less readable.

However, I would still like the rule to catch valid cases, which mostly come in this form:

let someVariable;

if (condition) {
  someVariable = thisValue;
} else {
  someVariable = otherValue;
}

This is (correctly) corrected to:

const someVariable = condition ? thisValue : otherValue;

I propose that the rule should be configurable to only apply to assignments. This would exclude situations using await/throw/yield.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
yeldiRiumcommented, Jan 29, 2021

Regarding

const promise = condition ? doSomething() : doSomeOtherThing();
await promise;

I agree that it is better than what the rule suggests, but it is still much worse than

if (condition) {
  await doSomething();
} else {
  await doSomeOtherThing();
}

since it conceals the potential side-effects of those asynchronous operations.

I am for the suggested more options, since in our case we prefer assignments and returns to be checked. If there are no other voices on this, a single option that only checks assignments and returns would be fine in my opinion. But I can see that others might only want assignments or even only returns.

1reaction
sindresorhuscommented, Apr 21, 2021

const promise = shouldUpdate ? updateDeployment(process.env.SMART_DEPLOYMENT_ID) : createDeployment(); await promise

Yeah, I agree. It seemed nice at first but having used it in practice, I agree, an if-statement would be better here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule proposal: `prefer-ternary` · Issue #486 - GitHub
This rule could have options for whether it would be enabled for various situations (e.g. variable assignments, return statements, ...
Read more >
Order Approving a Proposed Rule Change to Amend FINRA ...
a Proposed Rule Change to Amend FINRA Rule 6750 Regarding the Publication of Aggregated. Transaction Information on U.S. Treasury Securities.
Read more >
Rule Change Proposals - NFHS
Rule Change Proposal. Directions. Only one proposal per form. Proposals must be stated clearly and concisely. Word the proposal exactly as you want...
Read more >
PR-85-2022 12/13/2022 - FDIC
WASHINGTON – The Federal Deposit Insurance Corporation (FDIC) Board of Directors today issued for public comment a proposed rule to amend ...
Read more >
Comment on Proposed Rule Changes by December 25 - TREC
Also recommended by the Unauthorized Practice of Law Working Group, proposed amendments to Section 537.11, Use of Standard Contract Forms, ...
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