Skip to content

Commit

Permalink
Reuse printFunctionType (prettier#14189)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Feb 7, 2024
1 parent 242fae8 commit 61b64f7
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 41 deletions.
40 changes: 30 additions & 10 deletions src/language-js/print/type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,28 @@ function printUnionType(path, options, print) {
return group(shouldIndent ? indent(code) : code);
}

// `TSFunctionType` and `FunctionTypeAnnotation`
/*
- `TSFunctionType` (TypeScript)
- `TSCallSignatureDeclaration` (TypeScript)
- `TSConstructorType` (TypeScript)
- `TSConstructSignatureDeclaration` (TypeScript)
- `FunctionTypeAnnotation` (Flow)
*/
function printFunctionType(path, options, print) {
const { node } = path;
const parts = [];

if (node.type === "TSConstructorType" && node.abstract) {
parts.push("abstract ");
}

if (
node.type === "TSConstructorType" ||
node.type === "TSConstructSignatureDeclaration"
) {
parts.push("new ");
}

// FunctionTypeAnnotation is ambiguous:
// declare function foo(a: B): void; OR
// var A: (a: B) => void;
Expand All @@ -226,15 +244,17 @@ function printFunctionType(path, options, print) {
const parentParentParent = path.getParentNode(2);
let isArrowFunctionTypeAnnotation =
node.type === "TSFunctionType" ||
!(
((parent.type === "ObjectTypeProperty" ||
parent.type === "ObjectTypeInternalSlot") &&
!parent.variance &&
!parent.optional &&
locStart(parent) === locStart(node)) ||
parent.type === "ObjectTypeCallProperty" ||
parentParentParent?.type === "DeclareFunction"
);
node.type === "TSConstructorType" ||
(node.type === "FunctionTypeAnnotation" &&
!(
((parent.type === "ObjectTypeProperty" ||
parent.type === "ObjectTypeInternalSlot") &&
!parent.variance &&
!parent.optional &&
locStart(parent) === locStart(node)) ||
parent.type === "ObjectTypeCallProperty" ||
parentParentParent?.type === "DeclareFunction"
));

let needsColon =
isArrowFunctionTypeAnnotation &&
Expand Down
34 changes: 3 additions & 31 deletions src/language-js/print/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,37 +258,6 @@ function printTypescript(path, options, print) {
return print("literal");
case "TSIndexedAccessType":
return printIndexedAccessType(path, options, print);
case "TSConstructSignatureDeclaration":
case "TSCallSignatureDeclaration":
case "TSConstructorType":
if (node.type === "TSConstructorType" && node.abstract) {
parts.push("abstract ");
}
if (node.type !== "TSCallSignatureDeclaration") {
parts.push("new ");
}

parts.push(
group(
printFunctionParameters(
path,
print,
options,
/* expandArg */ false,
/* printTypeParams */ true
)
)
);

if (node.returnType || node.typeAnnotation) {
const isType = node.type === "TSConstructorType";
parts.push(
isType ? " => " : ": ",
print("returnType"),
print("typeAnnotation")
);
}
return parts;

case "TSTypeOperator":
return [node.operator, " ", print("typeAnnotation")];
Expand Down Expand Up @@ -440,6 +409,9 @@ function printTypescript(path, options, print) {
case "TSUnionType":
return printUnionType(path, options, print);
case "TSFunctionType":
case "TSCallSignatureDeclaration":
case "TSConstructorType":
case "TSConstructSignatureDeclaration":
return printFunctionType(path, options, print);
case "TSTupleType":
return printArray(path, options, print);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`consistent.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
// TSFunctionType
type A = (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;
// TSConstructorType
type B = new (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;
type X = {
// TSCallSignatureDeclaration
(
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;
// TSConstructSignatureDeclaration
new (
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;
};
=====================================output=====================================
// TSFunctionType
type A = (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;
// TSConstructorType
type B = new (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;
type X = {
// TSCallSignatureDeclaration
(
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;
// TSConstructSignatureDeclaration
new (
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;
};
================================================================================
`;
exports[`single-parameter.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
25 changes: 25 additions & 0 deletions tests/format/typescript/function-type/consistent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// TSFunctionType
type A = (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;

// TSConstructorType
type B = new (
tpl: TemplateStringsArray,
...args: Array<unknown>
) => (replacements?: PublicReplacements) => T;

type X = {
// TSCallSignatureDeclaration
(
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;

// TSConstructSignatureDeclaration
new (
tpl: TemplateStringsArray,
...args: Array<unknown>
): (replacements?: PublicReplacements) => T;
};

0 comments on commit 61b64f7

Please sign in to comment.