Skip to content

Commit 98ce69c

Browse files
authored
fix: Fix rules and publishing (#6)
* ESLint * Import alias test * Docs * Fix rules and config * Alias test * Use key as sorted element * Add main field * Fix rules
1 parent 30da61f commit 98ce69c

9 files changed

+123
-36
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib/

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"extends": [
1010
"eslint:recommended",
11-
"plugin:prettier/recommended",
1211
"plugin:@typescript-eslint/recommended",
12+
"prettier",
1313
"prettier/@typescript-eslint"
1414
],
1515
"parser": "@typescript-eslint/parser",

README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,76 @@
11
# eslint-plugin-sort
22

33
[![Build status](https://github.com/mskelton/eslint-plugin-sort/workflows/Build/badge.svg)](https://github.com/mskelton/eslint-plugin-sort/actions)
4-
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg)](#contributors)
4+
[![npm](https://img.shields.io/npm/v/eslint-plugin-sort)](https://www.npmjs.com/package/eslint-plugin-sort)
55
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
6+
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg)](#contributors)
67

78
> Autofixable sort rules for ESLint.
89
910
## Installation
1011

1112
```sh
13+
# npm
1214
npm install -D eslint-plugin-sort
15+
16+
# Yarn
17+
yarn add -D eslint-plugin-sort
1318
```
1419

1520
## Usage
1621

17-
// TODO
22+
After installing, add `sort` to your list of ESLint plugins and extend the recommended configuration. This will enable all available rules as warnings.
23+
24+
```json
25+
{
26+
"extends": ["plugin:sort/recommended"],
27+
"plugins": ["sort"]
28+
}
29+
```
30+
31+
## Rules
32+
33+
While the recommended configuration is the simplest way to use this plugin, you can also configure the rules manually based on your needs.
34+
35+
### `sort/destructured-properties` 🔧
36+
37+
Sorts properties in object destructuring patterns.
38+
39+
Examples of **incorrect** code for this rule.
40+
41+
```js
42+
let { b, c, a } = {}
43+
let { C, b } = {}
44+
let { b: a, a: b } = {}
45+
```
46+
47+
Examples of **correct** code for this rule.
48+
49+
```js
50+
let { a, b, c } = {}
51+
let { b, C } = {}
52+
let { a: b, b: a } = {}
53+
```
54+
55+
### `sort/imported-variables` 🔧
56+
57+
Sorts imported variable names alphabetically and case insensitive.
58+
59+
Examples of **incorrect** code for this rule.
60+
61+
```js
62+
import { b, c, a } from "a"
63+
import { C, b } from "a"
64+
import { b as a, a as b } from "a"
65+
```
66+
67+
Examples of **correct** code for this rule.
68+
69+
```js
70+
import { a, b, c } from "a"
71+
import { b, C } from "a"
72+
import { a as b, b as a } from "a"
73+
```
1874

1975
## Contributors ✨
2076

package-lock.json

+29-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"sort",
1919
"fix"
2020
],
21+
"main": "lib/index.js",
2122
"files": [
2223
"lib"
2324
],
@@ -38,9 +39,10 @@
3839
"@types/eslint": "^6.8.0",
3940
"@types/node": "^13.13.2",
4041
"@typescript-eslint/eslint-plugin": "^2.29.0",
42+
"@typescript-eslint/parser": "^2.29.0",
4143
"babel-jest": "^25.4.0",
4244
"eslint": "^6.8.0",
43-
"eslint-plugin-prettier": "^3.1.3",
45+
"eslint-config-prettier": "^6.11.0",
4446
"jest": "^25.4.0",
4547
"prettier": "^2.0.5",
4648
"typescript": "^3.8.3"

src/__tests__/sort-import-specifiers.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ruleTester.run("sort/imported-variables", rule, {
4343
valid("* as React"),
4444
valid("React, {a, b}"),
4545

46-
//
46+
// Import aliases
4747
valid("{a as b, b as a}"),
4848

4949
// Comments
@@ -89,6 +89,14 @@ ruleTester.run("sort/imported-variables", rule, {
8989
error("a", "c")
9090
),
9191

92+
// Import aliases
93+
invalid(
94+
"{b as a, a as b}",
95+
"{a as b, b as a}",
96+
messages.unsortedSpecifiers,
97+
error("a", "b")
98+
),
99+
92100
// All properties are sorted with a single sort
93101
invalid(
94102
"{z,y,x,w,v,u,t,s,r,q,p}",

src/__tests__/sort-object-patterns.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ ruleTester.run("sort/destructured-properties", rule, {
4141
valid("{a, B, c, D}"),
4242
valid("{_, A, b}"),
4343

44+
// Aliases
45+
valid("{a: b, b: a}"),
46+
4447
// Rest element
4548
valid("{a, b, ...c}"),
4649
valid("{...rest}"),
@@ -80,6 +83,14 @@ ruleTester.run("sort/destructured-properties", rule, {
8083
error("B", "c")
8184
),
8285

86+
// Aliases
87+
invalid(
88+
"{b: a, a: b}",
89+
"{a: b, b: a}",
90+
messages.unsortedPattern,
91+
error("a", "b")
92+
),
93+
8394
// Rest element
8495
invalid(
8596
"{c, a, b, ...rest}",

src/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import sortImports from "./rules/sort-imports"
2-
import sortImportSpecifiers from "./rules/sort-imports"
1+
// import sortImports from "./rules/sort-imports"
2+
import sortImportSpecifiers from "./rules/sort-import-specifiers"
33
import sortObjectPatterns from "./rules/sort-object-patterns"
4-
import sortObjectProperties from "./rules/sort-object-properties"
4+
// import sortObjectProperties from "./rules/sort-object-properties"
55

66
module.exports = {
77
configs: {
88
recommended: {
99
plugins: ["sort"],
1010
rules: {
11-
"sort/imports": "warn",
12-
"sort/imported-variables": "warn",
1311
"sort/destructured-properties": "warn",
14-
"sort/object-properties": "warn",
12+
"sort/imported-variables": "warn",
13+
// "sort/imports": "warn",
14+
// "sort/object-properties": "warn",
1515
},
1616
},
1717
},
1818
rules: {
19-
"sort/imports": sortImports,
20-
"sort/imported-variables": sortImportSpecifiers,
21-
"sort/destructured-properties": sortObjectPatterns,
22-
"sort/object-properties": sortObjectProperties,
19+
"destructured-properties": sortObjectPatterns,
20+
"imported-variables": sortImportSpecifiers,
21+
// "imports": sortImports,
22+
// "object-properties": sortObjectProperties,
2323
},
2424
}

src/rules/sort-object-patterns.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type Property = AssignmentProperty | RestElement
1616

1717
function getNodeText(node: AssignmentProperty) {
18-
return (node.value as Identifier).name
18+
return (node.key as Identifier).name
1919
}
2020

2121
function getNodeSortValue(node: Property) {

0 commit comments

Comments
 (0)