flow-api-translator 0.32.0 → 0.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -208,7 +208,7 @@ const getTransforms = (originalCode, opts) => {
208
208
  });
209
209
  }
210
210
 
211
- static ClassDeclarationSuperClass(superClass, superTypeParameters) {
211
+ static ClassDeclarationSuperClass(superClass, superTypeArguments) {
212
212
  if (superClass == null) {
213
213
  return [];
214
214
  }
@@ -218,7 +218,7 @@ const getTransforms = (originalCode, opts) => {
218
218
  return [constructFlowNode({
219
219
  type: 'InterfaceExtends',
220
220
  id,
221
- typeParameters: Transform.TSTypeParameterInstantiationOpt(superTypeParameters)
221
+ typeParameters: Transform.TSTypeParameterInstantiationOpt(superTypeArguments)
222
222
  })];
223
223
  }
224
224
 
@@ -1282,7 +1282,7 @@ const getTransforms = (originalCode, opts) => {
1282
1282
  static TSTupleType(node) {
1283
1283
  return constructFlowNode({
1284
1284
  type: 'TupleTypeAnnotation',
1285
- types: node.elementTypes.map(node => Transform.TSTypeAnnotation(node)),
1285
+ elementTypes: node.elementTypes.map(node => Transform.TSTypeAnnotation(node)),
1286
1286
  inexact: false
1287
1287
  });
1288
1288
  }
@@ -225,7 +225,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
225
225
 
226
226
  static ClassDeclarationSuperClass(
227
227
  superClass: ?TSESTree.LeftHandSideExpression,
228
- superTypeParameters: ?TSESTree.TSTypeParameterInstantiation,
228
+ superTypeArguments: ?TSESTree.TSTypeParameterInstantiation,
229
229
  ): [FlowESTree.InterfaceExtends] | [] {
230
230
  if (superClass == null) {
231
231
  return [];
@@ -239,7 +239,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
239
239
  type: 'InterfaceExtends',
240
240
  id,
241
241
  typeParameters:
242
- Transform.TSTypeParameterInstantiationOpt(superTypeParameters),
242
+ Transform.TSTypeParameterInstantiationOpt(superTypeArguments),
243
243
  }),
244
244
  ];
245
245
  }
@@ -1440,7 +1440,9 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1440
1440
  ): FlowESTree.TupleTypeAnnotation {
1441
1441
  return constructFlowNode<FlowESTree.TupleTypeAnnotation>({
1442
1442
  type: 'TupleTypeAnnotation',
1443
- types: node.elementTypes.map(node => Transform.TSTypeAnnotation(node)),
1443
+ elementTypes: node.elementTypes.map(node =>
1444
+ Transform.TSTypeAnnotation(node),
1445
+ ),
1444
1446
  inexact: false,
1445
1447
  });
1446
1448
  }
@@ -1740,6 +1740,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1740
1740
  switch (node.declaration.type) {
1741
1741
  case 'ClassDeclaration':
1742
1742
  case 'ComponentDeclaration':
1743
+ case 'RecordDeclaration':
1743
1744
  case 'HookDeclaration':
1744
1745
  case 'FunctionDeclaration':
1745
1746
  case 'VariableDeclaration':
@@ -3415,7 +3416,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3415
3416
  ObjectTypeProperty(node) {
3416
3417
  var _node$variance3;
3417
3418
 
3418
- const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : transform.StringLiteral(node.key);
3419
+ const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : node.key.literalType === 'string' ? transform.StringLiteral(node.key) : null;
3420
+
3421
+ if (key == null) {
3422
+ throw unexpectedTranslationError(node, 'Unsupported key type');
3423
+ }
3419
3424
 
3420
3425
  if (node.method === true) {
3421
3426
  // flow has just one node for all object properties and relies upon the method flag
@@ -3583,8 +3588,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3583
3588
  },
3584
3589
 
3585
3590
  TupleTypeAnnotation(node) {
3586
- const allReadOnly = node.types.length > 0 && node.types.every(element => element.type === 'TupleTypeLabeledElement' && element.variance != null && element.variance.kind === 'plus');
3587
- const elems = node.types.map(element => {
3591
+ const allReadOnly = node.elementTypes.length > 0 && node.elementTypes.every(element => element.type === 'TupleTypeLabeledElement' && element.variance != null && element.variance.kind === 'plus');
3592
+ const elems = node.elementTypes.map(element => {
3588
3593
  switch (element.type) {
3589
3594
  case 'TupleTypeLabeledElement':
3590
3595
  if (!allReadOnly && element.variance != null) {
@@ -1935,6 +1935,7 @@ const getTransforms = (
1935
1935
  switch (node.declaration.type) {
1936
1936
  case 'ClassDeclaration':
1937
1937
  case 'ComponentDeclaration':
1938
+ case 'RecordDeclaration':
1938
1939
  case 'HookDeclaration':
1939
1940
  case 'FunctionDeclaration':
1940
1941
  case 'VariableDeclaration':
@@ -3723,7 +3724,13 @@ const getTransforms = (
3723
3724
  const key =
3724
3725
  node.key.type === 'Identifier'
3725
3726
  ? transform.Identifier(node.key)
3726
- : transform.StringLiteral(node.key);
3727
+ : node.key.literalType === 'string'
3728
+ ? transform.StringLiteral(node.key)
3729
+ : null;
3730
+
3731
+ if (key == null) {
3732
+ throw unexpectedTranslationError(node, 'Unsupported key type');
3733
+ }
3727
3734
 
3728
3735
  if (node.method === true) {
3729
3736
  // flow has just one node for all object properties and relies upon the method flag
@@ -3905,14 +3912,14 @@ const getTransforms = (
3905
3912
  node: FlowESTree.TupleTypeAnnotation,
3906
3913
  ): TSESTree.TSTupleType | TSESTree.TSTypeOperator {
3907
3914
  const allReadOnly =
3908
- node.types.length > 0 &&
3909
- node.types.every(
3915
+ node.elementTypes.length > 0 &&
3916
+ node.elementTypes.every(
3910
3917
  element =>
3911
3918
  element.type === 'TupleTypeLabeledElement' &&
3912
3919
  element.variance != null &&
3913
3920
  element.variance.kind === 'plus',
3914
3921
  );
3915
- const elems = node.types.map((element): TSESTree.TypeNode => {
3922
+ const elems = node.elementTypes.map((element): TSESTree.TypeNode => {
3916
3923
  switch (element.type) {
3917
3924
  case 'TupleTypeLabeledElement':
3918
3925
  if (!allReadOnly && element.variance != null) {
@@ -746,7 +746,7 @@ function convertInterfaceDeclaration(interface_, context) {
746
746
  function convertClassDeclaration(class_, context) {
747
747
  const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(class_.typeParameters, context);
748
748
  const implementsDeps = class_.implements.flatMap(impl => (0, _FlowAnalyze.analyzeTypeDependencies)(impl, context));
749
- const [resultSuperClass, superClassDeps] = convertSuperClass(class_.superClass, class_.superTypeParameters, context);
749
+ const [resultSuperClass, superClassDeps] = convertSuperClass(class_.superClass, class_.superTypeArguments, context);
750
750
  const [resultClassBody, bodyDeps] = convertClassBody(class_.body, context);
751
751
 
752
752
  if (class_.decorators.length > 0) {
@@ -816,8 +816,8 @@ function convertExpressionToTypeofIdentifier(node, context) {
816
816
  throw (0, _ErrorUtils.translationError)(node, `Expected ${node.type} to be an Identifier or Member with Identifier property, non-Super object.`, context);
817
817
  }
818
818
 
819
- function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeParameters, context) {
820
- const [resultTypeParams, typeParamsDeps] = convertTypeParameterInstantiationOrNull(superTypeParameters, context);
819
+ function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeArguments, context) {
820
+ const [resultTypeParams, typeParamsDeps] = convertTypeParameterInstantiationOrNull(superTypeArguments, context);
821
821
  const superDeps = (0, _FlowAnalyze.analyzeTypeDependencies)(nodeForDependencies, context);
822
822
  return [_hermesTransform.t.InterfaceExtends({
823
823
  id: detachedId,
@@ -825,7 +825,7 @@ function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeParam
825
825
  }), [...typeParamsDeps, ...superDeps]];
826
826
  }
827
827
 
828
- function convertSuperClass(superClass, superTypeParameters, context) {
828
+ function convertSuperClass(superClass, superTypeArguments, context) {
829
829
  if (superClass == null) {
830
830
  // $FlowFixMe[incompatible-type]
831
831
  return EMPTY_TRANSLATION_RESULT;
@@ -834,12 +834,12 @@ function convertSuperClass(superClass, superTypeParameters, context) {
834
834
  switch (superClass.type) {
835
835
  case 'Identifier':
836
836
  {
837
- return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(superClass), superClass, superTypeParameters, context);
837
+ return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(superClass), superClass, superTypeArguments, context);
838
838
  }
839
839
 
840
840
  case 'MemberExpression':
841
841
  {
842
- return convertSuperClassHelper(convertExpressionToIdentifier(superClass, context), superClass, superTypeParameters, context);
842
+ return convertSuperClassHelper(convertExpressionToIdentifier(superClass, context), superClass, superTypeArguments, context);
843
843
  }
844
844
 
845
845
  case 'TypeCastExpression':
@@ -848,7 +848,7 @@ function convertSuperClass(superClass, superTypeParameters, context) {
848
848
  const typeAnnotation = superClass.type === 'TypeCastExpression' ? superClass.typeAnnotation.typeAnnotation : superClass.typeAnnotation;
849
849
 
850
850
  if (typeAnnotation.type === 'GenericTypeAnnotation') {
851
- return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(typeAnnotation.id), typeAnnotation, superTypeParameters, context);
851
+ return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(typeAnnotation.id), typeAnnotation, superTypeArguments, context);
852
852
  }
853
853
 
854
854
  if (typeAnnotation.type === 'TypeofTypeAnnotation') {
@@ -980,7 +980,7 @@ function convertClassDeclaration(
980
980
  );
981
981
  const [resultSuperClass, superClassDeps] = convertSuperClass(
982
982
  class_.superClass,
983
- class_.superTypeParameters,
983
+ class_.superTypeArguments,
984
984
  context,
985
985
  );
986
986
  const [resultClassBody, bodyDeps] = convertClassBody(class_.body, context);
@@ -1059,11 +1059,11 @@ function convertExpressionToTypeofIdentifier(
1059
1059
  function convertSuperClassHelper(
1060
1060
  detachedId: DetachedNode<Identifier | QualifiedTypeIdentifier>,
1061
1061
  nodeForDependencies: ESNode,
1062
- superTypeParameters: ?TypeParameterInstantiation,
1062
+ superTypeArguments: ?TypeParameterInstantiation,
1063
1063
  context: TranslationContext,
1064
1064
  ): TranslatedResultOrNull<InterfaceExtends> {
1065
1065
  const [resultTypeParams, typeParamsDeps] =
1066
- convertTypeParameterInstantiationOrNull(superTypeParameters, context);
1066
+ convertTypeParameterInstantiationOrNull(superTypeArguments, context);
1067
1067
  const superDeps = analyzeTypeDependencies(nodeForDependencies, context);
1068
1068
  return [
1069
1069
  t.InterfaceExtends({
@@ -1076,7 +1076,7 @@ function convertSuperClassHelper(
1076
1076
 
1077
1077
  function convertSuperClass(
1078
1078
  superClass: ?Expression,
1079
- superTypeParameters: ?TypeParameterInstantiation,
1079
+ superTypeArguments: ?TypeParameterInstantiation,
1080
1080
  context: TranslationContext,
1081
1081
  ): TranslatedResultOrNull<InterfaceExtends> {
1082
1082
  if (superClass == null) {
@@ -1089,7 +1089,7 @@ function convertSuperClass(
1089
1089
  return convertSuperClassHelper(
1090
1090
  asDetachedNode(superClass),
1091
1091
  superClass,
1092
- superTypeParameters,
1092
+ superTypeArguments,
1093
1093
  context,
1094
1094
  );
1095
1095
  }
@@ -1097,7 +1097,7 @@ function convertSuperClass(
1097
1097
  return convertSuperClassHelper(
1098
1098
  convertExpressionToIdentifier(superClass, context),
1099
1099
  superClass,
1100
- superTypeParameters,
1100
+ superTypeArguments,
1101
1101
  context,
1102
1102
  );
1103
1103
  }
@@ -1112,7 +1112,7 @@ function convertSuperClass(
1112
1112
  return convertSuperClassHelper(
1113
1113
  asDetachedNode(typeAnnotation.id),
1114
1114
  typeAnnotation,
1115
- superTypeParameters,
1115
+ superTypeArguments,
1116
1116
  context,
1117
1117
  );
1118
1118
  }
package/dist/flowToJS.js CHANGED
@@ -35,5 +35,5 @@ function stripAtFlow(ast, _options) {
35
35
  }
36
36
 
37
37
  function flowToJS(sourceAST, _code, _scopeManager) {
38
- return [_hermesParser.Transforms.stripComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
38
+ return [_hermesParser.Transforms.transformComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
39
39
  }
@@ -37,7 +37,7 @@ export function flowToJS(
37
37
  _scopeManager: ScopeManager,
38
38
  ): Program {
39
39
  return [
40
- Transforms.stripComponentSyntax,
40
+ Transforms.transformComponentSyntax,
41
41
  Transforms.stripFlowTypes,
42
42
  stripAtFlow,
43
43
  ].reduce((ast, transform) => transform(ast, {}), sourceAST);
@@ -208,7 +208,7 @@ const getTransforms = (originalCode, opts) => {
208
208
  });
209
209
  }
210
210
 
211
- static ClassDeclarationSuperClass(superClass, superTypeParameters) {
211
+ static ClassDeclarationSuperClass(superClass, superTypeArguments) {
212
212
  if (superClass == null) {
213
213
  return [];
214
214
  }
@@ -218,7 +218,7 @@ const getTransforms = (originalCode, opts) => {
218
218
  return [constructFlowNode({
219
219
  type: 'InterfaceExtends',
220
220
  id,
221
- typeParameters: Transform.TSTypeParameterInstantiationOpt(superTypeParameters)
221
+ typeParameters: Transform.TSTypeParameterInstantiationOpt(superTypeArguments)
222
222
  })];
223
223
  }
224
224
 
@@ -1282,7 +1282,7 @@ const getTransforms = (originalCode, opts) => {
1282
1282
  static TSTupleType(node) {
1283
1283
  return constructFlowNode({
1284
1284
  type: 'TupleTypeAnnotation',
1285
- types: node.elementTypes.map(node => Transform.TSTypeAnnotation(node)),
1285
+ elementTypes: node.elementTypes.map(node => Transform.TSTypeAnnotation(node)),
1286
1286
  inexact: false
1287
1287
  });
1288
1288
  }
@@ -1740,6 +1740,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1740
1740
  switch (node.declaration.type) {
1741
1741
  case 'ClassDeclaration':
1742
1742
  case 'ComponentDeclaration':
1743
+ case 'RecordDeclaration':
1743
1744
  case 'HookDeclaration':
1744
1745
  case 'FunctionDeclaration':
1745
1746
  case 'VariableDeclaration':
@@ -3415,7 +3416,11 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3415
3416
  ObjectTypeProperty(node) {
3416
3417
  var _node$variance3;
3417
3418
 
3418
- const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : transform.StringLiteral(node.key);
3419
+ const key = node.key.type === 'Identifier' ? transform.Identifier(node.key) : node.key.literalType === 'string' ? transform.StringLiteral(node.key) : null;
3420
+
3421
+ if (key == null) {
3422
+ throw unexpectedTranslationError(node, 'Unsupported key type');
3423
+ }
3419
3424
 
3420
3425
  if (node.method === true) {
3421
3426
  // flow has just one node for all object properties and relies upon the method flag
@@ -3583,8 +3588,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3583
3588
  },
3584
3589
 
3585
3590
  TupleTypeAnnotation(node) {
3586
- const allReadOnly = node.types.length > 0 && node.types.every(element => element.type === 'TupleTypeLabeledElement' && element.variance != null && element.variance.kind === 'plus');
3587
- const elems = node.types.map(element => {
3591
+ const allReadOnly = node.elementTypes.length > 0 && node.elementTypes.every(element => element.type === 'TupleTypeLabeledElement' && element.variance != null && element.variance.kind === 'plus');
3592
+ const elems = node.elementTypes.map(element => {
3588
3593
  switch (element.type) {
3589
3594
  case 'TupleTypeLabeledElement':
3590
3595
  if (!allReadOnly && element.variance != null) {
@@ -746,7 +746,7 @@ function convertInterfaceDeclaration(interface_, context) {
746
746
  function convertClassDeclaration(class_, context) {
747
747
  const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(class_.typeParameters, context);
748
748
  const implementsDeps = class_.implements.flatMap(impl => (0, _FlowAnalyze.analyzeTypeDependencies)(impl, context));
749
- const [resultSuperClass, superClassDeps] = convertSuperClass(class_.superClass, class_.superTypeParameters, context);
749
+ const [resultSuperClass, superClassDeps] = convertSuperClass(class_.superClass, class_.superTypeArguments, context);
750
750
  const [resultClassBody, bodyDeps] = convertClassBody(class_.body, context);
751
751
 
752
752
  if (class_.decorators.length > 0) {
@@ -816,8 +816,8 @@ function convertExpressionToTypeofIdentifier(node, context) {
816
816
  throw (0, _ErrorUtils.translationError)(node, `Expected ${node.type} to be an Identifier or Member with Identifier property, non-Super object.`, context);
817
817
  }
818
818
 
819
- function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeParameters, context) {
820
- const [resultTypeParams, typeParamsDeps] = convertTypeParameterInstantiationOrNull(superTypeParameters, context);
819
+ function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeArguments, context) {
820
+ const [resultTypeParams, typeParamsDeps] = convertTypeParameterInstantiationOrNull(superTypeArguments, context);
821
821
  const superDeps = (0, _FlowAnalyze.analyzeTypeDependencies)(nodeForDependencies, context);
822
822
  return [_hermesTransform.t.InterfaceExtends({
823
823
  id: detachedId,
@@ -825,7 +825,7 @@ function convertSuperClassHelper(detachedId, nodeForDependencies, superTypeParam
825
825
  }), [...typeParamsDeps, ...superDeps]];
826
826
  }
827
827
 
828
- function convertSuperClass(superClass, superTypeParameters, context) {
828
+ function convertSuperClass(superClass, superTypeArguments, context) {
829
829
  if (superClass == null) {
830
830
  // $FlowFixMe[incompatible-type]
831
831
  return EMPTY_TRANSLATION_RESULT;
@@ -834,12 +834,12 @@ function convertSuperClass(superClass, superTypeParameters, context) {
834
834
  switch (superClass.type) {
835
835
  case 'Identifier':
836
836
  {
837
- return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(superClass), superClass, superTypeParameters, context);
837
+ return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(superClass), superClass, superTypeArguments, context);
838
838
  }
839
839
 
840
840
  case 'MemberExpression':
841
841
  {
842
- return convertSuperClassHelper(convertExpressionToIdentifier(superClass, context), superClass, superTypeParameters, context);
842
+ return convertSuperClassHelper(convertExpressionToIdentifier(superClass, context), superClass, superTypeArguments, context);
843
843
  }
844
844
 
845
845
  case 'TypeCastExpression':
@@ -848,7 +848,7 @@ function convertSuperClass(superClass, superTypeParameters, context) {
848
848
  const typeAnnotation = superClass.type === 'TypeCastExpression' ? superClass.typeAnnotation.typeAnnotation : superClass.typeAnnotation;
849
849
 
850
850
  if (typeAnnotation.type === 'GenericTypeAnnotation') {
851
- return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(typeAnnotation.id), typeAnnotation, superTypeParameters, context);
851
+ return convertSuperClassHelper((0, _hermesTransform.asDetachedNode)(typeAnnotation.id), typeAnnotation, superTypeArguments, context);
852
852
  }
853
853
 
854
854
  if (typeAnnotation.type === 'TypeofTypeAnnotation') {
@@ -35,5 +35,5 @@ function stripAtFlow(ast, _options) {
35
35
  }
36
36
 
37
37
  function flowToJS(sourceAST, _code, _scopeManager) {
38
- return [_hermesParser.Transforms.stripComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
38
+ return [_hermesParser.Transforms.transformComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
39
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-api-translator",
3
- "version": "0.32.0",
3
+ "version": "0.33.0",
4
4
  "description": "Toolkit for creating Flow and TypeScript compatible libraries from Flow source code.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -13,10 +13,10 @@
13
13
  "@typescript-eslint/parser": "8.38.0",
14
14
  "@typescript-eslint/visitor-keys": "8.38.0",
15
15
  "flow-enums-runtime": "^0.0.6",
16
- "hermes-eslint": "0.32.0",
17
- "hermes-estree": "0.32.0",
18
- "hermes-parser": "0.32.0",
19
- "hermes-transform": "0.32.0",
16
+ "hermes-eslint": "0.33.0",
17
+ "hermes-estree": "0.33.0",
18
+ "hermes-parser": "0.33.0",
19
+ "hermes-transform": "0.33.0",
20
20
  "typescript": "5.3.2"
21
21
  },
22
22
  "peerDependencies": {