Skip to content

Commit

Permalink
Support TS 4.7 instantiation expression (prettier#12898)
Browse files Browse the repository at this point in the history
* Handle `TSInstantiationExpression`

* Print `typeParameters` for `TSTypeQuery`

* Add tests

* Add more tests
  • Loading branch information
sosukesuzuki authored and medikoo committed Jan 3, 2024
1 parent 49d1f17 commit 52e86de
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function printTypescript(path, options, print) {

return parts;
case "TSTypeQuery":
return ["typeof ", print("exprName")];
return ["typeof ", print("exprName"), print("typeParameters")];
case "TSIndexSignature": {
const parent = path.getParentNode();

Expand Down Expand Up @@ -520,6 +520,8 @@ function printTypescript(path, options, print) {
return ["?", print("typeAnnotation")];
case "TSJSDocNonNullableType":
return ["!", print("typeAnnotation")];
case "TSInstantiationExpression":
return [print("expression"), print("typeParameters")];
default:
/* istanbul ignore next */
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,34 @@ type X9<U, T> = 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<T>;
// typeof
let x: typeof y.z<w>;
// new
new A<T>;
=====================================output=====================================
// basic
const foo = bar<T>;
// typeof
let x: typeof y.z<w>;
// new
new A<T>();
================================================================================
`;
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,8 @@
// basic
const foo = bar<T>;

// typeof
let x: typeof y.z<w>;

// new
new A<T>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`binary-expr.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
new A < B >
C
=====================================output=====================================
new A() < B > C;
================================================================================
`;
exports[`inferface-asi.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
interface Example {
(a: number): typeof a
<T>(): void
};
=====================================output=====================================
interface Example {
(a: number): typeof a;
<T>(): void;
}
================================================================================
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
new A < B >
C
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
interface Example {
(a: number): typeof a

<T>(): void
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["typescript"]);

0 comments on commit 52e86de

Please sign in to comment.