Skip to content

Commit aa29785

Browse files
committed
feat: Named imports and exports are collapsed by default.
1 parent 22b6fca commit aa29785

9 files changed

+70
-33
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ panic = "abort"
2424
wasm = ["serde_json", "dprint-core/wasm"]
2525

2626
[dependencies]
27-
dprint-core = { version = "0.28.0", features = ["formatting"] }
27+
dprint-core = { version = "0.31.0", features = ["formatting"] }
2828
swc_common = "=0.10.2"
2929
swc_ecmascript = { version = "=0.7.7", features = ["parser"] }
3030
serde = { version = "1.0.88", features = ["derive"] }

src/configuration/resolve_config.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
4040
let trailing_commas = get_value(&mut config, "trailingCommas", TrailingCommas::OnlyMultiLine, &mut diagnostics);
4141
let use_braces = get_value(&mut config, "useBraces", UseBraces::WhenNotSingleLine, &mut diagnostics);
4242
let prefer_hanging = get_value(&mut config, "preferHanging", false, &mut diagnostics);
43-
let prefer_single_line = get_value(&mut config, "preferSingleLine", false, &mut diagnostics);
43+
let prefer_single_line_nullable = get_nullable_value(&mut config, "preferSingleLine", &mut diagnostics);
44+
let prefer_single_line = prefer_single_line_nullable.unwrap_or(false);
4445
let type_literal_separator_kind = get_value(&mut config, "typeLiteral.separatorKind", SemiColonOrComma::SemiColon, &mut diagnostics);
4546

4647
let resolved_config = Configuration {
@@ -151,9 +152,9 @@ pub fn resolve_config(config: ConfigKeyMap, global_config: &GlobalConfiguration)
151152
conditional_expression_prefer_single_line: get_value(&mut config, "conditionalExpression.preferSingleLine", prefer_single_line, &mut diagnostics),
152153
conditional_type_prefer_single_line: get_value(&mut config, "conditionalType.preferSingleLine", prefer_single_line, &mut diagnostics),
153154
decorators_prefer_single_line: get_value(&mut config, "decorators.preferSingleLine", prefer_single_line, &mut diagnostics),
154-
export_declaration_prefer_single_line: get_value(&mut config, "exportDeclaration.preferSingleLine", prefer_single_line, &mut diagnostics),
155+
export_declaration_prefer_single_line: get_value(&mut config, "exportDeclaration.preferSingleLine", prefer_single_line_nullable.unwrap_or(true), &mut diagnostics),
155156
for_statement_prefer_single_line: get_value(&mut config, "forStatement.preferSingleLine", prefer_single_line, &mut diagnostics),
156-
import_declaration_prefer_single_line: get_value(&mut config, "importDeclaration.preferSingleLine", prefer_single_line, &mut diagnostics),
157+
import_declaration_prefer_single_line: get_value(&mut config, "importDeclaration.preferSingleLine", prefer_single_line_nullable.unwrap_or(true), &mut diagnostics),
157158
jsx_attributes_prefer_single_line: get_value(&mut config, "jsxAttributes.preferSingleLine", prefer_single_line, &mut diagnostics),
158159
jsx_element_prefer_single_line: get_value(&mut config, "jsxElement.preferSingleLine", prefer_single_line, &mut diagnostics),
159160
mapped_type_prefer_single_line: get_value(&mut config, "mappedType.preferSingleLine", prefer_single_line, &mut diagnostics),

tests/specs/declarations/export/ExportNamedDeclaration_All.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,17 @@ export {
6363
test,
6464
testingThisOut,
6565
} from "testingtttttttttttttttttttttttttttttt";
66+
67+
== should prefer single line by default ==
68+
export {
69+
a
70+
} from "test";
71+
export {
72+
b // test
73+
} from "test";
74+
75+
[expect]
76+
export { a } from "test";
77+
export {
78+
b, // test
79+
} from "test";

tests/specs/declarations/export/NamedExports_PreferHanging_True.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@ export { n1, n2, n3, n4 , n5, n6} from "test";
1212
export { n1, n2, n3, n4, n5,
1313
n6 } from "test";
1414

15-
== should format with newlines if the first is on a different line ==
15+
== should not format with newlines if the first is on a different line since preferSingleLine is true by default for export decls ==
1616
export {
1717
n1, n2, n3, n4 , n5, n6} from "test";
1818

1919
[expect]
20-
export {
21-
n1,
22-
n2,
23-
n3,
24-
n4,
25-
n5,
26-
n6,
27-
} from "test";
20+
export { n1, n2, n3, n4, n5,
21+
n6 } from "test";
2822

2923
== should always do a newline as a unit ==
3024
export { test1, test2, test3 as alias } from "test";
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
~~ lineWidth: 40, exportDeclaration.trailingCommas: never ~~
22
== should not use trailing commas when multi-line ==
33
export {
4-
n1, n2,
5-
n3 } from "test";
4+
testing1, testing2,
5+
testing3 } from "test";
66

77
[expect]
88
export {
9-
n1,
10-
n2,
11-
n3
9+
testing1,
10+
testing2,
11+
testing3
1212
} from "test";
1313

1414
== should sort the exports in alphabetical order taking into account the comma ==
1515
export {
16-
n3, n2, n1
16+
testing3, testing2, testing1
1717
} from "test";
1818

1919
[expect]
2020
export {
21-
n1,
22-
n2,
23-
n3
21+
testing1,
22+
testing2,
23+
testing3
2424
} from "test";

tests/specs/declarations/import/ImportDeclaration_All.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,17 @@ import { d, c, B, a, E, f2, f, f1} from "test";
9292

9393
[expect]
9494
import { a, B, c, d, E, f, f1, f2 } from "test";
95+
96+
== should prefer single line by default ==
97+
import {
98+
a
99+
} from "test";
100+
import {
101+
b // test
102+
} from "test";
103+
104+
[expect]
105+
import { a } from "test";
106+
import {
107+
b, // test
108+
} from "test";
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
~~ lineWidth: 40, importDeclaration.trailingCommas: never ~~
22
== should not use trailing commas when multi-line ==
33
import {
4-
n1, n2,
5-
n3 } from "test";
4+
testing1, testing2,
5+
testing3 } from "test";
66

77
[expect]
88
import {
9-
n1,
10-
n2,
11-
n3
9+
testing1,
10+
testing2,
11+
testing3
1212
} from "test";
1313

1414
== should sort the imports in alphabetical order taking into account the comma ==
1515
import {
16-
n3, n2, n1
16+
testing3, testing2, testing1
1717
} from "test";
1818

1919
[expect]
2020
import {
21-
n1,
22-
n2,
23-
n3
21+
testing1,
22+
testing2,
23+
testing3
2424
} from "test";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
~~ deno: true ~~
2+
== should collapse imports and exports when able ==
3+
import {
4+
testing,
5+
thisOut,
6+
} from "test.ts";
7+
export {
8+
testing,
9+
thisOut,
10+
} from "test.ts";
11+
12+
[expect]
13+
import { testing, thisOut } from "test.ts";
14+
export { testing, thisOut } from "test.ts";

0 commit comments

Comments
 (0)