Skip to content
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

Conversation

ujwalkumar1995
Copy link

@ujwalkumar1995 ujwalkumar1995 commented May 20, 2023

Hey @ljharb
I have submitted a PR for the issue #2708
Could you please review this?

Fixes #2708.

@ujwalkumar1995 ujwalkumar1995 force-pushed the issue-2708-documentation-for-no-use-before-define branch from 6575d9b to b79b5eb Compare May 20, 2023 11:12
Copy link
Collaborator

@ljharb ljharb left a 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 Show resolved Hide resolved
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [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.

README.md Outdated Show resolved Hide resolved
@ujwalkumar1995
Copy link
Author

Hey @ljharb
I have added the why section. I have also added a reference link to the official eslint docs for reference. Please review.

Copy link
Collaborator

@ljharb ljharb left a 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> 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:

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@airbnb airbnb deleted a comment from Viyom29 May 26, 2023
@ljharb ljharb force-pushed the issue-2708-documentation-for-no-use-before-define branch 2 times, most recently from 40d3e3d to 7916d6f Compare May 30, 2023 20:22
@ljharb ljharb merged commit 7916d6f into airbnb:master May 30, 2023
@Viyom29 Viyom29 mentioned this pull request Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation for 'no-use-before-define' is missing
3 participants