prettier-plugin-sort 0.1.0 → 0.1.1
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 +2 -2
- package/README.zh.md +9 -9
- package/dist/index.js +8 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ import react from 'react';
|
|
|
56
56
|
import App from './App.tsx';
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
Each import belongs to a group. For example, `node:fs` is a `builtin` (covers Node.js
|
|
59
|
+
Each import belongs to a group. For example, `node:fs` is a `builtin` (covers Node.js and Bun **runtime** built-ins), while `react` and `lodash` are `external` npm packages. The plugin first classifies each import by group, then sorts alphabetically within each group.
|
|
60
60
|
|
|
61
61
|
Grouping and ordering follow the conventions of [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import)'s [import/order](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/order.md) rule.
|
|
62
62
|
|
|
@@ -76,7 +76,7 @@ Group matchers:
|
|
|
76
76
|
|
|
77
77
|
| Group | Matches | Examples |
|
|
78
78
|
| ---------- | ----------------------------------------------------------- | ---------------------------------- |
|
|
79
|
-
| `builtin` | `node:*`, `bun:*`, `
|
|
79
|
+
| `builtin` | `node:*`, `bun:*`, `bun`, and unprefixed Node built-ins | `node:fs`, `path`, `bun` |
|
|
80
80
|
| `external` | npm packages, and anything that doesn't match another group | `react`, `@scope/pkg` |
|
|
81
81
|
| `internal` | Project absolute paths and aliases | `/utils`, `~/app`, `@/shared` |
|
|
82
82
|
| `parent` | Parent-relative paths | `../Button` |
|
package/README.zh.md
CHANGED
|
@@ -56,7 +56,7 @@ import react from 'react';
|
|
|
56
56
|
import App from './App.tsx';
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
import 的模块一般可以按其来源分为不同的类别,例如上面的示例代码,'node:fs' 就属于 builtin 分类,该分类涵盖 Node.js
|
|
59
|
+
import 的模块一般可以按其来源分为不同的类别,例如上面的示例代码,'node:fs' 就属于 builtin 分类,该分类涵盖 Node.js 和 Bun **运行时**的内置模块。而像 react 和 lodash 这种直接从 npm 下载的依赖模块,属于 external 分类。
|
|
60
60
|
|
|
61
61
|
插件会先根据不同的模块进行分类,然后在模块内部基于字母顺序进行排列。
|
|
62
62
|
|
|
@@ -76,14 +76,14 @@ import 的模块一般可以按其来源分为不同的类别,例如上面的
|
|
|
76
76
|
|
|
77
77
|
各分组的匹配规则如下:
|
|
78
78
|
|
|
79
|
-
| 分组 | 匹配内容
|
|
80
|
-
| ---------- |
|
|
81
|
-
| `builtin` | `node:*`、`bun:*`、`
|
|
82
|
-
| `external` | npm 包,以及不属于其他分组的模块
|
|
83
|
-
| `internal` | 项目绝对路径与别名
|
|
84
|
-
| `parent` | 向上跳级的相对路径
|
|
85
|
-
| `sibling` | 同级相对路径(不包含 index)
|
|
86
|
-
| `index` | 当前目录的 index 模块
|
|
79
|
+
| 分组 | 匹配内容 | 示例 |
|
|
80
|
+
| ---------- | ---------------------------------------------------- | ---------------------------------- |
|
|
81
|
+
| `builtin` | `node:*`、`bun:*`、`bun`,以及无前缀的 Node 内置模块 | `node:fs`、`path`、`bun` |
|
|
82
|
+
| `external` | npm 包,以及不属于其他分组的模块 | `react`、`@scope/pkg` |
|
|
83
|
+
| `internal` | 项目绝对路径与别名 | `/utils`、`~/app`、`@/shared` |
|
|
84
|
+
| `parent` | 向上跳级的相对路径 | `../Button` |
|
|
85
|
+
| `sibling` | 同级相对路径(不包含 index) | `./Icon` |
|
|
86
|
+
| `index` | 当前目录的 index 模块 | `.`、`./`、`./index`、`./index.ts` |
|
|
87
87
|
|
|
88
88
|
> **关于 `internal` 的检测方式:** 插件目前通过说明符的前缀硬编码(`/`、`~`、`@/`)来判断,不会读取 tsconfig `paths` 或任何构建工具的配置。后续版本可能会提供 `importOrderInternalPatterns` 选项,支持自定义正则匹配。
|
|
89
89
|
|
package/dist/index.js
CHANGED
|
@@ -173,7 +173,7 @@ function stripTypePrefix(member) {
|
|
|
173
173
|
import { builtinModules } from "node:module";
|
|
174
174
|
var NODE_BUILTINS = new Set(builtinModules);
|
|
175
175
|
function detectGroup(source) {
|
|
176
|
-
if (source
|
|
176
|
+
if (source === "bun" || source.startsWith("bun:") || source.startsWith("node:")) {
|
|
177
177
|
return "builtin";
|
|
178
178
|
}
|
|
179
179
|
const slashIndex = source.indexOf("/");
|
|
@@ -251,7 +251,7 @@ function parseImport(statement) {
|
|
|
251
251
|
};
|
|
252
252
|
}
|
|
253
253
|
function extractImportBlock(text) {
|
|
254
|
-
const firstRe = /(?:^|\n)(?:[ \t]*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)[ \t]*\n)*[ \t]*import\b/;
|
|
254
|
+
const firstRe = /(?:^|\n)(?:[ \t]*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)[ \t]*\n)*[ \t]*import\b(?![.(])/;
|
|
255
255
|
const first = firstRe.exec(text);
|
|
256
256
|
if (!first) {
|
|
257
257
|
return null;
|
|
@@ -260,7 +260,7 @@ function extractImportBlock(text) {
|
|
|
260
260
|
` ? 1 : 0);
|
|
261
261
|
const statements = [];
|
|
262
262
|
const skipRe = /(?:[ \t]*(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)?[ \t]*\n)*/y;
|
|
263
|
-
const importRe = /[ \t]*(import\b[\s\S]*?(?:from\s*(['"])[^'"]+\2|(['"])[^'"]+\3)(?:\s+with\s*\{[^}]*\})?\s*;?)/y;
|
|
263
|
+
const importRe = /[ \t]*(import\b(?![.(])[\s\S]*?(?:from\s*(['"])[^'"]+\2|(['"])[^'"]+\3)(?:\s+with\s*\{[^}]*\})?\s*;?)/y;
|
|
264
264
|
let cursor = start;
|
|
265
265
|
while (cursor < text.length) {
|
|
266
266
|
skipRe.lastIndex = cursor;
|
|
@@ -338,7 +338,7 @@ function normalizeTypeClause(importDecl) {
|
|
|
338
338
|
};
|
|
339
339
|
}
|
|
340
340
|
function mergeImportsFromSameSource(imports) {
|
|
341
|
-
const
|
|
341
|
+
const indexByKey = new Map;
|
|
342
342
|
const result = [];
|
|
343
343
|
for (const rawImport of imports) {
|
|
344
344
|
const importDecl = normalizeTypeClause(rawImport);
|
|
@@ -346,9 +346,10 @@ function mergeImportsFromSameSource(imports) {
|
|
|
346
346
|
result.push(importDecl);
|
|
347
347
|
continue;
|
|
348
348
|
}
|
|
349
|
-
const
|
|
349
|
+
const mergeKey = `${importDecl.source}\x00${importDecl.attributes ?? ""}`;
|
|
350
|
+
const existingIndex = indexByKey.get(mergeKey);
|
|
350
351
|
if (existingIndex === undefined) {
|
|
351
|
-
|
|
352
|
+
indexByKey.set(mergeKey, result.length);
|
|
352
353
|
result.push(importDecl);
|
|
353
354
|
continue;
|
|
354
355
|
}
|
|
@@ -360,7 +361,7 @@ function mergeImportsFromSameSource(imports) {
|
|
|
360
361
|
defaultSpec: existing.defaultSpec ?? importDecl.defaultSpec,
|
|
361
362
|
namespaceSpec: existing.namespaceSpec ?? importDecl.namespaceSpec,
|
|
362
363
|
members: existing.members === null && importDecl.members === null ? null : [...existing.members ?? [], ...importDecl.members ?? []],
|
|
363
|
-
attributes: existing.attributes
|
|
364
|
+
attributes: existing.attributes,
|
|
364
365
|
leadingComments: existing.leadingComments
|
|
365
366
|
};
|
|
366
367
|
}
|