Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/tsv_debug/src/cli/commands/gap_audit_known.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,15 @@ DROPPED >(⟨⟩␣ line
DROPPED >)⟨⟩␣ annotation,jsdoc_cast
DROPPED >,⟨⟩>() annotation,block,jsdoc_cast,multiline
DROPPED >?(⟨⟩IDENT annotation,block,jsdoc_cast,multiline
DROPPED >[⟨⟩]; annotation,block,jsdoc_cast,line,multiline
DROPPED >[⟨⟩]]> annotation,block,jsdoc_cast,line,multiline
DROPPED >}}⟨⟩␣ annotation,block,jsdoc_cast
DROPPED >}⟨⟩>() annotation,block,jsdoc_cast,multiline
DROPPED >⟨⟩( line
DROPPED >⟨⟩(() line
DROPPED >⟨⟩) annotation,jsdoc_cast
DROPPED >⟨⟩,>( annotation,block,jsdoc_cast,multiline
DROPPED >⟨⟩[]; annotation,block,jsdoc_cast
DROPPED >⟨⟩[]] annotation,block,jsdoc_cast
DROPPED >⟨⟩␣ annotation,block,jsdoc_cast,multiline
DROPPED ?(⟨⟩// annotation,block,jsdoc_cast,multiline
Expand Down
4 changes: 2 additions & 2 deletions crates/tsv_ts/src/printer/class_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<'a> Printer<'a> {
) {
value_parts.push(doc);
}
value_parts.push(self.build_type_arguments_doc_wrapping(type_args));
value_parts.push(self.build_type_arguments_doc(type_args));
}
let value_doc = d.concat(&value_parts);
let mut ext_parts = smallvec![d.text("extends")];
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> Printer<'a> {
) {
ext_parts.push(doc);
}
ext_parts.push(self.build_type_arguments_doc_wrapping(type_args));
ext_parts.push(self.build_type_arguments_doc(type_args));
}
Some(d.concat(&ext_parts))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tsv_ts/src/printer/comments/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ impl<'a> Printer<'a> {
) {
h_parts.push(doc);
}
h_parts.push(self.build_type_arguments_doc_wrapping(type_args));
h_parts.push(self.build_type_arguments_doc(type_args));
}
if let Some(next) = items.get(i + 1) {
let item_end = heritage_item_end(heritage);
Expand Down
7 changes: 5 additions & 2 deletions crates/tsv_ts/src/printer/expressions/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ impl<'a> Printer<'a> {
}
}

/// Build a Doc for a conditional expression with wrapping support
pub(in crate::printer) fn build_conditional_doc_with_wrapping(
/// Build a Doc for a conditional expression — the default layout.
///
/// The paired [`Self::build_conditional_doc_with_binary_test_indent`] is the same
/// thing with `indent_binary_test`, which is the only axis between them.
pub(in crate::printer) fn build_conditional_doc(
&self,
cond: &internal::ConditionalExpression<'_>,
) -> DocId {
Expand Down
12 changes: 5 additions & 7 deletions crates/tsv_ts/src/printer/expressions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ impl<'a> Printer<'a> {
self.is_expression_statement.set(was_expr_stmt);
self.build_member_doc(member)
}
Expression::ConditionalExpression(cond) => {
self.build_conditional_doc_with_wrapping(cond)
}
Expression::ConditionalExpression(cond) => self.build_conditional_doc(cond),
Expression::ArrowFunctionExpression(arrow) => self.build_arrow_doc(arrow),
Expression::FunctionExpression(func) => {
self.maybe_wrap_expr_stmt_paren(func.span, self.build_function_doc(func))
Expand Down Expand Up @@ -409,7 +407,7 @@ impl<'a> Printer<'a> {
let type_end = type_assert.type_annotation.span().end;
let expr_start = type_assert.expression.span().start;
let close_angle = self.find_assertion_close_angle(type_end, expr_start);
let type_doc = self.build_type_doc_with_wrapping_type_args(type_assert.type_annotation);
let type_doc = self.build_type_doc(type_assert.type_annotation);

// Comments in the cast stay where the author wrote them. Block comments hug
// inline (`</* c */ T>`, `<T /* c */>`, `<T>/* c */ expr`); a `//` runs to
Expand Down Expand Up @@ -620,7 +618,7 @@ impl<'a> Printer<'a> {
if self.comments_force_own_line_between(kw_end, type_start) {
parts.push(d.text(" "));
parts.push(d.text(keyword));
let type_doc = self.build_type_doc_with_wrapping_type_args(type_annotation);
let type_doc = self.build_type_doc(type_annotation);
self.append_keyword_value_line_comments(&mut parts, kw_end, type_start, type_doc);
return d.concat(&parts);
}
Expand Down Expand Up @@ -663,9 +661,9 @@ impl<'a> Printer<'a> {
CommentSpacing::Trailing,
));
}
parts.push(self.build_type_doc_with_wrapping_type_args(type_annotation));
parts.push(self.build_type_doc(type_annotation));
} else {
parts.push(self.build_type_doc_with_wrapping_type_args(type_annotation));
parts.push(self.build_type_doc(type_annotation));
}

d.concat(&parts)
Expand Down
10 changes: 5 additions & 5 deletions crates/tsv_ts/src/printer/statements/control_flow/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl<'a> Printer<'a> {
/// adjustClause(consequent), // body handling
/// ])
/// ```
fn build_if_statement_with_wrapping_doc(&self, stmt: &internal::IfStatement<'_>) -> DocId {
fn build_if_statement_plain_doc(&self, stmt: &internal::IfStatement<'_>) -> DocId {
let d = self.d();
// Find paren positions for comment handling
let open_paren = self.find_open_paren_after(stmt.span.start);
Expand Down Expand Up @@ -288,19 +288,19 @@ impl<'a> Printer<'a> {
self.has_comments_to_emit_between(consequent_end, alternate_start)
});

// A comment in the consequent→alternate gap is the only axis here; both paths
// group and wrap the condition identically.
if has_if_else_comments {
// Build doc with inline comments between } and else
self.build_if_statement_with_comments_doc(stmt)
} else {
// Delegate to the sophisticated version that handles width-based wrapping
self.build_if_statement_with_wrapping_doc(stmt)
self.build_if_statement_plain_doc(stmt)
}
}

/// Build if statement doc with comments between consequent and alternate
fn build_if_statement_with_comments_doc(&self, stmt: &internal::IfStatement<'_>) -> DocId {
let d = self.d();
// Build condition group (same as build_if_statement_with_wrapping_doc)
// Build condition group (same as build_if_statement_plain_doc)
let open_paren = self.find_open_paren_after(stmt.span.start);
let close_paren = open_paren.and_then(|o| self.matching_close_paren(o));
let if_keyword_end = stmt.span.start + "if".len() as u32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'a> Printer<'a> {
///
/// Matches Prettier's architecture: the condition wraps to multiple lines
/// when the `while (condition)` line exceeds print width.
fn build_while_statement_with_wrapping_doc(
pub(in crate::printer::statements) fn build_while_statement_doc(
&self,
stmt: &internal::WhileStatement<'_>,
) -> DocId {
Expand Down Expand Up @@ -74,14 +74,6 @@ impl<'a> Printer<'a> {
}
}

pub(in crate::printer::statements) fn build_while_statement_doc(
&self,
stmt: &internal::WhileStatement<'_>,
) -> DocId {
// Delegate to the wrapping version for proper condition grouping
self.build_while_statement_with_wrapping_doc(stmt)
}

pub(in crate::printer::statements) fn build_do_while_statement_doc(
&self,
stmt: &internal::DoWhileStatement<'_>,
Expand Down
11 changes: 1 addition & 10 deletions crates/tsv_ts/src/printer/statements/control_flow/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> Printer<'a> {
///
/// Matches Prettier's architecture: the discriminant wraps to multiple lines
/// when the `switch (discriminant) {` line exceeds print width.
fn build_switch_statement_with_wrapping_doc(
pub(in crate::printer::statements) fn build_switch_statement_doc(
&self,
stmt: &internal::SwitchStatement<'_>,
) -> DocId {
Expand Down Expand Up @@ -409,13 +409,4 @@ impl<'a> Printer<'a> {

d.concat(&parts)
}

#[inline]
pub(in crate::printer::statements) fn build_switch_statement_doc(
&self,
stmt: &internal::SwitchStatement<'_>,
) -> DocId {
// Delegate to the wrapping version which handles proper indentation structure
self.build_switch_statement_with_wrapping_doc(stmt)
}
}
2 changes: 1 addition & 1 deletion crates/tsv_ts/src/printer/statements/type_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl<'a> Printer<'a> {
} else if type_has_internal_breaking(value_type) {
// Types with internal breaking (braces, brackets, parens, angle brackets) stay hugged
// Use wrapping version so TypeReference type args break internally when too long
let type_doc = self.build_type_doc_with_wrapping_type_args(value_type);
let type_doc = self.build_type_doc(value_type);
parts.push(d.text(" "));
parts.push(type_doc);
} else if has_complex_params {
Expand Down
5 changes: 2 additions & 3 deletions crates/tsv_ts/src/printer/types/function_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ impl<'a> Printer<'a> {
.as_ref()
.is_some_and(type_args_should_wrap_for_return_type) =>
{
// Use build_type_doc_inner with wrap_type_args=true to enable
// wrapping inside the type reference's type arguments
let type_doc = self.build_type_doc_inner(return_type.type_annotation, true);
// The type reference's own type arguments wrap internally when too wide.
let type_doc = self.build_type_doc(return_type.type_annotation);
joined(d.text(arrow_sp), type_doc)
}
_ => joined(
Expand Down
29 changes: 6 additions & 23 deletions crates/tsv_ts/src/printer/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,12 @@ impl<'a> Printer<'a> {
// Main Type Doc Builders
//

/// Build a Doc for a TypeScript type expression
pub(in crate::printer) fn build_type_doc(&self, ts_type: &TSType<'_>) -> DocId {
self.build_type_doc_inner(ts_type, false)
}

/// Build a Doc for a TypeScript type expression with wrapping type arguments.
/// Build a Doc for a TypeScript type expression.
///
/// Used in type alias RHS where TypeReference type arguments should break
/// internally (e.g., `Promise<LongType | null>` breaks inside `<>`).
pub(in crate::printer) fn build_type_doc_with_wrapping_type_args(
&self,
ts_type: &TSType<'_>,
) -> DocId {
self.build_type_doc_inner(ts_type, true)
}

/// Inner implementation for type doc building.
/// When `wrap_type_args` is true, TypeReference uses wrapping type arguments.
pub(super) fn build_type_doc_inner(&self, ts_type: &TSType<'_>, wrap_type_args: bool) -> DocId {
/// A `TypeReference`'s own type arguments always break internally when too wide
/// (`Promise<LongType | null>` breaks inside the `<>`) — `build_type_arguments_doc` is the
/// single builder for every type-argument position, so no caller has to opt into that.
pub(in crate::printer) fn build_type_doc(&self, ts_type: &TSType<'_>) -> DocId {
let d = self.d();
match ts_type {
TSType::Keyword(kw) => d.text(kw.kind.as_str()),
Expand All @@ -89,11 +76,7 @@ impl<'a> Printer<'a> {
) {
parts.push(doc);
}
if wrap_type_args {
parts.push(self.build_type_arguments_doc_wrapping(type_args));
} else {
parts.push(self.build_type_arguments_doc(type_args));
}
parts.push(self.build_type_arguments_doc(type_args));
}
d.concat(&parts)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/tsv_ts/src/printer/types/type_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl<'a> Printer<'a> {

/// Build type annotation doc with width-aware type argument wrapping.
///
/// For `TypeReference<Args>`, uses `build_type_arguments_doc_wrapping` so
/// type arguments wrap at width boundary.
/// For `TypeReference<Args>`, `build_type_arguments_doc` wraps the type
/// arguments at the width boundary.
///
/// For Union types, uses break-after-colon layout:
/// ```text
Expand Down Expand Up @@ -229,7 +229,7 @@ impl<'a> Printer<'a> {
{
parts.push(name_ta_comments);
}
parts.push(self.build_type_arguments_doc_wrapping(type_args));
parts.push(self.build_type_arguments_doc(type_args));
return d.concat(&parts);
}

Expand Down Expand Up @@ -360,7 +360,7 @@ impl<'a> Printer<'a> {
{
parts.push(comments_doc);
}
parts.push(self.build_type_doc_with_wrapping_type_args(&intersection.types[0]));
parts.push(self.build_type_doc(&intersection.types[0]));
return d.concat(&parts);
}

Expand Down
Loading