@1adybug/prettier-plugin-sort-imports 0.0.27 → 0.0.29
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 +23 -5
- package/README.zh-CN.md +23 -5
- package/dist/index.d.ts +6 -0
- package/dist/index.js +31 -3
- package/dist/sorter.d.ts +2 -1
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
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?: "add" | "remove"
|
|
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: "add", // "add" to add node: prefix ("remove" 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: "add",
|
|
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
|
-
|
|
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
|
+
- `"add"`: add `node:` prefix
|
|
382
|
+
- `"remove"`: remove `node:` prefix
|
|
383
|
+
|
|
384
|
+
```ts
|
|
385
|
+
// Before
|
|
386
|
+
// nodeProtocol: "remove"
|
|
387
|
+
import fs from "fs"
|
|
388
|
+
// nodeProtocol: "add"
|
|
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?: "add" | "remove"
|
|
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: "add", // "add" 为添加 node: 前缀("remove" 为移除)
|
|
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: "add",
|
|
197
201
|
}),
|
|
198
202
|
],
|
|
199
203
|
}
|
|
@@ -343,7 +347,8 @@ export default {
|
|
|
343
347
|
|
|
344
348
|
```tsx
|
|
345
349
|
// 排序前
|
|
346
|
-
|
|
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
|
+
- `"add"`:自动添加 `node:` 前缀
|
|
378
|
+
- `"remove"`:自动移除 `node:` 前缀
|
|
379
|
+
|
|
380
|
+
```ts
|
|
381
|
+
// 排序前
|
|
382
|
+
// nodeProtocol: "remove"
|
|
383
|
+
import fs from "fs"
|
|
384
|
+
// nodeProtocol: "add"
|
|
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
|
+
* "add": add, "remove": remove, undefined: no change.
|
|
23
|
+
* @default undefined
|
|
24
|
+
*/
|
|
25
|
+
nodeProtocol?: "add" | "remove";
|
|
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,23 @@ 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 ("add" === nodeProtocol) return hasNodePrefix ? modulePath : `node:${modulePath}`;
|
|
601
|
+
if ("remove" === nodeProtocol) return hasNodePrefix ? normalizedPath : modulePath;
|
|
602
|
+
return modulePath;
|
|
603
|
+
}
|
|
586
604
|
function getImportRanges(imports) {
|
|
587
605
|
const ranges = imports.map((statement)=>({
|
|
588
606
|
start: statement.start ?? 0,
|
|
@@ -632,12 +650,17 @@ function preprocessImports(text, options, config = {}) {
|
|
|
632
650
|
sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
|
|
633
651
|
groupSeparator: config.groupSeparator ?? optionsConfig.groupSeparator,
|
|
634
652
|
sortSideEffect: config.sortSideEffect ?? optionsConfig.sortSideEffect ?? false,
|
|
635
|
-
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false
|
|
653
|
+
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false,
|
|
654
|
+
nodeProtocol: config.nodeProtocol ?? optionsConfig.nodeProtocol
|
|
636
655
|
};
|
|
637
656
|
const importRanges = getImportRanges(imports);
|
|
638
657
|
const textWithoutImports = removeRangesFromText(text, importRanges);
|
|
639
658
|
let processedImports = imports;
|
|
640
659
|
if (finalConfig.removeUnusedImports) processedImports = removeUnusedImportsFromStatements(imports, textWithoutImports);
|
|
660
|
+
if (void 0 !== finalConfig.nodeProtocol) processedImports = processedImports.map((statement)=>({
|
|
661
|
+
...statement,
|
|
662
|
+
path: applyNodeProtocol(statement.path, finalConfig.nodeProtocol)
|
|
663
|
+
}));
|
|
641
664
|
const sortedImports = sortImports(processedImports, finalConfig);
|
|
642
665
|
const mergedImports = mergeImports(sortedImports);
|
|
643
666
|
let formattedImports;
|
|
@@ -702,6 +725,11 @@ function createPluginInstance(config = {}) {
|
|
|
702
725
|
category: "Import Sort",
|
|
703
726
|
description: "�Ƿ�ɾ��δʹ�õĵ���",
|
|
704
727
|
default: false
|
|
728
|
+
},
|
|
729
|
+
nodeProtocol: {
|
|
730
|
+
type: "string",
|
|
731
|
+
category: "Import Sort",
|
|
732
|
+
description: "Add/remove node: prefix for Node.js builtin modules"
|
|
705
733
|
}
|
|
706
734
|
};
|
|
707
735
|
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?: PluginConfig["nodeProtocol"];
|
|
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?: "add" | "remove";
|
|
79
81
|
/** 要合并的其他 Prettier 插件,按传入顺序执行 */
|
|
80
82
|
otherPlugins?: Plugin[];
|
|
81
83
|
/** 传递给其他插件的 Prettier 配置选项 */
|