@1adybug/prettier-plugin-sort-imports 0.0.19 → 0.0.20
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 +6 -18
- package/README.zh-CN.md +6 -18
- package/dist/index.js +6 -6
- package/dist/sorter.d.ts +1 -1
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -234,29 +234,19 @@ export default createPlugin({
|
|
|
234
234
|
const path = statement.path
|
|
235
235
|
|
|
236
236
|
// React and related libraries
|
|
237
|
-
if (path.startsWith("react") || path.startsWith("@react"))
|
|
238
|
-
return "react"
|
|
239
|
-
}
|
|
237
|
+
if (path.startsWith("react") || path.startsWith("@react")) return "react"
|
|
240
238
|
|
|
241
239
|
// UI libraries
|
|
242
|
-
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra"))
|
|
243
|
-
return "ui"
|
|
244
|
-
}
|
|
240
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) return "ui"
|
|
245
241
|
|
|
246
242
|
// Utility libraries
|
|
247
|
-
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns"))
|
|
248
|
-
return "utils"
|
|
249
|
-
}
|
|
243
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) return "utils"
|
|
250
244
|
|
|
251
245
|
// External packages (node_modules)
|
|
252
|
-
if (!path.startsWith(".") && !path.startsWith("@/"))
|
|
253
|
-
return "external"
|
|
254
|
-
}
|
|
246
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) return "external"
|
|
255
247
|
|
|
256
248
|
// Internal aliases (@/)
|
|
257
|
-
if (path.startsWith("@/"))
|
|
258
|
-
return "internal"
|
|
259
|
-
}
|
|
249
|
+
if (path.startsWith("@/")) return "internal"
|
|
260
250
|
|
|
261
251
|
// Relative imports
|
|
262
252
|
return "relative"
|
|
@@ -272,9 +262,7 @@ export default createPlugin({
|
|
|
272
262
|
// Custom import content sorting
|
|
273
263
|
sortImportContent: (a, b) => {
|
|
274
264
|
// Types first, then variables
|
|
275
|
-
if (a.type !== b.type)
|
|
276
|
-
return a.type === "type" ? -1 : 1
|
|
277
|
-
}
|
|
265
|
+
if (a.type !== b.type) return a.type === "type" ? -1 : 1
|
|
278
266
|
|
|
279
267
|
// Alphabetical order within same type
|
|
280
268
|
const aName = a.alias ?? a.name
|
package/README.zh-CN.md
CHANGED
|
@@ -230,29 +230,19 @@ export default createPlugin({
|
|
|
230
230
|
const path = statement.path
|
|
231
231
|
|
|
232
232
|
// React 及相关库
|
|
233
|
-
if (path.startsWith("react") || path.startsWith("@react"))
|
|
234
|
-
return "react"
|
|
235
|
-
}
|
|
233
|
+
if (path.startsWith("react") || path.startsWith("@react")) return "react"
|
|
236
234
|
|
|
237
235
|
// UI 库
|
|
238
|
-
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra"))
|
|
239
|
-
return "ui"
|
|
240
|
-
}
|
|
236
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) return "ui"
|
|
241
237
|
|
|
242
238
|
// 工具库
|
|
243
|
-
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns"))
|
|
244
|
-
return "utils"
|
|
245
|
-
}
|
|
239
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) return "utils"
|
|
246
240
|
|
|
247
241
|
// 外部包 (node_modules)
|
|
248
|
-
if (!path.startsWith(".") && !path.startsWith("@/"))
|
|
249
|
-
return "external"
|
|
250
|
-
}
|
|
242
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) return "external"
|
|
251
243
|
|
|
252
244
|
// 内部别名 (@/)
|
|
253
|
-
if (path.startsWith("@/"))
|
|
254
|
-
return "internal"
|
|
255
|
-
}
|
|
245
|
+
if (path.startsWith("@/")) return "internal"
|
|
256
246
|
|
|
257
247
|
// 相对导入
|
|
258
248
|
return "relative"
|
|
@@ -268,9 +258,7 @@ export default createPlugin({
|
|
|
268
258
|
// 自定义导入内容排序
|
|
269
259
|
sortImportContent: (a, b) => {
|
|
270
260
|
// 类型在前,变量在后
|
|
271
|
-
if (a.type !== b.type)
|
|
272
|
-
return a.type === "type" ? -1 : 1
|
|
273
|
-
}
|
|
261
|
+
if (a.type !== b.type) return a.type === "type" ? -1 : 1
|
|
274
262
|
|
|
275
263
|
// 同类型内按字母顺序
|
|
276
264
|
const aName = a.alias ?? a.name
|
package/dist/index.js
CHANGED
|
@@ -195,10 +195,8 @@ function parseImports(code, filepath) {
|
|
|
195
195
|
return importStatements;
|
|
196
196
|
}
|
|
197
197
|
function parseImportNode(node, comments, usedComments, code, isFirstImport, filepath) {
|
|
198
|
-
node.type;
|
|
199
198
|
const source = node.source?.value ?? "";
|
|
200
199
|
const nodeStartLine = node.loc?.start.line ?? 0;
|
|
201
|
-
node.loc?.end.line;
|
|
202
200
|
const nodeStart = node.start ?? 0;
|
|
203
201
|
let nodeEnd = node.end ?? 0;
|
|
204
202
|
const leadingComments = [];
|
|
@@ -463,21 +461,23 @@ function groupImports(imports, userConfig) {
|
|
|
463
461
|
const groupMap = new Map();
|
|
464
462
|
for (const statement of imports){
|
|
465
463
|
const groupName = config.getGroup(statement);
|
|
466
|
-
const key = `${groupName}|||${statement.isSideEffect}`;
|
|
464
|
+
const key = `${groupName}|||${statement.isSideEffect}|||${statement.isExport}`;
|
|
467
465
|
const statements = groupMap.get(key) ?? [];
|
|
468
466
|
statements.push(statement);
|
|
469
467
|
groupMap.set(key, statements);
|
|
470
468
|
}
|
|
471
469
|
const groups = [];
|
|
472
470
|
for (const [key, statements] of Array.from(groupMap.entries())){
|
|
473
|
-
const
|
|
474
|
-
const
|
|
475
|
-
const isSideEffect = "true" ===
|
|
471
|
+
const parts = key.split("|||");
|
|
472
|
+
const isExport = "true" === parts.pop();
|
|
473
|
+
const isSideEffect = "true" === parts.pop();
|
|
474
|
+
const name = parts.join("|||");
|
|
476
475
|
const filepath = statements[0].filepath;
|
|
477
476
|
groups.push({
|
|
478
477
|
filepath,
|
|
479
478
|
name,
|
|
480
479
|
isSideEffect,
|
|
480
|
+
isExport,
|
|
481
481
|
importStatements: statements
|
|
482
482
|
});
|
|
483
483
|
}
|
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
|
-
/** 对导入语句进行分组,同时根据 name 和
|
|
9
|
+
/** 对导入语句进行分组,同时根据 name、isSideEffect 和 isExport 区分 */
|
|
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/dist/types.d.ts
CHANGED