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 additional messaging for RulesOfHooks lint error #20692

Merged
merged 4 commits into from Feb 23, 2021

Conversation

@callmetwan
Copy link
Contributor

@callmetwan callmetwan commented Jan 29, 2021

Summary

The ESLint rule eslint-plugin-react-hooks enforces a naming convention on hooks; they must all start with use. This is in line with the documentation around writing custom hooks. This is all good, works as intended.

The error message you get does not tell you this though. Example:

const doesNotStartWithUse = () => {
  useEffect(() => {}, []);
};

Resulting message:

ESLint: React Hook "useEffect" is called in function "doesNotStartWithUse" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter.(react-hooks/rules-of-hooks)

It is not clear from the error that the issue is the name. This PR simply adds more information to the error reported.

Test Plan

Create a new React project.

Create a custom hook that does not begin with the word use:

const doesNotStartWithUse = () => {
  useEffect(() => {}, []);
};

Run yarn lint, yarn linc, or view IDE reported lint warnings.

The message should be:

Lint: React Hook "useEffect" is called in function "doesNotStartWithUse" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hooks should start with the word use.

Emphasis added for my addition.

@codesandbox
Copy link

@codesandbox codesandbox bot commented Jan 29, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ec4b81d:

Sandbox Source
React Configuration
@sizebot
Copy link

@sizebot sizebot commented Jan 29, 2021

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against ec4b81d

@sizebot
Copy link

@sizebot sizebot commented Jan 29, 2021

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against ec4b81d

@rickhanlonii
Copy link
Member

@rickhanlonii rickhanlonii commented Feb 1, 2021

Hey @callmetwan, thanks for the suggestion. Looks like tests are failing and the whitespace is off.

This seems like an improvement to me, but I defer to @gaearon.

@callmetwan
Copy link
Contributor Author

@callmetwan callmetwan commented Feb 13, 2021

@rickhanlonii Sorry about the delay on this. Fixed the whitespace issue and updated tests.

@callmetwan
Copy link
Contributor Author

@callmetwan callmetwan commented Feb 22, 2021

@rickhanlonii Just wanted to follow up on this.

Copy link
Member

@rickhanlonii rickhanlonii left a comment

LGTM!

@rickhanlonii rickhanlonii merged commit c62986c into facebook:master Feb 23, 2021
36 checks passed
36 checks passed
Facebook CLA Check Contributor License Agreement is valid!
Details
ci/circleci: RELEASE_CHANNEL_stable_yarn_build Your tests passed on CircleCI!
Details
ci/circleci: RELEASE_CHANNEL_stable_yarn_lint_build Your tests passed on CircleCI!
Details
ci/circleci: RELEASE_CHANNEL_stable_yarn_test_dom_fixtures Your tests passed on CircleCI!
Details
ci/circleci: build_devtools_and_process_artifacts Your tests passed on CircleCI!
Details
ci/circleci: build_devtools_scheduling_profiler Your tests passed on CircleCI!
Details
ci/circleci: process_artifacts Your tests passed on CircleCI!
Details
ci/circleci: process_artifacts_combined Your tests passed on CircleCI!
Details
ci/circleci: process_artifacts_experimental Your tests passed on CircleCI!
Details
ci/circleci: setup Your tests passed on CircleCI!
Details
ci/circleci: sizebot_experimental Your tests passed on CircleCI!
Details
ci/circleci: sizebot_stable Your tests passed on CircleCI!
Details
ci/circleci: yarn_build Your tests passed on CircleCI!
Details
ci/circleci: yarn_build_combined Your tests passed on CircleCI!
Details
ci/circleci: yarn_flow Your tests passed on CircleCI!
Details
ci/circleci: yarn_lint Your tests passed on CircleCI!
Details
ci/circleci: yarn_lint_build Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=experimental --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=experimental --env=production Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=stable --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=stable --env=development --persistent Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=stable --env=production Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-classic --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-classic --env=development --variant Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-classic --env=production Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-classic --env=production --variant Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-modern --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-modern --env=development --variant Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-modern --env=production Your tests passed on CircleCI!
Details
ci/circleci: yarn_test--r=www-modern --env=production --variant Your tests passed on CircleCI!
Details
ci/circleci: yarn_test_build---project=devtools -r=experimental Your tests passed on CircleCI!
Details
ci/circleci: yarn_test_build--r=experimental --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test_build--r=experimental --env=production Your tests passed on CircleCI!
Details
ci/circleci: yarn_test_build--r=stable --env=development Your tests passed on CircleCI!
Details
ci/circleci: yarn_test_build--r=stable --env=production Your tests passed on CircleCI!
Details
ci/codesandbox Building packages succeeded.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants