@1adybug/prettier-plugin-sort-imports 0.0.16 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -10
- package/README.zh-CN.md +26 -4
- package/dist/index.js +7 -4
- package/dist/sorter.d.ts +1 -1
- package/package.json +65 -71
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Prettier Plugin
|
|
1
|
+
# Prettier Plugin Sort Imports
|
|
2
2
|
|
|
3
3
|
[中文文档](https://github.com/1adybug/prettier-plugin-sort-imports/blob/main/README.zh-CN.md)
|
|
4
4
|
|
|
@@ -44,7 +44,12 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
44
44
|
### Basic Sorting
|
|
45
45
|
|
|
46
46
|
```typescript
|
|
47
|
+
import { Button } from "antd"
|
|
48
|
+
import React, { useEffect, useState } from "react"
|
|
49
|
+
|
|
50
|
+
import { sum } from "./utils"
|
|
47
51
|
|
|
52
|
+
import "./styles.css"
|
|
48
53
|
```
|
|
49
54
|
|
|
50
55
|
### Custom Grouping and Sorting
|
|
@@ -78,6 +83,14 @@ export default {
|
|
|
78
83
|
Result:
|
|
79
84
|
|
|
80
85
|
```typescript
|
|
86
|
+
import { Button } from "antd"
|
|
87
|
+
import { format } from "date-fns"
|
|
88
|
+
import React, { useState } from "react"
|
|
89
|
+
|
|
90
|
+
import { Header } from "./components/Header"
|
|
91
|
+
|
|
92
|
+
import { sum } from "./utils"
|
|
93
|
+
|
|
81
94
|
import "./styles.css"
|
|
82
95
|
```
|
|
83
96
|
|
|
@@ -191,12 +204,8 @@ export default {
|
|
|
191
204
|
|
|
192
205
|
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
193
206
|
},
|
|
194
|
-
sortImportStatement: (a, b) =>
|
|
195
|
-
|
|
196
|
-
},
|
|
197
|
-
sortImportContent: (a, b) => {
|
|
198
|
-
return a.name.localeCompare(b.name)
|
|
199
|
-
},
|
|
207
|
+
sortImportStatement: (a, b) => a.path.localeCompare(b.path),
|
|
208
|
+
sortImportContent: (a, b) => a.name.localeCompare(b.name),
|
|
200
209
|
|
|
201
210
|
// Configuration
|
|
202
211
|
separator: "\n",
|
|
@@ -438,7 +447,8 @@ separator: (group, index) => {
|
|
|
438
447
|
3. Named imports are sorted by `type` priority, then alphabetically by final import name
|
|
439
448
|
|
|
440
449
|
```typescript
|
|
441
|
-
|
|
450
|
+
import Default, * as Namespace from "module"
|
|
451
|
+
import { type TypeA, type TypeB, VariableA, VariableB } from "module"
|
|
442
452
|
```
|
|
443
453
|
|
|
444
454
|
**Custom behavior**:
|
|
@@ -457,7 +467,7 @@ createPlugin({
|
|
|
457
467
|
```
|
|
458
468
|
|
|
459
469
|
```typescript
|
|
460
|
-
|
|
470
|
+
import { type User, API_KEY, getUser } from "api"
|
|
461
471
|
```
|
|
462
472
|
|
|
463
473
|
### Import Statement Sorting
|
|
@@ -465,7 +475,9 @@ createPlugin({
|
|
|
465
475
|
Import statements are sorted alphabetically by module path:
|
|
466
476
|
|
|
467
477
|
```typescript
|
|
468
|
-
|
|
478
|
+
import { a } from "a-module"
|
|
479
|
+
import { b } from "b-module"
|
|
480
|
+
import { c } from "c-module"
|
|
469
481
|
```
|
|
470
482
|
|
|
471
483
|
### Comment Handling
|
|
@@ -473,7 +485,13 @@ Import statements are sorted alphabetically by module path:
|
|
|
473
485
|
Comments follow the import statements they are attached to:
|
|
474
486
|
|
|
475
487
|
```typescript
|
|
488
|
+
// UI components
|
|
489
|
+
import { Button } from "antd"
|
|
490
|
+
// React related imports
|
|
491
|
+
import React from "react"
|
|
476
492
|
|
|
493
|
+
// Utilities
|
|
494
|
+
import { sum } from "./utils"
|
|
477
495
|
```
|
|
478
496
|
|
|
479
497
|
## Implementation Details
|
package/README.zh-CN.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Prettier Plugin
|
|
1
|
+
# Prettier Plugin Sort Imports
|
|
2
2
|
|
|
3
3
|
[English](https://github.com/1adybug/prettier-plugin-sort-imports/blob/main/README.md)
|
|
4
4
|
|
|
@@ -43,7 +43,12 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
43
43
|
### 基本排序
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
|
+
import { Button } from "antd"
|
|
47
|
+
import React, { useEffect, useState } from "react"
|
|
48
|
+
|
|
49
|
+
import { sum } from "./utils"
|
|
46
50
|
|
|
51
|
+
import "./styles.css"
|
|
47
52
|
```
|
|
48
53
|
|
|
49
54
|
### 自定义分组和排序
|
|
@@ -77,6 +82,14 @@ export default {
|
|
|
77
82
|
结果:
|
|
78
83
|
|
|
79
84
|
```typescript
|
|
85
|
+
import { Button } from "antd"
|
|
86
|
+
import { format } from "date-fns"
|
|
87
|
+
import React, { useState } from "react"
|
|
88
|
+
|
|
89
|
+
import { Header } from "./components/Header"
|
|
90
|
+
|
|
91
|
+
import { sum } from "./utils"
|
|
92
|
+
|
|
80
93
|
import "./styles.css"
|
|
81
94
|
```
|
|
82
95
|
|
|
@@ -430,7 +443,8 @@ separator: (group, index) => {
|
|
|
430
443
|
3. 命名导入按照 `type` 类型优先,然后按最终导入名称字母顺序排序
|
|
431
444
|
|
|
432
445
|
```typescript
|
|
433
|
-
|
|
446
|
+
import Default, * as Namespace from "module"
|
|
447
|
+
import { type TypeA, type TypeB, VariableA, VariableB } from "module"
|
|
434
448
|
```
|
|
435
449
|
|
|
436
450
|
**自定义行为**:
|
|
@@ -449,7 +463,7 @@ createPlugin({
|
|
|
449
463
|
```
|
|
450
464
|
|
|
451
465
|
```typescript
|
|
452
|
-
|
|
466
|
+
import { type User, API_KEY, getUser } from "api"
|
|
453
467
|
```
|
|
454
468
|
|
|
455
469
|
### 导入语句排序
|
|
@@ -457,7 +471,9 @@ createPlugin({
|
|
|
457
471
|
导入语句按模块路径的字母顺序排序:
|
|
458
472
|
|
|
459
473
|
```typescript
|
|
460
|
-
|
|
474
|
+
import { a } from "a-module"
|
|
475
|
+
import { b } from "b-module"
|
|
476
|
+
import { c } from "c-module"
|
|
461
477
|
```
|
|
462
478
|
|
|
463
479
|
### 注释处理
|
|
@@ -465,7 +481,13 @@ createPlugin({
|
|
|
465
481
|
注释会跟随它们所附加的导入语句一起移动:
|
|
466
482
|
|
|
467
483
|
```typescript
|
|
484
|
+
// UI 组件
|
|
485
|
+
import { Button } from "antd"
|
|
486
|
+
// React 相关导入
|
|
487
|
+
import React from "react"
|
|
468
488
|
|
|
489
|
+
// 工具函数
|
|
490
|
+
import { sum } from "./utils"
|
|
469
491
|
```
|
|
470
492
|
|
|
471
493
|
## 实现细节
|
package/dist/index.js
CHANGED
|
@@ -459,13 +459,16 @@ function groupImports(imports, userConfig) {
|
|
|
459
459
|
const groupMap = new Map();
|
|
460
460
|
for (const statement of imports){
|
|
461
461
|
const groupName = config.getGroup(statement);
|
|
462
|
-
const
|
|
462
|
+
const key = `${groupName}|||${statement.isSideEffect}`;
|
|
463
|
+
const statements = groupMap.get(key) ?? [];
|
|
463
464
|
statements.push(statement);
|
|
464
|
-
groupMap.set(
|
|
465
|
+
groupMap.set(key, statements);
|
|
465
466
|
}
|
|
466
467
|
const groups = [];
|
|
467
|
-
for (const [
|
|
468
|
-
const
|
|
468
|
+
for (const [key, statements] of Array.from(groupMap.entries())){
|
|
469
|
+
const separatorIndex = key.lastIndexOf("|||");
|
|
470
|
+
const name = key.slice(0, separatorIndex);
|
|
471
|
+
const isSideEffect = "true" === key.slice(separatorIndex + 3);
|
|
469
472
|
groups.push({
|
|
470
473
|
name,
|
|
471
474
|
isSideEffect,
|
package/dist/sorter.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" |
|
|
|
6
6
|
}
|
|
7
7
|
/** 对导入语句进行排序 */
|
|
8
8
|
export declare function sortImports(imports: ImportStatement[], userConfig: PluginConfig): ImportStatement[];
|
|
9
|
-
/**
|
|
9
|
+
/** 对导入语句进行分组,同时根据 name 和 isSideEffect 区分 */
|
|
10
10
|
export declare function groupImports(imports: ImportStatement[], userConfig: PluginConfig): Group[];
|
|
11
11
|
/** 对分组进行排序 */
|
|
12
12
|
export declare function sortGroups(groups: Group[], userConfig: PluginConfig): Group[];
|
package/package.json
CHANGED
|
@@ -1,73 +1,67 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"exports": {
|
|
31
|
-
".": {
|
|
32
|
-
"types": "./dist/index.d.ts",
|
|
33
|
-
"import": "./dist/index.js",
|
|
34
|
-
"require": "./dist/index.js"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
"main": "./dist/index.js",
|
|
38
|
-
"module": "./dist/index.js",
|
|
39
|
-
"types": "./dist/index.d.ts",
|
|
40
|
-
"files": [
|
|
41
|
-
"dist"
|
|
42
|
-
],
|
|
43
|
-
"scripts": {
|
|
44
|
-
"build": "rslib build",
|
|
45
|
-
"dev": "rslib build --watch",
|
|
46
|
-
"prepublishOnly": "npm run build",
|
|
47
|
-
"format": "prettier --write .",
|
|
48
|
-
"test": "bun test",
|
|
49
|
-
"test:watch": "bun test --watch",
|
|
50
|
-
"fg": "npm run format && git add . && git commit -m \"✨feature: format\""
|
|
51
|
-
},
|
|
52
|
-
"devDependencies": {
|
|
53
|
-
"@rslib/core": "^0.15.0",
|
|
54
|
-
"@types/babel__core": "^7.20.5",
|
|
55
|
-
"@types/bun": "latest",
|
|
56
|
-
"@types/node": "^22.18.6",
|
|
57
|
-
"json5": "^2.2.3",
|
|
58
|
-
"prettier": "^3.6.2",
|
|
59
|
-
"prettier-plugin-block-padding": "^0.0.6",
|
|
60
|
-
"prettier-plugin-tailwindcss": "^0.7.0",
|
|
61
|
-
"supports-color": "^10.2.2",
|
|
62
|
-
"typescript": "^5.9.2"
|
|
63
|
-
},
|
|
64
|
-
"peerDependencies": {
|
|
65
|
-
"prettier": "^3.0.0"
|
|
66
|
-
},
|
|
67
|
-
"dependencies": {
|
|
68
|
-
"@babel/core": "^7.28.4",
|
|
69
|
-
"@babel/parser": "^7.28.4",
|
|
70
|
-
"@babel/traverse": "^7.28.4",
|
|
71
|
-
"@babel/types": "^7.28.4"
|
|
2
|
+
"name": "@1adybug/prettier-plugin-sort-imports",
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"prettier",
|
|
7
|
+
"plugin",
|
|
8
|
+
"import",
|
|
9
|
+
"sort",
|
|
10
|
+
"organize",
|
|
11
|
+
"typescript",
|
|
12
|
+
"javascript"
|
|
13
|
+
],
|
|
14
|
+
"author": "1adybug <lurongv@qq.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/1adybug/prettier.git"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/1adybug/prettier/tree/main/packages/prettier-plugin-sort-imports",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/1adybug/prettier/issues"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"require": "./dist/index.js"
|
|
72
30
|
}
|
|
73
|
-
}
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.js",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"registry": "https://registry.npmjs.com/"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@babel/core": "^7.28.4",
|
|
45
|
+
"@babel/parser": "^7.28.4",
|
|
46
|
+
"@babel/traverse": "^7.28.4",
|
|
47
|
+
"@babel/types": "^7.28.4"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@rslib/core": "^0.15.0",
|
|
51
|
+
"@types/babel__core": "^7.20.5",
|
|
52
|
+
"@types/bun": "latest",
|
|
53
|
+
"@types/node": "^22.18.6",
|
|
54
|
+
"json5": "^2.2.3",
|
|
55
|
+
"supports-color": "^10.2.2",
|
|
56
|
+
"typescript": "^5.9.2"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"prettier": "^3.0.0"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "rslib build",
|
|
63
|
+
"dev": "rslib build --watch",
|
|
64
|
+
"test": "bun test",
|
|
65
|
+
"test:watch": "bun test --watch"
|
|
66
|
+
}
|
|
67
|
+
}
|