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.

Change no-use-before-define default behaviour for functions

See original GitHub issue

What rule do you want to change? no-use-before-define Does this change cause the rule to produce more or fewer warnings? This change will cause fewer amount of warnings How will the change be implemented? (New option, new default behavior, etc.)? New default behaviour - disable the no-use-before-define for functions by default, because it seems like there is no reason to have it enabled. Quote from the no-use-before-define rule page itself:

functions (boolean) - The flag which shows whether or not this rule checks function declarations. … . Function declarations are hoisted, so it’s safe. Default is true.

Why is it true then? Having private functions above public makes code less readable, and defining private functions below public ones is the idiomatic way of writing code in many languages, and it’s not only my opinion, (aand another link). Please provide some example code that this change will affect:

// named export functions declared on top to make it easy
// to discover the public API exposed by the module
export function namedPublicMethod() {
  // do things
  return privateHelperFunction();
}

// less often read private utility functions declared on the bottom of the file
function privateHelperFunction() {
  // do things
}

Code was used from the similar issue here What does the rule currently do for this code? Currently this code will produce an error, because the privateHelperFunction was used before it was declared. What will the rule do after it’s changed? It will not produce the warning anymore. Are you willing to submit a pull request to implement this change? Yep

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, May 6, 2020

It isn’t always safe. For example, this would throw on f():

f();

const a = 1;

function f() {
  return a;
}

I’d personally expect from this rule to enforce “no use before define” on everything by default, which is the actual behavior.

0reactions
nzakascommented, May 28, 2020

Yeah, I don’t see us changing the default behavior on this rule, which is one of our oldest. There are plenty of options for changing how the rule behaves, including using a disable comment. If you want to create your own version, I’d suggest looking at eslint-rule-composer, which lets you run an existing rule and then filter out the messages you don’t want to see.

Thanks for the suggestion, but closing as we won’t be making this change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-use-before-define - ESLint - Pluggable JavaScript Linter
This rule will warn when it encounters a reference to an identifier that has not yet been declared. Examples of incorrect code for...
Read more >
ESLint no-use-before-define - javascript
Actually, having this rule turned on for functions doesn't make any sense - they ARE hoisted, while classes and variables are not. Like...
Read more >
no-use-before-define needs exception option for arrow ...
Description of no-use-before-define suggests ReferenceError will be thrown with any attempt to access the variable which is not the case for ...
Read more >
no-use-before-define - TypeScript ESLint
Disallow the use of variables before they are defined. Examples​. This rule extends the base eslint/no-use-before-define rule. It adds support for ...
Read more >
this - JavaScript | MDN
In most cases, the value of this is determined by how a function is called (runtime binding). It can't be set by assignment...
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