From 8c6e4fa1acf1182b39864c8267a813da37bf69e0 Mon Sep 17 00:00:00 2001 From: Kevin Barabash Date: Sat, 6 Mar 2021 23:32:00 -0500 Subject: [PATCH] Add support for callables, fixes #128 --- src/transform.js | 27 ++++++++++++++++++- .../convert/object-types/callables01/flow.js | 5 ++++ .../convert/object-types/callables01/ts.js | 5 ++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/convert/object-types/callables01/flow.js create mode 100644 test/fixtures/convert/object-types/callables01/ts.js diff --git a/src/transform.js b/src/transform.js index 46e357e..db98a79 100644 --- a/src/transform.js +++ b/src/transform.js @@ -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 { @@ -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"); @@ -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; diff --git a/test/fixtures/convert/object-types/callables01/flow.js b/test/fixtures/convert/object-types/callables01/flow.js new file mode 100644 index 0000000..ac1233c --- /dev/null +++ b/test/fixtures/convert/object-types/callables01/flow.js @@ -0,0 +1,5 @@ +type Callable = { + (): void, + (string): number, + foo: boolean, +}; diff --git a/test/fixtures/convert/object-types/callables01/ts.js b/test/fixtures/convert/object-types/callables01/ts.js new file mode 100644 index 0000000..d21fa9a --- /dev/null +++ b/test/fixtures/convert/object-types/callables01/ts.js @@ -0,0 +1,5 @@ +type Callable = { + (): void; + (arg0: string): number; + foo: boolean; +};