Skip to content

Commit

Permalink
fix formatting for typescript Enum with computed members (#12930)
Browse files Browse the repository at this point in the history
* fix formatting for typescript Enum with computed members

* add change log

* remove extra examples in change log
  • Loading branch information
HosokawaR committed May 27, 2022
1 parent 3147244 commit a089819
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
19 changes: 19 additions & 0 deletions changelog_unreleased/typescript/12930.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#### fix formatting for typescript Enum with computed members (#12930 by @HosokawaR)

<!-- prettier-ignore -->
```tsx
// Input
enum A {
[i++],
}

// Prettier stable
enum A {
i++,
}

// Prettier main
enum A {
[i++],
}
```
7 changes: 6 additions & 1 deletion src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ function printTypescript(path, options, print) {

return parts;
case "TSEnumMember":
parts.push(print("id"));
if (node.computed) {
parts.push("[", print("id"), "]");
} else {
parts.push(print("id"));
}

if (node.initializer) {
parts.push(" = ", print("initializer"));
}
Expand Down
48 changes: 48 additions & 0 deletions tests/format/typescript/enum/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`computed-members.ts [babel-ts] format 1`] = `
"Unexpected token (2:3)
1 | enum A {
> 2 | [i++],
| ^
3 | }
4 |
5 | const bar = "bar""
`;

exports[`computed-members.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
enum A {
[i++],
}
const bar = "bar"
enum B {
[bar] = 2,
}
const foo = () => "foo";
enum C {
[foo()] = 2,
}
=====================================output=====================================
enum A {
[i++],
}
const bar = "bar";
enum B {
[bar] = 2,
}
const foo = () => "foo";
enum C {
[foo()] = 2,
}
================================================================================
`;

exports[`enum.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
13 changes: 13 additions & 0 deletions tests/format/typescript/enum/computed-members.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum A {
[i++],
}

const bar = "bar"
enum B {
[bar] = 2,
}

const foo = () => "foo";
enum C {
[foo()] = 2,
}
6 changes: 5 additions & 1 deletion tests/format/typescript/enum/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
run_spec(__dirname, ["typescript"]);
run_spec(__dirname, ["typescript"], {
errors: {
"babel-ts": ["computed-members.ts"],
},
});

0 comments on commit a089819

Please sign in to comment.