@1adybug/prettier-plugin-sort-imports 0.0.27 → 0.0.28

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
@@ -152,6 +152,8 @@ interface PluginConfig {
152
152
  sortSideEffect?: boolean
153
153
  /** Whether to remove unused imports, defaults to false */
154
154
  removeUnusedImports?: boolean
155
+ /** Whether to add/remove the node: prefix for Node.js builtin modules */
156
+ nodeProtocol?: boolean
155
157
  }
156
158
  ```
157
159
 
@@ -167,6 +169,7 @@ export default {
167
169
  sortSideEffect: false, // Whether to sort side effect imports
168
170
  groupSeparator: "", // Group separator
169
171
  removeUnusedImports: false, // Whether to remove unused imports
172
+ nodeProtocol: true, // Add node: prefix for Node.js builtin modules (false to remove)
170
173
  }
171
174
  ```
172
175
 
@@ -198,6 +201,7 @@ export default {
198
201
  groupSeparator: "\n",
199
202
  sortSideEffect: true,
200
203
  removeUnusedImports: false,
204
+ nodeProtocol: true,
201
205
  }),
202
206
  ],
203
207
  }
@@ -347,7 +351,8 @@ Whether to remove unused imports, defaults to `false`.
347
351
 
348
352
  ```tsx
349
353
  // Before sorting
350
- import { useState } from "react"
354
+ // After sorting (with removeUnusedImports enabled)
355
+ import React, { useState } from "react"
351
356
 
352
357
  import { Button } from "antd"
353
358
 
@@ -356,10 +361,6 @@ function MyComponent() {
356
361
  return <Button>Click me</Button>
357
362
  }
358
363
 
359
- // After sorting (with removeUnusedImports enabled)
360
- import React, { useState } from "react"
361
- import { Button } from "antd"
362
-
363
364
  function MyComponent() {
364
365
  const [count, setCount] = useState(0)
365
366
  return <Button>Click me</Button>
@@ -373,6 +374,23 @@ function MyComponent() {
373
374
  - Analysis is AST-based and identifies actually used identifiers in code
374
375
  - Supports identifying JSX components, TypeScript type references, etc.
375
376
 
377
+ ### nodeProtocol
378
+
379
+ Whether to add/remove the `node:` prefix for Node.js builtin modules. Defaults to `undefined` (no change).
380
+
381
+ - `true`: add `node:` prefix
382
+ - `false`: remove `node:` prefix
383
+
384
+ ```ts
385
+ // Before
386
+ // nodeProtocol: false
387
+ import fs from "fs"
388
+ // nodeProtocol: true
389
+ import fs from "node:fs"
390
+ import path from "node:path"
391
+ import path from "path"
392
+ ```
393
+
376
394
  ### sortSideEffect
377
395
 
378
396
  Whether to sort side effect imports, defaults to `false`.
package/README.zh-CN.md CHANGED
@@ -151,6 +151,8 @@ interface PluginConfig {
151
151
  sortSideEffect?: boolean
152
152
  /** 是否删除未使用的导入,默认为 false */
153
153
  removeUnusedImports?: boolean
154
+ /** 是否为 Node.js 内置模块自动添加/移除 node: 前缀 */
155
+ nodeProtocol?: boolean
154
156
  }
155
157
  ```
156
158
 
@@ -166,6 +168,7 @@ export default {
166
168
  sortSideEffect: false, // 是否对副作用导入排序
167
169
  groupSeparator: "", // 分组分隔符
168
170
  removeUnusedImports: false, // 是否删除未使用的导入
171
+ nodeProtocol: true, // 为 Node.js 内置模块添加 node: 前缀(false 为移除)
169
172
  }
170
173
  ```
171
174
 
@@ -194,6 +197,7 @@ export default {
194
197
  groupSeparator: "",
195
198
  sortSideEffect: true,
196
199
  removeUnusedImports: false,
200
+ nodeProtocol: true,
197
201
  }),
198
202
  ],
199
203
  }
@@ -343,7 +347,8 @@ export default {
343
347
 
344
348
  ```tsx
345
349
  // 排序前
346
- import { useState } from "react"
350
+ // 排序后(开启 removeUnusedImports)
351
+ import React, { useState } from "react"
347
352
 
348
353
  import { Button } from "antd"
349
354
 
@@ -352,10 +357,6 @@ function MyComponent() {
352
357
  return <Button>Click me</Button>
353
358
  }
354
359
 
355
- // 排序后(开启 removeUnusedImports)
356
- import React, { useState } from "react"
357
- import { Button } from "antd"
358
-
359
360
  function MyComponent() {
360
361
  const [count, setCount] = useState(0)
361
362
  return <Button>Click me</Button>
@@ -369,6 +370,23 @@ function MyComponent() {
369
370
  - 分析基于 AST,会识别代码中实际使用的标识符
370
371
  - 支持识别 JSX 组件、TypeScript 类型引用等
371
372
 
373
+ ### nodeProtocol
374
+
375
+ 是否为 Node.js 内置模块自动添加/移除 `node:` 前缀,默认为 `undefined`(不处理)。
376
+
377
+ - `true`:自动添加 `node:` 前缀
378
+ - `false`:自动移除 `node:` 前缀
379
+
380
+ ```ts
381
+ // 排序前
382
+ // nodeProtocol: false
383
+ import fs from "fs"
384
+ // nodeProtocol: true
385
+ import fs from "node:fs"
386
+ import path from "node:path"
387
+ import path from "path"
388
+ ```
389
+
372
390
  ### sortSideEffect
373
391
 
374
392
  是否对副作用导入进行排序,默认为 `false`。
package/dist/index.d.ts CHANGED
@@ -17,6 +17,12 @@ export interface Options extends PrettierOptions {
17
17
  * @default false
18
18
  */
19
19
  removeUnusedImports?: boolean;
20
+ /**
21
+ * Whether to add/remove the node: prefix for Node.js builtin modules.
22
+ * true: add, false: remove, undefined: no change.
23
+ * @default undefined
24
+ */
25
+ nodeProtocol?: boolean;
20
26
  /**
21
27
  * 自定义获取分组名称。
22
28
  */
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createRequire } from "module";
1
+ import { builtinModules, createRequire } from "module";
2
2
  import { format } from "prettier";
3
3
  import { parse as parser_parse } from "@babel/parser";
4
4
  import traverse from "@babel/traverse";
@@ -419,7 +419,8 @@ function mergeConfig(userConfig) {
419
419
  sortImportContent: userConfig.sortImportContent ?? DEFAULT_CONFIG.sortImportContent,
420
420
  groupSeparator: userConfig.groupSeparator,
421
421
  sortSideEffect: userConfig.sortSideEffect ?? DEFAULT_CONFIG.sortSideEffect,
422
- removeUnusedImports: userConfig.removeUnusedImports ?? false
422
+ removeUnusedImports: userConfig.removeUnusedImports ?? false,
423
+ nodeProtocol: userConfig.nodeProtocol
423
424
  };
424
425
  }
425
426
  function sortImports(imports, userConfig) {
@@ -583,6 +584,22 @@ function mergeImports(imports) {
583
584
  return Array.from(mergedMap.values());
584
585
  }
585
586
  const src_require = createRequire(import.meta.url);
587
+ const NODE_BUILTIN_MODULES = new Set(builtinModules.map((moduleName)=>moduleName.startsWith("node:") ? moduleName.slice(5) : moduleName));
588
+ function isNodeBuiltinModule(modulePath) {
589
+ const normalizedPath = modulePath.startsWith("node:") ? modulePath.slice(5) : modulePath;
590
+ if (NODE_BUILTIN_MODULES.has(normalizedPath)) return true;
591
+ const slashIndex = normalizedPath.indexOf("/");
592
+ if (-1 === slashIndex) return false;
593
+ return NODE_BUILTIN_MODULES.has(normalizedPath.slice(0, slashIndex));
594
+ }
595
+ function applyNodeProtocol(modulePath, nodeProtocol) {
596
+ if (void 0 === nodeProtocol) return modulePath;
597
+ const hasNodePrefix = modulePath.startsWith("node:");
598
+ const normalizedPath = hasNodePrefix ? modulePath.slice(5) : modulePath;
599
+ if (!isNodeBuiltinModule(normalizedPath)) return modulePath;
600
+ if (nodeProtocol) return hasNodePrefix ? modulePath : `node:${modulePath}`;
601
+ return hasNodePrefix ? normalizedPath : modulePath;
602
+ }
586
603
  function getImportRanges(imports) {
587
604
  const ranges = imports.map((statement)=>({
588
605
  start: statement.start ?? 0,
@@ -632,12 +649,17 @@ function preprocessImports(text, options, config = {}) {
632
649
  sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
633
650
  groupSeparator: config.groupSeparator ?? optionsConfig.groupSeparator,
634
651
  sortSideEffect: config.sortSideEffect ?? optionsConfig.sortSideEffect ?? false,
635
- removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false
652
+ removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false,
653
+ nodeProtocol: config.nodeProtocol ?? optionsConfig.nodeProtocol
636
654
  };
637
655
  const importRanges = getImportRanges(imports);
638
656
  const textWithoutImports = removeRangesFromText(text, importRanges);
639
657
  let processedImports = imports;
640
658
  if (finalConfig.removeUnusedImports) processedImports = removeUnusedImportsFromStatements(imports, textWithoutImports);
659
+ if (void 0 !== finalConfig.nodeProtocol) processedImports = processedImports.map((statement)=>({
660
+ ...statement,
661
+ path: applyNodeProtocol(statement.path, finalConfig.nodeProtocol)
662
+ }));
641
663
  const sortedImports = sortImports(processedImports, finalConfig);
642
664
  const mergedImports = mergeImports(sortedImports);
643
665
  let formattedImports;
@@ -702,6 +724,11 @@ function createPluginInstance(config = {}) {
702
724
  category: "Import Sort",
703
725
  description: "�Ƿ�ɾ��δʹ�õĵ���",
704
726
  default: false
727
+ },
728
+ nodeProtocol: {
729
+ type: "boolean",
730
+ category: "Import Sort",
731
+ description: "Use node: prefix for Node.js builtin modules"
705
732
  }
706
733
  };
707
734
  const otherPlugins = config.otherPlugins || [];
package/dist/sorter.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Group, ImportContent, ImportStatement, PluginConfig } from "./types";
2
2
  /** 合并后的配置 */
3
- export interface MergedConfig extends Omit<Required<PluginConfig>, "groupSeparator" | "removeUnusedImports" | "otherPlugins" | "prettierOptions"> {
3
+ export interface MergedConfig extends Omit<Required<PluginConfig>, "groupSeparator" | "removeUnusedImports" | "nodeProtocol" | "otherPlugins" | "prettierOptions"> {
4
4
  groupSeparator: PluginConfig["groupSeparator"];
5
5
  removeUnusedImports: boolean;
6
+ nodeProtocol?: boolean;
6
7
  }
7
8
  /** 对导入语句进行排序 */
8
9
  export declare function sortImports(imports: ImportStatement[], userConfig: PluginConfig): ImportStatement[];
package/dist/types.d.ts CHANGED
@@ -76,6 +76,8 @@ export interface PluginConfig {
76
76
  sortSideEffect?: boolean;
77
77
  /** 是否删除未使用的导入,默认为 false */
78
78
  removeUnusedImports?: boolean;
79
+ /** Whether to add/remove the node: prefix for Node.js builtin modules */
80
+ nodeProtocol?: boolean;
79
81
  /** 要合并的其他 Prettier 插件,按传入顺序执行 */
80
82
  otherPlugins?: Plugin[];
81
83
  /** 传递给其他插件的 Prettier 配置选项 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1adybug/prettier-plugin-sort-imports",
3
- "version": "0.0.27",
3
+ "version": "0.0.28",
4
4
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
5
5
  "keywords": [
6
6
  "prettier",