3.4.3 • Published 3 months ago

@reatom/eslint-plugin v3.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@reatom/eslint-plugin

Reatom-specific ESLint rules.

Installation

npm i -D @reatom/eslint-plugin

Usage

Add @reatom to plugins and specify extends or rules in your config.

{
  "plugins": ["@reatom"],
  "extends": ["plugin:@reatom/recommended"]
}
{
  "plugins": ["@reatom"],
  "rules": {
    "@reatom/async-rule": "error",
    "@reatom/unit-naming-rule": "error"
  }
}

Here is an example of React + TypeScript + Prettier config with Reatom.

ESLint setup commit

{
  "env": {
    "browser": true,
    "es2022": true
  },
  "extends": [
    "plugin:react/recommended",
    "plugin:react/jsx-runtime",
    "standard-with-typescript",
    "plugin:@reatom/recommended",
    "plugin:prettier/recommended"
  ],
  "overrides": [],
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module",
    "project": ["tsconfig.json"]
  },
  "plugins": ["react", "@reatom", "prettier"],
  "rules": {
    "@typescript-eslint/explicit-function-return-type": "off",
    "prettier/prettier": "error"
  },
  "settings": {
    "atomPostfix": "Atom"
  }
}

Rules

unit-naming-rule

Ensures that all Reatom entities specify the name parameter used for debugging. We assume that Reatom entity factories are atom, action and all reatom* (like reatomAsync) functions imported from @reatom/* packages.

The name must be equal to the name of a variable or a property an entity is assigned to, like this:

const count = atom(0, 'count')

const someNamespace = {
  count: atom(0, 'count'),
}

When creating atoms dynamically with factories, you can also specify the "namespace" of the name before the . symbol:

const reatomUser = (_name: string) => {
  const name = atom(_name, 'reatomUser.name')

  return { name }
}

For private atoms, _ prefix can be used:

const secretState = atom(0, '_secretState')

You can also ensure that atom names have a prefix or a postfix through the configuration, for example:

{
  atomPrefix: '',
  atomPostfix: 'Atom',
}

async-rule

Ensures that asynchronous interactions within Reatom functions are wrapped with ctx.schedule. Read the docs for more info.

Motivation

The primary purpose of this plugin is to automate generation of atom and action names using ESLint autofixes. Many have asked why not make a Babel plugin for naming, why keep it in source, here is our opinion:

  • Build infrastructure is diverse and divergent - it's hard to support a plenty of tools used today;
  • Plugin's output may be unexpected;
  • Such plugin is hard to implement because of variety of naming strategies, especially in factories;

These are the problems we faced back in 2019 when the first version of Reatom was released. They made it clear for us that the game is not worth the candle.

On the contrary, explicit unit names have multiple advantages:

  • No risk of unexpected plugin behaviour, full control over unit names;
  • Requires no build infrastructure and is not that hard to do;
  • Writing names is simplified even further by AI coding helpers (i.e. Copilot) and this set of ESLint rules.
3.6.0

3 months ago

3.5.0

3 months ago

3.4.0

10 months ago

3.4.3

7 months ago

3.4.2

7 months ago

3.4.1

8 months ago

3.3.0

1 year ago

3.1.3

1 year ago

3.1.2

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.2.0

1 year ago