Skip to content

Commit

Permalink
- Add conversion of the iterator from Flow to TS (#1604)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1604

`@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 and asyncIterator from Flow to TS

Reviewed By: pieterv

Differential Revision: D68776171

fbshipit-source-id: 9532eb287755aa3b384c4743a1b942bbde08bc55
  • Loading branch information
coado authored and facebook-github-bot committed Jan 31, 2025
1 parent 89fe002 commit ede7def
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
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>;
}
"
`;
36 changes: 34 additions & 2 deletions 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,44 @@ const getTransforms = (
cloneJSDocCommentsToNewNode(member, newNode);
classMembers.push(newNode);
} else {
const [key, computed] = (() => {
const _key = member.key;
if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
const name = _key.name.slice(2);
if (['iterator', 'asyncIterator'].includes(name)) {
return [
{
type: 'MemberExpression',
computed: false,
object: {
type: 'Identifier',
name: 'Symbol',
optional: false,
loc: DUMMY_LOC,
},
optional: false,
property: {
type: 'Identifier',
name,
optional: false,
loc: DUMMY_LOC,
},
loc: DUMMY_LOC,
},
true,
];
}
}

return [member.key, member.computed];
})();

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

0 comments on commit ede7def

Please sign in to comment.