@1adybug/prettier-plugin-sort-imports 0.0.22 → 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 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
- separator: "",
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
- separator?: string | ((group: Group, index: number) => string | undefined)
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
- importSortSideEffect: false, // Whether to sort side effect imports
183
- importSortSeparator: "", // Group separator
184
- importSortRemoveUnused: false, // Whether to remove unused imports
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
- separator: "\n",
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
- separator: "\n",
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
- separator: "\n",
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
- ### importSortRemoveUnused
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
- ```typescript
348
+ ```tsx
364
349
  // Before sorting
365
- import React, { useState, useEffect } from "react"
366
- import { Button, Input } from "antd"
367
- import { helper } from "./utils"
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
- ### importSortSideEffect
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
- ### separator
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
- separator: ""
402
+ groupSeparator: ""
418
403
 
419
404
  // Function: flexible control
420
- separator: (group, index) => {
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
- import Default, * as Namespace from "module"
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
- import { type User, API_KEY, getUser } from "api"
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
- import { a } from "a-module"
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 `separator` configuration
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
- separator: "",
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
- separator?: string | ((group: Group, index: number) => string | undefined)
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
- importSortSideEffect: false, // 是否对副作用导入排序
182
- importSortSeparator: "", // 分组分隔符
183
- importSortRemoveUnused: false, // 是否删除未使用的导入
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
- separator: "",
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
- separator: "\n",
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
- separator: "\n",
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
- ### importSortRemoveUnused
336
+ ### removeUnusedImports
352
337
 
353
338
  是否删除未使用的导入,默认为 `false`。
354
339
 
@@ -356,11 +341,11 @@ export default {
356
341
 
357
342
  **开启后(true)**:自动分析代码并删除未使用的导入。
358
343
 
359
- ```typescript
344
+ ```tsx
360
345
  // 排序前
361
- import React, { useState, useEffect } from "react"
362
- import { Button, Input } from "antd"
363
- import { helper } from "./utils"
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
- ### importSortSideEffect
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
- ### separator
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
- separator: ""
398
+ groupSeparator: ""
414
399
 
415
400
  // 函数:灵活控制
416
- separator: (group, index) => {
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
- import Default, * as Namespace from "module"
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
- import { type User, API_KEY, getUser } from "api"
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
- import { a } from "a-module"
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
- - 根据 `separator` 配置在分组之间插入分隔符
494
+ - 根据 `groupSeparator` 配置在分组之间插入分隔符
520
495
  - 保持注释关联
521
496
 
522
497
  #### 5. 插件入口 (`src/index.ts`)
package/dist/index.js CHANGED
@@ -153,7 +153,7 @@ function formatImportStatement(statement) {
153
153
  }
154
154
  function formatGroups(groups, config) {
155
155
  const lines = [];
156
- const separator = config.separator;
156
+ const separator = config.groupSeparator;
157
157
  for(let i = 0; i < groups.length; i++){
158
158
  const group = groups[i];
159
159
  if (void 0 !== separator && i > 0) {
@@ -399,7 +399,7 @@ function mergeConfig(userConfig) {
399
399
  sortGroup: userConfig.sortGroup ?? DEFAULT_CONFIG.sortGroup,
400
400
  sortImportStatement: userConfig.sortImportStatement ?? DEFAULT_CONFIG.sortImportStatement,
401
401
  sortImportContent: userConfig.sortImportContent ?? DEFAULT_CONFIG.sortImportContent,
402
- separator: userConfig.separator,
402
+ groupSeparator: userConfig.groupSeparator,
403
403
  sortSideEffect: userConfig.sortSideEffect ?? DEFAULT_CONFIG.sortSideEffect,
404
404
  removeUnusedImports: userConfig.removeUnusedImports ?? false
405
405
  };
@@ -583,9 +583,9 @@ function preprocessImports(text, options, config = {}) {
583
583
  sortGroup: config.sortGroup ?? optionsConfig.sortGroup,
584
584
  sortImportStatement: config.sortImportStatement ?? optionsConfig.sortImportStatement,
585
585
  sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
586
- separator: config.separator ?? optionsConfig.importSortSeparator ?? optionsConfig.separator,
587
- sortSideEffect: config.sortSideEffect ?? optionsConfig.importSortSideEffect ?? false,
588
- removeUnusedImports: config.removeUnusedImports ?? optionsConfig.importSortRemoveUnused ?? false
586
+ groupSeparator: config.groupSeparator ?? optionsConfig.groupSeparator,
587
+ sortSideEffect: config.sortSideEffect ?? optionsConfig.sortSideEffect ?? false,
588
+ removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false
589
589
  };
590
590
  let processedImports = imports;
591
591
  if (finalConfig.removeUnusedImports) {
@@ -642,21 +642,21 @@ function createCombinedPreprocess(parserName, config) {
642
642
  }
643
643
  function createPluginInstance(config = {}) {
644
644
  const baseOptions = {
645
- importSortSeparator: {
645
+ groupSeparator: {
646
646
  type: "string",
647
647
  category: "Import Sort",
648
- description: "分组之间的分隔符"
648
+ description: "����֮��ķָ���"
649
649
  },
650
- importSortSideEffect: {
650
+ sortSideEffect: {
651
651
  type: "boolean",
652
652
  category: "Import Sort",
653
- description: "是否对副作用导入进行排序",
653
+ description: "�Ƿ�Ը����õ����������",
654
654
  default: false
655
655
  },
656
- importSortRemoveUnused: {
656
+ removeUnusedImports: {
657
657
  type: "boolean",
658
658
  category: "Import Sort",
659
- description: "是否删除未使用的导入",
659
+ description: "�Ƿ�ɾ��δʹ�õĵ���",
660
660
  default: false
661
661
  }
662
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>, "separator" | "removeUnusedImports" | "otherPlugins" | "prettierOptions"> {
4
- separator: PluginConfig["separator"];
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
@@ -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 SeparatorFunction = (group: Group, index: number) => string | undefined;
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
- separator?: string | SeparatorFunction;
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.22",
3
+ "version": "0.0.23",
4
4
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
5
5
  "keywords": [
6
6
  "prettier",