@1adybug/prettier-plugin-sort-imports 0.0.23 → 0.0.25
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/formatter.d.ts +4 -3
- package/dist/index.js +26 -8
- package/package.json +1 -1
package/dist/formatter.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { ParserOptions } from "prettier";
|
|
1
2
|
import { Group, ImportStatement, PluginConfig } from "./types"; /** 格式化导入语句 */
|
|
2
|
-
export declare function formatImportStatement(statement: ImportStatement): string;
|
|
3
|
+
export declare function formatImportStatement(statement: ImportStatement, trailingComma?: ParserOptions["trailingComma"]): string;
|
|
3
4
|
/** 格式化分组 */
|
|
4
|
-
export declare function formatGroups(groups: Group[], config: PluginConfig): string;
|
|
5
|
+
export declare function formatGroups(groups: Group[], config: PluginConfig, trailingComma?: ParserOptions["trailingComma"]): string;
|
|
5
6
|
/** 格式化导入语句列表(不使用分组) */
|
|
6
|
-
export declare function formatImportStatements(statements: ImportStatement[]): string;
|
|
7
|
+
export declare function formatImportStatements(statements: ImportStatement[], trailingComma?: ParserOptions["trailingComma"]): string;
|
package/dist/index.js
CHANGED
|
@@ -79,9 +79,10 @@ function removeUnusedImportsFromStatements(importStatements, code) {
|
|
|
79
79
|
}
|
|
80
80
|
return filteredStatements;
|
|
81
81
|
}
|
|
82
|
-
function formatImportStatement(statement) {
|
|
82
|
+
function formatImportStatement(statement, trailingComma) {
|
|
83
83
|
const { path, isExport, isSideEffect, importContents, leadingComments, trailingComments, removedTrailingComments, emptyLinesAfterComments } = statement;
|
|
84
84
|
const lines = [];
|
|
85
|
+
const shouldAddTrailingComma = (trailingComma ?? "all") !== "none";
|
|
85
86
|
if (leadingComments && leadingComments.length > 0) {
|
|
86
87
|
lines.push(...leadingComments);
|
|
87
88
|
const emptyLines = emptyLinesAfterComments ?? 0;
|
|
@@ -130,8 +131,25 @@ function formatImportStatement(statement) {
|
|
|
130
131
|
const defaultPart = parts.length > 0 ? parts.join(", ") + ", " : "";
|
|
131
132
|
const importStart = `${keyword} ${typeKeyword}${defaultPart}{`;
|
|
132
133
|
const importEnd = `} from "${path}"`;
|
|
134
|
+
const formatWithTrailingComma = (item)=>{
|
|
135
|
+
const lines = item.split("\n");
|
|
136
|
+
const lastLine = lines.pop();
|
|
137
|
+
const lineWithComma = (()=>{
|
|
138
|
+
const lineCommentIndex = lastLine.indexOf(" //");
|
|
139
|
+
const blockCommentIndex = lastLine.indexOf(" /*");
|
|
140
|
+
const commentIndex = -1 === lineCommentIndex ? blockCommentIndex : -1 === blockCommentIndex ? lineCommentIndex : Math.min(lineCommentIndex, blockCommentIndex);
|
|
141
|
+
if (-1 === commentIndex) return `${lastLine},`;
|
|
142
|
+
return `${lastLine.slice(0, commentIndex)},${lastLine.slice(commentIndex)}`;
|
|
143
|
+
})();
|
|
144
|
+
lines.push(lineWithComma);
|
|
145
|
+
return lines.join("\n");
|
|
146
|
+
};
|
|
133
147
|
lines.push(importStart);
|
|
134
|
-
|
|
148
|
+
const formattedParts = [
|
|
149
|
+
...namedPartsWithComments
|
|
150
|
+
];
|
|
151
|
+
if (shouldAddTrailingComma) formattedParts[formattedParts.length - 1] = formatWithTrailingComma(formattedParts[formattedParts.length - 1]);
|
|
152
|
+
lines.push(` ${formattedParts.join(",\n ")}`);
|
|
135
153
|
lines.push(importEnd);
|
|
136
154
|
} else {
|
|
137
155
|
if (namedParts.length > 0) if (allNamedImportsAreTypes && !hasDefaultOrNamespace) {
|
|
@@ -151,7 +169,7 @@ function formatImportStatement(statement) {
|
|
|
151
169
|
}
|
|
152
170
|
return lines.join("\n");
|
|
153
171
|
}
|
|
154
|
-
function formatGroups(groups, config) {
|
|
172
|
+
function formatGroups(groups, config, trailingComma) {
|
|
155
173
|
const lines = [];
|
|
156
174
|
const separator = config.groupSeparator;
|
|
157
175
|
for(let i = 0; i < groups.length; i++){
|
|
@@ -164,12 +182,12 @@ function formatGroups(groups, config) {
|
|
|
164
182
|
if ("" !== separatorStr) lines.push(separatorStr);
|
|
165
183
|
}
|
|
166
184
|
}
|
|
167
|
-
for (const statement of group.importStatements)lines.push(formatImportStatement(statement));
|
|
185
|
+
for (const statement of group.importStatements)lines.push(formatImportStatement(statement, trailingComma));
|
|
168
186
|
}
|
|
169
187
|
return lines.join("\n");
|
|
170
188
|
}
|
|
171
|
-
function formatImportStatements(statements) {
|
|
172
|
-
return statements.map(formatImportStatement).join("\n");
|
|
189
|
+
function formatImportStatements(statements, trailingComma) {
|
|
190
|
+
return statements.map((statement)=>formatImportStatement(statement, trailingComma)).join("\n");
|
|
173
191
|
}
|
|
174
192
|
function parseImports(code, filepath) {
|
|
175
193
|
const hasImportOrExport = /^\s*(import|export)\s/m.test(code);
|
|
@@ -599,8 +617,8 @@ function preprocessImports(text, options, config = {}) {
|
|
|
599
617
|
if (finalConfig.getGroup) {
|
|
600
618
|
const groups = groupImports(mergedImports, finalConfig);
|
|
601
619
|
const sortedGroups = sortGroups(groups, finalConfig);
|
|
602
|
-
formattedImports = formatGroups(sortedGroups, finalConfig);
|
|
603
|
-
} else formattedImports = formatImportStatements(mergedImports);
|
|
620
|
+
formattedImports = formatGroups(sortedGroups, finalConfig, options.trailingComma);
|
|
621
|
+
} else formattedImports = formatImportStatements(mergedImports, options.trailingComma);
|
|
604
622
|
const firstImport = imports[0];
|
|
605
623
|
const lastImport = imports[imports.length - 1];
|
|
606
624
|
const startIndex = firstImport.start ?? 0;
|