Skip to content

Commit

Permalink
Allow TS interfaces to extend qualified identifiers, Translate $Array…
Browse files Browse the repository at this point in the history
…Like to ArrayLike

Summary:
Adds two thinkgs to `flow-api-translator`:
1. Support for translating interfaces extending qualified names
2. Changing Flow's `$ArrayLike` to TS's `ArrayLike`

Those changes were originally made in `xplat/hermes/tools/hermes-parser/js`: D70316488, D70387099.

Reviewed By: pieterv

Differential Revision: D70545344

fbshipit-source-id: 950c40d63ebe6de0e17e779ee0d9ab1aa99774f5
  • Loading branch information
j-piasecki authored and facebook-github-bot committed Mar 5, 2025
1 parent 41f59f7 commit 6ac3b5c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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
*/

interface Foo extends Bar.Baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`flowDefToTSDef interface/extends/qualified-name 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
*/

interface Foo extends Bar.Baz {}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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
*/

type T = $ArrayLike<Foo>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`flowDefToTSDef types/utilities/$ArrayLike 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
*/

type T = ArrayLike<Foo>;
"
`;
24 changes: 22 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 @@ -2069,6 +2069,24 @@ const getTransforms = (
return unsupportedAnnotation(node, fullTypeName);
}

case '$ArrayLike': {
// `$ArrayLike<T>` => `ArrayLike<T>`
return {
type: 'TSTypeReference',
loc: DUMMY_LOC,
typeName: {
type: 'Identifier',
loc: DUMMY_LOC,
name: 'ArrayLike',
},
typeParameters: {
type: 'TSTypeParameterInstantiation',
loc: DUMMY_LOC,
params: assertHasExactlyNTypeParameters(1),
},
};
}

case '$Diff':
case '$Rest': {
// `$Diff<A, B>` => `Pick<A, Exclude<keyof A, keyof B>>`
Expand Down Expand Up @@ -3079,8 +3097,10 @@ const getTransforms = (
return {
type: 'TSInterfaceHeritage',
loc: DUMMY_LOC,
// Bug: node.id can be qualified
expression: transform.Identifier((node.id: $FlowFixMe), false),
expression:
node.id.type === 'QualifiedTypeIdentifier'
? transform.QualifiedTypeIdentifier(node.id)
: transform.Identifier(node.id, false),
typeParameters:
node.typeParameters == null
? undefined
Expand Down

0 comments on commit 6ac3b5c

Please sign in to comment.