@1adybug/prettier-plugin-sort-imports 0.0.4 → 0.0.6
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 +164 -14
- package/README.zh-CN.md +150 -6
- package/dist/index.d.ts +4 -3
- package/dist/index.js +81 -51
- package/dist/parser.d.ts +2 -1
- package/dist/sorter.d.ts +1 -1
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ A powerful Prettier plugin for intelligently grouping and sorting import stateme
|
|
|
19
19
|
### Installation
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm install prettier-plugin-
|
|
22
|
+
npm install @1adybug/prettier-plugin-sort-imports --save-dev
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Basic Configuration
|
|
@@ -28,7 +28,7 @@ Add the plugin to your `prettier.config.mjs`:
|
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
30
|
export default {
|
|
31
|
-
plugins: ["prettier-plugin-
|
|
31
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
@@ -50,7 +50,7 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
50
50
|
|
|
51
51
|
```javascript
|
|
52
52
|
// prettier.config.mjs
|
|
53
|
-
import { createPlugin } from "prettier-plugin-
|
|
53
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
54
54
|
|
|
55
55
|
export default {
|
|
56
56
|
plugins: [
|
|
@@ -157,40 +157,46 @@ interface PluginConfig {
|
|
|
157
157
|
|
|
158
158
|
### Method 1: Simple Configuration
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
Use the default plugin with basic options:
|
|
161
161
|
|
|
162
162
|
```javascript
|
|
163
163
|
export default {
|
|
164
|
-
plugins: ["prettier-plugin-
|
|
164
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
165
165
|
importSortSideEffect: false, // Whether to sort side effect imports
|
|
166
166
|
importSortSeparator: "", // Group separator
|
|
167
167
|
importSortRemoveUnused: false, // Whether to remove unused imports
|
|
168
168
|
}
|
|
169
169
|
```
|
|
170
170
|
|
|
171
|
-
### Method 2: Advanced Configuration
|
|
171
|
+
### Method 2: Advanced Configuration
|
|
172
172
|
|
|
173
|
-
Use `createPlugin` function
|
|
173
|
+
Use `createPlugin` function for full control and plugin compatibility:
|
|
174
174
|
|
|
175
175
|
```javascript
|
|
176
|
-
import { createPlugin } from "prettier-plugin-
|
|
176
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
177
177
|
|
|
178
178
|
export default {
|
|
179
179
|
plugins: [
|
|
180
180
|
createPlugin({
|
|
181
|
+
// Custom sorting functions
|
|
181
182
|
getGroup: statement => {
|
|
182
|
-
|
|
183
|
+
if (statement.path.startsWith("react")) return "react"
|
|
184
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
185
|
+
return "local"
|
|
183
186
|
},
|
|
184
187
|
sortGroup: (a, b) => {
|
|
185
|
-
|
|
188
|
+
const order = ["react", "external", "local"]
|
|
189
|
+
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
186
190
|
},
|
|
187
191
|
sortImportStatement: (a, b) => {
|
|
188
|
-
|
|
192
|
+
return a.path.localeCompare(b.path)
|
|
189
193
|
},
|
|
190
194
|
sortImportContent: (a, b) => {
|
|
191
|
-
|
|
195
|
+
return a.name.localeCompare(b.name)
|
|
192
196
|
},
|
|
193
|
-
|
|
197
|
+
|
|
198
|
+
// Configuration
|
|
199
|
+
separator: "\n",
|
|
194
200
|
sortSideEffect: true,
|
|
195
201
|
removeUnusedImports: false,
|
|
196
202
|
}),
|
|
@@ -198,6 +204,150 @@ export default {
|
|
|
198
204
|
}
|
|
199
205
|
```
|
|
200
206
|
|
|
207
|
+
### Method 3: Custom Plugin Module
|
|
208
|
+
|
|
209
|
+
Create a custom plugin module for better organization and reusability:
|
|
210
|
+
|
|
211
|
+
**Step 1**: Create a custom plugin file `prettier-plugin-sort-imports.mjs`:
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
// prettier-plugin-sort-imports.mjs
|
|
215
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
216
|
+
|
|
217
|
+
export default createPlugin({
|
|
218
|
+
// Custom grouping logic
|
|
219
|
+
getGroup: statement => {
|
|
220
|
+
const path = statement.path
|
|
221
|
+
|
|
222
|
+
// React and related libraries
|
|
223
|
+
if (path.startsWith("react") || path.startsWith("@react")) {
|
|
224
|
+
return "react"
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// UI libraries
|
|
228
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) {
|
|
229
|
+
return "ui"
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Utility libraries
|
|
233
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) {
|
|
234
|
+
return "utils"
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// External packages (node_modules)
|
|
238
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) {
|
|
239
|
+
return "external"
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Internal aliases (@/)
|
|
243
|
+
if (path.startsWith("@/")) {
|
|
244
|
+
return "internal"
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Relative imports
|
|
248
|
+
return "relative"
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
// Define group order
|
|
252
|
+
sortGroup: (a, b) => {
|
|
253
|
+
const order = ["react", "external", "ui", "utils", "internal", "relative"]
|
|
254
|
+
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
// Custom import content sorting
|
|
258
|
+
sortImportContent: (a, b) => {
|
|
259
|
+
// Types first, then variables
|
|
260
|
+
if (a.type !== b.type) {
|
|
261
|
+
return a.type === "type" ? -1 : 1
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Alphabetical order within same type
|
|
265
|
+
const aName = a.alias ?? a.name
|
|
266
|
+
const bName = b.alias ?? b.name
|
|
267
|
+
return aName.localeCompare(bName)
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
// Add blank lines between groups
|
|
271
|
+
separator: "\n",
|
|
272
|
+
|
|
273
|
+
// Sort side effects
|
|
274
|
+
sortSideEffect: true,
|
|
275
|
+
})
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**Step 2**: Use the custom plugin in your `prettier.config.mjs`:
|
|
279
|
+
|
|
280
|
+
```javascript
|
|
281
|
+
// prettier.config.mjs
|
|
282
|
+
export default {
|
|
283
|
+
plugins: ["./prettier-plugin-sort-imports.mjs"],
|
|
284
|
+
// Other prettier options...
|
|
285
|
+
semi: false,
|
|
286
|
+
tabWidth: 4,
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
**Benefits of this approach**:
|
|
291
|
+
- ✅ **Reusable**: Share the same configuration across multiple projects
|
|
292
|
+
- ✅ **Version Control**: Track your import sorting rules in git
|
|
293
|
+
- ✅ **Maintainable**: Keep complex logic separate from prettier config
|
|
294
|
+
- ✅ **Team Collaboration**: Consistent import sorting rules across team members
|
|
295
|
+
|
|
296
|
+
### Method 4: Plugin Compatibility
|
|
297
|
+
|
|
298
|
+
Use `createPlugin` with `otherPlugins` to merge with other Prettier plugins and avoid conflicts:
|
|
299
|
+
|
|
300
|
+
```javascript
|
|
301
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
302
|
+
import * as tailwindPlugin from "prettier-plugin-tailwindcss"
|
|
303
|
+
|
|
304
|
+
export default {
|
|
305
|
+
plugins: [
|
|
306
|
+
createPlugin({
|
|
307
|
+
// Your import sorting configuration
|
|
308
|
+
getGroup: statement => {
|
|
309
|
+
if (statement.path.startsWith("react")) return "react"
|
|
310
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
311
|
+
return "local"
|
|
312
|
+
},
|
|
313
|
+
separator: "\n",
|
|
314
|
+
|
|
315
|
+
// Other Prettier plugins to combine with (Plugin objects only)
|
|
316
|
+
otherPlugins: [
|
|
317
|
+
tailwindPlugin, // Import the plugin directly
|
|
318
|
+
// Add more plugins as needed...
|
|
319
|
+
],
|
|
320
|
+
|
|
321
|
+
// Configuration options for other plugins
|
|
322
|
+
prettierOptions: {
|
|
323
|
+
// TailwindCSS plugin options
|
|
324
|
+
tailwindConfig: "./tailwind.config.js",
|
|
325
|
+
tailwindFunctions: ["clsx", "cn", "cva"],
|
|
326
|
+
tailwindAttributes: ["class", "className", "ngClass", ":class"],
|
|
327
|
+
|
|
328
|
+
// Other plugin options can go here...
|
|
329
|
+
},
|
|
330
|
+
}),
|
|
331
|
+
],
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
**Important Notes:**
|
|
336
|
+
|
|
337
|
+
- `otherPlugins` only accepts imported Plugin objects, not string plugin names
|
|
338
|
+
- You must import the plugins yourself to ensure proper module resolution
|
|
339
|
+
- This approach avoids complex module loading issues and gives you full control
|
|
340
|
+
|
|
341
|
+
**Plugin Execution Order:**
|
|
342
|
+
|
|
343
|
+
- Other plugins are executed in the order they appear in the `otherPlugins` array
|
|
344
|
+
- Import sorting is always executed last to ensure compatibility
|
|
345
|
+
|
|
346
|
+
**Configuration Passing:**
|
|
347
|
+
|
|
348
|
+
- Options in `prettierOptions` are passed to all other plugins
|
|
349
|
+
- This allows other plugins to receive their configuration even when merged
|
|
350
|
+
|
|
201
351
|
### importSortRemoveUnused
|
|
202
352
|
|
|
203
353
|
Whether to remove unused imports, defaults to `false`.
|
|
@@ -390,7 +540,7 @@ Prettier natively cannot accept functions as configuration parameters (because c
|
|
|
390
540
|
|
|
391
541
|
```javascript
|
|
392
542
|
// Factory function is called in config file, returning a plugin instance
|
|
393
|
-
import { createPlugin } from "prettier-plugin-
|
|
543
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
394
544
|
|
|
395
545
|
export default {
|
|
396
546
|
plugins: [
|
package/README.zh-CN.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
### 安装
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
npm install prettier-plugin-
|
|
22
|
+
npm install @1adybug/prettier-plugin-sort-imports --save-dev
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### 基础配置
|
|
@@ -28,7 +28,7 @@ npm install prettier-plugin-import-sorts --save-dev
|
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
30
|
export default {
|
|
31
|
-
plugins: ["prettier-plugin-
|
|
31
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
@@ -50,7 +50,7 @@ npx prettier --write "src/**/*.{js,ts,jsx,tsx}"
|
|
|
50
50
|
|
|
51
51
|
```javascript
|
|
52
52
|
// prettier.config.mjs
|
|
53
|
-
import { createPlugin } from "prettier-plugin-
|
|
53
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
54
54
|
|
|
55
55
|
export default {
|
|
56
56
|
plugins: [
|
|
@@ -161,7 +161,7 @@ interface PluginConfig {
|
|
|
161
161
|
|
|
162
162
|
```javascript
|
|
163
163
|
export default {
|
|
164
|
-
plugins: ["prettier-plugin-
|
|
164
|
+
plugins: ["@1adybug/prettier-plugin-sort-imports"],
|
|
165
165
|
importSortSideEffect: false, // 是否对副作用导入排序
|
|
166
166
|
importSortSeparator: "", // 分组分隔符
|
|
167
167
|
importSortRemoveUnused: false, // 是否删除未使用的导入
|
|
@@ -173,7 +173,7 @@ export default {
|
|
|
173
173
|
使用 `createPlugin` 函数可以传递自定义函数:
|
|
174
174
|
|
|
175
175
|
```javascript
|
|
176
|
-
import { createPlugin } from "prettier-plugin-
|
|
176
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
177
177
|
|
|
178
178
|
export default {
|
|
179
179
|
plugins: [
|
|
@@ -198,6 +198,150 @@ export default {
|
|
|
198
198
|
}
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
+
### 方式 3:自定义插件模块
|
|
202
|
+
|
|
203
|
+
创建自定义插件模块以获得更好的组织性和可复用性:
|
|
204
|
+
|
|
205
|
+
**步骤 1**:创建自定义插件文件 `prettier-plugin-sort-imports.mjs`:
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
// prettier-plugin-sort-imports.mjs
|
|
209
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
210
|
+
|
|
211
|
+
export default createPlugin({
|
|
212
|
+
// 自定义分组逻辑
|
|
213
|
+
getGroup: statement => {
|
|
214
|
+
const path = statement.path
|
|
215
|
+
|
|
216
|
+
// React 及相关库
|
|
217
|
+
if (path.startsWith("react") || path.startsWith("@react")) {
|
|
218
|
+
return "react"
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// UI 库
|
|
222
|
+
if (path.includes("antd") || path.includes("@mui") || path.includes("chakra")) {
|
|
223
|
+
return "ui"
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// 工具库
|
|
227
|
+
if (path.includes("lodash") || path.includes("ramda") || path.includes("date-fns")) {
|
|
228
|
+
return "utils"
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 外部包 (node_modules)
|
|
232
|
+
if (!path.startsWith(".") && !path.startsWith("@/")) {
|
|
233
|
+
return "external"
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 内部别名 (@/)
|
|
237
|
+
if (path.startsWith("@/")) {
|
|
238
|
+
return "internal"
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 相对导入
|
|
242
|
+
return "relative"
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
// 定义分组顺序
|
|
246
|
+
sortGroup: (a, b) => {
|
|
247
|
+
const order = ["react", "external", "ui", "utils", "internal", "relative"]
|
|
248
|
+
return order.indexOf(a.name) - order.indexOf(b.name)
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
// 自定义导入内容排序
|
|
252
|
+
sortImportContent: (a, b) => {
|
|
253
|
+
// 类型在前,变量在后
|
|
254
|
+
if (a.type !== b.type) {
|
|
255
|
+
return a.type === "type" ? -1 : 1
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 同类型内按字母顺序
|
|
259
|
+
const aName = a.alias ?? a.name
|
|
260
|
+
const bName = b.alias ?? b.name
|
|
261
|
+
return aName.localeCompare(bName)
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
// 在分组间添加空行
|
|
265
|
+
separator: "\n",
|
|
266
|
+
|
|
267
|
+
// 排序副作用导入
|
|
268
|
+
sortSideEffect: true,
|
|
269
|
+
})
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**步骤 2**:在 `prettier.config.mjs` 中使用自定义插件:
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
// prettier.config.mjs
|
|
276
|
+
export default {
|
|
277
|
+
plugins: ["./prettier-plugin-sort-imports.mjs"],
|
|
278
|
+
// 其他 prettier 选项...
|
|
279
|
+
semi: false,
|
|
280
|
+
tabWidth: 4,
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
**此方法的优点**:
|
|
285
|
+
- ✅ **可复用**:在多个项目间共享相同配置
|
|
286
|
+
- ✅ **版本控制**:在 git 中跟踪你的导入排序规则
|
|
287
|
+
- ✅ **易维护**:将复杂逻辑从 prettier 配置中分离
|
|
288
|
+
- ✅ **团队协作**:团队成员间保持一致的导入排序规则
|
|
289
|
+
|
|
290
|
+
### 方式 4:插件兼容性
|
|
291
|
+
|
|
292
|
+
使用 `createPlugin` 的 `otherPlugins` 参数与其他 Prettier 插件合并,避免冲突:
|
|
293
|
+
|
|
294
|
+
```javascript
|
|
295
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
296
|
+
import * as tailwindPlugin from "prettier-plugin-tailwindcss"
|
|
297
|
+
|
|
298
|
+
export default {
|
|
299
|
+
plugins: [
|
|
300
|
+
createPlugin({
|
|
301
|
+
// 你的导入排序配置
|
|
302
|
+
getGroup: statement => {
|
|
303
|
+
if (statement.path.startsWith("react")) return "react"
|
|
304
|
+
if (!statement.path.startsWith(".")) return "external"
|
|
305
|
+
return "local"
|
|
306
|
+
},
|
|
307
|
+
separator: "\n",
|
|
308
|
+
|
|
309
|
+
// 要合并的其他 Prettier 插件(仅支持 Plugin 对象)
|
|
310
|
+
otherPlugins: [
|
|
311
|
+
tailwindPlugin, // 直接导入插件
|
|
312
|
+
// 根据需要添加更多插件...
|
|
313
|
+
],
|
|
314
|
+
|
|
315
|
+
// 其他插件的配置选项
|
|
316
|
+
prettierOptions: {
|
|
317
|
+
// TailwindCSS 插件选项
|
|
318
|
+
tailwindConfig: "./tailwind.config.js",
|
|
319
|
+
tailwindFunctions: ["clsx", "cn", "cva"],
|
|
320
|
+
tailwindAttributes: ["class", "className", "ngClass", ":class"],
|
|
321
|
+
|
|
322
|
+
// 其他插件选项可以在这里配置...
|
|
323
|
+
},
|
|
324
|
+
}),
|
|
325
|
+
],
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
**重要说明**:
|
|
330
|
+
|
|
331
|
+
- `otherPlugins` 只接受导入的 Plugin 对象,不支持字符串插件名称
|
|
332
|
+
- 你必须自己导入插件以确保正确的模块解析
|
|
333
|
+
- 这种方法避免了复杂的模块加载问题,给你完全的控制权
|
|
334
|
+
|
|
335
|
+
**插件执行顺序**:
|
|
336
|
+
|
|
337
|
+
- 其他插件按照在 `otherPlugins` 数组中出现的顺序执行
|
|
338
|
+
- 导入排序始终最后执行以确保兼容性
|
|
339
|
+
|
|
340
|
+
**配置传递**:
|
|
341
|
+
|
|
342
|
+
- `prettierOptions` 中的选项会传递给所有其他插件
|
|
343
|
+
- 这允许其他插件即使在合并时也能接收到它们的配置
|
|
344
|
+
|
|
201
345
|
### importSortRemoveUnused
|
|
202
346
|
|
|
203
347
|
是否删除未使用的导入,默认为 `false`。
|
|
@@ -390,7 +534,7 @@ Prettier 原生无法接受函数作为配置参数(因为配置需要序列
|
|
|
390
534
|
|
|
391
535
|
```javascript
|
|
392
536
|
// 工厂函数在配置文件中被调用,返回一个插件实例
|
|
393
|
-
import { createPlugin } from "prettier-plugin-
|
|
537
|
+
import { createPlugin } from "@1adybug/prettier-plugin-sort-imports"
|
|
394
538
|
|
|
395
539
|
export default {
|
|
396
540
|
plugins: [
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Plugin } from "prettier";
|
|
2
|
-
import { PluginConfig } from "./types";
|
|
3
|
-
|
|
4
|
-
declare const plugin: Plugin;
|
|
2
|
+
import type { PluginConfig } from "./types";
|
|
3
|
+
export * from "./types";
|
|
5
4
|
/** 创建自定义配置的插件(工厂函数) */
|
|
6
5
|
export declare function createPlugin(config?: PluginConfig): Plugin;
|
|
6
|
+
/** 默认插件实例(用于简单使用) */
|
|
7
|
+
declare const plugin: Plugin;
|
|
7
8
|
export default plugin;
|
package/dist/index.js
CHANGED
|
@@ -235,7 +235,8 @@ function parseImportNode(node, comments, usedComments) {
|
|
|
235
235
|
start,
|
|
236
236
|
end
|
|
237
237
|
};
|
|
238
|
-
const
|
|
238
|
+
const isTypeOnlyExport = "type" === node.exportKind;
|
|
239
|
+
const importContents = parseExportSpecifiers(node, isTypeOnlyExport);
|
|
239
240
|
return {
|
|
240
241
|
path: source,
|
|
241
242
|
isExport: true,
|
|
@@ -289,7 +290,7 @@ function parseImportSpecifiers(node, isTypeOnlyImport = false) {
|
|
|
289
290
|
}
|
|
290
291
|
return contents;
|
|
291
292
|
}
|
|
292
|
-
function parseExportSpecifiers(node) {
|
|
293
|
+
function parseExportSpecifiers(node, isTypeOnlyExport = false) {
|
|
293
294
|
const contents = [];
|
|
294
295
|
if (!node.specifiers) return contents;
|
|
295
296
|
for (const specifier of node.specifiers)if ("ExportSpecifier" === specifier.type) {
|
|
@@ -305,7 +306,7 @@ function parseExportSpecifiers(node) {
|
|
|
305
306
|
}
|
|
306
307
|
const localName = "Identifier" === specifier.local.type ? specifier.local.name : specifier.local.value;
|
|
307
308
|
const exportedName = "Identifier" === specifier.exported.type ? specifier.exported.name : specifier.exported.value;
|
|
308
|
-
const isTypeExport = "type" === specifier.exportKind;
|
|
309
|
+
const isTypeExport = isTypeOnlyExport || "type" === specifier.exportKind;
|
|
309
310
|
contents.push({
|
|
310
311
|
name: localName,
|
|
311
312
|
alias: localName !== exportedName ? exportedName : void 0,
|
|
@@ -525,33 +526,33 @@ function mergeImports(imports) {
|
|
|
525
526
|
return Array.from(mergedMap.values());
|
|
526
527
|
}
|
|
527
528
|
const src_require = createRequire(import.meta.url);
|
|
528
|
-
|
|
529
|
-
function preprocessImports(text, options) {
|
|
529
|
+
function preprocessImports(text, options, config = {}) {
|
|
530
530
|
try {
|
|
531
531
|
const imports = parseImports(text);
|
|
532
532
|
if (0 === imports.length) return text;
|
|
533
|
-
const
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
533
|
+
const optionsConfig = options;
|
|
534
|
+
const finalConfig = {
|
|
535
|
+
getGroup: config.getGroup ?? optionsConfig.getGroup,
|
|
536
|
+
sortGroup: config.sortGroup ?? optionsConfig.sortGroup,
|
|
537
|
+
sortImportStatement: config.sortImportStatement ?? optionsConfig.sortImportStatement,
|
|
538
|
+
sortImportContent: config.sortImportContent ?? optionsConfig.sortImportContent,
|
|
539
|
+
separator: config.separator ?? optionsConfig.importSortSeparator ?? optionsConfig.separator,
|
|
540
|
+
sortSideEffect: config.sortSideEffect ?? optionsConfig.importSortSideEffect ?? false,
|
|
541
|
+
removeUnusedImports: config.removeUnusedImports ?? optionsConfig.importSortRemoveUnused ?? false
|
|
541
542
|
};
|
|
542
543
|
let processedImports = imports;
|
|
543
|
-
if (
|
|
544
|
+
if (finalConfig.removeUnusedImports) {
|
|
544
545
|
const lastImport = imports[imports.length - 1];
|
|
545
546
|
const codeAfterImports = text.slice(lastImport.end ?? 0);
|
|
546
547
|
processedImports = removeUnusedImportsFromStatements(imports, codeAfterImports);
|
|
547
548
|
}
|
|
548
|
-
const sortedImports = sortImports(processedImports,
|
|
549
|
+
const sortedImports = sortImports(processedImports, finalConfig);
|
|
549
550
|
const mergedImports = mergeImports(sortedImports);
|
|
550
551
|
let formattedImports;
|
|
551
|
-
if (
|
|
552
|
-
const groups = groupImports(mergedImports,
|
|
553
|
-
const sortedGroups = sortGroups(groups,
|
|
554
|
-
formattedImports = formatGroups(sortedGroups,
|
|
552
|
+
if (finalConfig.getGroup) {
|
|
553
|
+
const groups = groupImports(mergedImports, finalConfig);
|
|
554
|
+
const sortedGroups = sortGroups(groups, finalConfig);
|
|
555
|
+
formattedImports = formatGroups(sortedGroups, finalConfig);
|
|
555
556
|
} else formattedImports = formatImportStatements(mergedImports);
|
|
556
557
|
const firstImport = imports[0];
|
|
557
558
|
const lastImport = imports[imports.length - 1];
|
|
@@ -567,50 +568,79 @@ function preprocessImports(text, options) {
|
|
|
567
568
|
return text;
|
|
568
569
|
}
|
|
569
570
|
}
|
|
570
|
-
const
|
|
571
|
-
const
|
|
572
|
-
const
|
|
573
|
-
function
|
|
571
|
+
const { parsers: { babel } } = src_require("prettier/parser-babel");
|
|
572
|
+
const { parsers: { typescript } } = src_require("prettier/parser-typescript");
|
|
573
|
+
const { parsers: { "babel-ts": babelTs } } = src_require("prettier/parser-babel");
|
|
574
|
+
function createCombinedPreprocess(parserName, config) {
|
|
575
|
+
return function(text, options) {
|
|
576
|
+
const otherPlugins = config.otherPlugins || [];
|
|
577
|
+
if (0 === otherPlugins.length) return preprocessImports(text, options, config);
|
|
578
|
+
const prettierOptions = config.prettierOptions || {};
|
|
579
|
+
const mergedOptions = {
|
|
580
|
+
...options,
|
|
581
|
+
...prettierOptions
|
|
582
|
+
};
|
|
583
|
+
const preprocessFunctions = [];
|
|
584
|
+
for (const plugin of otherPlugins){
|
|
585
|
+
const parser = plugin?.parsers?.[parserName];
|
|
586
|
+
if (parser?.preprocess && "function" == typeof parser.preprocess) preprocessFunctions.push(parser.preprocess);
|
|
587
|
+
}
|
|
588
|
+
preprocessFunctions.push((text, options)=>preprocessImports(text, options, config));
|
|
589
|
+
let processedText = text;
|
|
590
|
+
for (const preprocess of preprocessFunctions)try {
|
|
591
|
+
processedText = preprocess(processedText, mergedOptions);
|
|
592
|
+
} catch (error) {
|
|
593
|
+
console.warn("Plugin preprocess failed:", error instanceof Error ? error.message : String(error));
|
|
594
|
+
}
|
|
595
|
+
return processedText;
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function createPluginInstance(config = {}) {
|
|
599
|
+
const baseOptions = {
|
|
600
|
+
importSortSeparator: {
|
|
601
|
+
type: "string",
|
|
602
|
+
category: "Import Sort",
|
|
603
|
+
description: "分组之间的分隔符"
|
|
604
|
+
},
|
|
605
|
+
importSortSideEffect: {
|
|
606
|
+
type: "boolean",
|
|
607
|
+
category: "Import Sort",
|
|
608
|
+
description: "是否对副作用导入进行排序",
|
|
609
|
+
default: false
|
|
610
|
+
},
|
|
611
|
+
importSortRemoveUnused: {
|
|
612
|
+
type: "boolean",
|
|
613
|
+
category: "Import Sort",
|
|
614
|
+
description: "是否删除未使用的导入",
|
|
615
|
+
default: false
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
const otherPlugins = config.otherPlugins || [];
|
|
619
|
+
const mergedOptions = {
|
|
620
|
+
...baseOptions
|
|
621
|
+
};
|
|
622
|
+
for (const plugin of otherPlugins)if (plugin?.options) Object.assign(mergedOptions, plugin.options);
|
|
574
623
|
return {
|
|
575
624
|
parsers: {
|
|
576
625
|
babel: {
|
|
577
|
-
...
|
|
578
|
-
preprocess:
|
|
626
|
+
...babel,
|
|
627
|
+
preprocess: createCombinedPreprocess("babel", config)
|
|
579
628
|
},
|
|
580
629
|
typescript: {
|
|
581
|
-
...
|
|
582
|
-
preprocess:
|
|
630
|
+
...typescript,
|
|
631
|
+
preprocess: createCombinedPreprocess("typescript", config)
|
|
583
632
|
},
|
|
584
633
|
"babel-ts": {
|
|
585
|
-
...
|
|
586
|
-
preprocess:
|
|
634
|
+
...babelTs,
|
|
635
|
+
preprocess: createCombinedPreprocess("babel-ts", config)
|
|
587
636
|
}
|
|
588
637
|
},
|
|
589
|
-
options:
|
|
590
|
-
importSortSeparator: {
|
|
591
|
-
type: "string",
|
|
592
|
-
category: "Import Sort",
|
|
593
|
-
description: "分组之间的分隔符"
|
|
594
|
-
},
|
|
595
|
-
importSortSideEffect: {
|
|
596
|
-
type: "boolean",
|
|
597
|
-
category: "Import Sort",
|
|
598
|
-
description: "是否对副作用导入进行排序",
|
|
599
|
-
default: false
|
|
600
|
-
},
|
|
601
|
-
importSortRemoveUnused: {
|
|
602
|
-
type: "boolean",
|
|
603
|
-
category: "Import Sort",
|
|
604
|
-
description: "是否删除未使用的导入",
|
|
605
|
-
default: false
|
|
606
|
-
}
|
|
607
|
-
}
|
|
638
|
+
options: mergedOptions
|
|
608
639
|
};
|
|
609
640
|
}
|
|
610
|
-
const src_plugin = createPluginInstance();
|
|
611
641
|
function createPlugin(config = {}) {
|
|
612
|
-
|
|
613
|
-
return createPluginInstance();
|
|
642
|
+
return createPluginInstance(config);
|
|
614
643
|
}
|
|
644
|
+
const src_plugin = createPluginInstance();
|
|
615
645
|
const src = src_plugin;
|
|
616
646
|
export { createPlugin, src as default };
|
package/dist/parser.d.ts
CHANGED
package/dist/sorter.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Group, ImportContent, ImportStatement, PluginConfig } from "./types"; /** 导入类型 */
|
|
2
2
|
/** 合并后的配置 */
|
|
3
|
-
export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" | "removeUnusedImports"> {
|
|
3
|
+
export interface MergedConfig extends Omit<Required<PluginConfig>, "separator" | "removeUnusedImports" | "otherPlugins" | "prettierOptions"> {
|
|
4
4
|
separator: PluginConfig["separator"];
|
|
5
5
|
removeUnusedImports: boolean;
|
|
6
6
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Plugin } from "prettier";
|
|
1
2
|
/** 导入内容 */
|
|
2
3
|
export interface ImportContent {
|
|
3
4
|
/** 导入的内容的名称 */
|
|
@@ -67,4 +68,8 @@ export interface PluginConfig {
|
|
|
67
68
|
sortSideEffect?: boolean;
|
|
68
69
|
/** 是否删除未使用的导入,默认为 false */
|
|
69
70
|
removeUnusedImports?: boolean;
|
|
71
|
+
/** 要合并的其他 Prettier 插件,按传入顺序执行 */
|
|
72
|
+
otherPlugins?: Plugin[];
|
|
73
|
+
/** 传递给其他插件的 Prettier 配置选项 */
|
|
74
|
+
prettierOptions?: Record<string, any>;
|
|
70
75
|
}
|