@1adybug/prettier-plugin-sort-imports 0.0.11 → 0.0.13

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,6 +1,6 @@
1
1
  import { ImportStatement } from "./types";
2
2
  /** 分析代码中使用的标识符 */
3
- export declare function analyzeUsedIdentifiers(code: string): Set<string>;
3
+ export declare function analyzeUsedIdentifiers(code: string): Set<string> | null;
4
4
  /** 过滤未使用的导入内容 */
5
5
  export declare function filterUnusedImports(importStatement: ImportStatement, usedIdentifiers: Set<string>): ImportStatement;
6
6
  /** 从导入语句列表中移除未使用的导入 */
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ function analyzeUsedIdentifiers(code) {
17
17
  Identifier (path) {
18
18
  const node = path.node;
19
19
  const parent = path.parent;
20
- if (path.isBindingIdentifier()) return;
20
+ if (!path.isReferenced()) return;
21
21
  if (parent?.type === "ObjectProperty" && parent.key === node && !parent.computed) return;
22
22
  usedIdentifiers.add(node.name);
23
23
  },
@@ -49,7 +49,7 @@ function analyzeUsedIdentifiers(code) {
49
49
  }
50
50
  });
51
51
  } catch (error) {
52
- console.error("Failed to analyze used identifiers:", error);
52
+ return null;
53
53
  }
54
54
  return usedIdentifiers;
55
55
  }
@@ -70,6 +70,7 @@ function filterUnusedImports(importStatement, usedIdentifiers) {
70
70
  }
71
71
  function removeUnusedImportsFromStatements(importStatements, code) {
72
72
  const usedIdentifiers = analyzeUsedIdentifiers(code);
73
+ if (null === usedIdentifiers) return importStatements;
73
74
  const filteredStatements = [];
74
75
  for (const statement of importStatements){
75
76
  const filteredStatement = filterUnusedImports(statement, usedIdentifiers);
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.11",
4
+ "version": "0.0.13",
5
5
  "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
6
6
  "keywords": [
7
7
  "prettier",