Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print as instead of : for babel-ts parser #12706

Merged
merged 6 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog_unreleased/typescript/12706.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#### Print `as` instead of `:` for `babel-ts` parser (#12706 by @HosokawaR)

<!-- prettier-ignore -->
```tsx
// Input
[x as any] = x;

// Prettier stable
[x: any] = x;

// Prettier main
[x as any] = x;
```
5 changes: 5 additions & 0 deletions src/language-js/print/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ function printTypeAnnotation(path, options, print) {

const parentNode = path.getParentNode();

// Workaround for https://github.com/babel/babel/issues/14498
if (parentNode.type === "ArrayPattern" && options.parser === "babel-ts") {
return [" as ", print("typeAnnotation")];
}
Comment on lines +59 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should fix this in babel-parser side, but this seems to be work well as temporary fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been reported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const isFunctionDeclarationIdentifier =
parentNode.type === "DeclareFunction" && parentNode.id === node;

Expand Down
14 changes: 14 additions & 0 deletions tests/format/typescript/as/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`array-pattern.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
[x as any] = x;

=====================================output=====================================
[x as any] = x;

================================================================================
`;

exports[`as.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
1 change: 1 addition & 0 deletions tests/format/typescript/as/array-pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[x as any] = x;