@1adybug/prettier-plugin-sort-imports 0.0.25 → 0.0.27
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/dist/index.d.ts +35 -2
- package/dist/index.js +37 -11
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
|
-
import { Plugin } from "prettier";
|
|
2
|
-
import type { PluginConfig } from "./types";
|
|
1
|
+
import { Plugin, Options as PrettierOptions } from "prettier";
|
|
2
|
+
import type { GetGroupFunction, GroupSeparatorFunction, PluginConfig, SortGroupFunction, SortImportContentFunction, SortImportStatementFunction } from "./types";
|
|
3
3
|
export * from "./types";
|
|
4
|
+
export interface Options extends PrettierOptions {
|
|
5
|
+
/**
|
|
6
|
+
* 分组之间的分隔符,支持字符串或函数返回。
|
|
7
|
+
* @default undefined
|
|
8
|
+
*/
|
|
9
|
+
groupSeparator?: string | GroupSeparatorFunction;
|
|
10
|
+
/**
|
|
11
|
+
* 是否对副作用导入进行排序。
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
sortSideEffect?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否删除未使用的导入。
|
|
17
|
+
* @default false
|
|
18
|
+
*/
|
|
19
|
+
removeUnusedImports?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* 自定义获取分组名称。
|
|
22
|
+
*/
|
|
23
|
+
getGroup?: GetGroupFunction;
|
|
24
|
+
/**
|
|
25
|
+
* 自定义分组排序逻辑。
|
|
26
|
+
*/
|
|
27
|
+
sortGroup?: SortGroupFunction;
|
|
28
|
+
/**
|
|
29
|
+
* 自定义导入语句排序逻辑。
|
|
30
|
+
*/
|
|
31
|
+
sortImportStatement?: SortImportStatementFunction;
|
|
32
|
+
/**
|
|
33
|
+
* 自定义导入内容排序逻辑。
|
|
34
|
+
*/
|
|
35
|
+
sortImportContent?: SortImportContentFunction;
|
|
36
|
+
}
|
|
4
37
|
/** 创建自定义配置的插件(工厂函数) */
|
|
5
38
|
export declare function createPlugin(config?: PluginConfig): Plugin;
|
|
6
39
|
/** 默认插件实例(用于简单使用) */
|
package/dist/index.js
CHANGED
|
@@ -209,7 +209,7 @@ function parseImports(code, filepath) {
|
|
|
209
209
|
const statement = parseImportNode(node, ast.comments ?? [], usedComments, code, isFirstImport, filepath);
|
|
210
210
|
importStatements.push(statement);
|
|
211
211
|
isFirstImport = false;
|
|
212
|
-
}
|
|
212
|
+
}
|
|
213
213
|
return importStatements;
|
|
214
214
|
}
|
|
215
215
|
function parseImportNode(node, comments, usedComments, code, isFirstImport, filepath) {
|
|
@@ -583,6 +583,35 @@ function mergeImports(imports) {
|
|
|
583
583
|
return Array.from(mergedMap.values());
|
|
584
584
|
}
|
|
585
585
|
const src_require = createRequire(import.meta.url);
|
|
586
|
+
function getImportRanges(imports) {
|
|
587
|
+
const ranges = imports.map((statement)=>({
|
|
588
|
+
start: statement.start ?? 0,
|
|
589
|
+
end: statement.end ?? 0
|
|
590
|
+
})).filter((range)=>range.end > range.start).sort((a, b)=>a.start - b.start);
|
|
591
|
+
const merged = [];
|
|
592
|
+
for (const range of ranges){
|
|
593
|
+
const last = merged[merged.length - 1];
|
|
594
|
+
if (last && range.start <= last.end) {
|
|
595
|
+
last.end = Math.max(last.end, range.end);
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
merged.push({
|
|
599
|
+
...range
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
return merged;
|
|
603
|
+
}
|
|
604
|
+
function removeRangesFromText(text, ranges) {
|
|
605
|
+
if (0 === ranges.length) return text;
|
|
606
|
+
let result = "";
|
|
607
|
+
let cursor = 0;
|
|
608
|
+
for (const range of ranges){
|
|
609
|
+
result += text.slice(cursor, range.start);
|
|
610
|
+
cursor = range.end;
|
|
611
|
+
}
|
|
612
|
+
result += text.slice(cursor);
|
|
613
|
+
return result;
|
|
614
|
+
}
|
|
586
615
|
function preprocessImports(text, options, config = {}) {
|
|
587
616
|
try {
|
|
588
617
|
const parser = options.parser;
|
|
@@ -605,12 +634,10 @@ function preprocessImports(text, options, config = {}) {
|
|
|
605
634
|
sortSideEffect: config.sortSideEffect ?? optionsConfig.sortSideEffect ?? false,
|
|
606
635
|
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.removeUnusedImports ?? false
|
|
607
636
|
};
|
|
637
|
+
const importRanges = getImportRanges(imports);
|
|
638
|
+
const textWithoutImports = removeRangesFromText(text, importRanges);
|
|
608
639
|
let processedImports = imports;
|
|
609
|
-
if (finalConfig.removeUnusedImports)
|
|
610
|
-
const lastImport = imports[imports.length - 1];
|
|
611
|
-
const codeAfterImports = text.slice(lastImport.end ?? 0);
|
|
612
|
-
processedImports = removeUnusedImportsFromStatements(imports, codeAfterImports);
|
|
613
|
-
}
|
|
640
|
+
if (finalConfig.removeUnusedImports) processedImports = removeUnusedImportsFromStatements(imports, textWithoutImports);
|
|
614
641
|
const sortedImports = sortImports(processedImports, finalConfig);
|
|
615
642
|
const mergedImports = mergeImports(sortedImports);
|
|
616
643
|
let formattedImports;
|
|
@@ -620,13 +647,12 @@ function preprocessImports(text, options, config = {}) {
|
|
|
620
647
|
formattedImports = formatGroups(sortedGroups, finalConfig, options.trailingComma);
|
|
621
648
|
} else formattedImports = formatImportStatements(mergedImports, options.trailingComma);
|
|
622
649
|
const firstImport = imports[0];
|
|
623
|
-
const lastImport = imports[imports.length - 1];
|
|
624
650
|
const startIndex = firstImport.start ?? 0;
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
651
|
+
const beforeImports = textWithoutImports.slice(0, startIndex);
|
|
652
|
+
let afterImports = textWithoutImports.slice(startIndex);
|
|
653
|
+
if (afterImports) afterImports = afterImports.replace(/^\n+/, "\n");
|
|
628
654
|
const needsExtraNewline = afterImports && !afterImports.startsWith("\n");
|
|
629
|
-
const separator = needsExtraNewline ? "\n\n" : "\n";
|
|
655
|
+
const separator = afterImports ? needsExtraNewline ? "\n\n" : "\n" : "";
|
|
630
656
|
return beforeImports + formattedImports + separator + afterImports;
|
|
631
657
|
} catch (error) {
|
|
632
658
|
return text;
|
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.27",
|
|
4
4
|
"description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prettier",
|
|
@@ -41,23 +41,23 @@
|
|
|
41
41
|
"registry": "https://registry.npmjs.com/"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@babel/core": "^7.28.
|
|
45
|
-
"@babel/parser": "^7.28.
|
|
46
|
-
"@babel/traverse": "^7.28.
|
|
47
|
-
"@babel/types": "^7.28.
|
|
44
|
+
"@babel/core": "^7.28.6",
|
|
45
|
+
"@babel/parser": "^7.28.6",
|
|
46
|
+
"@babel/traverse": "^7.28.6",
|
|
47
|
+
"@babel/types": "^7.28.6"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@rslib/core": "^0.15.
|
|
50
|
+
"@rslib/core": "^0.15.1",
|
|
51
51
|
"@types/babel__core": "^7.20.5",
|
|
52
52
|
"@types/babel__traverse": "^7.28.0",
|
|
53
53
|
"@types/bun": "latest",
|
|
54
|
-
"@types/node": "^
|
|
54
|
+
"@types/node": "^24.10.8",
|
|
55
55
|
"json5": "^2.2.3",
|
|
56
56
|
"supports-color": "^10.2.2",
|
|
57
|
-
"typescript": "^5.9.
|
|
57
|
+
"typescript": "^5.9.3"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"prettier": "^3.
|
|
60
|
+
"prettier": "^3.8.0"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "rslib build",
|