@1adybug/prettier-plugin-sort-imports 0.0.16 → 0.0.17

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
@@ -1,4 +1,4 @@
1
- # Prettier Plugin Import Sorts
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,10 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
44
44
  ### Basic Sorting
45
45
 
46
46
  ```typescript
47
-
47
+ import React, { useEffect, useState } from "react"
48
+ import { Button } from "antd"
49
+ import { sum } from "./utils"
50
+ import "./styles.css"
48
51
  ```
49
52
 
50
53
  ### Custom Grouping and Sorting
@@ -78,6 +81,13 @@ export default {
78
81
  Result:
79
82
 
80
83
  ```typescript
84
+ import React, { useState } from "react"
85
+
86
+ import { Button } from "antd"
87
+ import { format } from "date-fns"
88
+
89
+ import { Header } from "./components/Header"
90
+ import { sum } from "./utils"
81
91
  import "./styles.css"
82
92
  ```
83
93
 
@@ -438,7 +448,13 @@ separator: (group, index) => {
438
448
  3. Named imports are sorted by `type` priority, then alphabetically by final import name
439
449
 
440
450
  ```typescript
441
-
451
+ import Default, * as Namespace from "module"
452
+ import {
453
+ type TypeA,
454
+ type TypeB,
455
+ VariableA,
456
+ VariableB,
457
+ } from "module"
442
458
  ```
443
459
 
444
460
  **Custom behavior**:
@@ -457,7 +473,11 @@ createPlugin({
457
473
  ```
458
474
 
459
475
  ```typescript
460
-
476
+ import {
477
+ API_KEY,
478
+ type User,
479
+ getUser,
480
+ } from "api"
461
481
  ```
462
482
 
463
483
  ### Import Statement Sorting
@@ -465,7 +485,9 @@ createPlugin({
465
485
  Import statements are sorted alphabetically by module path:
466
486
 
467
487
  ```typescript
468
-
488
+ import { a } from "a-module"
489
+ import { b } from "b-module"
490
+ import { c } from "c-module"
469
491
  ```
470
492
 
471
493
  ### Comment Handling
@@ -473,7 +495,14 @@ Import statements are sorted alphabetically by module path:
473
495
  Comments follow the import statements they are attached to:
474
496
 
475
497
  ```typescript
498
+ // React related imports
499
+ import React from "react"
500
+
501
+ // UI components
502
+ import { Button } from "antd"
476
503
 
504
+ // Utilities
505
+ import { sum } from "./utils"
477
506
  ```
478
507
 
479
508
  ## Implementation Details
package/README.zh-CN.md CHANGED
@@ -1,4 +1,4 @@
1
- # Prettier Plugin Import Sorts
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,10 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
43
43
  ### 基本排序
44
44
 
45
45
  ```typescript
46
-
46
+ import React, { useEffect, useState } from "react"
47
+ import { Button } from "antd"
48
+ import { sum } from "./utils"
49
+ import "./styles.css"
47
50
  ```
48
51
 
49
52
  ### 自定义分组和排序
@@ -77,6 +80,13 @@ export default {
77
80
  结果:
78
81
 
79
82
  ```typescript
83
+ import React, { useState } from "react"
84
+
85
+ import { Button } from "antd"
86
+ import { format } from "date-fns"
87
+
88
+ import { Header } from "./components/Header"
89
+ import { sum } from "./utils"
80
90
  import "./styles.css"
81
91
  ```
82
92
 
@@ -430,7 +440,13 @@ separator: (group, index) => {
430
440
  3. 命名导入按照 `type` 类型优先,然后按最终导入名称字母顺序排序
431
441
 
432
442
  ```typescript
433
-
443
+ import Default, * as Namespace from "module"
444
+ import {
445
+ type TypeA,
446
+ type TypeB,
447
+ VariableA,
448
+ VariableB,
449
+ } from "module"
434
450
  ```
435
451
 
436
452
  **自定义行为**:
@@ -449,7 +465,11 @@ createPlugin({
449
465
  ```
450
466
 
451
467
  ```typescript
452
-
468
+ import {
469
+ API_KEY,
470
+ type User,
471
+ getUser,
472
+ } from "api"
453
473
  ```
454
474
 
455
475
  ### 导入语句排序
@@ -457,7 +477,9 @@ createPlugin({
457
477
  导入语句按模块路径的字母顺序排序:
458
478
 
459
479
  ```typescript
460
-
480
+ import { a } from "a-module"
481
+ import { b } from "b-module"
482
+ import { c } from "c-module"
461
483
  ```
462
484
 
463
485
  ### 注释处理
@@ -465,7 +487,14 @@ createPlugin({
465
487
  注释会跟随它们所附加的导入语句一起移动:
466
488
 
467
489
  ```typescript
490
+ // React 相关导入
491
+ import React from "react"
492
+
493
+ // UI 组件
494
+ import { Button } from "antd"
468
495
 
496
+ // 工具函数
497
+ import { sum } from "./utils"
469
498
  ```
470
499
 
471
500
  ## 实现细节
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 statements = groupMap.get(groupName) ?? [];
462
+ const key = `${groupName}|||${statement.isSideEffect}`;
463
+ const statements = groupMap.get(key) ?? [];
463
464
  statements.push(statement);
464
- groupMap.set(groupName, statements);
465
+ groupMap.set(key, statements);
465
466
  }
466
467
  const groups = [];
467
- for (const [name, statements] of Array.from(groupMap.entries())){
468
- const isSideEffect = statements.every((s)=>s.isSideEffect);
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@1adybug/prettier-plugin-sort-imports",
3
3
  "type": "module",
4
- "version": "0.0.16",
4
+ "version": "0.0.17",
5
5
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
6
6
  "keywords": [
7
7
  "prettier",
@@ -47,7 +47,8 @@
47
47
  "format": "prettier --write .",
48
48
  "test": "bun test",
49
49
  "test:watch": "bun test --watch",
50
- "fg": "npm run format && git add . && git commit -m \"✨feature: format\""
50
+ "fg": "npm run format && git add . && git commit -m \"✨feature: format\"",
51
+ "ucr": "npx zixulu acr"
51
52
  },
52
53
  "devDependencies": {
53
54
  "@rslib/core": "^0.15.0",