Skip to content

Commit 10d560c

Browse files
committed
fix: #59 - Fix sorted named imports/exports incorrectly adding a blank line in some cases.
1 parent b3e1246 commit 10d560c

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -2,7 +2,7 @@
22
name = "dprint-plugin-typescript"
33
description = "TypeScript code formatting plugin for dprint."
44
keywords = ["formatting", "formatter", "typescript"]
5-
version = "0.32.0"
5+
version = "0.32.1"
66
authors = ["David Sherret <[email protected]>"]
77
edition = "2018"
88
license = "MIT"

src/parsing/parser.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ fn parse_named_import_or_export_specifiers<'a>(parent: &Node<'a>, specifiers: Ve
11541154
prefer_hanging: get_prefer_hanging(parent, context),
11551155
prefer_single_line: get_prefer_single_line(parent, context),
11561156
surround_single_line_with_spaces: get_use_space(parent, context),
1157+
allow_blank_lines: false,
11571158
node_sorter: get_node_sorter(parent, context),
11581159
}, context);
11591160

@@ -1971,6 +1972,7 @@ fn parse_object_lit<'a>(node: &'a ObjectLit, context: &mut Context<'a>) -> Print
19711972
prefer_hanging: context.config.object_expression_prefer_hanging,
19721973
prefer_single_line: context.config.object_expression_prefer_single_line,
19731974
surround_single_line_with_spaces: true,
1975+
allow_blank_lines: true,
19741976
node_sorter: None,
19751977
}, context)
19761978
}
@@ -2443,6 +2445,7 @@ fn parse_type_lit<'a>(node: &'a TsTypeLit, context: &mut Context<'a>) -> PrintIt
24432445
prefer_hanging: context.config.type_literal_prefer_hanging,
24442446
prefer_single_line: context.config.type_literal_prefer_single_line,
24452447
surround_single_line_with_spaces: true,
2448+
allow_blank_lines: true,
24462449
node_sorter: None,
24472450
}, context);
24482451

@@ -2828,6 +2831,7 @@ fn parse_object_pat<'a>(node: &'a ObjectPat, context: &mut Context<'a>) -> Print
28282831
prefer_hanging: context.config.object_pattern_prefer_hanging,
28292832
prefer_single_line: context.config.object_pattern_prefer_single_line,
28302833
surround_single_line_with_spaces: true,
2834+
allow_blank_lines: true,
28312835
node_sorter: None,
28322836
}, context));
28332837
if node.optional { items.push_str("?"); }
@@ -5111,6 +5115,13 @@ fn parse_separated_values_with_result<'a>(
51115115
let indent_width = context.config.indent_width;
51125116
let compute_lines_span = opts.allow_blank_lines; // save time otherwise
51135117
let node_sorter = opts.node_sorter;
5118+
5119+
// would need to make this take into account the new position of the nodes
5120+
#[cfg(debug_assertions)]
5121+
if node_sorter.is_some() && compute_lines_span {
5122+
panic!("Not implemented scenario. Cannot computed lines span and allow blank lines");
5123+
}
5124+
51145125
parser_helpers::parse_separated_values(|is_multi_line_or_hanging_ref| {
51155126
let is_multi_line_or_hanging = is_multi_line_or_hanging_ref.create_resolver();
51165127
let mut parsed_nodes = Vec::new();
@@ -5423,6 +5434,7 @@ struct ParseObjectLikeNodeOptions<'a> {
54235434
prefer_hanging: bool,
54245435
prefer_single_line: bool,
54255436
surround_single_line_with_spaces: bool,
5437+
allow_blank_lines: bool,
54265438
node_sorter: Option<Box<dyn Fn(&Option<Node<'a>>, &Option<Node<'a>>, &mut Context<'a>) -> std::cmp::Ordering>>,
54275439
}
54285440

@@ -5448,7 +5460,7 @@ fn parse_object_like_node<'a>(opts: ParseObjectLikeNodeOptions<'a>, context: &mu
54485460
nodes: opts.members.into_iter().map(|x| Some(x)).collect(),
54495461
prefer_hanging: opts.prefer_hanging,
54505462
force_use_new_lines: force_multi_line,
5451-
allow_blank_lines: true,
5463+
allow_blank_lines: opts.allow_blank_lines,
54525464
separator: opts.separator,
54535465
single_line_space_at_start: opts.surround_single_line_with_spaces,
54545466
single_line_space_at_end: opts.surround_single_line_with_spaces,

tests/specs/issues/issue0059.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
~~ deno: true ~~
2+
== should format without adding newlines ==
3+
import {
4+
assert,
5+
assertThrows,
6+
assertEquals,
7+
assertStringContains,
8+
unitTest,
9+
} from "./test_util.ts";
10+
11+
[expect]
12+
import {
13+
assert,
14+
assertEquals,
15+
assertStringContains,
16+
assertThrows,
17+
unitTest,
18+
} from "./test_util.ts";

0 commit comments

Comments
 (0)