From e3fda0bf41868e0722145b0d2f2c9b87175f2880 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Wed, 19 Feb 2020 13:38:34 +0200 Subject: [PATCH 1/6] support TypeScript 3.8 --- tests/typescript_class/standard_private_fields.ts | 11 +++++++++++ tests/typescript_export/export-as-ns.ts | 1 + tests/typescript_import_export/jsfmt.spec.js | 1 + tests/typescript_import_export/type-modifier.ts | 5 +++++ 4 files changed, 18 insertions(+) create mode 100644 tests/typescript_class/standard_private_fields.ts create mode 100644 tests/typescript_export/export-as-ns.ts create mode 100644 tests/typescript_import_export/jsfmt.spec.js create mode 100644 tests/typescript_import_export/type-modifier.ts diff --git a/tests/typescript_class/standard_private_fields.ts b/tests/typescript_class/standard_private_fields.ts new file mode 100644 index 000000000000..30c39cf207f1 --- /dev/null +++ b/tests/typescript_class/standard_private_fields.ts @@ -0,0 +1,11 @@ +class Square { + #sideLength: number; + + constructor(sideLength: number) { + this.#sideLength = sideLength; + } + + equals(other: any) { + return this.#sideLength === other.#sideLength; + } +} diff --git a/tests/typescript_export/export-as-ns.ts b/tests/typescript_export/export-as-ns.ts new file mode 100644 index 000000000000..18be0e6c74af --- /dev/null +++ b/tests/typescript_export/export-as-ns.ts @@ -0,0 +1 @@ +export * as utilities from "./utilities.js"; diff --git a/tests/typescript_import_export/jsfmt.spec.js b/tests/typescript_import_export/jsfmt.spec.js new file mode 100644 index 000000000000..2ea3bb6eb2e4 --- /dev/null +++ b/tests/typescript_import_export/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]); diff --git a/tests/typescript_import_export/type-modifier.ts b/tests/typescript_import_export/type-modifier.ts new file mode 100644 index 000000000000..85727b127fb3 --- /dev/null +++ b/tests/typescript_import_export/type-modifier.ts @@ -0,0 +1,5 @@ +import type { SomeThing } from "./some-module.js"; +export type { SomeThing }; + +import type Foo from "./foo.js"; +export type Foo; From c6607e5ed9098703f9fdd390d0ad29da16e5ebfa Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 24 Feb 2020 16:25:22 +0200 Subject: [PATCH 2/6] Update standard_private_fields.ts --- tests/typescript_class/standard_private_fields.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/typescript_class/standard_private_fields.ts b/tests/typescript_class/standard_private_fields.ts index 30c39cf207f1..0cf9dfb905b6 100644 --- a/tests/typescript_class/standard_private_fields.ts +++ b/tests/typescript_class/standard_private_fields.ts @@ -1,8 +1,10 @@ class Square { #sideLength: number; + readonly #area: number; constructor(sideLength: number) { this.#sideLength = sideLength; + this.#area = this.#sideLength ** 2; } equals(other: any) { From 17b7c58a00692a692b0a2a1cba2c4bfbcb27f58b Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 9 Mar 2020 23:31:42 +0200 Subject: [PATCH 3/6] improve tests for import/export type modifier --- tests/typescript_import_export/type-modifier.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/typescript_import_export/type-modifier.ts b/tests/typescript_import_export/type-modifier.ts index 85727b127fb3..a613d087c00a 100644 --- a/tests/typescript_import_export/type-modifier.ts +++ b/tests/typescript_import_export/type-modifier.ts @@ -1,5 +1,16 @@ -import type { SomeThing } from "./some-module.js"; export type { SomeThing }; +export type { A as B }; +export type { B as C } from './a'; +export type { foo } from 'bar'; +export type * from 'bar'; +export type { foo }; + +// this should be treated as a normal import statement +import type from './foo'; -import type Foo from "./foo.js"; -export type Foo; +import type { SomeThing } from "./some-module.js"; +import type { foo, bar } from 'baz'; +import type { foo as bar } from 'baz'; +import type * as foo from './bar'; +import type foo from 'bar'; +import type foo, { bar } from 'bar'; From a75c8d8d01f07d180e5b7aee327fa3e82d59bfa2 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Wed, 11 Mar 2020 14:39:33 +0200 Subject: [PATCH 4/6] temporary hacks for private fields --- src/language-js/postprocess.js | 19 ++++++++++++++++++- src/language-js/printer-estree.js | 4 ++++ .../standard_private_fields.ts | 10 +++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/language-js/postprocess.js b/src/language-js/postprocess.js index fe82fba30585..91b62cce7b9d 100644 --- a/src/language-js/postprocess.js +++ b/src/language-js/postprocess.js @@ -1,6 +1,9 @@ "use strict"; -const { getLast } = require("../common/util"); +const { + getLast, + getNextNonSpaceNonCommentCharacter +} = require("../common/util"); const { composeLoc, locEnd } = require("./loc"); function postprocess(ast, options) { @@ -48,6 +51,20 @@ function postprocess(ast, options) { node.end = getLast(node.expressions).end; } break; + case "ClassProperty": + // TODO: Temporary auto-generated node type. To remove when typescript-estree has proper support for private fields. + if ( + node.key && + node.key.type === "TSPrivateIdentifier" && + getNextNonSpaceNonCommentCharacter( + options.originalText, + node.key, + locEnd + ) === "?" + ) { + node.optional = true; + } + break; } }); diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 87a825c03666..962b8e2b971a 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3521,6 +3521,10 @@ function printPathNoParens(path, options, print, args) { case "PrivateName": return concat(["#", path.call(print, "id")]); + // TODO: Temporary auto-generated node type. To remove when typescript-estree has proper support for private fields. + case "TSPrivateIdentifier": + return n.escapedText; + case "TSConditionalType": return printTernaryOperator(path, options, print, { beforeParts: () => [ diff --git a/tests/typescript_class/standard_private_fields.ts b/tests/typescript_class/standard_private_fields.ts index 0cf9dfb905b6..2c1c1d43c845 100644 --- a/tests/typescript_class/standard_private_fields.ts +++ b/tests/typescript_class/standard_private_fields.ts @@ -1,13 +1,21 @@ class Square { #sideLength: number; readonly #area: number; + #unit?: string; - constructor(sideLength: number) { + constructor(sideLength: number, unit?: string) { this.#sideLength = sideLength; this.#area = this.#sideLength ** 2; + if (unit) { + this.#unit = unit; + } } equals(other: any) { return this.#sideLength === other.#sideLength; } + + getArea() { + return this.#area + (this.#unit ?? 'px') + '²'; + } } From 305595b88cd033ca9b07bf6c969574d63adeb707 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Wed, 11 Mar 2020 14:41:45 +0200 Subject: [PATCH 5/6] comment out some tests, update snapshots --- .../__snapshots__/jsfmt.spec.js.snap | 54 +++++++++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 16 ++++++ tests/typescript_export/export-as-ns.ts | 3 +- .../__snapshots__/jsfmt.spec.js.snap | 45 ++++++++++++++++ tests/typescript_import_export/jsfmt.spec.js | 3 +- 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/typescript_import_export/__snapshots__/jsfmt.spec.js.snap diff --git a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap index e7fe7830a603..8ff7e937a879 100644 --- a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap @@ -253,3 +253,57 @@ class User { ================================================================================ `; + +exports[`standard_private_fields.ts 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Square { + #sideLength: number; + readonly #area: number; + #unit?: string; + + constructor(sideLength: number, unit?: string) { + this.#sideLength = sideLength; + this.#area = this.#sideLength ** 2; + if (unit) { + this.#unit = unit; + } + } + + equals(other: any) { + return this.#sideLength === other.#sideLength; + } + + getArea() { + return this.#area + (this.#unit ?? 'px') + '²'; + } +} + +=====================================output===================================== +class Square { + #sideLength: number; + readonly #area: number; + #unit?: string; + + constructor(sideLength: number, unit?: string) { + this.#sideLength = sideLength; + this.#area = this.#sideLength ** 2; + if (unit) { + this.#unit = unit; + } + } + + equals(other: any) { + return this.#sideLength === other.#sideLength; + } + + getArea() { + return this.#area + (this.#unit ?? "px") + "²"; + } +} + +================================================================================ +`; diff --git a/tests/typescript_export/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_export/__snapshots__/jsfmt.spec.js.snap index b70c4f989347..e12dad1d3bfd 100644 --- a/tests/typescript_export/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_export/__snapshots__/jsfmt.spec.js.snap @@ -60,6 +60,22 @@ declare module "hello" { ================================================================================ `; +exports[`export-as-ns.ts 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// TODO: uncomment when typescript-estree gets support for this syntax +// export * as utilities from "./utilities.js"; + +=====================================output===================================== +// TODO: uncomment when typescript-estree gets support for this syntax +// export * as utilities from "./utilities.js"; + +================================================================================ +`; + exports[`export-class.js 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript_export/export-as-ns.ts b/tests/typescript_export/export-as-ns.ts index 18be0e6c74af..48bd11f9e57b 100644 --- a/tests/typescript_export/export-as-ns.ts +++ b/tests/typescript_export/export-as-ns.ts @@ -1 +1,2 @@ -export * as utilities from "./utilities.js"; +// TODO: uncomment when typescript-estree gets support for this syntax +// export * as utilities from "./utilities.js"; diff --git a/tests/typescript_import_export/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_import_export/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..47d3492b9b50 --- /dev/null +++ b/tests/typescript_import_export/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,45 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`type-modifier.ts 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +export type { SomeThing }; +export type { A as B }; +export type { B as C } from './a'; +export type { foo } from 'bar'; +export type * from 'bar'; +export type { foo }; + +// this should be treated as a normal import statement +import type from './foo'; + +import type { SomeThing } from "./some-module.js"; +import type { foo, bar } from 'baz'; +import type { foo as bar } from 'baz'; +import type * as foo from './bar'; +import type foo from 'bar'; +import type foo, { bar } from 'bar'; + +=====================================output===================================== +export type { SomeThing }; +export type { A as B }; +export type { B as C } from "./a"; +export type { foo } from "bar"; +export type * from "bar"; +export type { foo }; + +// this should be treated as a normal import statement +import type from "./foo"; + +import type { SomeThing } from "./some-module.js"; +import type { foo, bar } from "baz"; +import type { foo as bar } from "baz"; +import type * as foo from "./bar"; +import type foo from "bar"; +import type foo, { bar } from "bar"; + +================================================================================ +`; diff --git a/tests/typescript_import_export/jsfmt.spec.js b/tests/typescript_import_export/jsfmt.spec.js index 2ea3bb6eb2e4..34992613347d 100644 --- a/tests/typescript_import_export/jsfmt.spec.js +++ b/tests/typescript_import_export/jsfmt.spec.js @@ -1 +1,2 @@ -run_spec(__dirname, ["typescript"]); +// TODO: remove disableBabelTS when Babel's TS plugin gets support for import/export `type` modifier +run_spec(__dirname, ["typescript"], { disableBabelTS: true }); From 41911af745fbc2dfb9d1be0b7146f4c5f8b4f4c5 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Wed, 11 Mar 2020 15:15:47 +0200 Subject: [PATCH 6/6] add changelog entry --- changelog_unreleased/typescript/pr-7631.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 changelog_unreleased/typescript/pr-7631.md diff --git a/changelog_unreleased/typescript/pr-7631.md b/changelog_unreleased/typescript/pr-7631.md new file mode 100644 index 000000000000..a7b9b5047dde --- /dev/null +++ b/changelog_unreleased/typescript/pr-7631.md @@ -0,0 +1,9 @@ +#### TypeScript 3.8 ([#7631](https://github.com/prettier/prettier/pull/7631) by [@thorn0](https://github.com/thorn0)) + +Prettier doesn't yet fully support the new syntax added in TypeScript 3.8: + +| Syntax | `typescript` parser | `babel-ts` parser | +| ------------------------------------------------------------------------------------------------------------------------------- | ------------------- | ----------------- | +| [Type-Only Imports and Exports](https://devblogs.microsoft.com/typescript/announcing-typescript-3-8/#type-only-imports-exports) | ✔️ | ❌ | +| [ECMAScript Private Fields](https://devblogs.microsoft.com/typescript/announcing-typescript-3-8/#ecmascript-private-fields) | ✔️ | ✔️ | +| [`export * as ns`](https://devblogs.microsoft.com/typescript/announcing-typescript-3-8/#export-star-as-namespace-syntax) | ❌ | ❌ |