From b8f2b084e279240b0fd7fb1f25cddfb8fbf0b663 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 28 May 2022 03:37:30 +0900 Subject: [PATCH] Add tests for TS 4.7 features (#12924) * Add tests for instantiation expressions * Add tests for infer extends * Add tests for optional variance * Update changelog --- changelog_unreleased/typescript/12896.md | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 284 +---------------- .../ts-4.7-instantiation-expression.ts | 6 - .../__snapshots__/jsfmt.spec.js.snap | 35 +++ .../infer-extends/basic.ts} | 0 .../typescript/infer-extends/jsfmt.spec.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 32 ++ .../instantiation-expression/basic.ts | 2 + .../instantiation-expression/new.ts | 2 + .../__snapshots__/jsfmt.spec.js.snap | 285 ++++++++++++++++++ .../optional-variance/basic.ts} | 0 .../optional-variance/jsfmt.spec.js | 3 + .../optional-variance/with-jsx.tsx} | 0 13 files changed, 369 insertions(+), 283 deletions(-) create mode 100644 tests/format/typescript/infer-extends/__snapshots__/jsfmt.spec.js.snap rename tests/format/{misc/typescript-babel-only/ts-4.7-infer-extends.ts => typescript/infer-extends/basic.ts} (100%) create mode 100644 tests/format/typescript/infer-extends/jsfmt.spec.js create mode 100644 tests/format/typescript/instantiation-expression/basic.ts create mode 100644 tests/format/typescript/instantiation-expression/new.ts create mode 100644 tests/format/typescript/optional-variance/__snapshots__/jsfmt.spec.js.snap rename tests/format/{misc/typescript-babel-only/ts-4.7-optional-variance.ts => typescript/optional-variance/basic.ts} (100%) create mode 100644 tests/format/typescript/optional-variance/jsfmt.spec.js rename tests/format/{misc/typescript-babel-only/ts-4.7-optional-variance-with-jsx.tsx => typescript/optional-variance/with-jsx.tsx} (100%) diff --git a/changelog_unreleased/typescript/12896.md b/changelog_unreleased/typescript/12896.md index 263f0e5c642f..d1b07bd0dcc4 100644 --- a/changelog_unreleased/typescript/12896.md +++ b/changelog_unreleased/typescript/12896.md @@ -1,4 +1,4 @@ -#### [HIGHLIGHT]Support TypeScript 4.7 (#12896, #12897, #12898, #12900 by @sosukesuzuki) +#### [HIGHLIGHT]Support TypeScript 4.7 (#12896, #12897, #12898, #12900, #12921, #12924 by @sosukesuzuki) Support [TypeScript 4.7](https://devblogs.microsoft.com/typescript/announcing-typescript-4-7) new features! diff --git a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap index 17a0b9c495b3..469a07a78b7b 100644 --- a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`declare-index-signature.ts format 1`] = ` +exports["declare-index-signature.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -20,7 +20,7 @@ class C { ================================================================================ `; -exports[`invalid-modifiers.ts format 1`] = ` +exports["invalid-modifiers.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -60,7 +60,7 @@ class Bar { ================================================================================ `; -exports[`parenthesized-decorators-call-expression.ts format 1`] = ` +exports["parenthesized-decorators-call-expression.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -76,7 +76,7 @@ class X {} ================================================================================ `; -exports[`parenthesized-decorators-tagged-template.ts format 1`] = ` +exports["parenthesized-decorators-tagged-template.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -96,7 +96,7 @@ class Test { ================================================================================ `; -exports[`prettier-ignore-nested-unions.ts format 1`] = ` +exports["prettier-ignore-nested-unions.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -162,7 +162,7 @@ export type b = ================================================================================ `; -exports[`prettier-ignore-parenthesized-type.ts format 1`] = ` +exports["prettier-ignore-parenthesized-type.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 @@ -184,291 +184,23 @@ type Foo = ================================================================================ `; -exports[`ts-4.7-infer-extends.ts format 1`] = ` +exports["ts-4.7-instantiation-expression.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 | printWidth =====================================input====================================== -type X3 = T extends [infer U extends number] ? MustBeNumber : never; -type X4 = T extends [infer U extends number, infer U extends number] ? MustBeNumber : never; -type X5 = T extends [infer U extends number, infer U] ? MustBeNumber : never; -type X6 = T extends [infer U, infer U extends number] ? MustBeNumber : never; -type X7 = T extends [infer U extends string, infer U extends number] ? U : never; -type X8 = T extends infer U extends number ? U : T; -type X9 = T extends (infer U extends number ? U : T) ? U : T; - -=====================================output===================================== -type X3 = T extends [infer U extends number] ? MustBeNumber : never; -type X4 = T extends [infer U extends number, infer U extends number] - ? MustBeNumber - : never; -type X5 = T extends [infer U extends number, infer U] - ? MustBeNumber - : never; -type X6 = T extends [infer U, infer U extends number] - ? MustBeNumber - : never; -type X7 = T extends [infer U extends string, infer U extends number] - ? U - : never; -type X8 = T extends infer U extends number ? U : T; -type X9 = T extends (infer U extends number ? U : T) ? U : T; - -================================================================================ -`; - -exports[`ts-4.7-instantiation-expression.ts format 1`] = ` -====================================options===================================== -parsers: ["babel-ts"] -printWidth: 80 - | printWidth -=====================================input====================================== -// basic -const foo = bar; - // typeof let x: typeof y.z; -// new -new A; - =====================================output===================================== -// basic -const foo = bar; - // typeof let x: typeof y.z; -// new -new A(); - -================================================================================ -`; - -exports[`ts-4.7-optional-variance.ts format 1`] = ` -====================================options===================================== -parsers: ["babel-ts"] -printWidth: 80 - | printWidth -=====================================input====================================== -type Covariant = { - x: T; -} -type Contravariant = { - f: (x: T) => void; -} -type Invariant = { - f: (x: T) => T; -} -type T10 = T; -type T11 = keyof T; -type T12 = T[K]; -type T13 = T[keyof T]; - -type Covariant1 = { - x: T; -} - -type Contravariant1 = keyof T; - -type Contravariant2 = { - f: (x: T) => void; -} - -type Invariant1 = { - f: (x: T) => T; -} - -type Invariant2 = { - f: (x: T) => T; -} -type Foo1 = { - x: T; - f: FooFn1; -} - -type Foo2 = { - x: T; - f: FooFn2; -} - -type Foo3 = { - x: T; - f: FooFn3; -} - -type T21 = T; - -interface Baz {} -interface Baz {} - -interface Parent { - child: Child | null; - parent: Parent | null; -} - -declare class StateNode { - _storedEvent: TEvent; - _action: ActionObject; - _state: StateNode; -} - -=====================================output===================================== -type Covariant = { x: T }; -type Contravariant = { f: (x: T) => void }; -type Invariant = { f: (x: T) => T }; -type T10 = T; -type T11 = keyof T; -type T12 = T[K]; -type T13 = T[keyof T]; - -type Covariant1 = { x: T }; - -type Contravariant1 = keyof T; - -type Contravariant2 = { f: (x: T) => void }; - -type Invariant1 = { f: (x: T) => T }; - -type Invariant2 = { f: (x: T) => T }; -type Foo1 = { x: T; f: FooFn1 }; - -type Foo2 = { x: T; f: FooFn2 }; - -type Foo3 = { x: T; f: FooFn3 }; - -type T21 = T; - -interface Baz {} -interface Baz {} - -interface Parent { child: Child | null; parent: Parent | null } - -declare class StateNode { - _storedEvent: TEvent; - _action: ActionObject; - _state: StateNode; -} - -================================================================================ -`; - -exports[`ts-4.7-optional-variance-with-jsx.tsx format 1`] = ` -====================================options===================================== -parsers: ["babel-ts"] -printWidth: 80 - | printWidth -=====================================input====================================== -// valid JSX -() => {}; - -type Covariant = { - x: T; -} -type Contravariant = { - f: (x: T) => void; -} -type Invariant = { - f: (x: T) => T; -} -type T10 = T; -type T11 = keyof T; -type T12 = T[K]; -type T13 = T[keyof T]; - -type Covariant1 = { - x: T; -} - -type Contravariant1 = keyof T; - -type Contravariant2 = { - f: (x: T) => void; -} - -type Invariant1 = { - f: (x: T) => T; -} - -type Invariant2 = { - f: (x: T) => T; -} -type Foo1 = { - x: T; - f: FooFn1; -} - -type Foo2 = { - x: T; - f: FooFn2; -} - -type Foo3 = { - x: T; - f: FooFn3; -} - -type T21 = T; - -interface Baz {} -interface Baz {} - -interface Parent { - child: Child | null; - parent: Parent | null; -} - -declare class StateNode { - _storedEvent: TEvent; - _action: ActionObject; - _state: StateNode; -} - -=====================================output===================================== -// valid JSX -() => {}; - -type Covariant = { x: T }; -type Contravariant = { f: (x: T) => void }; -type Invariant = { f: (x: T) => T }; -type T10 = T; -type T11 = keyof T; -type T12 = T[K]; -type T13 = T[keyof T]; - -type Covariant1 = { x: T }; - -type Contravariant1 = keyof T; - -type Contravariant2 = { f: (x: T) => void }; - -type Invariant1 = { f: (x: T) => T }; - -type Invariant2 = { f: (x: T) => T }; -type Foo1 = { x: T; f: FooFn1 }; - -type Foo2 = { x: T; f: FooFn2 }; - -type Foo3 = { x: T; f: FooFn3 }; - -type T21 = T; - -interface Baz {} -interface Baz {} - -interface Parent { child: Child | null; parent: Parent | null } - -declare class StateNode { - _storedEvent: TEvent; - _action: ActionObject; - _state: StateNode; -} - ================================================================================ `; -exports[`tuple-labeled-ts.ts format 1`] = ` +exports["tuple-labeled-ts.ts format 1"] = ` ====================================options===================================== parsers: ["babel-ts"] printWidth: 80 diff --git a/tests/format/misc/typescript-babel-only/ts-4.7-instantiation-expression.ts b/tests/format/misc/typescript-babel-only/ts-4.7-instantiation-expression.ts index 7334d2dfbb81..f5e71be1f8ff 100644 --- a/tests/format/misc/typescript-babel-only/ts-4.7-instantiation-expression.ts +++ b/tests/format/misc/typescript-babel-only/ts-4.7-instantiation-expression.ts @@ -1,8 +1,2 @@ -// basic -const foo = bar; - // typeof let x: typeof y.z; - -// new -new A; diff --git a/tests/format/typescript/infer-extends/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/infer-extends/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..53d6048d8481 --- /dev/null +++ b/tests/format/typescript/infer-extends/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,35 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`basic.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type X3 = T extends [infer U extends number] ? MustBeNumber : never; +type X4 = T extends [infer U extends number, infer U extends number] ? MustBeNumber : never; +type X5 = T extends [infer U extends number, infer U] ? MustBeNumber : never; +type X6 = T extends [infer U, infer U extends number] ? MustBeNumber : never; +type X7 = T extends [infer U extends string, infer U extends number] ? U : never; +type X8 = T extends infer U extends number ? U : T; +type X9 = T extends (infer U extends number ? U : T) ? U : T; + +=====================================output===================================== +type X3 = T extends [infer U extends number] ? MustBeNumber : never; +type X4 = T extends [infer U extends number, infer U extends number] + ? MustBeNumber + : never; +type X5 = T extends [infer U extends number, infer U] + ? MustBeNumber + : never; +type X6 = T extends [infer U, infer U extends number] + ? MustBeNumber + : never; +type X7 = T extends [infer U extends string, infer U extends number] + ? U + : never; +type X8 = T extends infer U extends number ? U : T; +type X9 = T extends (infer U extends number ? U : T) ? U : T; + +================================================================================ +`; diff --git a/tests/format/misc/typescript-babel-only/ts-4.7-infer-extends.ts b/tests/format/typescript/infer-extends/basic.ts similarity index 100% rename from tests/format/misc/typescript-babel-only/ts-4.7-infer-extends.ts rename to tests/format/typescript/infer-extends/basic.ts diff --git a/tests/format/typescript/infer-extends/jsfmt.spec.js b/tests/format/typescript/infer-extends/jsfmt.spec.js new file mode 100644 index 000000000000..2ea3bb6eb2e4 --- /dev/null +++ b/tests/format/typescript/infer-extends/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]); diff --git a/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap index ce37081d8cfe..29fb2537be05 100644 --- a/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/instantiation-expression/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`basic.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// basic +const foo = bar; + +=====================================output===================================== +// basic +const foo = bar; + +================================================================================ +`; + exports[`binary-expr.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -36,3 +52,19 @@ interface Example { ================================================================================ `; + +exports[`new.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// new +new A; + +=====================================output===================================== +// new +new A(); + +================================================================================ +`; diff --git a/tests/format/typescript/instantiation-expression/basic.ts b/tests/format/typescript/instantiation-expression/basic.ts new file mode 100644 index 000000000000..6bba4f1adb49 --- /dev/null +++ b/tests/format/typescript/instantiation-expression/basic.ts @@ -0,0 +1,2 @@ +// basic +const foo = bar; diff --git a/tests/format/typescript/instantiation-expression/new.ts b/tests/format/typescript/instantiation-expression/new.ts new file mode 100644 index 000000000000..92cb2ff1b585 --- /dev/null +++ b/tests/format/typescript/instantiation-expression/new.ts @@ -0,0 +1,2 @@ +// new +new A; diff --git a/tests/format/typescript/optional-variance/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/optional-variance/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..ff238fb554f1 --- /dev/null +++ b/tests/format/typescript/optional-variance/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,285 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`basic.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Covariant = { + x: T; +} +type Contravariant = { + f: (x: T) => void; +} +type Invariant = { + f: (x: T) => T; +} +type T10 = T; +type T11 = keyof T; +type T12 = T[K]; +type T13 = T[keyof T]; + +type Covariant1 = { + x: T; +} + +type Contravariant1 = keyof T; + +type Contravariant2 = { + f: (x: T) => void; +} + +type Invariant1 = { + f: (x: T) => T; +} + +type Invariant2 = { + f: (x: T) => T; +} +type Foo1 = { + x: T; + f: FooFn1; +} + +type Foo2 = { + x: T; + f: FooFn2; +} + +type Foo3 = { + x: T; + f: FooFn3; +} + +type T21 = T; + +interface Baz {} +interface Baz {} + +interface Parent { + child: Child | null; + parent: Parent | null; +} + +declare class StateNode { + _storedEvent: TEvent; + _action: ActionObject; + _state: StateNode; +} + +=====================================output===================================== +type Covariant = { + x: T; +}; +type Contravariant = { + f: (x: T) => void; +}; +type Invariant = { + f: (x: T) => T; +}; +type T10 = T; +type T11 = keyof T; +type T12 = T[K]; +type T13 = T[keyof T]; + +type Covariant1 = { + x: T; +}; + +type Contravariant1 = keyof T; + +type Contravariant2 = { + f: (x: T) => void; +}; + +type Invariant1 = { + f: (x: T) => T; +}; + +type Invariant2 = { + f: (x: T) => T; +}; +type Foo1 = { + x: T; + f: FooFn1; +}; + +type Foo2 = { + x: T; + f: FooFn2; +}; + +type Foo3 = { + x: T; + f: FooFn3; +}; + +type T21 = T; + +interface Baz {} +interface Baz {} + +interface Parent { + child: Child | null; + parent: Parent | null; +} + +declare class StateNode { + _storedEvent: TEvent; + _action: ActionObject; + _state: StateNode; +} + +================================================================================ +`; + +exports[`with-jsx.tsx [babel-ts] format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// valid JSX +() => {}; + +type Covariant = { + x: T; +} +type Contravariant = { + f: (x: T) => void; +} +type Invariant = { + f: (x: T) => T; +} +type T10 = T; +type T11 = keyof T; +type T12 = T[K]; +type T13 = T[keyof T]; + +type Covariant1 = { + x: T; +} + +type Contravariant1 = keyof T; + +type Contravariant2 = { + f: (x: T) => void; +} + +type Invariant1 = { + f: (x: T) => T; +} + +type Invariant2 = { + f: (x: T) => T; +} +type Foo1 = { + x: T; + f: FooFn1; +} + +type Foo2 = { + x: T; + f: FooFn2; +} + +type Foo3 = { + x: T; + f: FooFn3; +} + +type T21 = T; + +interface Baz {} +interface Baz {} + +interface Parent { + child: Child | null; + parent: Parent | null; +} + +declare class StateNode { + _storedEvent: TEvent; + _action: ActionObject; + _state: StateNode; +} + +=====================================output===================================== +// valid JSX +() => {}; + +type Covariant = { + x: T; +}; +type Contravariant = { + f: (x: T) => void; +}; +type Invariant = { + f: (x: T) => T; +}; +type T10 = T; +type T11 = keyof T; +type T12 = T[K]; +type T13 = T[keyof T]; + +type Covariant1 = { + x: T; +}; + +type Contravariant1 = keyof T; + +type Contravariant2 = { + f: (x: T) => void; +}; + +type Invariant1 = { + f: (x: T) => T; +}; + +type Invariant2 = { + f: (x: T) => T; +}; +type Foo1 = { + x: T; + f: FooFn1; +}; + +type Foo2 = { + x: T; + f: FooFn2; +}; + +type Foo3 = { + x: T; + f: FooFn3; +}; + +type T21 = T; + +interface Baz {} +interface Baz {} + +interface Parent { + child: Child | null; + parent: Parent | null; +} + +declare class StateNode { + _storedEvent: TEvent; + _action: ActionObject; + _state: StateNode; +} + +================================================================================ +`; + +exports[`with-jsx.tsx [typescript] format 1`] = ` +"Unexpected token. Did you mean \`{'>'}\` or \`>\`? (2:11) + 1 | // valid JSX +> 2 | () => {}; + | ^ + 3 | + 4 | type Covariant = { + 5 | x: T;" +`; diff --git a/tests/format/misc/typescript-babel-only/ts-4.7-optional-variance.ts b/tests/format/typescript/optional-variance/basic.ts similarity index 100% rename from tests/format/misc/typescript-babel-only/ts-4.7-optional-variance.ts rename to tests/format/typescript/optional-variance/basic.ts diff --git a/tests/format/typescript/optional-variance/jsfmt.spec.js b/tests/format/typescript/optional-variance/jsfmt.spec.js new file mode 100644 index 000000000000..a156e3b55d18 --- /dev/null +++ b/tests/format/typescript/optional-variance/jsfmt.spec.js @@ -0,0 +1,3 @@ +run_spec(__dirname, ["typescript"], { + errors: { typescript: ["with-jsx.tsx"] }, +}); diff --git a/tests/format/misc/typescript-babel-only/ts-4.7-optional-variance-with-jsx.tsx b/tests/format/typescript/optional-variance/with-jsx.tsx similarity index 100% rename from tests/format/misc/typescript-babel-only/ts-4.7-optional-variance-with-jsx.tsx rename to tests/format/typescript/optional-variance/with-jsx.tsx