@1adybug/prettier-plugin-sort-imports 0.0.18 → 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 +7 -4
- package/README.zh-CN.md +7 -4
- package/dist/index.js +12 -4
- package/dist/parser.d.ts +1 -1
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,9 +44,10 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
44
44
|
### Basic Sorting
|
|
45
45
|
|
|
46
46
|
```typescript
|
|
47
|
-
import { Button } from "antd"
|
|
48
47
|
import React, { useEffect, useState } from "react"
|
|
49
48
|
|
|
49
|
+
import { Button } from "antd"
|
|
50
|
+
|
|
50
51
|
import { sum } from "./utils"
|
|
51
52
|
|
|
52
53
|
import "./styles.css"
|
|
@@ -83,9 +84,10 @@ export default {
|
|
|
83
84
|
Result:
|
|
84
85
|
|
|
85
86
|
```typescript
|
|
87
|
+
import React, { useState } from "react"
|
|
88
|
+
|
|
86
89
|
import { Button } from "antd"
|
|
87
90
|
import { format } from "date-fns"
|
|
88
|
-
import React, { useState } from "react"
|
|
89
91
|
|
|
90
92
|
import { Header } from "./components/Header"
|
|
91
93
|
|
|
@@ -485,11 +487,12 @@ import { c } from "c-module"
|
|
|
485
487
|
Comments follow the import statements they are attached to:
|
|
486
488
|
|
|
487
489
|
```typescript
|
|
488
|
-
// UI components
|
|
489
|
-
import { Button } from "antd"
|
|
490
490
|
// React related imports
|
|
491
491
|
import React from "react"
|
|
492
492
|
|
|
493
|
+
// UI components
|
|
494
|
+
import { Button } from "antd"
|
|
495
|
+
|
|
493
496
|
// Utilities
|
|
494
497
|
import { sum } from "./utils"
|
|
495
498
|
```
|
package/README.zh-CN.md
CHANGED
|
@@ -43,9 +43,10 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
43
43
|
### 基本排序
|
|
44
44
|
|
|
45
45
|
```typescript
|
|
46
|
-
import { Button } from "antd"
|
|
47
46
|
import React, { useEffect, useState } from "react"
|
|
48
47
|
|
|
48
|
+
import { Button } from "antd"
|
|
49
|
+
|
|
49
50
|
import { sum } from "./utils"
|
|
50
51
|
|
|
51
52
|
import "./styles.css"
|
|
@@ -82,9 +83,10 @@ export default {
|
|
|
82
83
|
结果:
|
|
83
84
|
|
|
84
85
|
```typescript
|
|
86
|
+
import React, { useState } from "react"
|
|
87
|
+
|
|
85
88
|
import { Button } from "antd"
|
|
86
89
|
import { format } from "date-fns"
|
|
87
|
-
import React, { useState } from "react"
|
|
88
90
|
|
|
89
91
|
import { Header } from "./components/Header"
|
|
90
92
|
|
|
@@ -481,11 +483,12 @@ import { c } from "c-module"
|
|
|
481
483
|
注释会跟随它们所附加的导入语句一起移动:
|
|
482
484
|
|
|
483
485
|
```typescript
|
|
484
|
-
// UI 组件
|
|
485
|
-
import { Button } from "antd"
|
|
486
486
|
// React 相关导入
|
|
487
487
|
import React from "react"
|
|
488
488
|
|
|
489
|
+
// UI 组件
|
|
490
|
+
import { Button } from "antd"
|
|
491
|
+
|
|
489
492
|
// 工具函数
|
|
490
493
|
import { sum } from "./utils"
|
|
491
494
|
```
|
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
|
|
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
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 */
|