@1adybug/prettier-plugin-sort-imports 0.0.17 → 0.0.19

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 CHANGED
@@ -45,8 +45,11 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
45
45
 
46
46
  ```typescript
47
47
  import React, { useEffect, useState } from "react"
48
+
48
49
  import { Button } from "antd"
50
+
49
51
  import { sum } from "./utils"
52
+
50
53
  import "./styles.css"
51
54
  ```
52
55
 
@@ -87,7 +90,9 @@ import { Button } from "antd"
87
90
  import { format } from "date-fns"
88
91
 
89
92
  import { Header } from "./components/Header"
93
+
90
94
  import { sum } from "./utils"
95
+
91
96
  import "./styles.css"
92
97
  ```
93
98
 
@@ -201,12 +206,8 @@ export default {
201
206
 
202
207
  return order.indexOf(a.name) - order.indexOf(b.name)
203
208
  },
204
- sortImportStatement: (a, b) => {
205
- return a.path.localeCompare(b.path)
206
- },
207
- sortImportContent: (a, b) => {
208
- return a.name.localeCompare(b.name)
209
- },
209
+ sortImportStatement: (a, b) => a.path.localeCompare(b.path),
210
+ sortImportContent: (a, b) => a.name.localeCompare(b.name),
210
211
 
211
212
  // Configuration
212
213
  separator: "\n",
@@ -449,12 +450,7 @@ separator: (group, index) => {
449
450
 
450
451
  ```typescript
451
452
  import Default, * as Namespace from "module"
452
- import {
453
- type TypeA,
454
- type TypeB,
455
- VariableA,
456
- VariableB,
457
- } from "module"
453
+ import { type TypeA, type TypeB, VariableA, VariableB } from "module"
458
454
  ```
459
455
 
460
456
  **Custom behavior**:
@@ -473,11 +469,7 @@ createPlugin({
473
469
  ```
474
470
 
475
471
  ```typescript
476
- import {
477
- API_KEY,
478
- type User,
479
- getUser,
480
- } from "api"
472
+ import { type User, API_KEY, getUser } from "api"
481
473
  ```
482
474
 
483
475
  ### Import Statement Sorting
package/README.zh-CN.md CHANGED
@@ -44,8 +44,11 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
44
44
 
45
45
  ```typescript
46
46
  import React, { useEffect, useState } from "react"
47
+
47
48
  import { Button } from "antd"
49
+
48
50
  import { sum } from "./utils"
51
+
49
52
  import "./styles.css"
50
53
  ```
51
54
 
@@ -86,7 +89,9 @@ import { Button } from "antd"
86
89
  import { format } from "date-fns"
87
90
 
88
91
  import { Header } from "./components/Header"
92
+
89
93
  import { sum } from "./utils"
94
+
90
95
  import "./styles.css"
91
96
  ```
92
97
 
@@ -441,12 +446,7 @@ separator: (group, index) => {
441
446
 
442
447
  ```typescript
443
448
  import Default, * as Namespace from "module"
444
- import {
445
- type TypeA,
446
- type TypeB,
447
- VariableA,
448
- VariableB,
449
- } from "module"
449
+ import { type TypeA, type TypeB, VariableA, VariableB } from "module"
450
450
  ```
451
451
 
452
452
  **自定义行为**:
@@ -465,11 +465,7 @@ createPlugin({
465
465
  ```
466
466
 
467
467
  ```typescript
468
- import {
469
- API_KEY,
470
- type User,
471
- getUser,
472
- } from "api"
468
+ import { type User, API_KEY, getUser } from "api"
473
469
  ```
474
470
 
475
471
  ### 导入语句排序
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createRequire } from "module";
2
+ import { relative } from "path";
2
3
  import { parse } from "@babel/parser";
3
4
  import traverse from "@babel/traverse";
4
5
  const analyzer_traverse = "function" == typeof traverse ? traverse : traverse["default"];
@@ -170,7 +171,7 @@ function formatGroups(groups, config) {
170
171
  function formatImportStatements(statements) {
171
172
  return statements.map(formatImportStatement).join("\n");
172
173
  }
173
- function parseImports(code) {
174
+ function parseImports(code, filepath) {
174
175
  const hasImportOrExport = /^\s*(import|export)\s/m.test(code);
175
176
  if (!hasImportOrExport) return [];
176
177
  const ast = parse(code, {
@@ -187,13 +188,13 @@ function parseImports(code) {
187
188
  const usedComments = new Set();
188
189
  let isFirstImport = true;
189
190
  for (const node of body)if ("ImportDeclaration" === node.type || "ExportNamedDeclaration" === node.type && node.source || "ExportAllDeclaration" === node.type) {
190
- const statement = parseImportNode(node, ast.comments ?? [], usedComments, code, isFirstImport);
191
+ const statement = parseImportNode(node, ast.comments ?? [], usedComments, code, isFirstImport, filepath);
191
192
  importStatements.push(statement);
192
193
  isFirstImport = false;
193
194
  } else break;
194
195
  return importStatements;
195
196
  }
196
- function parseImportNode(node, comments, usedComments, code, isFirstImport) {
197
+ function parseImportNode(node, comments, usedComments, code, isFirstImport, filepath) {
197
198
  node.type;
198
199
  const source = node.source?.value ?? "";
199
200
  const nodeStartLine = node.loc?.start.line ?? 0;
@@ -242,6 +243,7 @@ function parseImportNode(node, comments, usedComments, code, isFirstImport) {
242
243
  const importContents = parseImportSpecifiers(node, isTypeOnlyImport);
243
244
  const isSideEffect = 0 === importContents.length;
244
245
  return {
246
+ filepath,
245
247
  path: source,
246
248
  isExport: false,
247
249
  isSideEffect,
@@ -254,6 +256,7 @@ function parseImportNode(node, comments, usedComments, code, isFirstImport) {
254
256
  };
255
257
  }
256
258
  if ("ExportAllDeclaration" === node.type) return {
259
+ filepath,
257
260
  path: source,
258
261
  isExport: true,
259
262
  isSideEffect: true,
@@ -267,6 +270,7 @@ function parseImportNode(node, comments, usedComments, code, isFirstImport) {
267
270
  const isTypeOnlyExport = "type" === node.exportKind;
268
271
  const importContents = parseExportSpecifiers(node, isTypeOnlyExport);
269
272
  return {
273
+ filepath,
270
274
  path: source,
271
275
  isExport: true,
272
276
  isSideEffect: false,
@@ -469,7 +473,9 @@ function groupImports(imports, userConfig) {
469
473
  const separatorIndex = key.lastIndexOf("|||");
470
474
  const name = key.slice(0, separatorIndex);
471
475
  const isSideEffect = "true" === key.slice(separatorIndex + 3);
476
+ const filepath = statements[0].filepath;
472
477
  groups.push({
478
+ filepath,
473
479
  name,
474
480
  isSideEffect,
475
481
  importStatements: statements
@@ -568,7 +574,9 @@ function preprocessImports(text, options, config = {}) {
568
574
  "babel-ts"
569
575
  ];
570
576
  if (!parser || !supportedParsers.includes(parser)) return text;
571
- const imports = parseImports(text);
577
+ const absoluteFilepath = options.filepath;
578
+ const relativeFilepath = absoluteFilepath ? `./${relative(process.cwd(), absoluteFilepath).replace(/\\/g, "/")}` : void 0;
579
+ const imports = parseImports(text, relativeFilepath);
572
580
  if (0 === imports.length) return text;
573
581
  const optionsConfig = options;
574
582
  const finalConfig = {
package/dist/parser.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { ImportStatement } from "./types";
2
2
  /** 解析导入语句 */
3
- export declare function parseImports(code: string): ImportStatement[];
3
+ export declare function parseImports(code: string, filepath?: string): ImportStatement[];
package/dist/types.d.ts CHANGED
@@ -14,6 +14,8 @@ export interface ImportContent {
14
14
  }
15
15
  /** 导入语句 */
16
16
  export interface ImportStatement {
17
+ /** 当前文件相对于当前路径的位置 */
18
+ filepath?: string;
17
19
  /** 导入的模块路径,可以是相对路径或绝对路径,比如 react, react-dom 或者 ./utils/index,@/utils/index 等 */
18
20
  path: string;
19
21
  /** 是否是导出语句,默认为 false */
@@ -37,6 +39,8 @@ export interface ImportStatement {
37
39
  }
38
40
  /** 分组 */
39
41
  export interface Group {
42
+ /** 当前文件相对于当前路径的位置 */
43
+ filepath?: string;
40
44
  /** 分组名称,默认为 default */
41
45
  name: string;
42
46
  /** 是否是副作用分组,默认为 false */
package/package.json CHANGED
@@ -1,74 +1,67 @@
1
1
  {
2
- "name": "@1adybug/prettier-plugin-sort-imports",
3
- "type": "module",
4
- "version": "0.0.17",
5
- "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
6
- "keywords": [
7
- "prettier",
8
- "plugin",
9
- "import",
10
- "sort",
11
- "organize",
12
- "typescript",
13
- "javascript"
14
- ],
15
- "author": "1adybug <lurongv@qq.com>",
16
- "license": "MIT",
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/1adybug/prettier-plugin-sort-imports.git"
20
- },
21
- "homepage": "https://github.com/1adybug/prettier-plugin-sort-imports#readme",
22
- "bugs": {
23
- "url": "https://github.com/1adybug/prettier-plugin-sort-imports/issues"
24
- },
25
- "sideEffects": false,
26
- "publishConfig": {
27
- "access": "public",
28
- "registry": "https://registry.npmjs.com/"
29
- },
30
- "exports": {
31
- ".": {
32
- "types": "./dist/index.d.ts",
33
- "import": "./dist/index.js",
34
- "require": "./dist/index.js"
35
- }
36
- },
37
- "main": "./dist/index.js",
38
- "module": "./dist/index.js",
39
- "types": "./dist/index.d.ts",
40
- "files": [
41
- "dist"
42
- ],
43
- "scripts": {
44
- "build": "rslib build",
45
- "dev": "rslib build --watch",
46
- "prepublishOnly": "npm run build",
47
- "format": "prettier --write .",
48
- "test": "bun test",
49
- "test:watch": "bun test --watch",
50
- "fg": "npm run format && git add . && git commit -m \"✨feature: format\"",
51
- "ucr": "npx zixulu acr"
52
- },
53
- "devDependencies": {
54
- "@rslib/core": "^0.15.0",
55
- "@types/babel__core": "^7.20.5",
56
- "@types/bun": "latest",
57
- "@types/node": "^22.18.6",
58
- "json5": "^2.2.3",
59
- "prettier": "^3.6.2",
60
- "prettier-plugin-block-padding": "^0.0.6",
61
- "prettier-plugin-tailwindcss": "^0.7.0",
62
- "supports-color": "^10.2.2",
63
- "typescript": "^5.9.2"
64
- },
65
- "peerDependencies": {
66
- "prettier": "^3.0.0"
67
- },
68
- "dependencies": {
69
- "@babel/core": "^7.28.4",
70
- "@babel/parser": "^7.28.4",
71
- "@babel/traverse": "^7.28.4",
72
- "@babel/types": "^7.28.4"
2
+ "name": "@1adybug/prettier-plugin-sort-imports",
3
+ "version": "0.0.19",
4
+ "description": "一个 Prettier 插件,用于对 JavaScript/TypeScript 文件的导入语句进行分组和排序",
5
+ "keywords": [
6
+ "prettier",
7
+ "plugin",
8
+ "import",
9
+ "sort",
10
+ "organize",
11
+ "typescript",
12
+ "javascript"
13
+ ],
14
+ "author": "1adybug <lurongv@qq.com>",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/1adybug/prettier.git"
19
+ },
20
+ "homepage": "https://github.com/1adybug/prettier/tree/main/packages/prettier-plugin-sort-imports",
21
+ "bugs": {
22
+ "url": "https://github.com/1adybug/prettier/issues"
23
+ },
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.js"
73
30
  }
74
- }
31
+ },
32
+ "main": "./dist/index.js",
33
+ "module": "./dist/index.js",
34
+ "types": "./dist/index.d.ts",
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "sideEffects": false,
39
+ "publishConfig": {
40
+ "access": "public",
41
+ "registry": "https://registry.npmjs.com/"
42
+ },
43
+ "dependencies": {
44
+ "@babel/core": "^7.28.4",
45
+ "@babel/parser": "^7.28.4",
46
+ "@babel/traverse": "^7.28.4",
47
+ "@babel/types": "^7.28.4"
48
+ },
49
+ "devDependencies": {
50
+ "@rslib/core": "^0.15.0",
51
+ "@types/babel__core": "^7.20.5",
52
+ "@types/bun": "latest",
53
+ "@types/node": "^22.18.6",
54
+ "json5": "^2.2.3",
55
+ "supports-color": "^10.2.2",
56
+ "typescript": "^5.9.2"
57
+ },
58
+ "peerDependencies": {
59
+ "prettier": "^3.0.0"
60
+ },
61
+ "scripts": {
62
+ "build": "rslib build",
63
+ "dev": "rslib build --watch",
64
+ "test": "bun test",
65
+ "test:watch": "bun test --watch"
66
+ }
67
+ }