Skip to content

Revamp plugin logic RE #26 #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'eslint-plugin', 'import', 'jest', 'prettier'],
env: {
Expand All @@ -8,13 +7,12 @@ module.exports = {
},
extends: [
'eslint:recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:import/recommended',
'plugin:eslint-plugin/all',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 10,
ecmaVersion: '2024',
sourceType: 'module',
},
rules: {
Expand All @@ -28,7 +26,7 @@ module.exports = {
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},

Expand All @@ -52,17 +50,15 @@ module.exports = {
'jest/prefer-to-have-length': 'warn',
'jest/prefer-spy-on': 'error',
'jest/valid-expect': 'error',
'jest/no-test-callback': 'off',
'jest/no-done-callback': 'off',
},
},
],
},
],
settings: {
'import/resolver': {
node: {
moduleDirectory: ['node_modules', 'src'],
},
typescript: true,
},
},
}
7 changes: 5 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 18
node-version: 20
- run: yarn
- run: yarn verify
env:
MINIFY: true

publish-npm:
needs: build
Expand All @@ -25,9 +27,10 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 18
node-version: 20
registry-url: https://registry.npmjs.org/
- run: yarn
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
MINIFY: true
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 18.x, 20.x]
node-version: [18.x, 20.x, 21.x]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
5 changes: 0 additions & 5 deletions .huskyrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
trailingComma: 'all',
arrowParens: 'avoid',
semi: false,
singleQuote: true,
}
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

# eslint-plugin-typescript-sort-keys

Sort interface and string enum keys

Inspired by and sourced from [eslint/sort-keys]([https://github.com/eslint/eslint/blob/master/docs/rules/sort-keys.md](https://github.com/eslint/eslint/blob/main/docs/src/rules/sort-keys.md))
Sort interface and enum keys. Inspired by and sourced from [eslint/sort-keys](<[https://github.com/eslint/eslint/blob/master/docs/rules/sort-keys.md](https://github.com/eslint/eslint/blob/main/docs/src/rules/sort-keys.md)>)

## Installation

You'll first need to install
This plugin is supported on **Node 18+**. You'll first need to install:

- [eslint](http://eslint.org)
- [typescript](http://www.typescriptlang.org/)
Expand Down Expand Up @@ -50,29 +48,38 @@ Then configure the rules you want to use under the rules section.
{
"rules": {
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error"
"typescript-sort-keys/enum": "error"
}
}
```

Or enable all rules with defaults
Or alternatively to the individual rules, enable all rules with defaults:

```json
{
"extends": ["plugin:typescript-sort-keys/recommended"]
}
```

If you'd like to enable the recommended config with interface required keys first:

```json
{
"extends": ["plugin:typescript-sort-keys/requiredFirst"]
}
```

## Supported Rules

<!-- begin rule list -->

**Key**: :heavy_check_mark: = recommended, :wrench: = fixable
**Key**: :heavy_check_mark: = recommended, :wrench: = fixable, :warning: = deprecated

<!-- prettier-ignore -->
| Name | Description | :heavy_check_mark: | :wrench: |
| Name | Description | | |
| ---- | ----------- | ------------------ | -------- |
| [`typescript-sort-keys/interface`](./docs/rules/interface.md) | require interface keys to be sorted | :heavy_check_mark: | :wrench: |
| [`typescript-sort-keys/string-enum`](./docs/rules/string-enum.md) | require string enum members to be sorted | :heavy_check_mark: | :wrench: |
| [`typescript-sort-keys/enum`](./docs/rules/enum.md) | require enum members to be sorted | :heavy_check_mark: | :wrench: |
| [`typescript-sort-keys/string-enum`](./docs/rules/string-enum.md) | (**DEPRECATED**) require string enum members to be sorted | :warning: | :wrench: |

<!-- end rule list -->
194 changes: 194 additions & 0 deletions docs/rules/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
# require enum members to be sorted (enum)

When declaring multiple members on an enum, some developers prefer to sort enum member names alphabetically to be able to find necessary members easier at the later time. Others feel that it adds complexity and becomes burden to maintain.

## Rule Details

This rule checks all members of an enum declaration and verifies that all keys are sorted alphabetically.

Examples of **incorrect** code for this rule:

```ts
/* eslint typescript-sort-keys/enum: "error" */

enum U {
a = 'T',
c = 'T',
b = 'T',
}

// Case-sensitive by default.
enum V {
a = 'T',
B = 'T',
c = 'T',
}

enum W {
a = 'T',
'c+-' = 'T',
b = 'T',
d = 'T',
}
```

Examples of **correct** code for this rule:

```ts
/* eslint typescript-sort-keys/enum: "error" */

enum U {
a = 'T',
b = 'T',
c = 'T',
}

// Case-sensitive by default.
enum V {
B = 'T',
a = 'T',
c = 'T',
}

// This rule sorts members which have an arbitrary string literal name as well.
enum W {
a = 'T',
b = 'T',
'c+-' = 'T',
d = 'T',
}
```

## Options

```json
{
"typescript-sort-keys/enum": [
"error",
"asc",
{ "caseSensitive": true, "natural": false }
]
}
```

The 1st option is `"asc"` or `"desc"`.

- `"asc"` (default) - enforce enum members to be in ascending order.
- `"desc"` - enforce enum members to be in descending order.

The 2nd option is an object which has 2 properties.

- `caseSensitive` - if `true`, enforce enum members to be in case-sensitive order. Default is `true`.
- `natural` - if `true`, enforce enum members to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.

### desc

Examples of **incorrect** code for the `"desc"` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "desc"] */

enum U {
b = 'T',
c = 'T',
a = 'T',
}

// Case-sensitive by default.
enum V {
a = 'T',
B = 'T',
c = 'T',
}
```

Examples of **correct** code for the `"desc"` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "desc"] */

enum U {
c = 'T',
b = 'T',
a = 'T',
}

// Case-sensitive by default.
enum V {
c = 'T',
a = 'T',
B = 'T',
}
```

### insensitive

Examples of **incorrect** code for the `{ caseSensitive: false }` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "asc", { caseSensitive: false }] */

enum U {
a = 'T',
c = 'T',
C = 'T',
b = 'T',
}
enum V {
a = 'T',
C = 'T',
c = 'T',
b = 'T',
}
```

Examples of **correct** code for the `{ caseSensitive: false }` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "asc", { caseSensitive: false }] */

enum U {
a = 'T',
b = 'T',
c = 'T',
C = 'T',
}
enum V {
a = 'T',
b = 'T',
C = 'T',
c = 'T',
}
```

### natural

Examples of **incorrect** code for the `{natural: true}` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "asc", { natural: true }] */

enum U {
a = 'T',
_ = 'T',
A = 'T',
$ = 'T',
}
```

Examples of **correct** code for the `{natural: true}` option:

```ts
/* eslint typescript-sort-keys/enum: ["error", "asc", { natural: true }] */

enum U {
a = 'T',
A = 'T',
_ = 'T',
$ = 'T',
}
```

## When Not To Use It

If you don't want to notify about enum members' order, then it's safe to disable this rule.
16 changes: 10 additions & 6 deletions docs/rules/string-enum.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Deprecation notice: This rule has been deprecated in favor of [typescript-sort-keys/enum](./enum.md). `string-enum` will continue to sort only string enums and may be removed in future updates.

---

# require string enum members to be sorted (string-enum)

When declaring multiple members on an string enum, some developers prefer to sort enum member names alphabetically to be able to find necessary members easier at the later time. Others feel that it adds complexity and becomes burden to maintain.
When declaring multiple members on a string enum, some developers prefer to sort string enum member names alphabetically to be able to find necessary members easier at the later time. Others feel that it adds complexity and becomes burden to maintain.

## Rule Details

Expand Down Expand Up @@ -77,13 +81,13 @@ enum U {

The 1st option is `"asc"` or `"desc"`.

- `"asc"` (default) - enforce enum members to be in ascending order.
- `"desc"` - enforce enum members to be in descending order.
- `"asc"` (default) - enforce string enum members to be in ascending order.
- `"desc"` - enforce string enum members to be in descending order.

The 2nd option is an object which has 2 properties.

- `caseSensitive` - if `true`, enforce enum members to be in case-sensitive order. Default is `true`.
- `natural` - if `true`, enforce enum members to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.
- `caseSensitive` - if `true`, enforce string enum members to be in case-sensitive order. Default is `true`.
- `natural` - if `true`, enforce string enum members to be in natural order. Default is `false`. Natural Order compares strings containing combination of letters and numbers in the way a human being would sort. It basically sorts numerically, instead of sorting alphabetically. So the number 10 comes after the number 3 in Natural Sorting.

### desc

Expand Down Expand Up @@ -205,4 +209,4 @@ enum U {

## When Not To Use It

If you don't want to notify about enum members' order, then it's safe to disable this rule.
If you don't want to notify about string enum members' order, then it's safe to disable this rule.
Loading