-
Notifications
You must be signed in to change notification settings - Fork 26.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation for no-use-before-define rule under hoisting section #2773
Add documentation for no-use-before-define rule under hoisting section #2773
Conversation
6575d9b
to
b79b5eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please add a "why" section. It can reference and link to http://johnkpaul.com/blog/2013/02/11/what-is-hoisting-really/ and https://snek.dev/blag/2019-07_js_hoisting, for example
README.md
Outdated
@@ -1961,6 +1961,39 @@ Other Style Guides | |||
} | |||
``` | |||
|
|||
<a name="no-use-before-define"></a><a name="14.5"></a> | |||
- [14.5](#no-use-before-define) Variables, classes and functions should be defined before they can be used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- [14.5](#no-use-before-define) Variables, classes and functions should be defined before they can be used. | |
- [14.5](#no-use-before-define) Variables, classes, and functions should be defined before they can be used. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Hey @ljharb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add examples of let
and const
(which are both hoisted) that shows the TDZ in operation.
README.md
Outdated
<a name="no-use-before-define"></a> | ||
- [14.5](#no-use-before-define) Variables, classes, and functions should be defined before they can be used. eslint: [`no-use-before-define`](https://eslint.org/docs/latest/rules/no-use-before-define) | ||
|
||
> Why? When variables, classes, or functions are declared before being used, it can lead to unexpected errors. As they are hoisted, it can also slow down the application as they are read before any code is executed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> Why? When variables, classes, or functions are declared before being used, it can lead to unexpected errors. As they are hoisted, it can also slow down the application as they are read before any code is executed. | |
> Why? When variables, classes, or functions are declared before being used, it can harm readability since a reader won't know what a thing that's referenced _is_. It's much clearer for a reader to first encounter the source of a thing (whether imported from another module, or defined in the file) before encountering a use of the thing. |
the latter isn't true, since the hoisting pass happens anyways. maybe this:
40d3e3d
to
7916d6f
Compare
Hey @ljharb
I have submitted a PR for the issue #2708
Could you please review this?
Fixes #2708.