@1adybug/prettier-plugin-sort-imports 0.0.1 → 0.0.2

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.
@@ -1,5 +1,4 @@
1
- import { ImportStatement } from "./types";
2
- /** 分析代码中使用的标识符 */
1
+ import { ImportStatement } from "./types"; /** 分析代码中使用的标识符 */
3
2
  export declare function analyzeUsedIdentifiers(code: string): Set<string>;
4
3
  /** 过滤未使用的导入内容 */
5
4
  export declare function filterUnusedImports(importStatement: ImportStatement, usedIdentifiers: Set<string>): ImportStatement;
@@ -1,5 +1,4 @@
1
- import { Group, ImportStatement, PluginConfig } from "./types";
2
- /** 格式化导入语句 */
1
+ import { Group, ImportStatement, PluginConfig } from "./types"; /** 格式化导入语句 */
3
2
  export declare function formatImportStatement(statement: ImportStatement): string;
4
3
  /** 格式化分组 */
5
4
  export declare function formatGroups(groups: Group[], config: PluginConfig): string;
package/dist/index.js CHANGED
@@ -72,16 +72,20 @@ function removeUnusedImportsFromStatements(importStatements, code) {
72
72
  return filteredStatements;
73
73
  }
74
74
  function formatImportStatement(statement) {
75
- const { path, isExport, isSideEffect, importContents, leadingComments } = statement;
75
+ const { path, isExport, isSideEffect, importContents, leadingComments, trailingComments, removedTrailingComments } = statement;
76
76
  const lines = [];
77
77
  if (leadingComments && leadingComments.length > 0) lines.push(...leadingComments);
78
78
  if (isSideEffect) {
79
- if (isExport) lines.push(`export * from "${path}"`);
80
- else lines.push(`import "${path}"`);
79
+ let importLine = "";
80
+ importLine = isExport ? `export * from "${path}"` : `import "${path}"`;
81
+ if (trailingComments && trailingComments.length > 0) importLine += ` ${trailingComments.join(" ")}`;
82
+ lines.push(importLine);
81
83
  return lines.join("\n");
82
84
  }
85
+ const hasNamedImportComments = importContents.some((content)=>"default" !== content.name && "*" !== content.name && (content.leadingComments && content.leadingComments.length > 0 || content.trailingComments && content.trailingComments.length > 0));
83
86
  const parts = [];
84
87
  const namedParts = [];
88
+ const namedPartsWithComments = [];
85
89
  for (const content of importContents){
86
90
  if ("default" === content.name) {
87
91
  parts.push(content.alias ?? "default");
@@ -92,13 +96,45 @@ function formatImportStatement(statement) {
92
96
  continue;
93
97
  }
94
98
  const typePrefix = "type" === content.type ? "type " : "";
95
- if (content.alias) namedParts.push(`${typePrefix}${content.name} as ${content.alias}`);
96
- else namedParts.push(`${typePrefix}${content.name}`);
99
+ let importItem = "";
100
+ importItem = content.alias ? `${typePrefix}${content.name} as ${content.alias}` : `${typePrefix}${content.name}`;
101
+ if (hasNamedImportComments) {
102
+ const itemLines = [];
103
+ if (content.leadingComments && content.leadingComments.length > 0) itemLines.push(...content.leadingComments);
104
+ let itemLine = importItem;
105
+ if (content.trailingComments && content.trailingComments.length > 0) itemLine += ` ${content.trailingComments.join(" ")}`;
106
+ itemLines.push(itemLine);
107
+ namedPartsWithComments.push(itemLines.join("\n "));
108
+ } else namedParts.push(importItem);
109
+ }
110
+ const namedImports = importContents.filter((c)=>"default" !== c.name && "*" !== c.name);
111
+ const allNamedImportsAreTypes = namedImports.every((c)=>"type" === c.type);
112
+ const hasDefaultOrNamespace = importContents.some((c)=>"default" === c.name || "*" === c.name);
113
+ if (hasNamedImportComments && namedPartsWithComments.length > 0) {
114
+ const keyword = isExport ? "export" : "import";
115
+ const typeKeyword = allNamedImportsAreTypes && !hasDefaultOrNamespace ? "type " : "";
116
+ const defaultPart = parts.length > 0 ? parts.join(", ") + ", " : "";
117
+ const importStart = `${keyword} ${typeKeyword}${defaultPart}{`;
118
+ const importEnd = `} from "${path}"`;
119
+ lines.push(importStart);
120
+ lines.push(` ${namedPartsWithComments.join(",\n ")},`);
121
+ lines.push(importEnd);
122
+ } else {
123
+ if (namedParts.length > 0) if (allNamedImportsAreTypes && !hasDefaultOrNamespace) {
124
+ const cleanedParts = namedParts.map((part)=>part.replace(/^type /, ""));
125
+ parts.push(`{ ${cleanedParts.join(", ")} }`);
126
+ } else parts.push(`{ ${namedParts.join(", ")} }`);
127
+ const importClause = parts.join(", ");
128
+ const typeKeyword = allNamedImportsAreTypes && !hasDefaultOrNamespace ? "type " : "";
129
+ let importLine = "";
130
+ importLine = isExport ? `export ${typeKeyword}${importClause} from "${path}"` : `import ${typeKeyword}${importClause} from "${path}"`;
131
+ if (trailingComments && trailingComments.length > 0) importLine += ` ${trailingComments.join(" ")}`;
132
+ lines.push(importLine);
133
+ }
134
+ if (removedTrailingComments && removedTrailingComments.length > 0) {
135
+ lines.push("");
136
+ lines.push(...removedTrailingComments);
97
137
  }
98
- if (namedParts.length > 0) parts.push(`{ ${namedParts.join(", ")} }`);
99
- const importClause = parts.join(", ");
100
- if (isExport) lines.push(`export ${importClause} from "${path}"`);
101
- else lines.push(`import ${importClause} from "${path}"`);
102
138
  return lines.join("\n");
103
139
  }
104
140
  function formatGroups(groups, config) {
@@ -128,30 +164,55 @@ function parseImports(code) {
128
164
  "typescript",
129
165
  "jsx"
130
166
  ],
131
- errorRecovery: true
167
+ errorRecovery: true,
168
+ attachComment: true
132
169
  });
133
170
  const importStatements = [];
134
171
  const { body } = ast.program;
172
+ const usedComments = new Set();
135
173
  for (const node of body)if ("ImportDeclaration" === node.type || "ExportNamedDeclaration" === node.type && node.source || "ExportAllDeclaration" === node.type) {
136
- const statement = parseImportNode(node, ast.comments ?? []);
174
+ const statement = parseImportNode(node, ast.comments ?? [], usedComments);
137
175
  importStatements.push(statement);
138
176
  } else break;
139
177
  return importStatements;
140
178
  }
141
- function parseImportNode(node, comments) {
179
+ function parseImportNode(node, comments, usedComments) {
142
180
  node.type;
143
181
  const source = node.source?.value ?? "";
182
+ node.loc?.start.line;
183
+ node.loc?.end.line;
184
+ const nodeStart = node.start ?? 0;
185
+ let nodeEnd = node.end ?? 0;
144
186
  const leadingComments = [];
145
- let start = node.start ?? 0;
187
+ const trailingComments = [];
188
+ let start = nodeStart;
146
189
  if (node.leadingComments) {
147
- const firstComment = node.leadingComments[0];
148
- if (null !== firstComment.start && void 0 !== firstComment.start) start = firstComment.start;
149
- for (const comment of node.leadingComments)if ("CommentLine" === comment.type) leadingComments.push(`//${comment.value}`);
150
- else if ("CommentBlock" === comment.type) leadingComments.push(`/*${comment.value}*/`);
190
+ for (const comment of node.leadingComments)if (!usedComments.has(comment)) {
191
+ if ("CommentLine" === comment.type) leadingComments.push(`//${comment.value}`);
192
+ else if ("CommentBlock" === comment.type) leadingComments.push(`/*${comment.value}*/`);
193
+ const commentStart = comment.start ?? 0;
194
+ if (commentStart < start) start = commentStart;
195
+ usedComments.add(comment);
196
+ }
197
+ }
198
+ if (node.trailingComments) {
199
+ for (const comment of node.trailingComments)if (!usedComments.has(comment)) {
200
+ const commentLoc = comment.loc;
201
+ const nodeLoc = node.loc;
202
+ const isSameLine = commentLoc && nodeLoc && commentLoc.start.line === nodeLoc.end.line;
203
+ if (isSameLine) {
204
+ if ("CommentLine" === comment.type) trailingComments.push(`//${comment.value}`);
205
+ else if ("CommentBlock" === comment.type) trailingComments.push(`/*${comment.value}*/`);
206
+ const commentEnd = comment.end ?? 0;
207
+ if (commentEnd > nodeEnd) nodeEnd = commentEnd;
208
+ usedComments.add(comment);
209
+ }
210
+ }
151
211
  }
152
- const end = node.end ?? 0;
212
+ const end = nodeEnd;
153
213
  if ("ImportDeclaration" === node.type) {
154
- const importContents = parseImportSpecifiers(node);
214
+ const isTypeOnlyImport = "type" === node.importKind;
215
+ const importContents = parseImportSpecifiers(node, isTypeOnlyImport);
155
216
  const isSideEffect = 0 === importContents.length;
156
217
  return {
157
218
  path: source,
@@ -159,6 +220,7 @@ function parseImportNode(node, comments) {
159
220
  isSideEffect,
160
221
  importContents,
161
222
  leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
223
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0,
162
224
  start,
163
225
  end
164
226
  };
@@ -169,6 +231,7 @@ function parseImportNode(node, comments) {
169
231
  isSideEffect: false,
170
232
  importContents: [],
171
233
  leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
234
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0,
172
235
  start,
173
236
  end
174
237
  };
@@ -179,31 +242,50 @@ function parseImportNode(node, comments) {
179
242
  isSideEffect: false,
180
243
  importContents,
181
244
  leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
245
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0,
182
246
  start,
183
247
  end
184
248
  };
185
249
  }
186
- function parseImportSpecifiers(node) {
250
+ function parseImportSpecifiers(node, isTypeOnlyImport = false) {
187
251
  const contents = [];
188
- for (const specifier of node.specifiers)if ("ImportDefaultSpecifier" === specifier.type) contents.push({
189
- name: "default",
190
- alias: specifier.local.name,
191
- type: "variable"
192
- });
193
- else if ("ImportNamespaceSpecifier" === specifier.type) contents.push({
194
- name: "*",
195
- alias: specifier.local.name,
196
- type: "variable"
197
- });
198
- else if ("ImportSpecifier" === specifier.type) {
199
- const importedName = "Identifier" === specifier.imported.type ? specifier.imported.name : specifier.imported.value;
200
- const localName = specifier.local.name;
201
- const isTypeImport = "type" === specifier.importKind;
202
- contents.push({
203
- name: importedName,
204
- alias: importedName !== localName ? localName : void 0,
205
- type: isTypeImport ? "type" : "variable"
252
+ for (const specifier of node.specifiers){
253
+ const leadingComments = [];
254
+ const trailingComments = [];
255
+ if (specifier.leadingComments) {
256
+ for (const comment of specifier.leadingComments)if ("CommentLine" === comment.type) leadingComments.push(`//${comment.value}`);
257
+ else if ("CommentBlock" === comment.type) leadingComments.push(`/*${comment.value}*/`);
258
+ }
259
+ if (specifier.trailingComments) {
260
+ for (const comment of specifier.trailingComments)if ("CommentLine" === comment.type) trailingComments.push(`//${comment.value}`);
261
+ else if ("CommentBlock" === comment.type) trailingComments.push(`/*${comment.value}*/`);
262
+ }
263
+ if ("ImportDefaultSpecifier" === specifier.type) contents.push({
264
+ name: "default",
265
+ alias: specifier.local.name,
266
+ type: isTypeOnlyImport ? "type" : "variable",
267
+ leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
268
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0
206
269
  });
270
+ else if ("ImportNamespaceSpecifier" === specifier.type) contents.push({
271
+ name: "*",
272
+ alias: specifier.local.name,
273
+ type: isTypeOnlyImport ? "type" : "variable",
274
+ leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
275
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0
276
+ });
277
+ else if ("ImportSpecifier" === specifier.type) {
278
+ const importedName = "Identifier" === specifier.imported.type ? specifier.imported.name : specifier.imported.value;
279
+ const localName = specifier.local.name;
280
+ const isTypeImport = isTypeOnlyImport || "type" === specifier.importKind;
281
+ contents.push({
282
+ name: importedName,
283
+ alias: importedName !== localName ? localName : void 0,
284
+ type: isTypeImport ? "type" : "variable",
285
+ leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
286
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0
287
+ });
288
+ }
207
289
  }
208
290
  return contents;
209
291
  }
@@ -211,13 +293,25 @@ function parseExportSpecifiers(node) {
211
293
  const contents = [];
212
294
  if (!node.specifiers) return contents;
213
295
  for (const specifier of node.specifiers)if ("ExportSpecifier" === specifier.type) {
296
+ const leadingComments = [];
297
+ const trailingComments = [];
298
+ if (specifier.leadingComments) {
299
+ for (const comment of specifier.leadingComments)if ("CommentLine" === comment.type) leadingComments.push(`//${comment.value}`);
300
+ else if ("CommentBlock" === comment.type) leadingComments.push(`/*${comment.value}*/`);
301
+ }
302
+ if (specifier.trailingComments) {
303
+ for (const comment of specifier.trailingComments)if ("CommentLine" === comment.type) trailingComments.push(`//${comment.value}`);
304
+ else if ("CommentBlock" === comment.type) trailingComments.push(`/*${comment.value}*/`);
305
+ }
214
306
  const localName = "Identifier" === specifier.local.type ? specifier.local.name : specifier.local.value;
215
307
  const exportedName = "Identifier" === specifier.exported.type ? specifier.exported.name : specifier.exported.value;
216
308
  const isTypeExport = "type" === specifier.exportKind;
217
309
  contents.push({
218
310
  name: localName,
219
311
  alias: localName !== exportedName ? exportedName : void 0,
220
- type: isTypeExport ? "type" : "variable"
312
+ type: isTypeExport ? "type" : "variable",
313
+ leadingComments: leadingComments.length > 0 ? leadingComments : void 0,
314
+ trailingComments: trailingComments.length > 0 ? trailingComments : void 0
221
315
  });
222
316
  }
223
317
  return contents;
@@ -375,6 +469,61 @@ function sortImportContents(contents, userConfig) {
375
469
  ...namedImports.sort(config.sortImportContent)
376
470
  ];
377
471
  }
472
+ function mergeImports(imports) {
473
+ const mergedMap = new Map();
474
+ for (const statement of imports){
475
+ if (statement.isSideEffect) {
476
+ const key = `${statement.path}|||${statement.isExport}|||sideEffect|||${statement.start}`;
477
+ mergedMap.set(key, statement);
478
+ continue;
479
+ }
480
+ const hasNamespaceImport = statement.importContents.some((c)=>"*" === c.name);
481
+ if (hasNamespaceImport) {
482
+ const key = `${statement.path}|||${statement.isExport}|||namespace|||${statement.start}`;
483
+ mergedMap.set(key, statement);
484
+ continue;
485
+ }
486
+ const key = `${statement.path}|||${statement.isExport}`;
487
+ const existing = mergedMap.get(key);
488
+ if (existing) {
489
+ const mergedContents = [
490
+ ...existing.importContents
491
+ ];
492
+ for (const content of statement.importContents){
493
+ const existingContent = mergedContents.find((c)=>c.name === content.name && c.alias === content.alias);
494
+ if (existingContent) {
495
+ if (content.leadingComments) existingContent.leadingComments = [
496
+ ...existingContent.leadingComments ?? [],
497
+ ...content.leadingComments
498
+ ];
499
+ if (content.trailingComments) existingContent.trailingComments = [
500
+ ...existingContent.trailingComments ?? [],
501
+ ...content.trailingComments
502
+ ];
503
+ } else mergedContents.push(content);
504
+ }
505
+ const mergedLeadingComments = [
506
+ ...existing.leadingComments ?? [],
507
+ ...statement.leadingComments ?? []
508
+ ];
509
+ const mergedTrailingComments = existing.trailingComments ?? [];
510
+ const removedTrailingComments = [
511
+ ...existing.removedTrailingComments ?? [],
512
+ ...statement.trailingComments ?? []
513
+ ];
514
+ mergedMap.set(key, {
515
+ ...existing,
516
+ importContents: mergedContents,
517
+ leadingComments: mergedLeadingComments.length > 0 ? mergedLeadingComments : void 0,
518
+ trailingComments: mergedTrailingComments.length > 0 ? mergedTrailingComments : void 0,
519
+ removedTrailingComments: removedTrailingComments.length > 0 ? removedTrailingComments : void 0
520
+ });
521
+ } else mergedMap.set(key, {
522
+ ...statement
523
+ });
524
+ }
525
+ return Array.from(mergedMap.values());
526
+ }
378
527
  const src_require = createRequire(import.meta.url);
379
528
  let src_userConfig = {};
380
529
  function preprocessImports(text, options) {
@@ -397,19 +546,22 @@ function preprocessImports(text, options) {
397
546
  processedImports = removeUnusedImportsFromStatements(imports, codeAfterImports);
398
547
  }
399
548
  const sortedImports = sortImports(processedImports, config);
549
+ const mergedImports = mergeImports(sortedImports);
400
550
  let formattedImports;
401
551
  if (config.getGroup) {
402
- const groups = groupImports(sortedImports, config);
552
+ const groups = groupImports(mergedImports, config);
403
553
  const sortedGroups = sortGroups(groups, config);
404
554
  formattedImports = formatGroups(sortedGroups, config);
405
- } else formattedImports = formatImportStatements(sortedImports);
555
+ } else formattedImports = formatImportStatements(mergedImports);
406
556
  const firstImport = imports[0];
407
557
  const lastImport = imports[imports.length - 1];
408
558
  const startIndex = firstImport.start ?? 0;
409
559
  const endIndex = lastImport.end ?? text.length;
410
560
  const beforeImports = text.slice(0, startIndex);
411
561
  const afterImports = text.slice(endIndex);
412
- return beforeImports + formattedImports + "\n" + afterImports;
562
+ const needsExtraNewline = afterImports && !afterImports.startsWith("\n");
563
+ const separator = needsExtraNewline ? "\n\n" : "\n";
564
+ return beforeImports + formattedImports + separator + afterImports;
413
565
  } catch (error) {
414
566
  console.error("Failed to sort imports:", error);
415
567
  return text;
package/dist/parser.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- import { ImportStatement } from "./types";
2
- /** 解析导入语句 */
1
+ import { ImportStatement } from "./types"; /** 解析导入语句 */
3
2
  export declare function parseImports(code: string): ImportStatement[];
package/dist/sorter.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Group, ImportContent, ImportStatement, PluginConfig } from "./types";
1
+ import { Group, ImportContent, ImportStatement, PluginConfig } from "./types"; /** 导入类型 */
2
2
  /** 合并后的配置 */
3
3
  export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" | "removeUnusedImports"> {
4
4
  separator: PluginConfig["separator"];
@@ -14,3 +14,5 @@ export declare function sortGroups(groups: Group[], userConfig: PluginConfig): G
14
14
  export declare function sortImportStatements(statements: ImportStatement[], userConfig: PluginConfig): ImportStatement[];
15
15
  /** 对导入内容进行排序 */
16
16
  export declare function sortImportContents(contents: ImportContent[], userConfig: PluginConfig): ImportContent[];
17
+ /** 合并来自同一模块的导入语句 */
18
+ export declare function mergeImports(imports: ImportStatement[]): ImportStatement[];
package/dist/types.d.ts CHANGED
@@ -6,6 +6,10 @@ export interface ImportContent {
6
6
  alias?: string;
7
7
  /** 导入的内容的类型,只有明确在导入前加入了 type 标记的才属于 type 类型,没有明确的加入 type 标记的都属于 variable 类型 */
8
8
  type: "type" | "variable";
9
+ /** 导入内容上方的注释 */
10
+ leadingComments?: string[];
11
+ /** 导入内容后方的行尾注释 */
12
+ trailingComments?: string[];
9
13
  }
10
14
  /** 导入语句 */
11
15
  export interface ImportStatement {
@@ -19,6 +23,10 @@ export interface ImportStatement {
19
23
  importContents: ImportContent[];
20
24
  /** 导入语句上方的注释 */
21
25
  leadingComments?: string[];
26
+ /** 导入语句后方的行尾注释 */
27
+ trailingComments?: string[];
28
+ /** 被移除的导入语句的行尾注释(合并时使用) */
29
+ removedTrailingComments?: string[];
22
30
  /** 在源代码中的起始位置(包括注释) */
23
31
  start?: number;
24
32
  /** 在源代码中的结束位置 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@1adybug/prettier-plugin-sort-imports",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
6
6
  "keywords": [
7
7
  "prettier",
@@ -41,11 +41,14 @@
41
41
  "build": "rslib build",
42
42
  "dev": "rslib build --watch",
43
43
  "prepublishOnly": "npm run build",
44
- "format": "prettier --config prettier.config.mjs --write ."
44
+ "format": "prettier --config prettier.config.mjs --write .",
45
+ "test": "bun test",
46
+ "test:watch": "bun test --watch"
45
47
  },
46
48
  "devDependencies": {
47
49
  "@rslib/core": "^0.15.0",
48
50
  "@types/babel__core": "^7.20.5",
51
+ "@types/bun": "latest",
49
52
  "@types/node": "^22.18.6",
50
53
  "prettier": "^3.6.2",
51
54
  "supports-color": "^10.2.2",