Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add support for callables, fixes #128 #242

Merged
merged 1 commit into from
Mar 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add support for callables, fixes #128
  • Loading branch information
kevinbarabash committed Mar 7, 2021
commit 8c6e4fa1acf1182b39864c8267a813da37bf69e0
27 changes: 26 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,25 @@ const transform = {
}
},
},
ObjectTypeCallProperty: {
exit(path, state) {
// NOTE: `value` has already been converted to a TSFunctionType
const { value, leadingComments, trailingComments, loc } = path.node;
const { typeParameters, parameters, typeAnnotation } = value;
const replacement = t.tsCallSignatureDeclaration(
typeParameters,
parameters,
typeAnnotation
);
replacement.leadingComments = leadingComments;
replacement.trailingComments = trailingComments;
replacement.loc = loc;

trackComments(replacement, state);

path.replaceWith(replacement);
},
},
ObjectTypeProperty: {
exit(path, state) {
const {
Expand Down Expand Up @@ -751,7 +770,7 @@ const transform = {
}
},
exit(path) {
const { exact, properties, indexers } = path.node; // TODO: callProperties, inexact
const { exact, callProperties, properties, indexers } = path.node; // TODO: inexact

if (exact) {
console.warn("downgrading exact object type");
Expand All @@ -762,6 +781,12 @@ const transform = {
const elements = [];
const spreads = [];

if (callProperties) {
for (const prop of callProperties) {
elements.push(prop);
}
}

for (const prop of properties) {
if (t.isObjectTypeSpreadProperty(prop)) {
const { argument } = prop;
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/convert/object-types/callables01/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Callable<T> = {
<T>(): void,
(string): number,
foo: boolean,
};
5 changes: 5 additions & 0 deletions test/fixtures/convert/object-types/callables01/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Callable<T> = {
<T>(): void;
(arg0: string): number;
foo: boolean;
};