Skip to content

Commit

Permalink
Support TSNamedTupleMember
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Aug 3, 2020
1 parent 8f58bff commit 7fe8067
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
18 changes: 18 additions & 0 deletions changelog_unreleased/typescript/pr-8885.md
@@ -0,0 +1,18 @@
#### Support `Labeled Tuple Elements` ([#8885](https://github.com/prettier/prettier/pull/8885) by [@fisker](https://github.com/fisker))

[`Labeled Tuple Elements`](https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#labeled-tuple-elements)

_Only works for `babel-ts` parser_

<!-- prettier-ignore -->
```ts
// Input
type Range = [start: number, end: number];

// Prettier stable
SyntaxError: Unexpected token, expected "," (1:20)
> 1 | type Range = [start: number, end: number];

// Prettier master
type Range = [start: number, end: number];
```
7 changes: 7 additions & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -2314,6 +2314,13 @@ function printPathNoParens(path, options, print, args) {

/* istanbul ignore next */
return "";
case "TSNamedTupleMember":
return concat([
path.call(print, "label"),
n.optional ? "?" : "",
": ",
path.call(print, "elementType"),
]);
case "TSTupleType":
case "TupleTypeAnnotation": {
const typesField = n.type === "TSTupleType" ? "elementTypes" : "types";
Expand Down
57 changes: 57 additions & 0 deletions tests/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`tuple-labeled.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
printWidth: 80
| printWidth
=====================================input======================================
// https://github.com/babel/babel/pull/11754
// TODO: move this file to \`test/typescript/tuple\`
type T = [x: A, y?: B, ...z: C];
type T = [A, y: B];
let x: [A: string, ...B: number[]]
type T = [foo: string, bar?: number];
type T = [x?: A, y: B];
type T = [x: A, ...B];
type T = [...B, x: A];
// Invalid label 1
type T = [x.y: A];
// Invalid label 2
type T = [x<y>: A];
=====================================output=====================================
// https://github.com/babel/babel/pull/11754
// TODO: move this file to \`test/typescript/tuple\`
type T = [x: A, y?: B, ...z: C];
type T = [A, y: B];
let x: [A: string, ...B: number[]];
type T = [foo: string, bar?: number];
type T = [x?: A, y: B];
type T = [x: A, ...B];
type T = [...B, x: A];
// Invalid label 1
type T = [x.y: A];
// Invalid label 2
type T = [x<y>: A];
================================================================================
`;
1 change: 1 addition & 0 deletions tests/misc/typescript-babel-only/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["babel-ts"]);
22 changes: 22 additions & 0 deletions tests/misc/typescript-babel-only/tuple-labeled.ts
@@ -0,0 +1,22 @@
// https://github.com/babel/babel/pull/11754
// TODO: move this file to `test/typescript/tuple`

type T = [x: A, y?: B, ...z: C];

type T = [A, y: B];

let x: [A: string, ...B: number[]]

type T = [foo: string, bar?: number];

type T = [x?: A, y: B];

type T = [x: A, ...B];

type T = [...B, x: A];

// Invalid label 1
type T = [x.y: A];

// Invalid label 2
type T = [x<y>: A];

0 comments on commit 7fe8067

Please sign in to comment.