prettier-plugin-sort 1.0.0 → 1.1.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/dist/options.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type ParserOptions, type SupportOptions } from 'prettier';
1
+ import { type ParserOptions } from 'prettier';
2
2
  /** import 声明分组。 */
3
3
  export type ImportGroup = 'builtin' | 'external' | 'internal' | 'parent' | 'sibling' | 'index';
4
4
  /** type import 的声明形式与成员顺序。 */
@@ -12,12 +12,76 @@ export interface SortOptions {
12
12
  esmImportMerge?: boolean;
13
13
  esmExportSpecifierSort?: boolean;
14
14
  packageSort?: boolean;
15
+ tsconfigSort?: boolean;
16
+ tsconfigSeparation?: boolean;
15
17
  }
18
+ export type ResolvedEsmOptions = Required<Pick<SortOptions, 'esmExportSpecifierSort' | 'esmImportGroups' | 'esmImportMerge' | 'esmImportSeparation' | 'esmImportSort' | 'esmImportTypeStyle'>>;
19
+ export type ResolvedTsconfigOptions = Required<Pick<SortOptions, 'tsconfigSeparation' | 'tsconfigSort'>>;
16
20
  type SortPluginParserOptions = ParserOptions & SortOptions;
17
- /**
18
- * Prettier 选项中提取插件配置,缺失或非法的参数会回退为默认值。
19
- */
20
- export declare function resolveSortOptions(prettierOptions: SortPluginParserOptions): Required<SortOptions>;
21
- /** Prettier 插件选项。 */
22
- export declare const options: SupportOptions;
21
+ export declare function resolveEsmOptions(prettierOptions: SortPluginParserOptions): ResolvedEsmOptions;
22
+ export declare function isPackageSortEnabled(prettierOptions: SortPluginParserOptions): boolean;
23
+ export declare function resolveTsconfigOptions(prettierOptions: SortPluginParserOptions): ResolvedTsconfigOptions;
24
+ export declare const options: {
25
+ esmImportSort: {
26
+ type: "boolean";
27
+ default: boolean;
28
+ category: string;
29
+ description: string;
30
+ };
31
+ esmImportGroups: {
32
+ type: "string";
33
+ array: true;
34
+ default: {
35
+ value: ImportGroup[];
36
+ }[];
37
+ category: string;
38
+ description: string;
39
+ };
40
+ esmImportSeparation: {
41
+ type: "boolean";
42
+ default: boolean;
43
+ category: string;
44
+ description: string;
45
+ };
46
+ esmImportTypeStyle: {
47
+ type: "choice";
48
+ default: TypeImportStyle;
49
+ category: string;
50
+ description: string;
51
+ choices: {
52
+ value: string;
53
+ description: string;
54
+ }[];
55
+ };
56
+ esmImportMerge: {
57
+ type: "boolean";
58
+ default: boolean;
59
+ category: string;
60
+ description: string;
61
+ };
62
+ esmExportSpecifierSort: {
63
+ type: "boolean";
64
+ default: boolean;
65
+ category: string;
66
+ description: string;
67
+ };
68
+ packageSort: {
69
+ type: "boolean";
70
+ default: boolean;
71
+ category: string;
72
+ description: string;
73
+ };
74
+ tsconfigSort: {
75
+ type: "boolean";
76
+ default: boolean;
77
+ category: string;
78
+ description: string;
79
+ };
80
+ tsconfigSeparation: {
81
+ type: "boolean";
82
+ default: boolean;
83
+ category: string;
84
+ description: string;
85
+ };
86
+ };
23
87
  export {};
@@ -1,4 +1,4 @@
1
- import { type SourceTextRange } from './utils/source-text';
1
+ import { type SourceTextRange } from '#/utils/source-text';
2
2
  /**
3
3
  * 已注册 Prettier parser 的源码偏移字段。
4
4
  *
@@ -22,6 +22,7 @@ interface ParserAstLocation extends Record<string, unknown> {
22
22
  * - `declaration`、`specifiers`、`source`、`imported`、`local`、`exported` 来自 ESTree ES2015 module 节点
23
23
  * - `program`、`comments` 来自 Babel `File` 包装节点及 Prettier 的顶层注释约定
24
24
  * - `importKind`、`phase` 来自 Babel/typescript-estree 的 import 扩展
25
+ * - `argument`、`elements`、`key`、`node`、`operator`、`properties` 来自 Babel JSON AST
25
26
  *
26
27
  * 不同 parser 的字段结构存在差异,因此属性均为可选,并在使用处收窄类型。
27
28
  *
@@ -36,9 +37,15 @@ export interface ParserAstNode extends ParserAstLocation {
36
37
  readonly type: string;
37
38
  readonly name?: unknown;
38
39
  readonly value?: unknown;
40
+ readonly argument?: ParserAstNode;
39
41
  readonly body?: ParserAstNode[];
40
42
  readonly comments?: ParserAstComment[];
43
+ readonly elements?: (ParserAstNode | null)[];
44
+ readonly key?: ParserAstNode;
45
+ readonly node?: ParserAstNode;
46
+ readonly operator?: unknown;
41
47
  readonly program?: ParserAstNode;
48
+ readonly properties?: ParserAstNode[];
42
49
  readonly declaration?: ParserAstNode | null;
43
50
  readonly specifiers?: ParserAstNode[];
44
51
  readonly source?: ParserAstNode | null;
@@ -62,11 +69,12 @@ export interface ParserAstCommentWithTextRange {
62
69
  readonly comment: ParserAstComment;
63
70
  readonly textRange: SourceTextRange;
64
71
  }
65
- export declare function getAstNodeTextRange(node: ParserAstNode | ParserAstComment | undefined | null): SourceTextRange | null;
72
+ export declare function isParserAstNode(value: unknown): value is ParserAstNode;
73
+ export declare function getAstNodeTextRange(node?: ParserAstNode | ParserAstComment | null): SourceTextRange | null;
66
74
  export declare function getSortedAstCommentsWithTextRanges(comments: readonly ParserAstComment[]): ParserAstCommentWithTextRange[];
67
75
  /** 使用二分查找定位指定源码位置之后的第一条注释。 */
68
76
  export declare function findCommentIndexAtOrAfter(sortedComments: readonly ParserAstCommentWithTextRange[], sourceIndex: number): number;
69
- export declare function getAstNodeName(node: ParserAstNode | undefined | null): string | null;
77
+ export declare function getAstNodeName(node?: ParserAstNode | null): string | null;
70
78
  export declare function getAstCommentText(comment: ParserAstComment): string | null;
71
79
  export declare function getProgramStatements(parserAst: ParserAstNode): readonly ParserAstNode[];
72
80
  export declare function getProgramComments(parserAst: ParserAstNode): readonly ParserAstComment[];
@@ -1,5 +1,5 @@
1
- import { type ParserAstCommentWithTextRange, type ParserAstNode } from './parser-ast';
2
- import { type SourceTextEdit } from './utils/source-text';
1
+ import { type ParserAstCommentWithTextRange, type ParserAstNode } from '#/parser-ast';
2
+ import { type SourceTextEdit } from '#/utils/source-text';
3
3
  /**
4
4
  * 为无注释的 `export { ... }` 生成 specifier 排序编辑。
5
5
  *
@@ -1,8 +1,8 @@
1
- import { type SortOptions } from './options';
2
- import { type ParserAstCommentWithTextRange, type ParserAstNode } from './parser-ast';
3
- import { type SourceTextEdit } from './utils/source-text';
1
+ import { type ResolvedEsmOptions } from '#/options';
2
+ import { type ParserAstCommentWithTextRange, type ParserAstNode } from '#/parser-ast';
3
+ import { type SourceTextEdit } from '#/utils/source-text';
4
4
  /**
5
5
  * 将顶层 import 划分为可独立排序的片段,并生成基于原文范围的编辑。
6
6
  * 被忽略的声明、独立注释和位置敏感指令不会进入编辑范围。
7
7
  */
8
- export declare function buildImportSortingEdits(sourceText: string, programStatements: readonly ParserAstNode[], sortedComments: readonly ParserAstCommentWithTextRange[], sortOptions: Required<SortOptions>, isPrettierFilePragmaPresent: boolean): SourceTextEdit[];
8
+ export declare function buildImportSortingEdits(sourceText: string, programStatements: readonly ParserAstNode[], sortedComments: readonly ParserAstCommentWithTextRange[], sortOptions: ResolvedEsmOptions, pathPatterns: readonly string[], isPrettierFilePragmaPresent: boolean): SourceTextEdit[];
@@ -0,0 +1,6 @@
1
+ import { type Parser, type ParserOptions } from 'prettier';
2
+ /**
3
+ * 只处理 tsconfig.json 和 tsconfig.*.json。
4
+ * AST、字段或注释归属无法安全处理时返回原文。
5
+ */
6
+ export declare function preprocessTsconfig(sourceText: string, prettierOptions: ParserOptions, parser: Parser): Promise<string>;
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * 基于 `sort-package-json` 4.0.0 默认规则的 `package.json` 顶层字段顺序。
3
+ * `$schema` 按 JSON Schema 惯例补充在最前。
3
4
  *
4
5
  * @see {@link https://github.com/keithamus/sort-package-json/blob/v4.0.0/defaultRules.md | sort-package-json 4.0.0 默认规则}
5
6
  */
@@ -0,0 +1,16 @@
1
+ /**
2
+ * 顶层只固定广泛采用的元信息、继承声明和输入文件字段。
3
+ * 其余字段保持原有相对顺序,不从示例推导完整规则。
4
+ *
5
+ * @see {@link https://www.typescriptlang.org/docs/handbook/tsconfig-json#tsconfig-bases | TypeScript TSConfig Bases}
6
+ * @see {@link https://www.typescriptlang.org/docs/handbook/tsconfig-json#examples | TypeScript TSConfig Examples}
7
+ */
8
+ export declare const TSCONFIG_ROOT_FIRST_FIELDS: readonly ["$schema", "extends"];
9
+ export declare const TSCONFIG_ROOT_LAST_FIELDS: readonly ["files", "include", "exclude"];
10
+ /**
11
+ * TypeScript 5.8.3 `tsc --init` 使用的 compiler option 分类和声明顺序。
12
+ * 默认模板隐藏的分类仍按生成器遍历 `optionDeclarations` 时的顺序接在后面。
13
+ *
14
+ * @see {@link https://github.com/microsoft/TypeScript/blob/v5.8.3/src/compiler/commandLineParser.ts | TypeScript 5.8.3 tsconfig 生成器}
15
+ */
16
+ export declare const COMPILER_OPTION_FIELD_GROUPS: readonly [readonly ["incremental", "composite", "tsBuildInfoFile", "disableSourceOfProjectReferenceRedirect", "disableSolutionSearching", "disableReferencedProjectLoad"], readonly ["target", "lib", "jsx", "libReplacement", "experimentalDecorators", "emitDecoratorMetadata", "jsxFactory", "jsxFragmentFactory", "jsxImportSource", "reactNamespace", "noLib", "useDefineForClassFields", "moduleDetection"], readonly ["module", "rootDir", "moduleResolution", "baseUrl", "paths", "rootDirs", "typeRoots", "types", "allowUmdGlobalAccess", "moduleSuffixes", "allowImportingTsExtensions", "rewriteRelativeImportExtensions", "resolvePackageJsonExports", "resolvePackageJsonImports", "customConditions", "noUncheckedSideEffectImports", "resolveJsonModule", "allowArbitraryExtensions", "noResolve"], readonly ["allowJs", "checkJs", "maxNodeModuleJsDepth"], readonly ["declaration", "declarationMap", "emitDeclarationOnly", "sourceMap", "inlineSourceMap", "noEmit", "outFile", "outDir", "removeComments", "importHelpers", "downlevelIteration", "sourceRoot", "mapRoot", "inlineSources", "emitBOM", "newLine", "stripInternal", "noEmitHelpers", "noEmitOnError", "preserveConstEnums", "declarationDir"], readonly ["isolatedModules", "verbatimModuleSyntax", "isolatedDeclarations", "erasableSyntaxOnly", "allowSyntheticDefaultImports", "esModuleInterop", "preserveSymlinks", "forceConsistentCasingInFileNames"], readonly ["strict", "noImplicitAny", "strictNullChecks", "strictFunctionTypes", "strictBindCallApply", "strictPropertyInitialization", "strictBuiltinIteratorReturn", "noImplicitThis", "useUnknownInCatchVariables", "alwaysStrict", "noUnusedLocals", "noUnusedParameters", "exactOptionalPropertyTypes", "noImplicitReturns", "noFallthroughCasesInSwitch", "noUncheckedIndexedAccess", "noImplicitOverride", "noPropertyAccessFromIndexSignature", "allowUnusedLabels", "allowUnreachableCode"], readonly ["skipDefaultLibCheck", "skipLibCheck"], readonly ["preserveWatchOutput", "pretty", "noErrorTruncation"], readonly ["listFiles", "explainFiles", "listEmittedFiles", "traceResolution", "diagnostics", "extendedDiagnostics", "generateCpuProfile", "generateTrace", "noCheck"], readonly ["assumeChangesOnlyAffectDirectDependencies"], readonly ["importsNotUsedAsValues", "out", "charset", "noImplicitUseStrict", "suppressExcessPropertyErrors", "suppressImplicitAnyIndexErrors", "noStrictGenericChecks", "preserveValueImports", "keyofStringsOnly"], readonly ["disableSizeLimit", "plugins"]];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "prettier-plugin-sort",
3
- "version": "1.0.0",
4
- "description": "A Prettier plugin for sorting ES module imports, export specifiers, and package.json keys.",
3
+ "version": "1.1.0",
4
+ "description": "A Prettier plugin for sorting ES module imports, export specifiers, and fields in package.json and tsconfig.json.",
5
5
  "keywords": [
6
6
  "imports",
7
7
  "order",
@@ -10,7 +10,8 @@
10
10
  "plugin",
11
11
  "prettier",
12
12
  "prettier-plugin",
13
- "sort"
13
+ "sort",
14
+ "tsconfig"
14
15
  ],
15
16
  "homepage": "https://github.com/xueelf/prettier-plugin-sort",
16
17
  "bugs": {
@@ -23,27 +24,32 @@
23
24
  "license": "MIT",
24
25
  "author": "Yuki <admin@yuki.sh>",
25
26
  "type": "module",
27
+ "imports": {
28
+ "#/*": {
29
+ "types": "./dist/*.d.ts"
30
+ }
31
+ },
26
32
  "exports": {
27
33
  ".": {
28
34
  "types": "./dist/index.d.ts",
29
35
  "default": "./dist/index.js"
30
36
  }
31
37
  },
32
- "main": "./dist/index.js",
33
- "types": "./dist/index.d.ts",
34
38
  "files": [
35
39
  "dist"
36
40
  ],
37
41
  "scripts": {
38
42
  "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",
43
+ "format": "prettier \"**/*.{json,md,ts,yaml,yml}\" --write --experimental-cli",
44
+ "format:check": "prettier \"**/*.{json,md,ts,yaml,yml}\" --check --experimental-cli",
45
+ "lint": "eslint --cache",
46
+ "lint:fix": "eslint --fix --cache",
42
47
  "prepare": "husky",
43
- "test": "bun test tests/",
48
+ "test": "bun test",
44
49
  "typecheck": "tsc --noEmit"
45
50
  },
46
51
  "dependencies": {
52
+ "get-tsconfig": "^4.14.0",
47
53
  "semver": "^7.8.5"
48
54
  },
49
55
  "devDependencies": {
@@ -53,7 +59,7 @@
53
59
  "@eslint/js": "^10.0.1",
54
60
  "@types/bun": "^1.3.14",
55
61
  "@types/semver": "^7.7.1",
56
- "annal": "^0.0.2",
62
+ "annal": "^0.2.0",
57
63
  "eslint": "^10.4.0",
58
64
  "husky": "^9.1.7",
59
65
  "typescript": "^6.0.3",