Skip to content

Commit

Permalink
fix(prettier): handle TSTypeLiteral as an object (#5946)
Browse files Browse the repository at this point in the history
- [x] semi is optional with the right configuration
  • Loading branch information
Sysix authored Sep 21, 2024
1 parent f4aefb5 commit 463c24e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
22 changes: 1 addition & 21 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,27 +1028,7 @@ impl<'a> Format<'a> for TSTupleType<'a> {

impl<'a> Format<'a> for TSTypeLiteral<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();

parts.push(ss!("{"));
if self.members.len() > 0 {
let mut indent_parts = p.vec();

for member in &self.members {
indent_parts.extend(hardline!());
indent_parts.push(member.format(p));

if let Some(semi) = p.semi() {
indent_parts.push(semi);
}
}

parts.push(Doc::Indent(indent_parts));
parts.extend(hardline!());
}
parts.push(ss!("}"));

Doc::Array(parts)
object::print_object_properties(p, ObjectLike::TSTypeLiteral(self))
}
}

Expand Down
45 changes: 37 additions & 8 deletions crates/oxc_prettier/src/format/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{
ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern, WithClause},
ast::{ObjectAssignmentTarget, ObjectExpression, ObjectPattern, TSTypeLiteral, WithClause},
AstKind,
};
use oxc_span::Span;
Expand All @@ -16,6 +16,7 @@ pub enum ObjectLike<'a, 'b> {
AssignmentTarget(&'b ObjectAssignmentTarget<'a>),
Pattern(&'b ObjectPattern<'a>),
WithClause(&'b WithClause<'a>),
TSTypeLiteral(&'b TSTypeLiteral<'a>),
}

impl<'a, 'b> ObjectLike<'a, 'b> {
Expand All @@ -25,6 +26,7 @@ impl<'a, 'b> ObjectLike<'a, 'b> {
Self::AssignmentTarget(target) => target.properties.len(),
Self::Pattern(object) => object.properties.len(),
Self::WithClause(attributes) => attributes.with_entries.len(),
Self::TSTypeLiteral(literal) => literal.members.len(),
}
}

Expand All @@ -33,7 +35,7 @@ impl<'a, 'b> ObjectLike<'a, 'b> {
Self::Expression(expr) => false,
Self::AssignmentTarget(target) => target.rest.is_some(),
Self::Pattern(object) => object.rest.is_some(),
Self::WithClause(_) => false,
Self::WithClause(_) | Self::TSTypeLiteral(_) => false,
}
}

Expand All @@ -43,6 +45,7 @@ impl<'a, 'b> ObjectLike<'a, 'b> {
Self::AssignmentTarget(object) => object.is_empty(),
Self::Pattern(object) => object.is_empty(),
Self::WithClause(attributes) => attributes.with_entries.is_empty(),
Self::TSTypeLiteral(literal) => literal.members.is_empty(),
}
}

Expand All @@ -56,6 +59,7 @@ impl<'a, 'b> ObjectLike<'a, 'b> {
Self::AssignmentTarget(object) => object.span,
Self::Pattern(object) => object.span,
Self::WithClause(attributes) => attributes.span,
Self::TSTypeLiteral(literal) => literal.span,
}
}

Expand All @@ -71,6 +75,22 @@ impl<'a, 'b> ObjectLike<'a, 'b> {
Self::WithClause(attributes) => {
Box::new(attributes.with_entries.iter().map(|entry| entry.format(p)))
}
Self::TSTypeLiteral(literal) => {
Box::new(literal.members.iter().map(|member| member.format(p)))
}
}
}

fn member_separator(self, p: &'b Prettier<'a>) -> &'a str {
match self {
Self::TSTypeLiteral(_) => {
if p.semi().is_some() {
";"
} else {
""
}
}
_ => ",",
}
}
}
Expand All @@ -83,6 +103,7 @@ pub(super) fn print_object_properties<'a>(
let right_brace = ss!("}");

let should_break = false;
let member_separator = object.member_separator(p);

let content = if object.is_empty() {
group![p, left_brace, softline!(), right_brace]
Expand All @@ -93,17 +114,21 @@ pub(super) fn print_object_properties<'a>(
let len = object.len();
let has_rest = object.has_rest();
let mut indent_parts = p.vec();

indent_parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
for (i, doc) in object.iter(p).enumerate() {
indent_parts.push(doc);
if i == len - 1 && !has_rest {
break;
}
indent_parts.push(ss!(","));

indent_parts.push(ss!(member_separator));
indent_parts.push(line!());
}
match object {
ObjectLike::Expression(_) | ObjectLike::WithClause(_) => {}
ObjectLike::Expression(_)
| ObjectLike::WithClause(_)
| ObjectLike::TSTypeLiteral(_) => {}
ObjectLike::AssignmentTarget(target) => {
if let Some(rest) = &target.rest {
indent_parts.push(rest.format(p));
Expand All @@ -119,17 +144,21 @@ pub(super) fn print_object_properties<'a>(
}));
if p.should_print_es5_comma()
&& match object {
ObjectLike::Expression(expr) => true,
ObjectLike::AssignmentTarget(target) => true,
ObjectLike::Pattern(pattern) => pattern.rest.is_none(),
ObjectLike::WithClause(_) => true,
_ => true,
}
{
parts.push(if_break!(p, ",", "", None));
parts.push(if_break!(p, member_separator, "", None));
}
parts.push(if p.options.bracket_spacing { line!() } else { softline!() });
parts.push(ss!("}"));

if matches!(p.current_kind(), AstKind::Program(_)) {
let should_break =
misc::has_new_line_in_range(p.source_text, object.span().start, object.span().end);
return Doc::Group(Group::new(parts).with_break(should_break));
}

let parent_kind = p.parent_kind();
if (object.is_object_pattern() && should_hug_the_only_parameter(p, parent_kind))
|| (!should_break
Expand Down
16 changes: 1 addition & 15 deletions tasks/prettier_conformance/snapshots/prettier.ts.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ts compatibility: 177/526 (33.65%)
ts compatibility: 185/526 (35.17%)

# Failed

Expand Down Expand Up @@ -45,7 +45,6 @@ ts compatibility: 177/526 (33.65%)
* assignment/issue-10848.tsx
* assignment/issue-10850.ts
* assignment/issue-2482.ts
* assignment/issue-3122.ts
* assignment/parenthesized.ts

### call-signature
Expand Down Expand Up @@ -165,9 +164,6 @@ ts compatibility: 177/526 (33.65%)
### conformance/expressions/functionCalls
* conformance/expressions/functionCalls/callWithSpreadES6.ts

### conformance/interfaces/interfaceDeclarations
* conformance/interfaces/interfaceDeclarations/interfaceWithMultipleBaseTypes2.ts

### conformance/internalModules/importDeclarations
* conformance/internalModules/importDeclarations/circularImportAlias.ts
* conformance/internalModules/importDeclarations/exportImportAlias.ts
Expand All @@ -194,9 +190,6 @@ ts compatibility: 177/526 (33.65%)
* conformance/types/functions/functionOverloadErrorsSyntax.ts
* conformance/types/functions/parameterInitializersForwardReferencing.ts

### conformance/types/intersectionType
* conformance/types/intersectionType/intersectionType.ts

### conformance/types/mappedType
* conformance/types/mappedType/mappedType.ts

Expand Down Expand Up @@ -423,9 +416,6 @@ ts compatibility: 177/526 (33.65%)

### method
* method/issue-10352-consistency.ts
* method/method-signature.ts
* method/semi.ts
* method/type_literal_optional_method.ts

### method-chain
* method-chain/comment.ts
Expand Down Expand Up @@ -461,10 +451,6 @@ ts compatibility: 177/526 (33.65%)
### optional-type
* optional-type/complex.ts

### optional-variance
* optional-variance/basic.ts
* optional-variance/with-jsx.tsx

### prettier-ignore
* prettier-ignore/issue-14238.ts
* prettier-ignore/mapped-types.ts
Expand Down

0 comments on commit 463c24e

Please sign in to comment.