prettier-plugin-sort 1.0.1 → 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/README.md +165 -115
- package/README.zh.md +167 -117
- package/dist/index.d.ts +2 -2
- package/dist/index.js +480 -96
- package/dist/options.d.ts +71 -7
- package/dist/parser-ast.d.ts +11 -3
- package/dist/sort-exports.d.ts +2 -2
- package/dist/sort-imports.d.ts +4 -4
- package/dist/sort-tsconfig.d.ts +6 -0
- package/dist/utils/package-rules.d.ts +1 -0
- package/dist/utils/tsconfig-rules.d.ts +16 -0
- package/package.json +11 -4
package/dist/index.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
1
9
|
// src/index.ts
|
|
2
10
|
import acornPlugin from "prettier/plugins/acorn";
|
|
3
11
|
import babelPlugin from "prettier/plugins/babel";
|
|
@@ -8,12 +16,21 @@ import typescriptPlugin from "prettier/plugins/typescript";
|
|
|
8
16
|
// src/options.ts
|
|
9
17
|
var DEFAULT_SORT_OPTIONS = {
|
|
10
18
|
esmImportSort: true,
|
|
11
|
-
esmImportGroups: [
|
|
19
|
+
esmImportGroups: [
|
|
20
|
+
"builtin",
|
|
21
|
+
"external",
|
|
22
|
+
"internal",
|
|
23
|
+
"parent",
|
|
24
|
+
"sibling",
|
|
25
|
+
"index"
|
|
26
|
+
],
|
|
12
27
|
esmImportSeparation: true,
|
|
13
28
|
esmImportTypeStyle: "separate",
|
|
14
29
|
esmImportMerge: true,
|
|
15
30
|
esmExportSpecifierSort: true,
|
|
16
|
-
packageSort: true
|
|
31
|
+
packageSort: true,
|
|
32
|
+
tsconfigSort: true,
|
|
33
|
+
tsconfigSeparation: true
|
|
17
34
|
};
|
|
18
35
|
var VALID_IMPORT_GROUPS = new Set([
|
|
19
36
|
"builtin",
|
|
@@ -38,7 +55,7 @@ function resolveBooleanSortOption(prettierOptions, optionName) {
|
|
|
38
55
|
}
|
|
39
56
|
return DEFAULT_SORT_OPTIONS[optionName];
|
|
40
57
|
}
|
|
41
|
-
function
|
|
58
|
+
function resolveEsmOptions(prettierOptions) {
|
|
42
59
|
const configuredImportGroups = Array.isArray(prettierOptions.esmImportGroups) ? [...new Set(prettierOptions.esmImportGroups.filter(isValidImportGroup))] : [];
|
|
43
60
|
const remainingDefaultImportGroups = DEFAULT_SORT_OPTIONS.esmImportGroups.filter((importGroup) => !configuredImportGroups.includes(importGroup));
|
|
44
61
|
const resolvedImportGroups = configuredImportGroups.length > 0 ? [...configuredImportGroups, ...remainingDefaultImportGroups] : [...DEFAULT_SORT_OPTIONS.esmImportGroups];
|
|
@@ -49,8 +66,16 @@ function resolveSortOptions(prettierOptions) {
|
|
|
49
66
|
esmImportSeparation: resolveBooleanSortOption(prettierOptions, "esmImportSeparation"),
|
|
50
67
|
esmImportTypeStyle: resolvedTypeImportStyle,
|
|
51
68
|
esmImportMerge: resolveBooleanSortOption(prettierOptions, "esmImportMerge"),
|
|
52
|
-
esmExportSpecifierSort: resolveBooleanSortOption(prettierOptions, "esmExportSpecifierSort")
|
|
53
|
-
|
|
69
|
+
esmExportSpecifierSort: resolveBooleanSortOption(prettierOptions, "esmExportSpecifierSort")
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function isPackageSortEnabled(prettierOptions) {
|
|
73
|
+
return resolveBooleanSortOption(prettierOptions, "packageSort");
|
|
74
|
+
}
|
|
75
|
+
function resolveTsconfigOptions(prettierOptions) {
|
|
76
|
+
return {
|
|
77
|
+
tsconfigSort: resolveBooleanSortOption(prettierOptions, "tsconfigSort"),
|
|
78
|
+
tsconfigSeparation: resolveBooleanSortOption(prettierOptions, "tsconfigSeparation")
|
|
54
79
|
};
|
|
55
80
|
}
|
|
56
81
|
var options = {
|
|
@@ -113,7 +138,19 @@ var options = {
|
|
|
113
138
|
type: "boolean",
|
|
114
139
|
default: DEFAULT_SORT_OPTIONS.packageSort,
|
|
115
140
|
category: "package.json",
|
|
116
|
-
description: "Sort
|
|
141
|
+
description: "Sort fields in package.json."
|
|
142
|
+
},
|
|
143
|
+
tsconfigSort: {
|
|
144
|
+
type: "boolean",
|
|
145
|
+
default: DEFAULT_SORT_OPTIONS.tsconfigSort,
|
|
146
|
+
category: "tsconfig.json",
|
|
147
|
+
description: "Sort fields in tsconfig.json."
|
|
148
|
+
},
|
|
149
|
+
tsconfigSeparation: {
|
|
150
|
+
type: "boolean",
|
|
151
|
+
default: DEFAULT_SORT_OPTIONS.tsconfigSeparation,
|
|
152
|
+
category: "tsconfig.json",
|
|
153
|
+
description: "Add blank lines between TypeScript 5.8 compilerOptions categories."
|
|
117
154
|
}
|
|
118
155
|
};
|
|
119
156
|
|
|
@@ -122,6 +159,9 @@ import findMinimumSemanticVersion from "semver/ranges/min-version.js";
|
|
|
122
159
|
import getValidSemanticVersionRange from "semver/ranges/valid.js";
|
|
123
160
|
|
|
124
161
|
// src/parser-ast.ts
|
|
162
|
+
function isParserAstNode(value) {
|
|
163
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
164
|
+
}
|
|
125
165
|
function getAstNodeTextRange(node) {
|
|
126
166
|
if (!node) {
|
|
127
167
|
return null;
|
|
@@ -479,7 +519,8 @@ class JsonSourceLiteral {
|
|
|
479
519
|
}
|
|
480
520
|
}
|
|
481
521
|
var SEQUENTIAL_SCRIPT_PATTERN = /(?<=^|[\s&;<>|(])(?:run-s|npm-run-all2? .*(?:--sequential|--serial|-s))(?=$|[\s&;<>|)])/;
|
|
482
|
-
|
|
522
|
+
var PACKAGE_JSON_FIELD_INDEXES = new Map(PACKAGE_JSON_FIELD_ORDER.map((fieldName, index) => [fieldName, index]));
|
|
523
|
+
function compareStringsCaseSensitive(leftText, rightText) {
|
|
483
524
|
if (leftText === rightText) {
|
|
484
525
|
return 0;
|
|
485
526
|
}
|
|
@@ -503,13 +544,10 @@ function getJsonString(jsonValue) {
|
|
|
503
544
|
function isJsonStringValue(jsonValue) {
|
|
504
545
|
return getJsonString(jsonValue) !== null;
|
|
505
546
|
}
|
|
506
|
-
function isParserJsonAstNode(value) {
|
|
507
|
-
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
508
|
-
}
|
|
509
547
|
function preserveJsonSourceLiterals(jsonValue, parserJsonNode, sourceText, fieldNameSourceTexts, jsonPath = []) {
|
|
510
548
|
if (parserJsonNode.type === "JsonRoot") {
|
|
511
549
|
const rootNode = parserJsonNode.node;
|
|
512
|
-
if (!
|
|
550
|
+
if (!isParserAstNode(rootNode)) {
|
|
513
551
|
return;
|
|
514
552
|
}
|
|
515
553
|
return preserveJsonSourceLiterals(jsonValue, rootNode, sourceText, fieldNameSourceTexts, jsonPath);
|
|
@@ -535,7 +573,7 @@ function preserveJsonSourceLiterals(jsonValue, parserJsonNode, sourceText, field
|
|
|
535
573
|
const propertyName = getAstNodeName(propertyNode.key);
|
|
536
574
|
const propertyNameRange = getAstNodeTextRange(propertyNode.key);
|
|
537
575
|
const propertyValueNode = propertyNode.value;
|
|
538
|
-
if (propertyNode.type !== "ObjectProperty" || propertyName === null || !propertyNameRange || propertyNames.has(propertyName) || !Object.hasOwn(jsonValue, propertyName) || !
|
|
576
|
+
if (propertyNode.type !== "ObjectProperty" || propertyName === null || !propertyNameRange || propertyNames.has(propertyName) || !Object.hasOwn(jsonValue, propertyName) || !isParserAstNode(propertyValueNode)) {
|
|
539
577
|
return;
|
|
540
578
|
}
|
|
541
579
|
propertyNames.add(propertyName);
|
|
@@ -617,7 +655,7 @@ function deduplicateStringValues(stringValues) {
|
|
|
617
655
|
}
|
|
618
656
|
return [...uniqueStringValues.values()];
|
|
619
657
|
}
|
|
620
|
-
function sortJsonObject(jsonObject, compareKeys =
|
|
658
|
+
function sortJsonObject(jsonObject, compareKeys = compareStringsCaseSensitive) {
|
|
621
659
|
return Object.fromEntries(Object.entries(jsonObject).sort(([leftKey], [rightKey]) => compareKeys(leftKey, rightKey)));
|
|
622
660
|
}
|
|
623
661
|
function sortJsonObjectByKeyOrder(jsonObject, keyOrder) {
|
|
@@ -636,7 +674,7 @@ function sortJsonObjectByKeyOrder(jsonObject, keyOrder) {
|
|
|
636
674
|
if (isRightKeyKnown) {
|
|
637
675
|
return 1;
|
|
638
676
|
}
|
|
639
|
-
return
|
|
677
|
+
return compareStringsCaseSensitive(leftKey, rightKey);
|
|
640
678
|
});
|
|
641
679
|
}
|
|
642
680
|
function sortJsonObjectRecursively(jsonObject, sortObject = sortJsonObject) {
|
|
@@ -662,7 +700,7 @@ function sortUniqueStringArrayValue(fieldValue) {
|
|
|
662
700
|
if (!isStringArray(fieldValue)) {
|
|
663
701
|
return fieldValue;
|
|
664
702
|
}
|
|
665
|
-
return deduplicateStringValues(fieldValue).sort((leftValue, rightValue) =>
|
|
703
|
+
return deduplicateStringValues(fieldValue).sort((leftValue, rightValue) => compareStringsCaseSensitive(getJsonString(leftValue), getJsonString(rightValue)));
|
|
666
704
|
}
|
|
667
705
|
function deduplicateStringArrayValue(fieldValue) {
|
|
668
706
|
return isStringArray(fieldValue) ? deduplicateStringValues(fieldValue) : fieldValue;
|
|
@@ -677,10 +715,9 @@ function sortPeopleArrayValue(fieldValue) {
|
|
|
677
715
|
return fieldValue.map((person) => sortPersonValue(person));
|
|
678
716
|
}
|
|
679
717
|
function sortPackageJsonFields(packageJson) {
|
|
680
|
-
const fieldOrderIndexes = new Map(PACKAGE_JSON_FIELD_ORDER.map((fieldName, index) => [fieldName, index]));
|
|
681
718
|
return sortJsonObject(packageJson, (leftFieldName, rightFieldName) => {
|
|
682
|
-
const leftIndex =
|
|
683
|
-
const rightIndex =
|
|
719
|
+
const leftIndex = PACKAGE_JSON_FIELD_INDEXES.get(leftFieldName);
|
|
720
|
+
const rightIndex = PACKAGE_JSON_FIELD_INDEXES.get(rightFieldName);
|
|
684
721
|
const isLeftFieldKnown = leftIndex !== undefined;
|
|
685
722
|
const isRightFieldKnown = rightIndex !== undefined;
|
|
686
723
|
if (isLeftFieldKnown && isRightFieldKnown) {
|
|
@@ -697,37 +734,16 @@ function sortPackageJsonFields(packageJson) {
|
|
|
697
734
|
if (isLeftFieldPrivate !== isRightFieldPrivate) {
|
|
698
735
|
return isLeftFieldPrivate ? 1 : -1;
|
|
699
736
|
}
|
|
700
|
-
return
|
|
737
|
+
return compareStringsCaseSensitive(leftFieldName, rightFieldName);
|
|
701
738
|
});
|
|
702
739
|
}
|
|
703
|
-
function
|
|
704
|
-
const packageManager = getJsonString(packageJson.packageManager);
|
|
705
|
-
if (packageManager !== null) {
|
|
706
|
-
return packageManager.startsWith("npm@");
|
|
707
|
-
}
|
|
708
|
-
const devEngines = packageJson.devEngines;
|
|
709
|
-
if (isJsonObject(devEngines)) {
|
|
710
|
-
const devPackageManager = devEngines.packageManager;
|
|
711
|
-
const devPackageManagerName = isJsonObject(devPackageManager) ? getJsonString(devPackageManager.name) : null;
|
|
712
|
-
if (devPackageManagerName !== null) {
|
|
713
|
-
return devPackageManagerName === "npm";
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
if (isJsonObject(packageJson.pnpm)) {
|
|
717
|
-
return false;
|
|
718
|
-
}
|
|
719
|
-
return true;
|
|
720
|
-
}
|
|
721
|
-
function sortDependencyObject(dependencyObject, packageJson) {
|
|
722
|
-
if (!isNpmDependencyOrderPreferred(packageJson)) {
|
|
723
|
-
return sortJsonObject(dependencyObject);
|
|
724
|
-
}
|
|
740
|
+
function sortDependencyObject(dependencyObject) {
|
|
725
741
|
return sortJsonObject(dependencyObject, (leftName, rightName) => leftName.localeCompare(rightName, "en"));
|
|
726
742
|
}
|
|
727
|
-
function sortDependencyValue(fieldValue
|
|
728
|
-
return sortJsonObjectValue(fieldValue,
|
|
743
|
+
function sortDependencyValue(fieldValue) {
|
|
744
|
+
return sortJsonObjectValue(fieldValue, sortDependencyObject);
|
|
729
745
|
}
|
|
730
|
-
function sortWorkspacesValue(fieldValue
|
|
746
|
+
function sortWorkspacesValue(fieldValue) {
|
|
731
747
|
if (!isJsonObject(fieldValue)) {
|
|
732
748
|
return fieldValue;
|
|
733
749
|
}
|
|
@@ -740,7 +756,7 @@ function sortWorkspacesValue(fieldValue, packageJson) {
|
|
|
740
756
|
sortedWorkspaces.packages = sortUniqueStringArrayValue(workspacePackages);
|
|
741
757
|
}
|
|
742
758
|
if (isJsonObject(sortedWorkspaces.catalog)) {
|
|
743
|
-
sortedWorkspaces.catalog = sortDependencyObject(sortedWorkspaces.catalog
|
|
759
|
+
sortedWorkspaces.catalog = sortDependencyObject(sortedWorkspaces.catalog);
|
|
744
760
|
}
|
|
745
761
|
return sortedWorkspaces;
|
|
746
762
|
}
|
|
@@ -770,7 +786,7 @@ function sortEslintConfigValue(fieldValue) {
|
|
|
770
786
|
return sortJsonObjectValue(fieldValue, sortEslintConfigObject);
|
|
771
787
|
}
|
|
772
788
|
function sortPrettierConfigObject(prettierConfig) {
|
|
773
|
-
const keyOrder = Object.keys(prettierConfig).filter((key) => key !== "overrides").sort(
|
|
789
|
+
const keyOrder = Object.keys(prettierConfig).filter((key) => key !== "overrides").sort(compareStringsCaseSensitive);
|
|
774
790
|
if (Object.hasOwn(prettierConfig, "overrides")) {
|
|
775
791
|
keyOrder.push("overrides");
|
|
776
792
|
}
|
|
@@ -849,13 +865,13 @@ function sortScriptNames(scriptNames, namespacePrefix = "") {
|
|
|
849
865
|
groupedScriptNames.push(scriptName);
|
|
850
866
|
scriptGroups.set(scriptGroupName, groupedScriptNames);
|
|
851
867
|
}
|
|
852
|
-
return [...scriptGroups.keys()].sort(
|
|
868
|
+
return [...scriptGroups.keys()].sort(compareStringsCaseSensitive).flatMap((scriptGroupName) => {
|
|
853
869
|
const groupedScriptNames = scriptGroups.get(scriptGroupName);
|
|
854
870
|
const isNestedGroup = groupedScriptNames.length > 1 && groupedScriptNames.some((scriptName) => scriptName !== scriptGroupName && scriptName.startsWith(`${scriptGroupName}:`));
|
|
855
871
|
if (!isNestedGroup) {
|
|
856
|
-
return groupedScriptNames.sort(
|
|
872
|
+
return groupedScriptNames.sort(compareStringsCaseSensitive);
|
|
857
873
|
}
|
|
858
|
-
const directScriptNames = groupedScriptNames.filter((scriptName) => scriptName === scriptGroupName || !scriptName.startsWith(`${scriptGroupName}:`)).sort(
|
|
874
|
+
const directScriptNames = groupedScriptNames.filter((scriptName) => scriptName === scriptGroupName || !scriptName.startsWith(`${scriptGroupName}:`)).sort(compareStringsCaseSensitive);
|
|
859
875
|
const nestedScriptNames = groupedScriptNames.filter((scriptName) => scriptName.startsWith(`${scriptGroupName}:`));
|
|
860
876
|
return [
|
|
861
877
|
...directScriptNames,
|
|
@@ -975,7 +991,7 @@ function comparePnpmOverrideSelectors(leftSelector, rightSelector) {
|
|
|
975
991
|
const leftVersion = getMinimumSemanticVersion(leftPackage.versionRange);
|
|
976
992
|
const rightVersion = getMinimumSemanticVersion(rightPackage.versionRange);
|
|
977
993
|
if (!leftVersion && !rightVersion) {
|
|
978
|
-
return
|
|
994
|
+
return compareStringsCaseSensitive(leftPackage.versionRange, rightPackage.versionRange);
|
|
979
995
|
}
|
|
980
996
|
if (!leftVersion) {
|
|
981
997
|
return 1;
|
|
@@ -987,7 +1003,7 @@ function comparePnpmOverrideSelectors(leftSelector, rightSelector) {
|
|
|
987
1003
|
}
|
|
988
1004
|
function sortDependencyMetadataValue(fieldValue) {
|
|
989
1005
|
return sortJsonObjectValue(fieldValue, (dependencyMetadata) => {
|
|
990
|
-
const compareDependencyIdentifiers = (leftIdentifier, rightIdentifier) =>
|
|
1006
|
+
const compareDependencyIdentifiers = (leftIdentifier, rightIdentifier) => compareStringsCaseSensitive(getDependencyName(leftIdentifier), getDependencyName(rightIdentifier));
|
|
991
1007
|
return sortJsonObjectRecursively(dependencyMetadata, (jsonObject) => sortJsonObject(jsonObject, compareDependencyIdentifiers));
|
|
992
1008
|
});
|
|
993
1009
|
}
|
|
@@ -1103,7 +1119,7 @@ function isPackageJsonFile(filePath) {
|
|
|
1103
1119
|
return filePath?.split(/[\\/]/).at(-1) === "package.json";
|
|
1104
1120
|
}
|
|
1105
1121
|
async function preprocessPackageJson(sourceText, prettierOptions, parser) {
|
|
1106
|
-
if (!isPackageJsonFile(prettierOptions.filepath) || !
|
|
1122
|
+
if (!isPackageJsonFile(prettierOptions.filepath) || !isPackageSortEnabled(prettierOptions)) {
|
|
1107
1123
|
return sourceText;
|
|
1108
1124
|
}
|
|
1109
1125
|
try {
|
|
@@ -1112,7 +1128,7 @@ async function preprocessPackageJson(sourceText, prettierOptions, parser) {
|
|
|
1112
1128
|
return sourceText;
|
|
1113
1129
|
}
|
|
1114
1130
|
const parserJsonAst = await parser.parse(sourceText, prettierOptions);
|
|
1115
|
-
if (!
|
|
1131
|
+
if (!isParserAstNode(parserJsonAst)) {
|
|
1116
1132
|
return sourceText;
|
|
1117
1133
|
}
|
|
1118
1134
|
const fieldNameSourceTexts = new Map;
|
|
@@ -1126,6 +1142,367 @@ async function preprocessPackageJson(sourceText, prettierOptions, parser) {
|
|
|
1126
1142
|
}
|
|
1127
1143
|
}
|
|
1128
1144
|
|
|
1145
|
+
// src/utils/source-text.ts
|
|
1146
|
+
function applySourceTextEdits(sourceText, sourceTextEdits) {
|
|
1147
|
+
if (sourceTextEdits.length === 0) {
|
|
1148
|
+
return sourceText;
|
|
1149
|
+
}
|
|
1150
|
+
let previousEditEnd = 0;
|
|
1151
|
+
const ascendingEdits = [...sourceTextEdits].sort((left, right) => left.start - right.start || left.end - right.end);
|
|
1152
|
+
for (const sourceTextEdit of ascendingEdits) {
|
|
1153
|
+
const { start, end } = sourceTextEdit;
|
|
1154
|
+
const isSourceTextEditValid = [start, end].every(Number.isSafeInteger) && start >= previousEditEnd && end >= start && end <= sourceText.length;
|
|
1155
|
+
if (!isSourceTextEditValid) {
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
previousEditEnd = end;
|
|
1159
|
+
}
|
|
1160
|
+
let editedText = sourceText;
|
|
1161
|
+
for (const sourceTextEdit of ascendingEdits.reverse()) {
|
|
1162
|
+
editedText = editedText.slice(0, sourceTextEdit.start) + sourceTextEdit.replacementText + editedText.slice(sourceTextEdit.end);
|
|
1163
|
+
}
|
|
1164
|
+
return editedText;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// src/utils/tsconfig-rules.ts
|
|
1168
|
+
var TSCONFIG_ROOT_FIRST_FIELDS = ["$schema", "extends"];
|
|
1169
|
+
var TSCONFIG_ROOT_LAST_FIELDS = [
|
|
1170
|
+
"files",
|
|
1171
|
+
"include",
|
|
1172
|
+
"exclude"
|
|
1173
|
+
];
|
|
1174
|
+
var COMPILER_OPTION_FIELD_GROUPS = [
|
|
1175
|
+
[
|
|
1176
|
+
"incremental",
|
|
1177
|
+
"composite",
|
|
1178
|
+
"tsBuildInfoFile",
|
|
1179
|
+
"disableSourceOfProjectReferenceRedirect",
|
|
1180
|
+
"disableSolutionSearching",
|
|
1181
|
+
"disableReferencedProjectLoad"
|
|
1182
|
+
],
|
|
1183
|
+
[
|
|
1184
|
+
"target",
|
|
1185
|
+
"lib",
|
|
1186
|
+
"jsx",
|
|
1187
|
+
"libReplacement",
|
|
1188
|
+
"experimentalDecorators",
|
|
1189
|
+
"emitDecoratorMetadata",
|
|
1190
|
+
"jsxFactory",
|
|
1191
|
+
"jsxFragmentFactory",
|
|
1192
|
+
"jsxImportSource",
|
|
1193
|
+
"reactNamespace",
|
|
1194
|
+
"noLib",
|
|
1195
|
+
"useDefineForClassFields",
|
|
1196
|
+
"moduleDetection"
|
|
1197
|
+
],
|
|
1198
|
+
[
|
|
1199
|
+
"module",
|
|
1200
|
+
"rootDir",
|
|
1201
|
+
"moduleResolution",
|
|
1202
|
+
"baseUrl",
|
|
1203
|
+
"paths",
|
|
1204
|
+
"rootDirs",
|
|
1205
|
+
"typeRoots",
|
|
1206
|
+
"types",
|
|
1207
|
+
"allowUmdGlobalAccess",
|
|
1208
|
+
"moduleSuffixes",
|
|
1209
|
+
"allowImportingTsExtensions",
|
|
1210
|
+
"rewriteRelativeImportExtensions",
|
|
1211
|
+
"resolvePackageJsonExports",
|
|
1212
|
+
"resolvePackageJsonImports",
|
|
1213
|
+
"customConditions",
|
|
1214
|
+
"noUncheckedSideEffectImports",
|
|
1215
|
+
"resolveJsonModule",
|
|
1216
|
+
"allowArbitraryExtensions",
|
|
1217
|
+
"noResolve"
|
|
1218
|
+
],
|
|
1219
|
+
["allowJs", "checkJs", "maxNodeModuleJsDepth"],
|
|
1220
|
+
[
|
|
1221
|
+
"declaration",
|
|
1222
|
+
"declarationMap",
|
|
1223
|
+
"emitDeclarationOnly",
|
|
1224
|
+
"sourceMap",
|
|
1225
|
+
"inlineSourceMap",
|
|
1226
|
+
"noEmit",
|
|
1227
|
+
"outFile",
|
|
1228
|
+
"outDir",
|
|
1229
|
+
"removeComments",
|
|
1230
|
+
"importHelpers",
|
|
1231
|
+
"downlevelIteration",
|
|
1232
|
+
"sourceRoot",
|
|
1233
|
+
"mapRoot",
|
|
1234
|
+
"inlineSources",
|
|
1235
|
+
"emitBOM",
|
|
1236
|
+
"newLine",
|
|
1237
|
+
"stripInternal",
|
|
1238
|
+
"noEmitHelpers",
|
|
1239
|
+
"noEmitOnError",
|
|
1240
|
+
"preserveConstEnums",
|
|
1241
|
+
"declarationDir"
|
|
1242
|
+
],
|
|
1243
|
+
[
|
|
1244
|
+
"isolatedModules",
|
|
1245
|
+
"verbatimModuleSyntax",
|
|
1246
|
+
"isolatedDeclarations",
|
|
1247
|
+
"erasableSyntaxOnly",
|
|
1248
|
+
"allowSyntheticDefaultImports",
|
|
1249
|
+
"esModuleInterop",
|
|
1250
|
+
"preserveSymlinks",
|
|
1251
|
+
"forceConsistentCasingInFileNames"
|
|
1252
|
+
],
|
|
1253
|
+
[
|
|
1254
|
+
"strict",
|
|
1255
|
+
"noImplicitAny",
|
|
1256
|
+
"strictNullChecks",
|
|
1257
|
+
"strictFunctionTypes",
|
|
1258
|
+
"strictBindCallApply",
|
|
1259
|
+
"strictPropertyInitialization",
|
|
1260
|
+
"strictBuiltinIteratorReturn",
|
|
1261
|
+
"noImplicitThis",
|
|
1262
|
+
"useUnknownInCatchVariables",
|
|
1263
|
+
"alwaysStrict",
|
|
1264
|
+
"noUnusedLocals",
|
|
1265
|
+
"noUnusedParameters",
|
|
1266
|
+
"exactOptionalPropertyTypes",
|
|
1267
|
+
"noImplicitReturns",
|
|
1268
|
+
"noFallthroughCasesInSwitch",
|
|
1269
|
+
"noUncheckedIndexedAccess",
|
|
1270
|
+
"noImplicitOverride",
|
|
1271
|
+
"noPropertyAccessFromIndexSignature",
|
|
1272
|
+
"allowUnusedLabels",
|
|
1273
|
+
"allowUnreachableCode"
|
|
1274
|
+
],
|
|
1275
|
+
["skipDefaultLibCheck", "skipLibCheck"],
|
|
1276
|
+
["preserveWatchOutput", "pretty", "noErrorTruncation"],
|
|
1277
|
+
[
|
|
1278
|
+
"listFiles",
|
|
1279
|
+
"explainFiles",
|
|
1280
|
+
"listEmittedFiles",
|
|
1281
|
+
"traceResolution",
|
|
1282
|
+
"diagnostics",
|
|
1283
|
+
"extendedDiagnostics",
|
|
1284
|
+
"generateCpuProfile",
|
|
1285
|
+
"generateTrace",
|
|
1286
|
+
"noCheck"
|
|
1287
|
+
],
|
|
1288
|
+
["assumeChangesOnlyAffectDirectDependencies"],
|
|
1289
|
+
[
|
|
1290
|
+
"importsNotUsedAsValues",
|
|
1291
|
+
"out",
|
|
1292
|
+
"charset",
|
|
1293
|
+
"noImplicitUseStrict",
|
|
1294
|
+
"suppressExcessPropertyErrors",
|
|
1295
|
+
"suppressImplicitAnyIndexErrors",
|
|
1296
|
+
"noStrictGenericChecks",
|
|
1297
|
+
"preserveValueImports",
|
|
1298
|
+
"keyofStringsOnly"
|
|
1299
|
+
],
|
|
1300
|
+
["disableSizeLimit", "plugins"]
|
|
1301
|
+
];
|
|
1302
|
+
|
|
1303
|
+
// src/sort-tsconfig.ts
|
|
1304
|
+
function createFieldIndexes(fieldNames) {
|
|
1305
|
+
return new Map(fieldNames.map((fieldName, fieldIndex) => [fieldName, fieldIndex]));
|
|
1306
|
+
}
|
|
1307
|
+
var COMPILER_OPTION_FIELD_INDEXES = createFieldIndexes(COMPILER_OPTION_FIELD_GROUPS.flat());
|
|
1308
|
+
var COMPILER_OPTION_FIELD_GROUP_INDEXES = new Map(COMPILER_OPTION_FIELD_GROUPS.flatMap((fieldGroup, groupIndex) => fieldGroup.map((fieldName) => [fieldName, groupIndex])));
|
|
1309
|
+
var ROOT_FIRST_FIELD_INDEXES = createFieldIndexes(TSCONFIG_ROOT_FIRST_FIELDS);
|
|
1310
|
+
var ROOT_LAST_FIELD_INDEXES = createFieldIndexes(TSCONFIG_ROOT_LAST_FIELDS);
|
|
1311
|
+
function getTsconfigRootNode(parserAst) {
|
|
1312
|
+
if (parserAst.type === "ObjectExpression") {
|
|
1313
|
+
return parserAst;
|
|
1314
|
+
}
|
|
1315
|
+
return parserAst.type === "JsonRoot" && parserAst.node?.type === "ObjectExpression" ? parserAst.node : null;
|
|
1316
|
+
}
|
|
1317
|
+
function getCompilerOptionsNode(rootNode) {
|
|
1318
|
+
const propertyNodes = rootNode.properties;
|
|
1319
|
+
if (!Array.isArray(propertyNodes)) {
|
|
1320
|
+
return null;
|
|
1321
|
+
}
|
|
1322
|
+
const compilerOptions = propertyNodes.filter((propertyNode) => getAstNodeName(propertyNode.key) === "compilerOptions");
|
|
1323
|
+
if (compilerOptions.length !== 1) {
|
|
1324
|
+
return null;
|
|
1325
|
+
}
|
|
1326
|
+
const property = compilerOptions[0];
|
|
1327
|
+
const value = property?.value;
|
|
1328
|
+
return property?.type === "ObjectProperty" && isParserAstNode(value) && value.type === "ObjectExpression" ? { property, value } : null;
|
|
1329
|
+
}
|
|
1330
|
+
function hasDirectComments(objectNode, propertyNodes, sortedComments) {
|
|
1331
|
+
const objectRange = getAstNodeTextRange(objectNode);
|
|
1332
|
+
const propertyRanges = propertyNodes.map(getAstNodeTextRange);
|
|
1333
|
+
if (!objectRange || propertyRanges.some((propertyRange) => !propertyRange)) {
|
|
1334
|
+
return true;
|
|
1335
|
+
}
|
|
1336
|
+
return sortedComments.some(({ textRange: commentRange }) => {
|
|
1337
|
+
if (commentRange.start < objectRange.start || commentRange.end > objectRange.end) {
|
|
1338
|
+
return false;
|
|
1339
|
+
}
|
|
1340
|
+
return !propertyRanges.some((propertyRange) => propertyRange && propertyRange.start <= commentRange.start && propertyRange.end >= commentRange.end);
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
function sortRenderedProperties(renderedProperties, fieldIndexes, lastFieldIndexes) {
|
|
1344
|
+
return [...renderedProperties].sort((left, right) => {
|
|
1345
|
+
const leftFieldIndex = fieldIndexes.get(left.name);
|
|
1346
|
+
const rightFieldIndex = fieldIndexes.get(right.name);
|
|
1347
|
+
if (leftFieldIndex !== undefined && rightFieldIndex !== undefined) {
|
|
1348
|
+
return leftFieldIndex - rightFieldIndex;
|
|
1349
|
+
}
|
|
1350
|
+
if (leftFieldIndex !== undefined) {
|
|
1351
|
+
return -1;
|
|
1352
|
+
}
|
|
1353
|
+
if (rightFieldIndex !== undefined) {
|
|
1354
|
+
return 1;
|
|
1355
|
+
}
|
|
1356
|
+
const leftLastFieldIndex = lastFieldIndexes?.get(left.name);
|
|
1357
|
+
const rightLastFieldIndex = lastFieldIndexes?.get(right.name);
|
|
1358
|
+
if (leftLastFieldIndex !== undefined && rightLastFieldIndex !== undefined) {
|
|
1359
|
+
return leftLastFieldIndex - rightLastFieldIndex;
|
|
1360
|
+
}
|
|
1361
|
+
if (leftLastFieldIndex !== undefined) {
|
|
1362
|
+
return 1;
|
|
1363
|
+
}
|
|
1364
|
+
if (rightLastFieldIndex !== undefined) {
|
|
1365
|
+
return -1;
|
|
1366
|
+
}
|
|
1367
|
+
return left.originalIndex - right.originalIndex;
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
function getPropertySeparator(property, shouldSeparateCategories, previousProperty) {
|
|
1371
|
+
if (!previousProperty) {
|
|
1372
|
+
return "";
|
|
1373
|
+
}
|
|
1374
|
+
if (!shouldSeparateCategories) {
|
|
1375
|
+
return ",";
|
|
1376
|
+
}
|
|
1377
|
+
const previousGroupIndex = COMPILER_OPTION_FIELD_GROUP_INDEXES.get(previousProperty.name);
|
|
1378
|
+
const groupIndex = COMPILER_OPTION_FIELD_GROUP_INDEXES.get(property.name);
|
|
1379
|
+
return previousGroupIndex !== undefined && groupIndex !== undefined && previousGroupIndex !== groupIndex ? `,
|
|
1380
|
+
|
|
1381
|
+
` : ",";
|
|
1382
|
+
}
|
|
1383
|
+
function renderProperty(sourceText, propertyNode, originalIndex, replacementText) {
|
|
1384
|
+
const propertyName = getAstNodeName(propertyNode.key);
|
|
1385
|
+
const propertyRange = getAstNodeTextRange(propertyNode);
|
|
1386
|
+
const propertyValue = propertyNode.value;
|
|
1387
|
+
if (propertyNode.type !== "ObjectProperty" || propertyName === null || !propertyRange || !isParserAstNode(propertyValue)) {
|
|
1388
|
+
return null;
|
|
1389
|
+
}
|
|
1390
|
+
return {
|
|
1391
|
+
name: propertyName,
|
|
1392
|
+
originalIndex,
|
|
1393
|
+
sourceText: replacementText ?? sourceText.slice(propertyRange.start, propertyRange.end)
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
function replacePropertyValue(sourceText, propertyNode, valueNode, replacementText) {
|
|
1397
|
+
const propertyRange = getAstNodeTextRange(propertyNode);
|
|
1398
|
+
const valueRange = getAstNodeTextRange(valueNode);
|
|
1399
|
+
if (!propertyRange || !valueRange || valueRange.start < propertyRange.start || valueRange.end > propertyRange.end) {
|
|
1400
|
+
return null;
|
|
1401
|
+
}
|
|
1402
|
+
return sourceText.slice(propertyRange.start, valueRange.start) + replacementText + sourceText.slice(valueRange.end, propertyRange.end);
|
|
1403
|
+
}
|
|
1404
|
+
function renderObject(sourceText, objectNode, sortedComments, fieldIndexes, shouldSeparateCategories, propertyReplacements, lastFieldIndexes) {
|
|
1405
|
+
const objectRange = getAstNodeTextRange(objectNode);
|
|
1406
|
+
const propertyNodes = objectNode.properties;
|
|
1407
|
+
if (objectNode.type !== "ObjectExpression" || !objectRange || !Array.isArray(propertyNodes)) {
|
|
1408
|
+
return null;
|
|
1409
|
+
}
|
|
1410
|
+
const objectSource = sourceText.slice(objectRange.start, objectRange.end);
|
|
1411
|
+
if (propertyNodes.length === 0) {
|
|
1412
|
+
return { hasOrderChanged: false, sourceText: objectSource };
|
|
1413
|
+
}
|
|
1414
|
+
if (hasDirectComments(objectNode, propertyNodes, sortedComments)) {
|
|
1415
|
+
return null;
|
|
1416
|
+
}
|
|
1417
|
+
const propertyNames = new Set;
|
|
1418
|
+
const renderedProperties = [];
|
|
1419
|
+
for (const [propertyIndex, propertyNode] of propertyNodes.entries()) {
|
|
1420
|
+
const renderedProperty = renderProperty(sourceText, propertyNode, propertyIndex, propertyReplacements?.get(propertyNode));
|
|
1421
|
+
if (!renderedProperty || propertyNames.has(renderedProperty.name)) {
|
|
1422
|
+
return null;
|
|
1423
|
+
}
|
|
1424
|
+
propertyNames.add(renderedProperty.name);
|
|
1425
|
+
renderedProperties.push(renderedProperty);
|
|
1426
|
+
}
|
|
1427
|
+
const sortedProperties = sortRenderedProperties(renderedProperties, fieldIndexes, lastFieldIndexes);
|
|
1428
|
+
const hasOrderChanged = sortedProperties.some((renderedProperty, propertyIndex) => renderedProperty.originalIndex !== propertyIndex);
|
|
1429
|
+
return {
|
|
1430
|
+
hasOrderChanged,
|
|
1431
|
+
sourceText: `{${sortedProperties.map((renderedProperty, propertyIndex) => getPropertySeparator(renderedProperty, shouldSeparateCategories, sortedProperties[propertyIndex - 1]) + renderedProperty.sourceText).join("")}}`
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
function isTsconfigFile(filePath) {
|
|
1435
|
+
const fileName = filePath?.split(/[\\/]/).at(-1);
|
|
1436
|
+
return fileName ? /^tsconfig(?:\..+)?\.json$/.test(fileName) : false;
|
|
1437
|
+
}
|
|
1438
|
+
async function preprocessTsconfig(sourceText, prettierOptions, parser) {
|
|
1439
|
+
if (!isTsconfigFile(prettierOptions.filepath)) {
|
|
1440
|
+
return sourceText;
|
|
1441
|
+
}
|
|
1442
|
+
const { tsconfigSeparation, tsconfigSort } = resolveTsconfigOptions(prettierOptions);
|
|
1443
|
+
if (!tsconfigSort) {
|
|
1444
|
+
return sourceText;
|
|
1445
|
+
}
|
|
1446
|
+
try {
|
|
1447
|
+
const parserAst = await parser.parse(sourceText, prettierOptions);
|
|
1448
|
+
if (!isParserAstNode(parserAst)) {
|
|
1449
|
+
return sourceText;
|
|
1450
|
+
}
|
|
1451
|
+
const rootNode = getTsconfigRootNode(parserAst);
|
|
1452
|
+
if (!rootNode) {
|
|
1453
|
+
return sourceText;
|
|
1454
|
+
}
|
|
1455
|
+
const sortedComments = getSortedAstCommentsWithTextRanges(getProgramComments(parserAst));
|
|
1456
|
+
const rootRange = getAstNodeTextRange(rootNode);
|
|
1457
|
+
if (!rootRange || isPrettierIgnored(sourceText, rootRange, sortedComments)) {
|
|
1458
|
+
return sourceText;
|
|
1459
|
+
}
|
|
1460
|
+
let compilerOptionsEdit;
|
|
1461
|
+
let propertyReplacements;
|
|
1462
|
+
const compilerOptions = getCompilerOptionsNode(rootNode);
|
|
1463
|
+
if (compilerOptions) {
|
|
1464
|
+
const propertyRange = getAstNodeTextRange(compilerOptions.property);
|
|
1465
|
+
const valueRange = getAstNodeTextRange(compilerOptions.value);
|
|
1466
|
+
if (propertyRange && valueRange && !isPrettierIgnored(sourceText, propertyRange, sortedComments)) {
|
|
1467
|
+
const renderedCompilerOptions = renderObject(sourceText, compilerOptions.value, sortedComments, COMPILER_OPTION_FIELD_INDEXES, tsconfigSeparation);
|
|
1468
|
+
const renderedProperty = renderedCompilerOptions ? replacePropertyValue(sourceText, compilerOptions.property, compilerOptions.value, renderedCompilerOptions.sourceText) : null;
|
|
1469
|
+
if (renderedCompilerOptions && renderedProperty) {
|
|
1470
|
+
compilerOptionsEdit = {
|
|
1471
|
+
start: valueRange.start,
|
|
1472
|
+
end: valueRange.end,
|
|
1473
|
+
replacementText: renderedCompilerOptions.sourceText
|
|
1474
|
+
};
|
|
1475
|
+
propertyReplacements = new Map([
|
|
1476
|
+
[compilerOptions.property, renderedProperty]
|
|
1477
|
+
]);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
const renderedRoot = renderObject(sourceText, rootNode, sortedComments, ROOT_FIRST_FIELD_INDEXES, false, propertyReplacements, ROOT_LAST_FIELD_INDEXES);
|
|
1482
|
+
const sourceTextEdits = [];
|
|
1483
|
+
if (renderedRoot && (renderedRoot.hasOrderChanged || propertyReplacements)) {
|
|
1484
|
+
sourceTextEdits.push({
|
|
1485
|
+
start: rootRange.start,
|
|
1486
|
+
end: rootRange.end,
|
|
1487
|
+
replacementText: renderedRoot.sourceText
|
|
1488
|
+
});
|
|
1489
|
+
} else if (compilerOptionsEdit) {
|
|
1490
|
+
sourceTextEdits.push(compilerOptionsEdit);
|
|
1491
|
+
}
|
|
1492
|
+
if (sourceTextEdits.length === 0) {
|
|
1493
|
+
return sourceText;
|
|
1494
|
+
}
|
|
1495
|
+
const editedSourceText = applySourceTextEdits(sourceText, sourceTextEdits);
|
|
1496
|
+
if (editedSourceText === null) {
|
|
1497
|
+
return sourceText;
|
|
1498
|
+
}
|
|
1499
|
+
await parser.parse(editedSourceText, prettierOptions);
|
|
1500
|
+
return editedSourceText;
|
|
1501
|
+
} catch {
|
|
1502
|
+
return sourceText;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1129
1506
|
// src/sort-exports.ts
|
|
1130
1507
|
function buildExportSortingEdits(sourceText, programStatements, sortedComments) {
|
|
1131
1508
|
const sortingEdits = [];
|
|
@@ -1190,28 +1567,6 @@ function buildExportSortingEdits(sourceText, programStatements, sortedComments)
|
|
|
1190
1567
|
return sortingEdits;
|
|
1191
1568
|
}
|
|
1192
1569
|
|
|
1193
|
-
// src/utils/source-text.ts
|
|
1194
|
-
function applySourceTextEdits(sourceText, sourceTextEdits) {
|
|
1195
|
-
if (sourceTextEdits.length === 0) {
|
|
1196
|
-
return sourceText;
|
|
1197
|
-
}
|
|
1198
|
-
let previousEditEnd = 0;
|
|
1199
|
-
const ascendingEdits = [...sourceTextEdits].sort((left, right) => left.start - right.start || left.end - right.end);
|
|
1200
|
-
for (const sourceTextEdit of ascendingEdits) {
|
|
1201
|
-
const { start, end } = sourceTextEdit;
|
|
1202
|
-
const isSourceTextEditValid = [start, end].every(Number.isSafeInteger) && start >= previousEditEnd && end >= start && end <= sourceText.length;
|
|
1203
|
-
if (!isSourceTextEditValid) {
|
|
1204
|
-
return null;
|
|
1205
|
-
}
|
|
1206
|
-
previousEditEnd = end;
|
|
1207
|
-
}
|
|
1208
|
-
let editedText = sourceText;
|
|
1209
|
-
for (const sourceTextEdit of ascendingEdits.reverse()) {
|
|
1210
|
-
editedText = editedText.slice(0, sourceTextEdit.start) + sourceTextEdit.replacementText + editedText.slice(sourceTextEdit.end);
|
|
1211
|
-
}
|
|
1212
|
-
return editedText;
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
1570
|
// src/sort-imports.ts
|
|
1216
1571
|
var INDEX_MODULE_PATTERN = /^\.\/index(?:\.[^/]+)*$/;
|
|
1217
1572
|
var PRETTIER_FILE_PRAGMA_DIRECTIVES = ["@format", "@prettier"];
|
|
@@ -1294,7 +1649,19 @@ function getPrettierFilePragmaComment(sortedComments, isPrettierFilePragmaPresen
|
|
|
1294
1649
|
}
|
|
1295
1650
|
return sortedComments.find(({ comment }) => getCommentLines(comment).some((commentLine) => hasCommentDirective(commentLine.trim(), PRETTIER_FILE_PRAGMA_DIRECTIVES)))?.comment ?? null;
|
|
1296
1651
|
}
|
|
1297
|
-
function
|
|
1652
|
+
function matchesPathPattern(moduleSpecifier, pathPattern) {
|
|
1653
|
+
const wildcardIndex = pathPattern.indexOf("*");
|
|
1654
|
+
if (wildcardIndex < 0) {
|
|
1655
|
+
return moduleSpecifier === pathPattern;
|
|
1656
|
+
}
|
|
1657
|
+
if (pathPattern === "*" || wildcardIndex !== pathPattern.lastIndexOf("*")) {
|
|
1658
|
+
return false;
|
|
1659
|
+
}
|
|
1660
|
+
const prefix = pathPattern.slice(0, wildcardIndex);
|
|
1661
|
+
const suffix = pathPattern.slice(wildcardIndex + 1);
|
|
1662
|
+
return moduleSpecifier.length >= prefix.length + suffix.length && moduleSpecifier.startsWith(prefix) && moduleSpecifier.endsWith(suffix);
|
|
1663
|
+
}
|
|
1664
|
+
function classifyImportGroup(moduleSpecifier, pathPatterns) {
|
|
1298
1665
|
if (moduleSpecifier === "bun" || moduleSpecifier.startsWith("bun:") || moduleSpecifier.startsWith("node:")) {
|
|
1299
1666
|
return "builtin";
|
|
1300
1667
|
}
|
|
@@ -1307,7 +1674,7 @@ function classifyImportGroup(moduleSpecifier) {
|
|
|
1307
1674
|
if (moduleSpecifier.startsWith("./")) {
|
|
1308
1675
|
return "sibling";
|
|
1309
1676
|
}
|
|
1310
|
-
if (
|
|
1677
|
+
if (pathPatterns.some((pathPattern) => matchesPathPattern(moduleSpecifier, pathPattern))) {
|
|
1311
1678
|
return "internal";
|
|
1312
1679
|
}
|
|
1313
1680
|
return "external";
|
|
@@ -1734,7 +2101,7 @@ function getImportBindingShapeRank(importDeclaration) {
|
|
|
1734
2101
|
}
|
|
1735
2102
|
return 2;
|
|
1736
2103
|
}
|
|
1737
|
-
function
|
|
2104
|
+
function renderSortedImportSegment(importDeclarations, sortOptions, pathPatterns, importGroupOrder) {
|
|
1738
2105
|
let mergedImportDeclarations = [...importDeclarations];
|
|
1739
2106
|
if (sortOptions.esmImportMerge) {
|
|
1740
2107
|
mergedImportDeclarations = mergeCompatibleImports(importDeclarations);
|
|
@@ -1743,7 +2110,7 @@ function sortImportSegment(importDeclarations, sortOptions, importGroupOrder) {
|
|
|
1743
2110
|
const unlistedGroupRank = sortOptions.esmImportGroups.length;
|
|
1744
2111
|
const rankedImportDeclarations = styledImportDeclarations.map((importDeclaration, originalIndex) => ({
|
|
1745
2112
|
importDeclaration,
|
|
1746
|
-
importGroup: classifyImportGroup(importDeclaration.moduleSpecifier),
|
|
2113
|
+
importGroup: classifyImportGroup(importDeclaration.moduleSpecifier, pathPatterns),
|
|
1747
2114
|
originalIndex
|
|
1748
2115
|
}));
|
|
1749
2116
|
rankedImportDeclarations.sort((left, right) => {
|
|
@@ -1764,17 +2131,17 @@ function sortImportSegment(importDeclarations, sortOptions, importGroupOrder) {
|
|
|
1764
2131
|
return getImportBindingShapeRank(left.importDeclaration) - getImportBindingShapeRank(right.importDeclaration) || left.originalIndex - right.originalIndex;
|
|
1765
2132
|
});
|
|
1766
2133
|
let previousImportGroup = null;
|
|
1767
|
-
const
|
|
2134
|
+
const renderedImports = [];
|
|
1768
2135
|
for (const { importDeclaration, importGroup } of rankedImportDeclarations) {
|
|
1769
2136
|
if (sortOptions.esmImportSeparation && previousImportGroup !== null && importGroup !== previousImportGroup) {
|
|
1770
|
-
|
|
2137
|
+
renderedImports.push("");
|
|
1771
2138
|
}
|
|
1772
|
-
|
|
2139
|
+
renderedImports.push(renderImportDeclaration(importDeclaration));
|
|
1773
2140
|
previousImportGroup = importGroup;
|
|
1774
2141
|
}
|
|
1775
|
-
return
|
|
2142
|
+
return renderedImports;
|
|
1776
2143
|
}
|
|
1777
|
-
function
|
|
2144
|
+
function renderSortedImports(importDeclarations, sortOptions, pathPatterns) {
|
|
1778
2145
|
let currentSortableImportDeclarations = [];
|
|
1779
2146
|
const importGroupOrder = new Map(sortOptions.esmImportGroups.map((importGroup, groupIndex) => [
|
|
1780
2147
|
importGroup,
|
|
@@ -1802,19 +2169,19 @@ function renderSortedImportLines(importDeclarations, sortOptions) {
|
|
|
1802
2169
|
});
|
|
1803
2170
|
}
|
|
1804
2171
|
let previousChunkKind = null;
|
|
1805
|
-
const
|
|
2172
|
+
const renderedImports = [];
|
|
1806
2173
|
for (const importChunk of importDeclarationChunks) {
|
|
1807
2174
|
if (sortOptions.esmImportSeparation && previousChunkKind !== null && previousChunkKind !== importChunk.kind) {
|
|
1808
|
-
|
|
2175
|
+
renderedImports.push("");
|
|
1809
2176
|
}
|
|
1810
2177
|
if (importChunk.kind === "sortable") {
|
|
1811
|
-
|
|
2178
|
+
renderedImports.push(...renderSortedImportSegment(importChunk.importDeclarations, sortOptions, pathPatterns, importGroupOrder));
|
|
1812
2179
|
} else {
|
|
1813
|
-
|
|
2180
|
+
renderedImports.push(renderImportDeclaration(importChunk.importDeclaration));
|
|
1814
2181
|
}
|
|
1815
2182
|
previousChunkKind = importChunk.kind;
|
|
1816
2183
|
}
|
|
1817
|
-
return
|
|
2184
|
+
return renderedImports;
|
|
1818
2185
|
}
|
|
1819
2186
|
function isCommentPresentWithinRange(sortedComments, start, end) {
|
|
1820
2187
|
const firstCommentIndex = findCommentIndexAtOrAfter(sortedComments, start);
|
|
@@ -1833,13 +2200,13 @@ function hasPositionSensitiveEslintCommentWithinRange(sourceText, sortedComments
|
|
|
1833
2200
|
}
|
|
1834
2201
|
return false;
|
|
1835
2202
|
}
|
|
1836
|
-
function buildImportSegmentEdits(sourceText, importEntries, sortOptions, lineEnding, isBlankLineRequired) {
|
|
2203
|
+
function buildImportSegmentEdits(sourceText, importEntries, sortOptions, pathPatterns, lineEnding, isBlankLineRequired) {
|
|
1837
2204
|
const [firstImportEntry] = importEntries;
|
|
1838
2205
|
if (!firstImportEntry) {
|
|
1839
2206
|
return null;
|
|
1840
2207
|
}
|
|
1841
2208
|
let trailingLineBreaks = lineEnding;
|
|
1842
|
-
const replacementText =
|
|
2209
|
+
const replacementText = renderSortedImports(importEntries.map((importEntry) => importEntry.parsedImportDeclaration), sortOptions, pathPatterns).join(lineEnding);
|
|
1843
2210
|
const removalEdits = importEntries.slice(1).map((importEntry) => ({
|
|
1844
2211
|
start: importEntry.start,
|
|
1845
2212
|
end: importEntry.end,
|
|
@@ -1864,7 +2231,7 @@ function buildImportSegmentEdits(sourceText, importEntries, sortOptions, lineEnd
|
|
|
1864
2231
|
...removalEdits
|
|
1865
2232
|
];
|
|
1866
2233
|
}
|
|
1867
|
-
function buildImportSortingEdits(sourceText, programStatements, sortedComments, sortOptions, isPrettierFilePragmaPresent) {
|
|
2234
|
+
function buildImportSortingEdits(sourceText, programStatements, sortedComments, sortOptions, pathPatterns, isPrettierFilePragmaPresent) {
|
|
1868
2235
|
const importDeclarationNodes = programStatements.filter((statement) => statement.type === "ImportDeclaration");
|
|
1869
2236
|
if (importDeclarationNodes.length === 0) {
|
|
1870
2237
|
return [];
|
|
@@ -1930,7 +2297,7 @@ function buildImportSortingEdits(sourceText, programStatements, sortedComments,
|
|
|
1930
2297
|
const lastImportDeclarationNode = importDeclarationNodes.at(-1);
|
|
1931
2298
|
for (const sortableSegment of sortableSegments) {
|
|
1932
2299
|
const isLastImportSegment = sortableSegment.at(-1)?.importDeclarationNode === lastImportDeclarationNode;
|
|
1933
|
-
const importSegmentEdits = buildImportSegmentEdits(sourceText, sortableSegment, sortOptions, lineEnding, isLastImportSegment);
|
|
2300
|
+
const importSegmentEdits = buildImportSegmentEdits(sourceText, sortableSegment, sortOptions, pathPatterns, lineEnding, isLastImportSegment);
|
|
1934
2301
|
if (importSegmentEdits === null) {
|
|
1935
2302
|
return [];
|
|
1936
2303
|
}
|
|
@@ -1940,8 +2307,20 @@ function buildImportSortingEdits(sourceText, programStatements, sortedComments,
|
|
|
1940
2307
|
}
|
|
1941
2308
|
|
|
1942
2309
|
// src/sort-typescript.ts
|
|
2310
|
+
async function getTsconfigPathPatterns(filePath) {
|
|
2311
|
+
if (!filePath) {
|
|
2312
|
+
return [];
|
|
2313
|
+
}
|
|
2314
|
+
try {
|
|
2315
|
+
const { getTsconfig } = await import("get-tsconfig");
|
|
2316
|
+
const paths = getTsconfig(filePath)?.config.compilerOptions?.paths;
|
|
2317
|
+
return paths ? Object.keys(paths) : [];
|
|
2318
|
+
} catch {
|
|
2319
|
+
return [];
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
1943
2322
|
async function sortTypeScript(sourceText, prettierOptions, parser) {
|
|
1944
|
-
const sortOptions =
|
|
2323
|
+
const sortOptions = resolveEsmOptions(prettierOptions);
|
|
1945
2324
|
if (!sortOptions.esmImportSort && !sortOptions.esmExportSpecifierSort) {
|
|
1946
2325
|
return sourceText;
|
|
1947
2326
|
}
|
|
@@ -1956,7 +2335,8 @@ async function sortTypeScript(sourceText, prettierOptions, parser) {
|
|
|
1956
2335
|
const sortingEdits = [];
|
|
1957
2336
|
if (sortOptions.esmImportSort) {
|
|
1958
2337
|
const isPrettierFilePragmaPresent = parser.hasPragma?.(sourceText) ?? false;
|
|
1959
|
-
|
|
2338
|
+
const pathPatterns = programStatements.some((statement) => statement.type === "ImportDeclaration") ? await getTsconfigPathPatterns(prettierOptions.filepath) : [];
|
|
2339
|
+
sortingEdits.push(...buildImportSortingEdits(sourceText, programStatements, sortedComments, sortOptions, pathPatterns, isPrettierFilePragmaPresent));
|
|
1960
2340
|
}
|
|
1961
2341
|
if (sortOptions.esmExportSpecifierSort) {
|
|
1962
2342
|
sortingEdits.push(...buildExportSortingEdits(sourceText, programStatements, sortedComments));
|
|
@@ -1986,6 +2366,10 @@ function wrapParserPreprocess(parser, preprocessTransform) {
|
|
|
1986
2366
|
}
|
|
1987
2367
|
};
|
|
1988
2368
|
}
|
|
2369
|
+
async function preprocessJson(sourceText, prettierOptions, parser) {
|
|
2370
|
+
const packagePreprocessedText = await preprocessPackageJson(sourceText, prettierOptions, parser);
|
|
2371
|
+
return preprocessTsconfig(packagePreprocessedText, prettierOptions, parser);
|
|
2372
|
+
}
|
|
1989
2373
|
var sortPlugin = {
|
|
1990
2374
|
options,
|
|
1991
2375
|
parsers: {
|
|
@@ -1995,7 +2379,7 @@ var sortPlugin = {
|
|
|
1995
2379
|
"babel-ts": wrapParserPreprocess(babelPlugin.parsers["babel-ts"], sortTypeScript),
|
|
1996
2380
|
espree: wrapParserPreprocess(acornPlugin.parsers.espree, sortTypeScript),
|
|
1997
2381
|
flow: wrapParserPreprocess(flowPlugin.parsers.flow, sortTypeScript),
|
|
1998
|
-
json: wrapParserPreprocess(babelPlugin.parsers.json,
|
|
2382
|
+
json: wrapParserPreprocess(babelPlugin.parsers.json, preprocessJson),
|
|
1999
2383
|
"json-stringify": wrapParserPreprocess(babelPlugin.parsers["json-stringify"], preprocessPackageJson),
|
|
2000
2384
|
meriyah: wrapParserPreprocess(meriyahPlugin.parsers.meriyah, sortTypeScript),
|
|
2001
2385
|
typescript: wrapParserPreprocess(typescriptPlugin.parsers.typescript, sortTypeScript)
|