Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse printFunctionType #14189

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
};