Skip to content

Commit

Permalink
feat(prettier): improve TSInterfaceDeclaration (#5817)
Browse files Browse the repository at this point in the history
last one for today :)

---------

Co-authored-by: Don Isaac <donald.isaac@gmail.com>
  • Loading branch information
Sysix and DonIsaac authored Sep 17, 2024
1 parent f942485 commit 18e4ac2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 36 additions & 0 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,40 @@ impl<'a> Format<'a> for JSDocUnknownType {
impl<'a> Format<'a> for TSInterfaceDeclaration<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();

if self.declare {
parts.push(ss!("declare "));
}

parts.push(ss!("interface "));
parts.push(format!(p, self.id));
parts.push(space!());

if let Some(extends) = &self.extends {
if extends.len() > 0 {
let mut extends_parts = p.vec();
let mut display_comma = false;

extends_parts.push(ss!("extends "));

for extend in extends {
if display_comma {
extends_parts.push(ss!(", "));
} else {
display_comma = true;
}

extends_parts.push(extend.expression.format(p));
if let Some(type_parameters) = &extend.type_parameters {
extends_parts.push(type_parameters.format(p));
}
}

parts.extend(extends_parts);
parts.push(space!());
}
}

parts.push(ss!("{"));
if self.body.body.len() > 0 {
let mut indent_parts = p.vec();
Expand Down Expand Up @@ -2379,6 +2410,11 @@ impl<'a> Format<'a> for BindingPattern<'a> {
BindingPatternKind::ArrayPattern(ref pattern) => pattern.format(p),
BindingPatternKind::AssignmentPattern(ref pattern) => pattern.format(p),
});

if self.optional {
parts.push(ss!("?"));
}

if let Some(typ) = &self.type_annotation {
parts.push(array![p, ss!(": "), typ.type_annotation.format(p)]);
}
Expand Down
10 changes: 1 addition & 9 deletions tasks/prettier_conformance/prettier.ts.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ts compatibility: 89/526 (16.92%)
ts compatibility: 95/526 (18.06%)

# Failed

Expand Down Expand Up @@ -84,7 +84,6 @@ ts compatibility: 89/526 (16.92%)
* class/optional.ts
* class/parameter-properties.ts
* class/quoted-property.ts
* class/standard_private_fields.ts

### class-comment
* class-comment/class-implements.ts
Expand Down Expand Up @@ -120,7 +119,6 @@ ts compatibility: 89/526 (16.92%)
* comments-2/last-arg.ts

### compiler
* compiler/anyIsAssignableToObject.ts
* compiler/castOfAwait.ts
* compiler/castParentheses.ts
* compiler/castTest.ts
Expand Down Expand Up @@ -392,9 +390,6 @@ ts compatibility: 89/526 (16.92%)
* definite/definite.ts
* definite/without-annotation.ts

### destructuring
* destructuring/destructuring.ts

### end-of-line
* end-of-line/multiline.ts

Expand Down Expand Up @@ -463,15 +458,13 @@ ts compatibility: 89/526 (16.92%)
* interface/generic.ts
* interface/ignore.ts
* interface/long-extends.ts
* interface/pattern-parameters.ts

### interface/long-type-parameters
* interface/long-type-parameters/long-type-parameters.ts

### interface2
* interface2/comments-declare.ts
* interface2/comments.ts
* interface2/module.ts

### interface2/break
* interface2/break/break.ts
Expand Down Expand Up @@ -666,7 +659,6 @@ ts compatibility: 89/526 (16.92%)
* type-alias/conditional.ts
* type-alias/issue-100857.ts
* type-alias/issue-9874.ts
* type-alias/pattern-parameter.ts

### type-arguments-bit-shift-left-like
* type-arguments-bit-shift-left-like/1.ts
Expand Down

0 comments on commit 18e4ac2

Please sign in to comment.