Skip to content

Commit

Permalink
- Add conversion of the iterator from Flow to TS
Browse files Browse the repository at this point in the history
Summary:
`@iterator` and `@asyncIterator` are valid syntaxes for Flow but not for TS which accepts [Symbol.iterator] and [Symbol.asyncIterator]. According to flow lexer these are the only valid syntaxes in this form so checking for other cases prefixed with `@@` should not be required.

Changelog:
[flow-api-translator] - Added conversion of the iterator from Flow to TS

Differential Revision: D68776171
  • Loading branch information
coado authored and facebook-github-bot committed Jan 29, 2025
1 parent bfb595f commit d0de615
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

declare class Foo {
@@iterator(): Iterator<string>;
@@asyncIterator(): AsyncIterator<string>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`flowDefToTSDef class/members/method/iterator 1`] = `
"/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/

declare class Foo {
[Symbol.iterator](): Iterator<string>;
[Symbol.asyncIterator](): AsyncIterator<string>;
}
"
`;
15 changes: 14 additions & 1 deletion tools/hermes-parser/js/flow-api-translator/src/flowDefToTSDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,25 @@ const getTransforms = (
cloneJSDocCommentsToNewNode(member, newNode);
classMembers.push(newNode);
} else {
const key =
member.key.type === 'Identifier' &&
member.key.name.startsWith('@@') &&
// $FlowFixMe[prop-missing]
['iterator', 'asyncIterator'].includes(member.key.name.slice(2))
? {
type: 'Identifier',
loc: DUMMY_LOC,
// $FlowFixMe[prop-missing]
name: `[Symbol.${member.key.name.slice(2)}]`,
}
: member.key;

const newNode: TSESTree.MethodDefinitionAmbiguous = {
type: 'MethodDefinition',
loc: DUMMY_LOC,
accessibility: member.accessibility,
computed: member.computed ?? false,
key: member.key,
key,
kind: member.kind,
optional: member.optional,
override: false,
Expand Down

0 comments on commit d0de615

Please sign in to comment.