Skip to content

Commit

Permalink
Fix arrow function syntax for class properties (#1593)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1593

Changelog:
[flow-api-translator]: Fixed generated arrow function type for class properties in flowToFlowDef

Reviewed By: pieterv

Differential Revision: D68160295

fbshipit-source-id: b548c5741bb67e91e642796026921b4006d699f5
  • Loading branch information
coado authored and facebook-github-bot committed Jan 21, 2025
1 parent 2f9c702 commit 6bde579
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ describe('flowToFlowDef', () => {
1: string;
}`,
);
await expectTranslate(
`export class A {
foo = () => {};
static foo = () => {};
}`,
`declare export class A {
foo: () => void;
static foo: () => void;
}`,
);
});
it('method', async () => {
await expectTranslate(
Expand Down
21 changes: 21 additions & 0 deletions tools/hermes-parser/js/flow-api-translator/src/flowToFlowDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,27 @@ function convertClassMember(
);
}

if (member.value?.type === 'ArrowFunctionExpression') {
const [resultTypeAnnotation, deps] = convertAFunction(
member.value,
context,
);

return [
t.ObjectTypePropertySignature({
// $FlowFixMe[incompatible-call]
key: asDetachedNode<
ClassPropertyNameComputed | ClassPropertyNameNonComputed,
>(member.key),
value: resultTypeAnnotation,
optional: member.optional,
static: member.static,
variance: member.variance,
}),
deps,
];
}

const [resultTypeAnnotation, deps] = convertTypeAnnotation(
member.typeAnnotation,
member,
Expand Down

0 comments on commit 6bde579

Please sign in to comment.