eslint-plugin-xstate
TODO add prefer-predictable, no-auto-forward + their docs
ESLint plugin to check for common mistakes and enforce good practices when using XState library.
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-async-guard": "error",
"xstate/event-names": ["warn", "macroCase"],
"xstate/state-names": ["warn", "camelCase"],
"xstate/no-inline-implementation": "warn",
"xstate/no-auto-forward": "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 v4
The default shareable configurations are for XState v5. If you use the older XState v4, append _v4
to the name of the configuration you want to use.
{
"extends": ["plugin:xstate/recommended_v4"]
}
{
"extends": ["plugin:xstate/all_v4"]
}
Supported Rules
Possible Errors
Best Practices
Stylistic Issues
Rule | Description | Recommended |
---|
event-names | Suggest consistent formatting of event names | |
state-names | Suggest consistent formatting of state names and prevent confusing names | |