Skip to content

Commit

Permalink
Merge branch 'next' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io authored Jul 16, 2024
2 parents 2256ce2 + 1b29cdb commit 561e157
Show file tree
Hide file tree
Showing 16 changed files with 1,793 additions and 131 deletions.
8 changes: 0 additions & 8 deletions configs/recommended-alphabetical.ts

This file was deleted.

8 changes: 0 additions & 8 deletions configs/recommended-line-length.ts

This file was deleted.

8 changes: 0 additions & 8 deletions configs/recommended-natural.ts

This file was deleted.

6 changes: 3 additions & 3 deletions docs/content/configs/recommended-alphabetical.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
{
source: dedent`
// eslint.config.js
import perfectionistAlphabetical from 'eslint-plugin-perfectionist/configs/recommended-alphabetical'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistAlphabetical,
perfectionist.configs['recommended-alphabetical'],
]
`,
name: 'Flat Config',
Expand All @@ -41,7 +41,7 @@ It makes it just a tiny bit faster to find a declaration in a large list. Rememb
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-alphabetical',
'plugin:perfectionist/recommended-alphabetical-legacy',
],
}
`,
Expand Down
6 changes: 3 additions & 3 deletions docs/content/configs/recommended-line-length.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ This configuration will make your code prettier and more pleasing to the eye.
{
source: dedent`
// eslint.config.js
import perfectionistLineLength from 'eslint-plugin-perfectionist/configs/recommended-line-length'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistLineLength,
perfectionist.configs['recommended-line-length'],
]
`,
name: 'Flat Config',
Expand All @@ -50,7 +50,7 @@ This configuration will make your code prettier and more pleasing to the eye.
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-line-length',
'plugin:perfectionist/recommended-line-length-legacy',
],
}
`,
Expand Down
6 changes: 3 additions & 3 deletions docs/content/configs/recommended-natural.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ This configuration will allow you to navigate through your code faster because a
{
source: dedent`
// eslint.config.js
import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionistNatural,
perfectionist.configs['recommended-natural'],
]
`,
name: 'Flat Config',
Expand All @@ -48,7 +48,7 @@ This configuration will allow you to navigate through your code faster because a
// .eslintrc.js
export default {
extends: [
'plugin:perfectionist/recommended-natural',
'plugin:perfectionist/recommended-natural-legacy',
],
}
`,
Expand Down
242 changes: 242 additions & 0 deletions docs/content/rules/sort-switch-case.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
---
title: sort-switch-case
description: Ensure consistent and readable switch statements with the sort-switch-case ESLint rule. Automatically sort case clauses within switch statements to improve code clarity and maintainability
shortDescription: Enforce sorted switch case statements
keywords:
- eslint
- sort switch case
- eslint rule
- coding standards
- code quality
- javascript linting
- switch statements
- case sorting
- switch case order
---

import CodeExample from '../../components/CodeExample.svelte'
import Important from '../../components/Important.astro'
import CodeTabs from '../../components/CodeTabs.svelte'
import { dedent } from 'ts-dedent'

Enforce sorted switch case statements.

Switch statements with numerous cases can quickly become cumbersome and hard to navigate. With this rule, you can easily locate specific cases and ensure that your codebase adheres to a predictable and standardized format.

This practice contributes to a more readable and maintainable codebase, allowing developers to quickly understand and modify the logic without getting lost in a jumble of unsorted case clauses.

By integrating this rule into your ESLint configuration, you can focus on the functionality of your code, confident that your switch statements are consistently structured and easy to manage.

## Try it out

<CodeExample
alphabetical={dedent`
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'ADD_USER':
return {
...state,
users: [...state.users, action.payload],
}
case 'DELETE_USER':
return {
...state,
users: state.users.filter(user => user.id !== action.payload.id),
}
case 'FETCH_USER_ERROR':
return {
...state,
loading: false,
error: action.payload,
}
case 'FETCH_USER_REQUEST':
return {
...state,
loading: true,
error: null,
}
case 'FETCH_USER_SUCCESS':
return {
...state,
loading: false,
currentUser: action.payload,
}
default:
return state
}
}
`}
lineLength={dedent`
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'FETCH_USER_REQUEST':
return {
...state,
loading: true,
error: null,
}
case 'FETCH_USER_SUCCESS':
return {
...state,
loading: false,
currentUser: action.payload,
}
case 'FETCH_USER_ERROR':
return {
...state,
loading: false,
error: action.payload,
}
case 'DELETE_USER':
return {
...state,
users: state.users.filter(user => user.id !== action.payload.id),
}
case 'ADD_USER':
return {
...state,
users: [...state.users, action.payload],
}
default:
return state
}
}
`}
initial={dedent`
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'FETCH_USER_ERROR':
return {
...state,
loading: false,
error: action.payload,
}
case 'FETCH_USER_SUCCESS':
return {
...state,
loading: false,
currentUser: action.payload,
}
case 'DELETE_USER':
return {
...state,
users: state.users.filter(user => user.id !== action.payload.id),
}
case 'FETCH_USER_REQUEST':
return {
...state,
loading: true,
error: null,
}
case 'ADD_USER':
return {
...state,
users: [...state.users, action.payload],
}
default:
return state
}
}
`}
client:load
lang="tsx"
/>

## Options

This rule accepts an options object with the following properties:

```ts
interface Options {
type?: 'alphabetical' | 'natural' | 'line-length'
order?: 'asc' | 'desc'
ignoreCase?: boolean
}
```

### type

<sub>(default: `'alphabetical'`)</sub>

- `alphabetical` — sort alphabetically.
- `natural` — sort in natural order.
- `line-length` — sort by code line length.

### order

<sub>(default: `'asc'`)</sub>

- `asc` — enforce properties to be in ascending order.
- `desc` — enforce properties to be in descending order.

### ignoreCase

<sub>(default: `false`)</sub>

Only affects alphabetical and natural sorting. When `true` the rule ignores the case-sensitivity of the order.

## Usage

<CodeTabs
code={[
{
source: dedent`
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-switch-case': [
'error',
{
type: 'natural',
order: 'asc',
},
],
},
},
]
`,
name: 'Flat Config',
value: 'flat',
},
{
source: dedent`
// .eslintrc.js
export default {
plugins: [
'perfectionist',
],
rules: {
'perfectionist/sort-switch-case': [
'error',
{
type: 'natural',
order: 'asc',
},
],
},
}
`,
name: 'Legacy Config',
value: 'legacy',
},
]}
type="config-type"
client:load
lang="ts"
/>

## Version

This rule was introduced in [v0.2.0](https://github.com/azat-io/eslint-plugin-perfectionist/releases/tag/v3.0.0).

## Resources

- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-switch-case.ts)
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-switch-case.test.ts)

Binary file modified docs/public/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 561e157

Please sign in to comment.