@1adybug/prettier-plugin-sort-imports 0.0.21 → 0.0.23
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 +21 -46
- package/README.zh-CN.md +21 -46
- package/dist/index.js +13 -15
- package/dist/sorter.d.ts +2 -2
- package/dist/types.d.ts +4 -4
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -44,12 +44,6 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
44
44
|
### Basic Sorting
|
|
45
45
|
|
|
46
46
|
```typescript
|
|
47
|
-
import React, { useEffect, useState } from "react"
|
|
48
|
-
|
|
49
|
-
import { Button } from "antd"
|
|
50
|
-
|
|
51
|
-
import { sum } from "./utils"
|
|
52
|
-
|
|
53
47
|
import "./styles.css"
|
|
54
48
|
```
|
|
55
49
|
|
|
@@ -75,7 +69,7 @@ export default {
|
|
|
75
69
|
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
76
70
|
},
|
|
77
71
|
// Add blank lines between groups
|
|
78
|
-
|
|
72
|
+
groupSeparator: "",
|
|
79
73
|
}),
|
|
80
74
|
],
|
|
81
75
|
}
|
|
@@ -84,15 +78,6 @@ export default {
|
|
|
84
78
|
Result:
|
|
85
79
|
|
|
86
80
|
```typescript
|
|
87
|
-
import React, { useState } from "react"
|
|
88
|
-
|
|
89
|
-
import { Button } from "antd"
|
|
90
|
-
import { format } from "date-fns"
|
|
91
|
-
|
|
92
|
-
import { Header } from "./components/Header"
|
|
93
|
-
|
|
94
|
-
import { sum } from "./utils"
|
|
95
|
-
|
|
96
81
|
import "./styles.css"
|
|
97
82
|
```
|
|
98
83
|
|
|
@@ -162,7 +147,7 @@ interface PluginConfig {
|
|
|
162
147
|
/** Custom import content sorting function */
|
|
163
148
|
sortImportContent?: (a: ImportContent, b: ImportContent) => number
|
|
164
149
|
/** Separator between groups */
|
|
165
|
-
|
|
150
|
+
groupSeparator?: string | ((group: Group, index: number) => string | undefined)
|
|
166
151
|
/** Whether to sort side effect imports, defaults to false */
|
|
167
152
|
sortSideEffect?: boolean
|
|
168
153
|
/** Whether to remove unused imports, defaults to false */
|
|
@@ -179,9 +164,9 @@ Use the default plugin with basic options:
|
|
|
179
164
|
```javascript
|
|
180
165
|
export default {
|
|
181
166
|
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
167
|
+
sortSideEffect: false, // Whether to sort side effect imports
|
|
168
|
+
groupSeparator: "", // Group separator
|
|
169
|
+
removeUnusedImports: false, // Whether to remove unused imports
|
|
185
170
|
}
|
|
186
171
|
```
|
|
187
172
|
|
|
@@ -210,7 +195,7 @@ export default {
|
|
|
210
195
|
sortImportContent: (a, b) => a.name.localeCompare(b.name),
|
|
211
196
|
|
|
212
197
|
// Configuration
|
|
213
|
-
|
|
198
|
+
groupSeparator: "\n",
|
|
214
199
|
sortSideEffect: true,
|
|
215
200
|
removeUnusedImports: false,
|
|
216
201
|
}),
|
|
@@ -271,7 +256,7 @@ export default createPlugin({
|
|
|
271
256
|
},
|
|
272
257
|
|
|
273
258
|
// Add blank lines between groups
|
|
274
|
-
|
|
259
|
+
groupSeparator: "\n",
|
|
275
260
|
|
|
276
261
|
// Sort side effects
|
|
277
262
|
sortSideEffect: true,
|
|
@@ -314,7 +299,7 @@ export default {
|
|
|
314
299
|
if (!statement.path.startsWith(".")) return "external"
|
|
315
300
|
return "local"
|
|
316
301
|
},
|
|
317
|
-
|
|
302
|
+
groupSeparator: "\n",
|
|
318
303
|
|
|
319
304
|
// Other Prettier plugins to combine with (Plugin objects only)
|
|
320
305
|
otherPlugins: [
|
|
@@ -352,7 +337,7 @@ export default {
|
|
|
352
337
|
- Options in `prettierOptions` are passed to all other plugins
|
|
353
338
|
- This allows other plugins to receive their configuration even when merged
|
|
354
339
|
|
|
355
|
-
###
|
|
340
|
+
### removeUnusedImports
|
|
356
341
|
|
|
357
342
|
Whether to remove unused imports, defaults to `false`.
|
|
358
343
|
|
|
@@ -360,11 +345,11 @@ Whether to remove unused imports, defaults to `false`.
|
|
|
360
345
|
|
|
361
346
|
**When enabled (true)**: Automatically analyzes code and removes unused imports.
|
|
362
347
|
|
|
363
|
-
```
|
|
348
|
+
```tsx
|
|
364
349
|
// Before sorting
|
|
365
|
-
import
|
|
366
|
-
|
|
367
|
-
import {
|
|
350
|
+
import { useState } from "react"
|
|
351
|
+
|
|
352
|
+
import { Button } from "antd"
|
|
368
353
|
|
|
369
354
|
function MyComponent() {
|
|
370
355
|
const [count, setCount] = useState(0)
|
|
@@ -388,7 +373,7 @@ function MyComponent() {
|
|
|
388
373
|
- Analysis is AST-based and identifies actually used identifiers in code
|
|
389
374
|
- Supports identifying JSX components, TypeScript type references, etc.
|
|
390
375
|
|
|
391
|
-
###
|
|
376
|
+
### sortSideEffect
|
|
392
377
|
|
|
393
378
|
Whether to sort side effect imports, defaults to `false`.
|
|
394
379
|
|
|
@@ -406,7 +391,7 @@ import "f-side-effect"
|
|
|
406
391
|
import "f-side-effect"
|
|
407
392
|
```
|
|
408
393
|
|
|
409
|
-
###
|
|
394
|
+
### groupSeparator
|
|
410
395
|
|
|
411
396
|
Separator between groups, defaults to `undefined` (no separator).
|
|
412
397
|
|
|
@@ -414,10 +399,10 @@ Can be a string or function:
|
|
|
414
399
|
|
|
415
400
|
```javascript
|
|
416
401
|
// String: add blank lines between all groups
|
|
417
|
-
|
|
402
|
+
groupSeparator: ""
|
|
418
403
|
|
|
419
404
|
// Function: flexible control
|
|
420
|
-
|
|
405
|
+
groupSeparator: (group, index) => {
|
|
421
406
|
// No separator for the first group
|
|
422
407
|
if (index === 0) return undefined
|
|
423
408
|
|
|
@@ -437,8 +422,7 @@ separator: (group, index) => {
|
|
|
437
422
|
3. Named imports are sorted by `type` priority, then alphabetically by final import name
|
|
438
423
|
|
|
439
424
|
```typescript
|
|
440
|
-
|
|
441
|
-
import { type TypeA, type TypeB, VariableA, VariableB } from "module"
|
|
425
|
+
|
|
442
426
|
```
|
|
443
427
|
|
|
444
428
|
**Custom behavior**:
|
|
@@ -457,7 +441,7 @@ createPlugin({
|
|
|
457
441
|
```
|
|
458
442
|
|
|
459
443
|
```typescript
|
|
460
|
-
|
|
444
|
+
|
|
461
445
|
```
|
|
462
446
|
|
|
463
447
|
### Import Statement Sorting
|
|
@@ -465,9 +449,7 @@ import { type User, API_KEY, getUser } from "api"
|
|
|
465
449
|
Import statements are sorted alphabetically by module path:
|
|
466
450
|
|
|
467
451
|
```typescript
|
|
468
|
-
|
|
469
|
-
import { b } from "b-module"
|
|
470
|
-
import { c } from "c-module"
|
|
452
|
+
|
|
471
453
|
```
|
|
472
454
|
|
|
473
455
|
### Comment Handling
|
|
@@ -475,14 +457,7 @@ import { c } from "c-module"
|
|
|
475
457
|
Comments follow the import statements they are attached to:
|
|
476
458
|
|
|
477
459
|
```typescript
|
|
478
|
-
// React related imports
|
|
479
|
-
import React from "react"
|
|
480
|
-
|
|
481
|
-
// UI components
|
|
482
|
-
import { Button } from "antd"
|
|
483
460
|
|
|
484
|
-
// Utilities
|
|
485
|
-
import { sum } from "./utils"
|
|
486
461
|
```
|
|
487
462
|
|
|
488
463
|
## Implementation Details
|
|
@@ -520,7 +495,7 @@ Converts sorted import statements back to code strings:
|
|
|
520
495
|
- Generate corresponding import/export code from `ImportStatement`
|
|
521
496
|
- Handle formatting of default imports, named imports, namespace imports
|
|
522
497
|
- Handle `type` import formatting
|
|
523
|
-
- Insert separators between groups according to `
|
|
498
|
+
- Insert separators between groups according to `groupSeparator` configuration
|
|
524
499
|
- Maintain comment associations
|
|
525
500
|
|
|
526
501
|
#### 5. Plugin Entry (`src/index.ts`)
|
package/README.zh-CN.md
CHANGED
|
@@ -43,12 +43,6 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
43
43
|
### 基本排序
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
|
-
import React, { useEffect, useState } from "react"
|
|
47
|
-
|
|
48
|
-
import { Button } from "antd"
|
|
49
|
-
|
|
50
|
-
import { sum } from "./utils"
|
|
51
|
-
|
|
52
46
|
import "./styles.css"
|
|
53
47
|
```
|
|
54
48
|
|
|
@@ -74,7 +68,7 @@ export default {
|
|
|
74
68
|
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
75
69
|
},
|
|
76
70
|
// 在分组之间添加空行
|
|
77
|
-
|
|
71
|
+
groupSeparator: "",
|
|
78
72
|
}),
|
|
79
73
|
],
|
|
80
74
|
}
|
|
@@ -83,15 +77,6 @@ export default {
|
|
|
83
77
|
结果:
|
|
84
78
|
|
|
85
79
|
```typescript
|
|
86
|
-
import React, { useState } from "react"
|
|
87
|
-
|
|
88
|
-
import { Button } from "antd"
|
|
89
|
-
import { format } from "date-fns"
|
|
90
|
-
|
|
91
|
-
import { Header } from "./components/Header"
|
|
92
|
-
|
|
93
|
-
import { sum } from "./utils"
|
|
94
|
-
|
|
95
80
|
import "./styles.css"
|
|
96
81
|
```
|
|
97
82
|
|
|
@@ -161,7 +146,7 @@ interface PluginConfig {
|
|
|
161
146
|
/** 自定义导入内容排序函数 */
|
|
162
147
|
sortImportContent?: (a: ImportContent, b: ImportContent) => number
|
|
163
148
|
/** 分组之间的分隔符 */
|
|
164
|
-
|
|
149
|
+
groupSeparator?: string | ((group: Group, index: number) => string | undefined)
|
|
165
150
|
/** 是否对副作用导入进行排序,默认为 false */
|
|
166
151
|
sortSideEffect?: boolean
|
|
167
152
|
/** 是否删除未使用的导入,默认为 false */
|
|
@@ -178,9 +163,9 @@ interface PluginConfig {
|
|
|
178
163
|
```javascript
|
|
179
164
|
export default {
|
|
180
165
|
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
166
|
+
sortSideEffect: false, // 是否对副作用导入排序
|
|
167
|
+
groupSeparator: "", // 分组分隔符
|
|
168
|
+
removeUnusedImports: false, // 是否删除未使用的导入
|
|
184
169
|
}
|
|
185
170
|
```
|
|
186
171
|
|
|
@@ -206,7 +191,7 @@ export default {
|
|
|
206
191
|
sortImportContent: (a, b) => {
|
|
207
192
|
/* 自定义排序 */
|
|
208
193
|
},
|
|
209
|
-
|
|
194
|
+
groupSeparator: "",
|
|
210
195
|
sortSideEffect: true,
|
|
211
196
|
removeUnusedImports: false,
|
|
212
197
|
}),
|
|
@@ -267,7 +252,7 @@ export default createPlugin({
|
|
|
267
252
|
},
|
|
268
253
|
|
|
269
254
|
// 在分组间添加空行
|
|
270
|
-
|
|
255
|
+
groupSeparator: "\n",
|
|
271
256
|
|
|
272
257
|
// 排序副作用导入
|
|
273
258
|
sortSideEffect: true,
|
|
@@ -310,7 +295,7 @@ export default {
|
|
|
310
295
|
if (!statement.path.startsWith(".")) return "external"
|
|
311
296
|
return "local"
|
|
312
297
|
},
|
|
313
|
-
|
|
298
|
+
groupSeparator: "\n",
|
|
314
299
|
|
|
315
300
|
// 要合并的其他 Prettier 插件(仅支持 Plugin 对象)
|
|
316
301
|
otherPlugins: [
|
|
@@ -348,7 +333,7 @@ export default {
|
|
|
348
333
|
- `prettierOptions` 中的选项会传递给所有其他插件
|
|
349
334
|
- 这允许其他插件即使在合并时也能接收到它们的配置
|
|
350
335
|
|
|
351
|
-
###
|
|
336
|
+
### removeUnusedImports
|
|
352
337
|
|
|
353
338
|
是否删除未使用的导入,默认为 `false`。
|
|
354
339
|
|
|
@@ -356,11 +341,11 @@ export default {
|
|
|
356
341
|
|
|
357
342
|
**开启后(true)**:自动分析代码并删除未使用的导入。
|
|
358
343
|
|
|
359
|
-
```
|
|
344
|
+
```tsx
|
|
360
345
|
// 排序前
|
|
361
|
-
import
|
|
362
|
-
|
|
363
|
-
import {
|
|
346
|
+
import { useState } from "react"
|
|
347
|
+
|
|
348
|
+
import { Button } from "antd"
|
|
364
349
|
|
|
365
350
|
function MyComponent() {
|
|
366
351
|
const [count, setCount] = useState(0)
|
|
@@ -384,7 +369,7 @@ function MyComponent() {
|
|
|
384
369
|
- 分析基于 AST,会识别代码中实际使用的标识符
|
|
385
370
|
- 支持识别 JSX 组件、TypeScript 类型引用等
|
|
386
371
|
|
|
387
|
-
###
|
|
372
|
+
### sortSideEffect
|
|
388
373
|
|
|
389
374
|
是否对副作用导入进行排序,默认为 `false`。
|
|
390
375
|
|
|
@@ -402,7 +387,7 @@ import "f-side-effect"
|
|
|
402
387
|
import "f-side-effect"
|
|
403
388
|
```
|
|
404
389
|
|
|
405
|
-
###
|
|
390
|
+
### groupSeparator
|
|
406
391
|
|
|
407
392
|
分组之间的分隔符,默认为 `undefined`(无分隔符)。
|
|
408
393
|
|
|
@@ -410,10 +395,10 @@ import "f-side-effect"
|
|
|
410
395
|
|
|
411
396
|
```javascript
|
|
412
397
|
// 字符串:在所有分组间添加空行
|
|
413
|
-
|
|
398
|
+
groupSeparator: ""
|
|
414
399
|
|
|
415
400
|
// 函数:灵活控制
|
|
416
|
-
|
|
401
|
+
groupSeparator: (group, index) => {
|
|
417
402
|
// 第一个分组不添加分隔符
|
|
418
403
|
if (index === 0) return undefined
|
|
419
404
|
|
|
@@ -433,8 +418,7 @@ separator: (group, index) => {
|
|
|
433
418
|
3. 命名导入按照 `type` 类型优先,然后按最终导入名称字母顺序排序
|
|
434
419
|
|
|
435
420
|
```typescript
|
|
436
|
-
|
|
437
|
-
import { type TypeA, type TypeB, VariableA, VariableB } from "module"
|
|
421
|
+
|
|
438
422
|
```
|
|
439
423
|
|
|
440
424
|
**自定义行为**:
|
|
@@ -453,7 +437,7 @@ createPlugin({
|
|
|
453
437
|
```
|
|
454
438
|
|
|
455
439
|
```typescript
|
|
456
|
-
|
|
440
|
+
|
|
457
441
|
```
|
|
458
442
|
|
|
459
443
|
### 导入语句排序
|
|
@@ -461,9 +445,7 @@ import { type User, API_KEY, getUser } from "api"
|
|
|
461
445
|
导入语句按模块路径的字母顺序排序:
|
|
462
446
|
|
|
463
447
|
```typescript
|
|
464
|
-
|
|
465
|
-
import { b } from "b-module"
|
|
466
|
-
import { c } from "c-module"
|
|
448
|
+
|
|
467
449
|
```
|
|
468
450
|
|
|
469
451
|
### 注释处理
|
|
@@ -471,14 +453,7 @@ import { c } from "c-module"
|
|
|
471
453
|
注释会跟随它们所附加的导入语句一起移动:
|
|
472
454
|
|
|
473
455
|
```typescript
|
|
474
|
-
// React 相关导入
|
|
475
|
-
import React from "react"
|
|
476
|
-
|
|
477
|
-
// UI 组件
|
|
478
|
-
import { Button } from "antd"
|
|
479
456
|
|
|
480
|
-
// 工具函数
|
|
481
|
-
import { sum } from "./utils"
|
|
482
457
|
```
|
|
483
458
|
|
|
484
459
|
## 实现细节
|
|
@@ -516,7 +491,7 @@ import { sum } from "./utils"
|
|
|
516
491
|
- 根据 `ImportStatement` 生成对应的 import/export 代码
|
|
517
492
|
- 处理默认导入、命名导入、命名空间导入的格式
|
|
518
493
|
- 处理 `type` 导入的格式
|
|
519
|
-
- 根据 `
|
|
494
|
+
- 根据 `groupSeparator` 配置在分组之间插入分隔符
|
|
520
495
|
- 保持注释关联
|
|
521
496
|
|
|
522
497
|
#### 5. 插件入口 (`src/index.ts`)
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
-
import { relative } from "path";
|
|
3
2
|
import { format } from "prettier";
|
|
4
3
|
import { parse as parser_parse } from "@babel/parser";
|
|
5
4
|
import traverse from "@babel/traverse";
|
|
@@ -154,7 +153,7 @@ function formatImportStatement(statement) {
|
|
|
154
153
|
}
|
|
155
154
|
function formatGroups(groups, config) {
|
|
156
155
|
const lines = [];
|
|
157
|
-
const separator = config.
|
|
156
|
+
const separator = config.groupSeparator;
|
|
158
157
|
for(let i = 0; i < groups.length; i++){
|
|
159
158
|
const group = groups[i];
|
|
160
159
|
if (void 0 !== separator && i > 0) {
|
|
@@ -400,7 +399,7 @@ function mergeConfig(userConfig) {
|
|
|
400
399
|
sortGroup: userConfig.sortGroup ?? DEFAULT_CONFIG.sortGroup,
|
|
401
400
|
sortImportStatement: userConfig.sortImportStatement ?? DEFAULT_CONFIG.sortImportStatement,
|
|
402
401
|
sortImportContent: userConfig.sortImportContent ?? DEFAULT_CONFIG.sortImportContent,
|
|
403
|
-
|
|
402
|
+
groupSeparator: userConfig.groupSeparator,
|
|
404
403
|
sortSideEffect: userConfig.sortSideEffect ?? DEFAULT_CONFIG.sortSideEffect,
|
|
405
404
|
removeUnusedImports: userConfig.removeUnusedImports ?? false
|
|
406
405
|
};
|
|
@@ -575,9 +574,8 @@ function preprocessImports(text, options, config = {}) {
|
|
|
575
574
|
"babel-ts"
|
|
576
575
|
];
|
|
577
576
|
if (!parser || !supportedParsers.includes(parser)) return text;
|
|
578
|
-
const
|
|
579
|
-
const
|
|
580
|
-
const imports = parseImports(text, relativeFilepath);
|
|
577
|
+
const filepath = options.filepath;
|
|
578
|
+
const imports = parseImports(text, filepath);
|
|
581
579
|
if (0 === imports.length) return text;
|
|
582
580
|
const optionsConfig = options;
|
|
583
581
|
const finalConfig = {
|
|
@@ -585,9 +583,9 @@ function preprocessImports(text, options, config = {}) {
|
|
|
585
583
|
sortGroup: config.sortGroup ?? optionsConfig.sortGroup,
|
|
586
584
|
sortImportStatement: config.sortImportStatement ?? optionsConfig.sortImportStatement,
|
|
587
585
|
sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
|
|
588
|
-
|
|
589
|
-
sortSideEffect: config.sortSideEffect ?? optionsConfig.
|
|
590
|
-
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.
|
|
586
|
+
groupSeparator: config.groupSeparator ?? optionsConfig.groupSeparator,
|
|
587
|
+
sortSideEffect: config.sortSideEffect ?? optionsConfig.sortSideEffect ?? false,
|
|
588
|
+
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false
|
|
591
589
|
};
|
|
592
590
|
let processedImports = imports;
|
|
593
591
|
if (finalConfig.removeUnusedImports) {
|
|
@@ -644,21 +642,21 @@ function createCombinedPreprocess(parserName, config) {
|
|
|
644
642
|
}
|
|
645
643
|
function createPluginInstance(config = {}) {
|
|
646
644
|
const baseOptions = {
|
|
647
|
-
|
|
645
|
+
groupSeparator: {
|
|
648
646
|
type: "string",
|
|
649
647
|
category: "Import Sort",
|
|
650
|
-
description: "
|
|
648
|
+
description: "����֮��ķָ���"
|
|
651
649
|
},
|
|
652
|
-
|
|
650
|
+
sortSideEffect: {
|
|
653
651
|
type: "boolean",
|
|
654
652
|
category: "Import Sort",
|
|
655
|
-
description: "
|
|
653
|
+
description: "�Ƿ�Ը����õ����������",
|
|
656
654
|
default: false
|
|
657
655
|
},
|
|
658
|
-
|
|
656
|
+
removeUnusedImports: {
|
|
659
657
|
type: "boolean",
|
|
660
658
|
category: "Import Sort",
|
|
661
|
-
description: "
|
|
659
|
+
description: "�Ƿ�ɾ��δʹ�õĵ���",
|
|
662
660
|
default: false
|
|
663
661
|
}
|
|
664
662
|
};
|
package/dist/sorter.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Group, ImportContent, ImportStatement, PluginConfig } from "./types";
|
|
2
2
|
/** 合并后的配置 */
|
|
3
|
-
export interface MergedConfig extends Omit<Required<PluginConfig>, "
|
|
4
|
-
|
|
3
|
+
export interface MergedConfig extends Omit<Required<PluginConfig>, "groupSeparator" | "removeUnusedImports" | "otherPlugins" | "prettierOptions"> {
|
|
4
|
+
groupSeparator: PluginConfig["groupSeparator"];
|
|
5
5
|
removeUnusedImports: boolean;
|
|
6
6
|
}
|
|
7
7
|
/** 对导入语句进行排序 */
|
package/dist/types.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface ImportContent {
|
|
|
14
14
|
}
|
|
15
15
|
/** 导入语句 */
|
|
16
16
|
export interface ImportStatement {
|
|
17
|
-
/**
|
|
17
|
+
/** Absolute path of current file */
|
|
18
18
|
filepath?: string;
|
|
19
19
|
/** 导入的模块路径,可以是相对路径或绝对路径,比如 react, react-dom 或者 ./utils/index,@/utils/index 等 */
|
|
20
20
|
path: string;
|
|
@@ -39,7 +39,7 @@ export interface ImportStatement {
|
|
|
39
39
|
}
|
|
40
40
|
/** 分组 */
|
|
41
41
|
export interface Group {
|
|
42
|
-
/**
|
|
42
|
+
/** Absolute path of current file */
|
|
43
43
|
filepath?: string;
|
|
44
44
|
/** 分组名称,默认为 default */
|
|
45
45
|
name: string;
|
|
@@ -59,7 +59,7 @@ export type SortImportStatementFunction = (a: ImportStatement, b: ImportStatemen
|
|
|
59
59
|
/** 导入内容排序函数 */
|
|
60
60
|
export type SortImportContentFunction = (a: ImportContent, b: ImportContent) => number;
|
|
61
61
|
/** 分隔符函数 */
|
|
62
|
-
export type
|
|
62
|
+
export type GroupSeparatorFunction = (group: Group, index: number) => string | undefined;
|
|
63
63
|
/** 插件配置 */
|
|
64
64
|
export interface PluginConfig {
|
|
65
65
|
/** 可选的,获取分组名称 */
|
|
@@ -71,7 +71,7 @@ export interface PluginConfig {
|
|
|
71
71
|
/** 可选的,默认按照导入内容的 name 的字母顺序排序,默认按照优先 type 类型在前,其次按照最终导入的内容名称的字母顺序排序 */
|
|
72
72
|
sortImportContent?: SortImportContentFunction;
|
|
73
73
|
/** 分隔符,分组之间的分隔符,默认为 undefined */
|
|
74
|
-
|
|
74
|
+
groupSeparator?: string | GroupSeparatorFunction;
|
|
75
75
|
/** 是否对副作用导入进行排序,默认为 false */
|
|
76
76
|
sortSideEffect?: boolean;
|
|
77
77
|
/** 是否删除未使用的导入,默认为 false */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1adybug/prettier-plugin-sort-imports",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prettier",
|
|
@@ -43,12 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@babel/core": "^7.28.4",
|
|
45
45
|
"@babel/parser": "^7.28.4",
|
|
46
|
-
"@babel/traverse": "^7.28.
|
|
46
|
+
"@babel/traverse": "^7.28.5",
|
|
47
47
|
"@babel/types": "^7.28.4"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@rslib/core": "^0.15.0",
|
|
51
51
|
"@types/babel__core": "^7.20.5",
|
|
52
|
+
"@types/babel__traverse": "^7.28.0",
|
|
52
53
|
"@types/bun": "latest",
|
|
53
54
|
"@types/node": "^22.18.6",
|
|
54
55
|
"json5": "^2.2.3",
|