prettier-plugin-sort 0.2.0 → 1.0.0
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 +97 -259
- package/README.zh.md +91 -258
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1742 -636
- package/dist/options.d.ts +13 -16
- package/dist/parser-ast.d.ts +78 -0
- package/dist/sort-exports.d.ts +6 -9
- package/dist/sort-imports.d.ts +8 -2
- package/dist/sort-package.d.ts +6 -2
- package/dist/sort-typescript.d.ts +6 -0
- package/dist/utils/package-rules.d.ts +12 -0
- package/dist/utils/source-text.d.ts +9 -0
- package/package.json +12 -8
- package/dist/order-package.d.ts +0 -10
- package/dist/utils.d.ts +0 -8
package/dist/options.d.ts
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import { type ParserOptions, type SupportOptions } from 'prettier';
|
|
2
|
-
/** import
|
|
2
|
+
/** import 声明分组。 */
|
|
3
3
|
export type ImportGroup = 'builtin' | 'external' | 'internal' | 'parent' | 'sibling' | 'index';
|
|
4
|
-
/**
|
|
5
|
-
export type
|
|
4
|
+
/** type import 的声明形式与成员顺序。 */
|
|
5
|
+
export type TypeImportStyle = 'separate' | 'inline-first' | 'inline-last' | 'mixed';
|
|
6
6
|
/** 插件排序配置。 */
|
|
7
7
|
export interface SortOptions {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
packageJsonOrderExcludeKeys?: string[];
|
|
8
|
+
esmImportSort?: boolean;
|
|
9
|
+
esmImportGroups?: ImportGroup[];
|
|
10
|
+
esmImportSeparation?: boolean;
|
|
11
|
+
esmImportTypeStyle?: TypeImportStyle;
|
|
12
|
+
esmImportMerge?: boolean;
|
|
13
|
+
esmExportSpecifierSort?: boolean;
|
|
14
|
+
packageSort?: boolean;
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
export declare const DEFAULT_SORT_OPTIONS: Required<SortOptions>;
|
|
19
|
-
type RawSortOptions = ParserOptions & Partial<SortOptions>;
|
|
16
|
+
type SortPluginParserOptions = ParserOptions & SortOptions;
|
|
20
17
|
/**
|
|
21
18
|
* 从 Prettier 选项中提取插件配置,缺失或非法的参数会回退为默认值。
|
|
22
19
|
*/
|
|
23
|
-
export declare function resolveSortOptions(
|
|
24
|
-
/**
|
|
20
|
+
export declare function resolveSortOptions(prettierOptions: SortPluginParserOptions): Required<SortOptions>;
|
|
21
|
+
/** Prettier 插件选项。 */
|
|
25
22
|
export declare const options: SupportOptions;
|
|
26
23
|
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type SourceTextRange } from './utils/source-text';
|
|
2
|
+
/**
|
|
3
|
+
* 已注册 Prettier parser 的源码偏移字段。
|
|
4
|
+
*
|
|
5
|
+
* `start`/`end` 是 Babel AST 使用的绝对偏移。
|
|
6
|
+
* `range` 是 typescript-estree 等 ESTree 兼容 AST 使用的 `[start, end]` 偏移元组。
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://babeljs.io/docs/babel-parser#options | Babel parser 位置选项}
|
|
9
|
+
* @see {@link https://typescript-eslint.io/packages/typescript-estree/#api | typescript-estree 的 range 选项}
|
|
10
|
+
*/
|
|
11
|
+
interface ParserAstLocation extends Record<string, unknown> {
|
|
12
|
+
readonly start?: number;
|
|
13
|
+
readonly end?: number;
|
|
14
|
+
readonly range?: readonly [number, number];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 排序逻辑使用的跨 parser AST 字段。
|
|
18
|
+
*
|
|
19
|
+
* 字段均来自已注册 parser 的上游 AST:
|
|
20
|
+
*
|
|
21
|
+
* - `type`、`name`、`value`、`body` 来自 ESTree 基础节点
|
|
22
|
+
* - `declaration`、`specifiers`、`source`、`imported`、`local`、`exported` 来自 ESTree ES2015 module 节点
|
|
23
|
+
* - `program`、`comments` 来自 Babel `File` 包装节点及 Prettier 的顶层注释约定
|
|
24
|
+
* - `importKind`、`phase` 来自 Babel/typescript-estree 的 import 扩展
|
|
25
|
+
*
|
|
26
|
+
* 不同 parser 的字段结构存在差异,因此属性均为可选,并在使用处收窄类型。
|
|
27
|
+
*
|
|
28
|
+
* @see {@link https://github.com/estree/estree/blob/master/es5.md#node-objects | ESTree 基础节点}
|
|
29
|
+
* @see {@link https://github.com/estree/estree/blob/master/es2015.md#modules | ESTree module 节点}
|
|
30
|
+
* @see {@link https://babeljs.io/docs/babel-types#file | Babel File 节点}
|
|
31
|
+
* @see {@link https://babeljs.io/docs/babel-types#importdeclaration | Babel ImportDeclaration 节点}
|
|
32
|
+
* @see {@link https://typescript-eslint.io/packages/typescript-estree/ast-spec/#importdeclaration | typescript-estree ImportDeclaration 节点}
|
|
33
|
+
* @see {@link https://prettier.io/docs/plugins#handling-comments-in-a-printer | Prettier 顶层注释约定}
|
|
34
|
+
*/
|
|
35
|
+
export interface ParserAstNode extends ParserAstLocation {
|
|
36
|
+
readonly type: string;
|
|
37
|
+
readonly name?: unknown;
|
|
38
|
+
readonly value?: unknown;
|
|
39
|
+
readonly body?: ParserAstNode[];
|
|
40
|
+
readonly comments?: ParserAstComment[];
|
|
41
|
+
readonly program?: ParserAstNode;
|
|
42
|
+
readonly declaration?: ParserAstNode | null;
|
|
43
|
+
readonly specifiers?: ParserAstNode[];
|
|
44
|
+
readonly source?: ParserAstNode | null;
|
|
45
|
+
readonly imported?: ParserAstNode | null;
|
|
46
|
+
readonly local?: ParserAstNode | null;
|
|
47
|
+
readonly exported?: ParserAstNode | null;
|
|
48
|
+
readonly importKind?: unknown;
|
|
49
|
+
readonly phase?: unknown;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 排序逻辑使用的 parser 注释字段。
|
|
53
|
+
*
|
|
54
|
+
* `value` 是不含注释分隔符的正文;源码偏移来自 parser 节点的 `start`/`end` 或 `range`。
|
|
55
|
+
*
|
|
56
|
+
* @see {@link https://prettier.io/docs/plugins#handling-comments-in-a-printer | Prettier 注释节点约定}
|
|
57
|
+
*/
|
|
58
|
+
export interface ParserAstComment extends ParserAstLocation {
|
|
59
|
+
readonly value?: unknown;
|
|
60
|
+
}
|
|
61
|
+
export interface ParserAstCommentWithTextRange {
|
|
62
|
+
readonly comment: ParserAstComment;
|
|
63
|
+
readonly textRange: SourceTextRange;
|
|
64
|
+
}
|
|
65
|
+
export declare function getAstNodeTextRange(node: ParserAstNode | ParserAstComment | undefined | null): SourceTextRange | null;
|
|
66
|
+
export declare function getSortedAstCommentsWithTextRanges(comments: readonly ParserAstComment[]): ParserAstCommentWithTextRange[];
|
|
67
|
+
/** 使用二分查找定位指定源码位置之后的第一条注释。 */
|
|
68
|
+
export declare function findCommentIndexAtOrAfter(sortedComments: readonly ParserAstCommentWithTextRange[], sourceIndex: number): number;
|
|
69
|
+
export declare function getAstNodeName(node: ParserAstNode | undefined | null): string | null;
|
|
70
|
+
export declare function getAstCommentText(comment: ParserAstComment): string | null;
|
|
71
|
+
export declare function getProgramStatements(parserAst: ParserAstNode): readonly ParserAstNode[];
|
|
72
|
+
export declare function getProgramComments(parserAst: ParserAstNode): readonly ParserAstComment[];
|
|
73
|
+
export declare function isSourceRangeWhitespaceOrComments(sourceText: string, sourceRange: SourceTextRange, sortedComments: readonly ParserAstCommentWithTextRange[]): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Prettier 会在解析器预处理之后才附着 `prettier-ignore` 注释,因此文本变换必须先自行识别。
|
|
76
|
+
*/
|
|
77
|
+
export declare function isPrettierIgnored(sourceText: string, statementRange: SourceTextRange, sortedComments: readonly ParserAstCommentWithTextRange[]): boolean;
|
|
78
|
+
export {};
|
package/dist/sort-exports.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ParserAstCommentWithTextRange, type ParserAstNode } from './parser-ast';
|
|
2
|
+
import { type SourceTextEdit } from './utils/source-text';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
* 不改变语句位置,不跨语句合并,只针对单条 export 语句的花括号内部。
|
|
4
|
+
* 为无注释的 `export { ... }` 生成 specifier 排序编辑。
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* export { a, b } from 'mod';
|
|
9
|
-
* export type { A, B };
|
|
10
|
-
* export type { A, B } from 'mod';
|
|
6
|
+
* 注释位于逗号两侧时,仅靠 parser 暴露的裸 AST 无法可靠判断它属于前一项还是后一项。
|
|
7
|
+
* 遇到这种声明时保持原样,避免为了排序改变注释语义。
|
|
11
8
|
*/
|
|
12
|
-
export declare function
|
|
9
|
+
export declare function buildExportSortingEdits(sourceText: string, programStatements: readonly ParserAstNode[], sortedComments: readonly ParserAstCommentWithTextRange[]): SourceTextEdit[];
|
package/dist/sort-imports.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type SortOptions } from './options';
|
|
2
|
+
import { type ParserAstCommentWithTextRange, type ParserAstNode } from './parser-ast';
|
|
3
|
+
import { type SourceTextEdit } from './utils/source-text';
|
|
4
|
+
/**
|
|
5
|
+
* 将顶层 import 划分为可独立排序的片段,并生成基于原文范围的编辑。
|
|
6
|
+
* 被忽略的声明、独立注释和位置敏感指令不会进入编辑范围。
|
|
7
|
+
*/
|
|
8
|
+
export declare function buildImportSortingEdits(sourceText: string, programStatements: readonly ParserAstNode[], sortedComments: readonly ParserAstCommentWithTextRange[], sortOptions: Required<SortOptions>, isPrettierFilePragmaPresent: boolean): SourceTextEdit[];
|
package/dist/sort-package.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
import { type ParserOptions } from 'prettier';
|
|
2
|
-
|
|
1
|
+
import { type Parser, type ParserOptions } from 'prettier';
|
|
2
|
+
/**
|
|
3
|
+
* 只处理 package.json。
|
|
4
|
+
* 解析失败、AST 结构不完整或数字无法还原时直接返回原文。
|
|
5
|
+
*/
|
|
6
|
+
export declare function preprocessPackageJson(sourceText: string, prettierOptions: ParserOptions, parser: Parser): Promise<string>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Parser, type ParserOptions } from 'prettier';
|
|
2
|
+
/**
|
|
3
|
+
* 顶层 ES module 排序事务:原文只解析一次,所有编辑都基于同一份 AST 范围生成。
|
|
4
|
+
* 编辑应用后再次解析;范围冲突或无效语法都会让整次变换返回原文。
|
|
5
|
+
*/
|
|
6
|
+
export declare function sortTypeScript(sourceText: string, prettierOptions: ParserOptions, parser: Parser): Promise<string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基于 `sort-package-json` 4.0.0 默认规则的 `package.json` 顶层字段顺序。
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://github.com/keithamus/sort-package-json/blob/v4.0.0/defaultRules.md | sort-package-json 4.0.0 默认规则}
|
|
5
|
+
*/
|
|
6
|
+
export declare const PACKAGE_JSON_FIELD_ORDER: readonly ["$schema", "name", "displayName", "version", "stableVersion", "private", "description", "categories", "keywords", "homepage", "bugs", "repository", "funding", "license", "qna", "author", "maintainers", "contributors", "publisher", "sideEffects", "type", "imports", "exports", "main", "svelte", "umd:main", "jsdelivr", "unpkg", "module", "source", "jsnext:main", "browser", "react-native", "types", "typesVersions", "typings", "style", "example", "examplestyle", "assets", "bin", "man", "directories", "files", "workspaces", "binary", "scripts", "betterScripts", "wireit", "l10n", "contributes", "activationEvents", "husky", "simple-git-hooks", "pre-commit", "commitlint", "lint-staged", "nano-staged", "config", "nodemonConfig", "browserify", "babel", "browserslist", "xo", "prettier", "eslintConfig", "eslintIgnore", "npmpkgjsonlint", "npmPackageJsonLintConfig", "npmpackagejsonlint", "release", "remarkConfig", "stylelint", "ava", "jest", "jest-junit", "jest-stare", "mocha", "nyc", "c8", "tap", "oclif", "resolutions", "overrides", "dependencies", "devDependencies", "dependenciesMeta", "peerDependencies", "peerDependenciesMeta", "optionalDependencies", "bundledDependencies", "bundleDependencies", "extensionPack", "extensionDependencies", "flat", "packageManager", "engines", "engineStrict", "devEngines", "volta", "languageName", "os", "cpu", "preferGlobal", "publishConfig", "icon", "badges", "galleryBanner", "preview", "markdown", "pnpm"];
|
|
7
|
+
export declare const DIRECTORY_FIELD_ORDER: readonly ["lib", "bin", "man", "doc", "example", "test"];
|
|
8
|
+
export declare const ESLINT_CONFIG_FIELD_ORDER: readonly ["files", "excludedFiles", "env", "parser", "parserOptions", "settings", "plugins", "extends", "rules", "overrides", "globals", "processor", "noInlineConfig", "reportUnusedDisableDirectives"];
|
|
9
|
+
export declare const GIT_HOOK_ORDER: readonly ["applypatch-msg", "pre-applypatch", "post-applypatch", "pre-commit", "pre-merge-commit", "prepare-commit-msg", "commit-msg", "post-commit", "pre-rebase", "post-checkout", "post-merge", "pre-push", "pre-receive", "update", "proc-receive", "post-receive", "post-update", "reference-transaction", "push-to-checkout", "pre-auto-gc", "post-rewrite", "sendemail-validate", "fsmonitor-watchman", "p4-changelist", "p4-prepare-changelist", "p4-post-changelist", "p4-pre-submit", "post-index-change"];
|
|
10
|
+
export declare const NPM_LIFECYCLE_SCRIPT_NAMES: ReadonlySet<string>;
|
|
11
|
+
export declare const PNPM_CONFIG_FIELD_ORDER: readonly ["peerDependencyRules", "neverBuiltDependencies", "onlyBuiltDependencies", "onlyBuiltDependenciesFile", "allowedDeprecatedVersions", "allowNonAppliedPatches", "updateConfig", "auditConfig", "requiredScripts", "supportedArchitectures", "overrides", "patchedDependencies", "packageExtensions"];
|
|
12
|
+
export declare const WIREIT_SCRIPT_FIELD_ORDER: readonly ["command", "dependencies", "files", "output"];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface SourceTextRange {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
}
|
|
5
|
+
export interface SourceTextEdit extends SourceTextRange {
|
|
6
|
+
replacementText: string;
|
|
7
|
+
}
|
|
8
|
+
/** 校验全部范围后倒序应用编辑;范围无效或相互重叠时返回 null。 */
|
|
9
|
+
export declare function applySourceTextEdits(sourceText: string, sourceTextEdits: readonly SourceTextEdit[]): string | null;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-sort",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Prettier plugin for sorting ES module imports, export specifiers, and package.json keys.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"imports",
|
|
7
7
|
"order",
|
|
@@ -35,28 +35,32 @@
|
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
|
-
"build": "bun run scripts/build
|
|
39
|
-
"format": "prettier **/*.ts --write",
|
|
40
|
-
"lint": "eslint
|
|
38
|
+
"build": "bun run scripts/build.ts",
|
|
39
|
+
"format": "prettier **/*.{json,ts,md} --write --experimental-cli",
|
|
40
|
+
"lint": "eslint src/ --cache",
|
|
41
|
+
"lint:fix": "eslint src/ --fix --cache",
|
|
41
42
|
"prepare": "husky",
|
|
42
|
-
"test": "bun test
|
|
43
|
+
"test": "bun test tests/",
|
|
43
44
|
"typecheck": "tsc --noEmit"
|
|
44
45
|
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"semver": "^7.8.5"
|
|
48
|
+
},
|
|
45
49
|
"devDependencies": {
|
|
46
50
|
"@commitlint/cli": "^21.0.1",
|
|
47
51
|
"@commitlint/config-conventional": "^21.0.1",
|
|
48
52
|
"@commitlint/types": "^21.0.1",
|
|
49
53
|
"@eslint/js": "^10.0.1",
|
|
50
54
|
"@types/bun": "^1.3.14",
|
|
55
|
+
"@types/semver": "^7.7.1",
|
|
51
56
|
"annal": "^0.0.2",
|
|
52
57
|
"eslint": "^10.4.0",
|
|
53
58
|
"husky": "^9.1.7",
|
|
54
|
-
"prettier": "^3.8.3",
|
|
55
59
|
"typescript": "^6.0.3",
|
|
56
60
|
"typescript-eslint": "^8.59.3"
|
|
57
61
|
},
|
|
58
62
|
"peerDependencies": {
|
|
59
|
-
"prettier": ">=3.
|
|
63
|
+
"prettier": ">=3.9.0 <4"
|
|
60
64
|
},
|
|
61
65
|
"devEngines": {
|
|
62
66
|
"runtime": {
|
package/dist/order-package.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `package.json` 顶层字段的排列顺序。
|
|
3
|
-
* 参考社区标准 sort-package-json 的字段列表:https://github.com/keithamus/sort-package-json/blob/main/index.js#L433-L564
|
|
4
|
-
*/
|
|
5
|
-
export declare const PACKAGE_JSON_TOP_LEVEL_ORDER: readonly string[];
|
|
6
|
-
/**
|
|
7
|
-
* 值为 `{ name: range }` 映射的依赖表字段。
|
|
8
|
-
* 始终按字母序排列,不受用户选项影响——npm install 每次都会按字母序重写。
|
|
9
|
-
*/
|
|
10
|
-
export declare const DEPENDENCY_FIELDS: readonly string[];
|
package/dist/utils.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 按 `separator` 分割字符串,忽略嵌套在 `{}` / `()` / `[]` 中的分隔符。
|
|
3
|
-
* 仅在顶层(深度为 0 的位置)分隔。返回结果会 trim 并剔除空段。
|
|
4
|
-
*
|
|
5
|
-
* 不使用 `String.prototype.split` 直接分隔,因为 import / export 的花括号内可能
|
|
6
|
-
* 出现形如 `{ a, b as c }`,其中嵌套结构内的逗号不应被当作分隔符。
|
|
7
|
-
*/
|
|
8
|
-
export declare function splitTopLevel(input: string, separator: string): string[];
|