Skip to content

Commit

Permalink
Support typescript 4.3 via babel (#10811)
Browse files Browse the repository at this point in the history
* Suppor type members get/set

* Add tests for static index signature in class

* Support override modifier in class

* Add changelog
  • Loading branch information
sosukesuzuki committed May 5, 2021
1 parent edc7a30 commit 711c6d0
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
26 changes: 26 additions & 0 deletions changelog_unreleased/typescript/10811.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#### Support TypeScript 4.3 via `babel-ts` (#10811 by @sosukesuzuki)

##### [`override` modifiers in class elements](https://devblogs.microsoft.com/typescript/announcing-typescript-4-3-beta/#override-and-the-noimplicitoverride-flag)

```ts
class Foo extends {
override method() {}
}
```

##### [static index signatures (`[key: KeyType]: ValueType`) in classes](https://devblogs.microsoft.com/typescript/announcing-typescript-4-3-beta/#static-index-signatures)

```ts
class Foo {
static [key: string]: Bar;
}
```

##### [`get` / `set` in type declarations](https://devblogs.microsoft.com/typescript/announcing-typescript-4-3-beta/#separate-write-types-on-properties)

```ts
interface Foo {
set foo(value);
get foo(): string;
}
```
6 changes: 6 additions & 0 deletions src/language-js/print/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ function printClassMethod(path, options, print) {
if (node.static) {
parts.push("static ");
}
if (node.override) {
parts.push("override ");
}
if (node.type === "TSAbstractMethodDefinition" || node.abstract) {
parts.push("abstract ");
}
Expand All @@ -195,6 +198,9 @@ function printClassProperty(path, options, print) {
if (node.static) {
parts.push("static ");
}
if (node.override) {
parts.push("override ");
}
if (node.type === "TSAbstractClassProperty" || node.abstract) {
parts.push("abstract ");
}
Expand Down
2 changes: 2 additions & 0 deletions src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,10 @@ function printTypescript(path, options, print) {
);
}
case "TSMethodSignature": {
const kind = node.kind && node.kind !== "method" ? `${node.kind} ` : "";
parts.push(
node.accessibility ? [node.accessibility, " "] : "",
kind,
node.export ? "export " : "",
node.static ? "static " : "",
node.readonly ? "readonly " : "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,68 @@ class Bar {
================================================================================
`;

exports[`ts-4.3-override-modifier.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
printWidth: 80
| printWidth
=====================================input======================================
class MyClass extends BaseClass {
override show() {}
public override show() {}
override size = 5;
override readonly size = 5;
}
=====================================output=====================================
class MyClass extends BaseClass {
override show() {}
public override show() {}
override size = 5;
override readonly size = 5;
}
================================================================================
`;

exports[`ts4.3-type-members-get-set.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
printWidth: 80
| printWidth
=====================================input======================================
interface Foo {
get foo(): string;
set bar(v);
}
type Foo = {
get foo(): string;
set bar(v);
}
interface Foo {
set bar(foo: string);
}
=====================================output=====================================
interface Foo {
get foo(): string;
set bar(v);
}
type Foo = {
get foo(): string;
set bar(v);
};
interface Foo {
set bar(foo: string);
}
================================================================================
`;

exports[`tuple-labeled-ts.ts format 1`] = `
====================================options=====================================
parsers: ["babel-ts"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MyClass extends BaseClass {
override show() {}
public override show() {}
override size = 5;
override readonly size = 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Foo {
get foo(): string;
set bar(v);
}

type Foo = {
get foo(): string;
set bar(v);
}

interface Foo {
set bar(foo: string);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ class LocalStorage implements Storage {
================================================================================
`;

exports[`static.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class Foo {
static [value: string]: Type;
}
=====================================output=====================================
class Foo {
static [value: string]: Type;
}
================================================================================
`;
3 changes: 3 additions & 0 deletions tests/format/typescript/index-signature/static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
static [value: string]: Type;
}

0 comments on commit 711c6d0

Please sign in to comment.