3.2.1 • Published 5 months ago

eslint-plugin-xstate v3.2.1

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

eslint-plugin-xstate

ESLint plugin to check for common mistakes and enforce good practices when using XState library.

npm version build status code style: prettier PRs Welcome

Installation

You'll first need to install ESLint:

$ npm i eslint --save-dev

Next, install eslint-plugin-xstate:

$ npm install eslint-plugin-xstate --save-dev

Usage

Add xstate to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["xstate"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "xstate/spawn-usage": "error",
    "xstate/no-infinite-loop": "error",
    "xstate/no-imperative-action": "error",
    "xstate/no-ondone-outside-compound-state": "error",
    "xstate/invoke-usage": "error",
    "xstate/entry-exit-action": "error",
    "xstate/prefer-always": "error",
    "xstate/prefer-predictable-action-arguments": "error",
    "xstate/no-misplaced-on-transition": "error",
    "xstate/no-invalid-transition-props": "error",
    "xstate/no-invalid-state-props": "error",
    "xstate/no-invalid-conditional-action": "error",
    "xstate/no-async-guard": "error",
    "xstate/event-names": ["warn", "macroCase"],
    "xstate/state-names": ["warn", "camelCase"],
    "xstate/no-inline-implementation": "warn",
    "xstate/no-auto-forward": "warn",
    "xstate/system-id": "warn"
  }
}

Shareable Configurations

This plugin exports a recommended configuration which checks for common mistakes. To enable this configuration use the extends property in your .eslintrc.js config file:

{
  "extends": ["plugin:xstate/recommended"]
}

There is also an all configuration which includes every available rule. It enforces both correct usage and best XState practices.

{
  "extends": ["plugin:xstate/all"]
}

XState Version

The default shareable configurations are for XState v5. If you use XState version 4, append _v4 to the name of the configuration you want to use.

{
  "extends": ["plugin:xstate/recommended_v4"]
}
{
  "extends": ["plugin:xstate/all_v4"]
}

If you do not use shareable configs, you need to manually specify the XState version in the ESLint config (defaults to 5):

{
  "settings": {
    "xstate": {
      "version": 4
    }
  }
}

Supported Rules

Possible Errors

RuleDescriptionRecommended
spawn-usageEnforce correct usage of spawn. Only for XState v4!:heavy_check_mark:
no-infinite-loopDetect infinite loops with eventless transitions:heavy_check_mark:
no-imperative-actionForbid using action creators imperatively:heavy_check_mark:
no-ondone-outside-compound-stateForbid onDone transitions on atomic, history and final nodes:heavy_check_mark:
invoke-usageEnforce correct invocation of services:heavy_check_mark:
entry-exit-actionForbid invalid declarations of entry/exit actions:heavy_check_mark:
no-misplaced-on-transitionForbid invalid declarations of on transitions:heavy_check_mark:
no-invalid-transition-propsForbid invalid properties in transition declarations:heavy_check_mark:
no-invalid-state-propsForbid invalid properties in state node declarations:heavy_check_mark:
no-async-guardForbid asynchronous guard functions:heavy_check_mark:
no-invalid-conditional-actionForbid invalid declarations inside the choose action creator:heavy_check_mark:

Best Practices

RuleDescriptionRecommended
no-inline-implementationSuggest refactoring guards, actions and services into machine options
prefer-alwaysSuggest using the always syntax for transient (eventless) transitions:heavy_check_mark:
prefer-predictable-action-argumentsSuggest turning on the predictableActionArguments option:heavy_check_mark:
no-auto-forwardForbid auto-forwarding events to invoked services or spawned actors
system-idSuggest using systemId for invoked or spawned actors

Stylistic Issues

RuleDescriptionRecommended
event-namesSuggest consistent formatting of event names
state-namesSuggest consistent formatting of state names and prevent confusing names

Comment Directives

By default, the plugin lints only code within the createMachine or Machine calls. However, if your machine configuration is imported from another file, you will need to enable this plugin's rules by adding a comment directive to the top of the file:

/* eslint-plugin-xstate-include */
// 💡 This machine config will now be linted too.
export const machine = {
  initial: 'active',
  context: {},
  // etc
}
3.2.1

5 months ago

3.2.0

7 months ago

1.1.3

7 months ago

3.1.0

7 months ago

3.0.1

7 months ago

3.0.0

7 months ago

2.2.0

8 months ago

2.1.0

9 months ago

2.0.0

9 months ago

1.1.1

9 months ago

1.1.2

9 months ago

1.1.0

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.16.0-beta.1

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.1

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.13.1

3 years ago

0.13.0

3 years ago

0.12.2

3 years ago

0.12.1

3 years ago

0.1.0-beta.1

3 years ago

0.10.0

3 years ago

0.1.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.11.0

3 years ago

0.10.1

3 years ago

0.9.0

3 years ago

0.12.0

3 years ago

0.8.0

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago