Skip to content

Commit

Permalink
feat(prettier): support typescript enums (#5790)
Browse files Browse the repository at this point in the history
The last failed test is only because of the comments. the oxc prettier
output is:

```
enum Direction {
  Up = 1,
  Down,
  Left,
  Right,
}

enum FileAccess {
  None,
  Read = // constant members
  1 << 1,
  Write = 1 << 2,
  ReadWrite = Read | Write,
  G = // computed member
  "123".length,
}

enum Empty {}

const enum Enum {
  A = 1,
  B = A * 2,
}
```
Expected output:
https://github.com/prettier/prettier/blob/aa3853b7765645b3f3d8a76e41cf6d70b93c01fd/tests/format/typescript/enum/__snapshots__/format.test.js.snap#L91-L99

Hope you can tell more why this happens :)

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sysix and autofix-ci[bot] authored Sep 16, 2024
1 parent d1d54a6 commit 52c6409
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
50 changes: 49 additions & 1 deletion crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,55 @@ impl<'a> Format<'a> for TSInterfaceDeclaration<'a> {

impl<'a> Format<'a> for TSEnumDeclaration<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
line!()
let mut parts = p.vec();
if self.declare {
parts.push(ss!("declare "));
}
if self.r#const {
parts.push(ss!("const "));
}
parts.push(ss!("enum "));
parts.push(self.id.format(p));
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));
}
parts.push(Doc::Indent(indent_parts));
parts.extend(hardline!());
}
parts.push(ss!("}"));

Doc::Array(parts)
}
}

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

if let Some(initializer) = &self.initializer {
parts.push(ss!(" = "));
parts.push(initializer.format(p));
}

parts.push(ss!(","));

Doc::Array(parts)
}
}

impl<'a> Format<'a> for TSEnumMemberName<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
match self {
TSEnumMemberName::StaticIdentifier(identifier) => identifier.format(p),
TSEnumMemberName::StaticStringLiteral(string_literal) => string_literal.format(p),
TSEnumMemberName::StaticTemplateLiteral(template_literal) => template_literal.format(p),
name => array!(p, ss!("["), name.as_expression().unwrap().format(p), ss!("]")),
}
}
}

Expand Down
11 changes: 1 addition & 10 deletions tasks/prettier_conformance/prettier.ts.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ts compatibility: 50/526 (9.51%)
ts compatibility: 55/526 (10.46%)

# Failed

Expand Down Expand Up @@ -239,12 +239,6 @@ ts compatibility: 50/526 (9.51%)
* conformance/types/any/anyAsConstructor.ts
* conformance/types/any/anyAsGenericFunctionCall.ts

### conformance/types/constKeyword
* conformance/types/constKeyword/constKeyword.ts

### conformance/types/enumDeclaration
* conformance/types/enumDeclaration/enumDeclaration.ts

### conformance/types/firstTypeNode
* conformance/types/firstTypeNode/firstTypeNode.ts

Expand Down Expand Up @@ -393,7 +387,6 @@ ts compatibility: 50/526 (9.51%)
### declare
* declare/declare-get-set-field.ts
* declare/declare_class_fields.ts
* declare/declare_enum.ts
* declare/declare_function.ts
* declare/declare_interface.ts
* declare/declare_module.ts
Expand Down Expand Up @@ -441,9 +434,7 @@ ts compatibility: 50/526 (9.51%)
* end-of-line/multiline.ts

### enum
* enum/computed-members.ts
* enum/enum.ts
* enum/multiline.ts

### error-recovery
* error-recovery/generic.ts
Expand Down

0 comments on commit 52c6409

Please sign in to comment.