Code Monkey home page Code Monkey logo

eslint-plugin-no-unnecessary-hook-definition's Introduction

eslint-plugin-no-unnecessary-hook-definition — Formidable, We build the modern web

eslint-plugin-no-unnecessary-hook-definition

This library contains a simple ESLint plugin with a single rule to disallow defining React "hooks" that perhaps shouldn't be considered "hooks". This library enforces the following rule:

A function should only be considered a React "hook" (e.g., its name starts with use) if it calls another hook.

This rule ensures your codebase is not littered with "fake hooks" like the following:

// ❌ This will get flagged
const useFirstName = (person) => {
  return person.firstName;
};

since this function, as an example, has no reliance on React or its hook ecosystem and is probably best declared as a plain 'ol function like getFirstName.

Setup & Usage

Start by installing the plugin:

npm install -D eslint-plugin-no-unnecessary-hook-definition

# Or...

yarn add -D eslint-plugin-no-unnecessary-hook-definition

Then add the plugin to your .eslintrc configuration file in both the plugins and rules fields:

{
  "plugins": ["no-unnecessary-hook-definition", "..."],
  "rules": {
    "no-unnecessary-hook-definition/rule": 2
  }
}

That's it! Happy coding!

eslint-plugin-no-unnecessary-hook-definition's People

Contributors

cpresler avatar gksander avatar paulmarsicloud avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-plugin-no-unnecessary-hook-definition's Issues

Lint/TypeCheck/Test in CI

Add appropriate Lint, TypeCheck, and Test scripts and run them in CI. PRs should be gated by these checks.

Invalid warning

With the code below I get a warning about not using any hooks in the hook.

import { useContext, useEffect } from "react";
import { useInView } from "react-intersection-observer";
import ScrollSpyContext from "./context";

const useScrollSpy = (
	id: string
): ((node?: Element | null | undefined) => void) => {
	const { register, unRegister, setInView } = useContext(ScrollSpyContext);

	const { ref, inView } = useInView({ rootMargin: "0px 0px 0px 0px " });

	useEffect(() => {
		register(id);

		return () => {
			unRegister(id);
		};
	}, [id, register, unRegister]);

	useEffect(() => {
		setInView(id, inView);
	}, [id, inView, setInView]);

	return ref;
};

export default useScrollSpy;

Ditch `eslint-traverse`

The plugin currently uses eslint-traverse for some sub-traversal. It'd be great if this plugin was dependency-free, and our use of eslint-traverse is pretty shallow – so we should look into hand-rolling the specific functionality we need from eslint-traverse.

Add accompanying docs

We need to add the following files:

  • LICENSE
  • CONTRIBUTING file
  • Pull request template

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.