@1adybug/prettier-plugin-sort-imports 0.0.20 → 0.0.22
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.js +38 -26
- package/dist/types.d.ts +2 -2
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { createRequire } from "module";
|
|
2
|
-
import {
|
|
3
|
-
import { parse } from "@babel/parser";
|
|
2
|
+
import { format } from "prettier";
|
|
3
|
+
import { parse as parser_parse } from "@babel/parser";
|
|
4
4
|
import traverse from "@babel/traverse";
|
|
5
5
|
const analyzer_traverse = "function" == typeof traverse ? traverse : traverse["default"];
|
|
6
6
|
function analyzeUsedIdentifiers(code) {
|
|
7
7
|
const usedIdentifiers = new Set();
|
|
8
8
|
try {
|
|
9
|
-
const ast =
|
|
9
|
+
const ast = parser_parse(code, {
|
|
10
10
|
sourceType: "module",
|
|
11
11
|
plugins: [
|
|
12
12
|
"typescript",
|
|
@@ -174,7 +174,7 @@ function formatImportStatements(statements) {
|
|
|
174
174
|
function parseImports(code, filepath) {
|
|
175
175
|
const hasImportOrExport = /^\s*(import|export)\s/m.test(code);
|
|
176
176
|
if (!hasImportOrExport) return [];
|
|
177
|
-
const ast =
|
|
177
|
+
const ast = parser_parse(code, {
|
|
178
178
|
sourceType: "module",
|
|
179
179
|
plugins: [
|
|
180
180
|
"typescript",
|
|
@@ -574,9 +574,8 @@ function preprocessImports(text, options, config = {}) {
|
|
|
574
574
|
"babel-ts"
|
|
575
575
|
];
|
|
576
576
|
if (!parser || !supportedParsers.includes(parser)) return text;
|
|
577
|
-
const
|
|
578
|
-
const
|
|
579
|
-
const imports = parseImports(text, relativeFilepath);
|
|
577
|
+
const filepath = options.filepath;
|
|
578
|
+
const imports = parseImports(text, filepath);
|
|
580
579
|
if (0 === imports.length) return text;
|
|
581
580
|
const optionsConfig = options;
|
|
582
581
|
const finalConfig = {
|
|
@@ -618,26 +617,25 @@ function preprocessImports(text, options, config = {}) {
|
|
|
618
617
|
const { parsers: { babel } } = src_require("prettier/parser-babel");
|
|
619
618
|
const { parsers: { typescript } } = src_require("prettier/parser-typescript");
|
|
620
619
|
const { parsers: { "babel-ts": babelTs } } = src_require("prettier/parser-babel");
|
|
620
|
+
const PROCESSING_MARKER = Symbol("prettier-plugin-sort-imports-processing");
|
|
621
621
|
function createCombinedPreprocess(parserName, config) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
for (const preprocess of preprocessFunctions)try {
|
|
638
|
-
processedText = preprocess(processedText, mergedOptions);
|
|
622
|
+
const otherPlugins = config.otherPlugins || [];
|
|
623
|
+
const pluginsWithPreprocess = otherPlugins.filter((plugin)=>{
|
|
624
|
+
const parser = plugin?.parsers?.[parserName];
|
|
625
|
+
return parser?.preprocess && "function" == typeof parser.preprocess;
|
|
626
|
+
});
|
|
627
|
+
return async function(text, options) {
|
|
628
|
+
if (options[PROCESSING_MARKER]) return text;
|
|
629
|
+
let processedText = preprocessImports(text, options, config);
|
|
630
|
+
if (0 === pluginsWithPreprocess.length) return processedText;
|
|
631
|
+
try {
|
|
632
|
+
processedText = await format(processedText, {
|
|
633
|
+
...options,
|
|
634
|
+
plugins: pluginsWithPreprocess,
|
|
635
|
+
[PROCESSING_MARKER]: true
|
|
636
|
+
});
|
|
639
637
|
} catch (error) {
|
|
640
|
-
console.warn("
|
|
638
|
+
console.warn("Failed to apply other plugins preprocess:", error instanceof Error ? error.message : String(error));
|
|
641
639
|
}
|
|
642
640
|
return processedText;
|
|
643
641
|
};
|
|
@@ -685,16 +683,30 @@ function createPluginInstance(config = {}) {
|
|
|
685
683
|
let merged = {
|
|
686
684
|
...baseParser
|
|
687
685
|
};
|
|
686
|
+
const transformASTFunctions = [];
|
|
688
687
|
for (const plugin of otherPlugins){
|
|
689
688
|
const otherParser = plugin?.parsers?.[parserName];
|
|
690
689
|
if (otherParser) {
|
|
691
|
-
|
|
690
|
+
if ("function" == typeof otherParser.__transformAST) transformASTFunctions.push(otherParser.__transformAST);
|
|
691
|
+
const { preprocess, parse, __transformAST, ...otherAttrs } = otherParser;
|
|
692
692
|
merged = {
|
|
693
693
|
...merged,
|
|
694
694
|
...otherAttrs
|
|
695
695
|
};
|
|
696
696
|
}
|
|
697
697
|
}
|
|
698
|
+
if (transformASTFunctions.length > 0) {
|
|
699
|
+
const originalParse = baseParser.parse;
|
|
700
|
+
merged.parse = function(text, options) {
|
|
701
|
+
let ast = originalParse(text, options);
|
|
702
|
+
for (const transformAST of transformASTFunctions)try {
|
|
703
|
+
ast = transformAST(ast, options);
|
|
704
|
+
} catch (error) {
|
|
705
|
+
console.warn("Plugin transformAST failed:", error instanceof Error ? error.message : String(error));
|
|
706
|
+
}
|
|
707
|
+
return ast;
|
|
708
|
+
};
|
|
709
|
+
}
|
|
698
710
|
merged.preprocess = createCombinedPreprocess(parserName, config);
|
|
699
711
|
mergedParsers[parserName] = merged;
|
|
700
712
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface ImportContent {
|
|
|
14
14
|
}
|
|
15
15
|
/** 导入语句 */
|
|
16
16
|
export interface ImportStatement {
|
|
17
|
-
/**
|
|
17
|
+
/** Absolute path of current file */
|
|
18
18
|
filepath?: string;
|
|
19
19
|
/** 导入的模块路径,可以是相对路径或绝对路径,比如 react, react-dom 或者 ./utils/index,@/utils/index 等 */
|
|
20
20
|
path: string;
|
|
@@ -39,7 +39,7 @@ export interface ImportStatement {
|
|
|
39
39
|
}
|
|
40
40
|
/** 分组 */
|
|
41
41
|
export interface Group {
|
|
42
|
-
/**
|
|
42
|
+
/** Absolute path of current file */
|
|
43
43
|
filepath?: string;
|
|
44
44
|
/** 分组名称,默认为 default */
|
|
45
45
|
name: string;
|
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.22",
|
|
4
4
|
"description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prettier",
|
|
@@ -43,12 +43,13 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@babel/core": "^7.28.4",
|
|
45
45
|
"@babel/parser": "^7.28.4",
|
|
46
|
-
"@babel/traverse": "^7.28.
|
|
46
|
+
"@babel/traverse": "^7.28.5",
|
|
47
47
|
"@babel/types": "^7.28.4"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@rslib/core": "^0.15.0",
|
|
51
51
|
"@types/babel__core": "^7.20.5",
|
|
52
|
+
"@types/babel__traverse": "^7.28.0",
|
|
52
53
|
"@types/bun": "latest",
|
|
53
54
|
"@types/node": "^22.18.6",
|
|
54
55
|
"json5": "^2.2.3",
|
|
@@ -56,7 +57,7 @@
|
|
|
56
57
|
"typescript": "^5.9.2"
|
|
57
58
|
},
|
|
58
59
|
"peerDependencies": {
|
|
59
|
-
"prettier": "^3.
|
|
60
|
+
"prettier": "^3.7.3"
|
|
60
61
|
},
|
|
61
62
|
"scripts": {
|
|
62
63
|
"build": "rslib build",
|