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 proposal: `prefer-immediate-return`

See original GitHub issue

In my opinion it’s never desirable to assign a value to a variable, only to be returned immediately. I believe this is often the leftover result of debugging.

In other words, if a value is declared, and returned immediately in the next statement, the two should be merged, removing the useless variable.

Fail

// A
function foo() {
  const result = bar();
  return result;
}
// B
function foo() {
  let result;
  return result;
}

Pass

function foo() {
  const result = bar();
  // Anything in between is allowed.
  console.log(result);
  return result;
}
function foo() {
  let result;
  result = bar();
  return result;
}
// Autofix result for A
function foo() {
  return bar();
}
// Autofix result for B
function foo() {
  return;
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
remcohaszingcommented, Nov 2, 2021

This rule would also play nice with no-return-await. Currently an intermediate variable could be used to circumvent this rule.

async function foo() {
  const value = await bar();
  return value;
}

Becomes (because of prefer-immediate-return):

async function foo() {
  return await bar();
}

Which then becomes (because of no-return-await):

async function foo() {
  return bar();
}
2reactions
fregantecommented, Sep 19, 2021

@fisker Why not a fan? It seems reasonable. When would that code be preferable?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Adopts Amendments to Modernize Shareholder Proposal ...
The Securities and Exchange Commission today voted to adopt amendments to modernize its shareholder proposal rule, which governs the process ...
Read more >
Why Some Evaluators Prefer Written Proposals | AiDA
They want to take their 50 or 100 pages or more of text to their corners of the source selection room and read...
Read more >
Part 15 - Contracting by Negotiation | Acquisition.GOV
Extremely bulky proposals must only be returned at the offeror's ... the acquisition is not based on prices set by law or regulation; ......
Read more >
A Guide to the Rulemaking Process - Federal Register
What are interim final rules & direct final rules? ... Following the preamble, the agency usually publishes the regulatory text of the proposal...
Read more >
Summary of the SEC's Re-Proposal on the Use of Derivatives ...
The Proposal also includes new rules under the Securities Exchange Act of 1934 (the “Exchange Act”) and Investment Advisers Act of 1940 (the ......
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