npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nossbigg/eslint-plugin-clean-code-react

v0.0.4

Published

ESLint rules to keep your React components squeaky clean ✨🧼

Downloads

3

Readme

@nossbigg/eslint-plugin-clean-code-react

ESLint rules to keep your React components squeaky clean ✨🧼

Rules

Note: All rule names are prefixed with @nossbigg/clean-code-react/:

  • eg. To use component-prop-typedef-name, the full rule name is @nossbigg/clean-code-react/component-prop-typedef-name

component:

Rules that apply to both Function and Class React Components

| Rule | Description | | ------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- | | component-prop-typedef-name | React Component typedef name contains 'Props'. | | max-component-lines | React Component declarations do not exceed max lines. | | max-component-nested-fn-lines | Nested functions within React Component does not exceed max lines. | | max-component-nested-fns | React Component does not exceed max number of nested functions. | | no-component-prop-inline-typedef | React Component typedefs do not contain inline typedefs. | | no-external-fn-definition-in-large-component-file | No external function definitions exist in the same file when a large component exists. | | no-other-component-in-large-component-file | No other components exist in the same file when a large component exists. | | no-styled-components-def-in-large-component-file | No styled components are declared in large component file. | | no-typedefs-in-large-component-file | Typedefs are not declared in the same file when a large component exists. |

jsx:

Rules that apply to JSX Elements

| Rule | Description | | -------------------------------------------------------------------------- | ------------------------------------------------------- | | max-jsx-expression-lines | JSX Expression blocks do not exceed max lines. | | max-jsx-lines | JSX Element blocks do not exceed max lines. | | max-jsx-prop-value-lines | JSX Element prop value does not exceed max lines. | | max-jsx-props | JSX Element does not exceed max number of props. | | max-jsx-spread-props | JSX Element does not exceed max number of spread props. | | no-jsx-complex-spread-prop | JSX Element does not contain complex spread props. | | no-jsx-inline-style-prop | JSX Element does not contain inline style prop. |

fn-component:

Rules that apply to Function React Component

| Rule | Description | | ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- | | max-fn-component-hooks | React Function Component does not use more than max React Hooks. | | no-fn-component-props-inline-destructure | No function component props inline destructure. |

class-component:

Rules that apply to Class React Component

| Rule | Description | | ---------------------------------------------------------------------------------- | --------------------------------------------- | | no-unnecessary-class-component | Unnecessary class components are not allowed. |

hooks:

Rules that apply to React Hooks

| Rule | Description | | ---------------------------------------------------------------------- | -------------------------------------------------- | | max-hooks-in-custom-hook | Custom React Hooks do not use more than max hooks. |

typedefs:

Rules that apply to typedefs with 'Props' | Rule | Description | | ---------------------------------------------------------------------- | -------------------------------------------------- | | max-properties-in-component-prop | Component prop typedef does not exceed max properties. | | no-nested-type-literal-in-component-prop | Component prop typedef does not contain nested typedef. |

Rule-level settings

This plugin allows for rule-level settings:

// .eslintrc.js
{
  "settings": {
    "@nossbigg/eslint-plugin-clean-code-react": {
      "jsCompatMode": false,
      "largeComponentLength": 50
    }
  }
}

Available settings:

  1. jsCompatMode

Purpose: Allows detection of React Function Component by name.

  • Example Function Component names: MyComponent, Table
  • Useful for non-TypeScript codebases, or codebases without a standardized type annotation convention.

Used by:

All rules in the following categories:

  • component
  • fn-component

Value: boolean, default: false

Note: May capture false positives (eg. SomeUtilMethod()).

2.largeComponentLength

Purpose: Determines the threshold for a large react component.

Used by:

  • no-external-fn-definition-in-large-component-file
  • no-other-component-in-large-component-file
  • no-styled-components-def-in-large-component-file
  • no-typedefs-in-large-component-file

Value: number, default: 50

Note: When largeComponentLength is defined in multiple places, the precedence order is as follows:

  • rule config > plugin setting config > default value

Supported Syntax

Supported Syntax:

// Class Component
class MyClassComponent extends React.Component {
  render() {
    return <></>;
  }
}

// === With type definition ===

// Function Component using arrow function
const MyFunctionComponent: React.FunctionComponent = () => <></>;
// Function Component using function expression
const MyFunctionComponent: React.FunctionComponent = function() => <></>;

// === Without type definition ===
// note: requires jsCompatMode = true

// Function Component using arrow function
const MyFunctionComponent = () => <></>;
// Function Component using function expression
const MyFunctionComponent = function() => <></>;
// Function Component using function declaration
function MyFunctionComponent() => <></>;

Supported Function Component types:

  • FunctionComponent / FC / React.FunctionComponent / React.FC
  • StatelessComponent / SFC / React.StatelessComponent / React.SFC
  • VoidFunctionComponent / VFC / React.VoidFunctionComponent / React.VFC

Supported Class Component types:

  • Component / React.Component
  • PureComponent / React.PureComponent

Unsupported Syntax:

// function component generator
const makeMyFunctionComponent = (): React.FunctionComponent => () => <></>;

Tip: Ensuring all function components are declared using arrow function.

You can use eslint-plugin-react's react/function-component-definition to ensure that all function components are declared using arrow functions.

// .eslintrc.js
{
  "rules": {
    "react/function-component-definition": [
      "error",
      {
        "namedComponents": "arrow-function",
        "unnamedComponents": "arrow-function"
      }
    ]
  }
}

Installation

Prerequisites:

Steps:

  1. Add lib as dev dependency
yarn add -D @nossbigg/eslint-plugin-clean-code-react
  1. Use plugin + rules
// .eslintrc.js
{
  // use plugin + recommended rules
  "plugins": ["@nossbigg/clean-code-react"],
  "extends": ["plugin:@nossbigg/clean-code-react/recommended"],

  // define specific rules
  "rules": {
    // without rule config
    "@nossbigg/clean-code-react/max-component-lines": "error",
    // with rule config
    "@nossbigg/clean-code-react/max-jsx-lines": ["error", { "maxJsxLines": 31 }]
  }
}