-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(state-names): add doc for state-names rule
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Suggest consistent formatting of state names | ||
|
||
Suggest using state names formatted with the preconfigured style (camelCase, snake_case, PascalCase or regex). | ||
|
||
# Rule Details | ||
|
||
While the XState library neither enforces nor recommends any particular format for state node names, maintaining a consistent formatting of state names helps readability. Four styles to choose from are available: | ||
|
||
- camelCase [*default*] | ||
- snake_case | ||
- PascalCase | ||
- regular expression | ||
|
||
The default camelCase for state names is used by the official XState documentation. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```javascript | ||
// ❌ state names not in camelCase | ||
/* eslint event-names: [ "warn", "camelCase" ] */ | ||
createMachine({ | ||
states: { | ||
PowerOn: {}, | ||
power_on: {}, | ||
'power:on': {}, | ||
'power.on': {}, | ||
}, | ||
}) | ||
|
||
// ❌ state names violates the given regex | ||
/* eslint event-names: [ "warn", "regex", { "regex": "^\\w+:\\w+$" } ] */ | ||
createMachine({ | ||
states: { | ||
PowerOn: {}, | ||
power_on: {}, | ||
}, | ||
}) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```javascript | ||
// ✅ | ||
/* eslint event-names: [ "warn", "camelCase" ] */ | ||
createMachine({ | ||
states: { | ||
powerOn: {}, | ||
powerOff: {}, | ||
}, | ||
}) | ||
|
||
// ✅ | ||
/* eslint event-names: [ "warn", "regex", { "regex": "^\\w+:\\w+$" } ] */ | ||
createMachine({ | ||
states: { | ||
'power:on': {}, | ||
'mode:1': {}, | ||
}, | ||
}) | ||
``` | ||
|
||
## Options | ||
|
||
| Option | Required | Default | Details | | ||
| -------- | -------- | ----------- | ----------------------------------------------------------------------------------------------- | | ||
| [string] | No | `camelCase` | Selects one of the available formatting styles: `camelCase`, `snakeCase`, `pascalCase`, `regex` | | ||
| [object] | No | `undefined` | The second option is an object with properties: `regex` (string) | | ||
|
||
## Example | ||
|
||
```json | ||
{ | ||
"xstate/state-names": ["warn", "PascalCase"] | ||
} | ||
|
||
{ | ||
"xstate/state-names": ["warn", "regex", { "regex": "^[a-z]+$" }] | ||
} | ||
``` |