Skip to content

Commit 22b6fca

Browse files
authored
feat: #28 - Sorted named import & export specifiers
1 parent 07ac8b5 commit 22b6fca

20 files changed

+487
-38
lines changed

src/configuration/builder.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,22 @@ impl ConfigurationBuilder {
401401
self.insert("typeLiteral.separatorKind.multiLine", value.to_string().into())
402402
}
403403

404+
/* sorting */
405+
406+
/// Alphabetically sorts the import declaration's named imports.
407+
///
408+
/// Default: Case insensitive
409+
pub fn import_declaration_sort_named_imports(&mut self, value: SortOrder) -> &mut Self {
410+
self.insert("importDeclaration.sortNamedImports", value.to_string().into())
411+
}
412+
413+
/// Alphabetically sorts the export declaration's named exports.
414+
///
415+
/// Default: Case insensitive
416+
pub fn export_declaration_sort_named_exports(&mut self, value: SortOrder) -> &mut Self {
417+
self.insert("exportDeclaration.sortNamedExports", value.to_string().into())
418+
}
419+
404420
/* ignore comments */
405421

406422
/// The text to use for an ignore comment (ex. `// dprint-ignore`).
@@ -859,6 +875,9 @@ mod tests {
859875
.type_literal_separator_kind(SemiColonOrComma::Comma)
860876
.type_literal_separator_kind_single_line(SemiColonOrComma::Comma)
861877
.type_literal_separator_kind_multi_line(SemiColonOrComma::Comma)
878+
/* sorting */
879+
.import_declaration_sort_named_imports(SortOrder::Maintain)
880+
.export_declaration_sort_named_exports(SortOrder::Maintain)
862881
/* ignore comments */
863882
.ignore_node_comment_text("ignore")
864883
.ignore_file_comment_text("ignore-file")
@@ -994,7 +1013,7 @@ mod tests {
9941013
.while_statement_space_after_while_keyword(true);
9951014

9961015
let inner_config = config.get_inner_config();
997-
assert_eq!(inner_config.len(), 141);
1016+
assert_eq!(inner_config.len(), 143);
9981017
let diagnostics = resolve_config(inner_config, &resolve_global_config(HashMap::new()).config).diagnostics;
9991018
assert_eq!(diagnostics.len(), 0);
10001019
}

src/configuration/resolve_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
5656
member_expression_line_per_expression: get_value(&mut config, "memberExpression.linePerExpression", false, &mut diagnostics),
5757
type_literal_separator_kind_single_line: get_value(&mut config, "typeLiteral.separatorKind.singleLine", type_literal_separator_kind, &mut diagnostics),
5858
type_literal_separator_kind_multi_line: get_value(&mut config, "typeLiteral.separatorKind.multiLine", type_literal_separator_kind, &mut diagnostics),
59+
/* sorting */
60+
import_declaration_sort_named_imports: get_value(&mut config, "importDeclaration.sortNamedImports", SortOrder::CaseInsensitive, &mut diagnostics),
61+
export_declaration_sort_named_exports: get_value(&mut config, "exportDeclaration.sortNamedExports", SortOrder::CaseInsensitive, &mut diagnostics),
5962
/* ignore comments */
6063
ignore_node_comment_text: get_value(&mut config, "ignoreNodeCommentText", String::from("dprint-ignore"), &mut diagnostics),
6164
ignore_file_comment_text: get_value(&mut config, "ignoreFileCommentText", String::from("dprint-ignore-file"), &mut diagnostics),

src/configuration/types.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,25 @@ generate_str_to_from![
233233
[Comma, "comma"]
234234
];
235235

236+
/// The kind of sort ordering to use.
237+
#[derive(Clone, PartialEq, Copy, Serialize, Deserialize)]
238+
#[serde(rename_all = "camelCase")]
239+
pub enum SortOrder {
240+
/// Maintains the current ordering.
241+
Maintain,
242+
/// Alphabetically and case sensitive.
243+
CaseSensitive,
244+
/// Alphabetically and case insensitive.
245+
CaseInsensitive,
246+
}
247+
248+
generate_str_to_from![
249+
SortOrder,
250+
[Maintain, "maintain"],
251+
[CaseSensitive, "caseSensitive"],
252+
[CaseInsensitive, "caseInsensitive"]
253+
];
254+
236255
#[derive(Clone, Serialize, Deserialize)]
237256
#[serde(rename_all = "camelCase")]
238257
pub struct Configuration {
@@ -253,6 +272,11 @@ pub struct Configuration {
253272
pub type_literal_separator_kind_single_line: SemiColonOrComma,
254273
#[serde(rename = "typeLiteral.separatorKind.multiLine")]
255274
pub type_literal_separator_kind_multi_line: SemiColonOrComma,
275+
/* sorting */
276+
#[serde(rename = "importDeclaration.sortNamedImports")]
277+
pub import_declaration_sort_named_imports: SortOrder,
278+
#[serde(rename = "exportDeclaration.sortNamedExports")]
279+
pub export_declaration_sort_named_exports: SortOrder,
256280
/* ignore comments */
257281
pub ignore_node_comment_text: String,
258282
pub ignore_file_comment_text: String,

0 commit comments

Comments
 (0)