Skip to content

Commit 52c6409

Browse files
feat(prettier): support typescript enums (#5790)
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>
1 parent d1d54a6 commit 52c6409

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

crates/oxc_prettier/src/format/mod.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,55 @@ impl<'a> Format<'a> for TSInterfaceDeclaration<'a> {
966966

967967
impl<'a> Format<'a> for TSEnumDeclaration<'a> {
968968
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
969-
line!()
969+
let mut parts = p.vec();
970+
if self.declare {
971+
parts.push(ss!("declare "));
972+
}
973+
if self.r#const {
974+
parts.push(ss!("const "));
975+
}
976+
parts.push(ss!("enum "));
977+
parts.push(self.id.format(p));
978+
parts.push(ss!(" {"));
979+
if self.members.len() > 0 {
980+
let mut indent_parts = p.vec();
981+
for member in &self.members {
982+
indent_parts.extend(hardline!());
983+
indent_parts.push(member.format(p));
984+
}
985+
parts.push(Doc::Indent(indent_parts));
986+
parts.extend(hardline!());
987+
}
988+
parts.push(ss!("}"));
989+
990+
Doc::Array(parts)
991+
}
992+
}
993+
994+
impl<'a> Format<'a> for TSEnumMember<'a> {
995+
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
996+
let mut parts = p.vec();
997+
parts.push(self.id.format(p));
998+
999+
if let Some(initializer) = &self.initializer {
1000+
parts.push(ss!(" = "));
1001+
parts.push(initializer.format(p));
1002+
}
1003+
1004+
parts.push(ss!(","));
1005+
1006+
Doc::Array(parts)
1007+
}
1008+
}
1009+
1010+
impl<'a> Format<'a> for TSEnumMemberName<'a> {
1011+
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
1012+
match self {
1013+
TSEnumMemberName::StaticIdentifier(identifier) => identifier.format(p),
1014+
TSEnumMemberName::StaticStringLiteral(string_literal) => string_literal.format(p),
1015+
TSEnumMemberName::StaticTemplateLiteral(template_literal) => template_literal.format(p),
1016+
name => array!(p, ss!("["), name.as_expression().unwrap().format(p), ss!("]")),
1017+
}
9701018
}
9711019
}
9721020

tasks/prettier_conformance/prettier.ts.snap.md

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 50/526 (9.51%)
1+
ts compatibility: 55/526 (10.46%)
22

33
# Failed
44

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

242-
### conformance/types/constKeyword
243-
* conformance/types/constKeyword/constKeyword.ts
244-
245-
### conformance/types/enumDeclaration
246-
* conformance/types/enumDeclaration/enumDeclaration.ts
247-
248242
### conformance/types/firstTypeNode
249243
* conformance/types/firstTypeNode/firstTypeNode.ts
250244

@@ -393,7 +387,6 @@ ts compatibility: 50/526 (9.51%)
393387
### declare
394388
* declare/declare-get-set-field.ts
395389
* declare/declare_class_fields.ts
396-
* declare/declare_enum.ts
397390
* declare/declare_function.ts
398391
* declare/declare_interface.ts
399392
* declare/declare_module.ts
@@ -441,9 +434,7 @@ ts compatibility: 50/526 (9.51%)
441434
* end-of-line/multiline.ts
442435

443436
### enum
444-
* enum/computed-members.ts
445437
* enum/enum.ts
446-
* enum/multiline.ts
447438

448439
### error-recovery
449440
* error-recovery/generic.ts

0 commit comments

Comments
 (0)