flow-api-translator 0.28.1 → 0.29.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.
@@ -96,7 +96,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
96
96
  return buildCodeFrame(node, message, code, false);
97
97
  }
98
98
  function addErrorComment(node: FlowESTree.ESNode, message: string): void {
99
- const comment = {
99
+ const comment: TSESTree.Comment = {
100
100
  type: 'Block',
101
101
  loc: DUMMY_LOC,
102
102
  value: `*${EOL} * ${message.replace(
@@ -811,7 +811,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
811
811
  type: 'TSClassImplements',
812
812
  loc: DUMMY_LOC,
813
813
  expression: transform.Identifier(node.id, false),
814
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
814
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
815
815
  };
816
816
  },
817
817
 
@@ -1005,7 +1005,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1005
1005
  id: transform.Identifier(node.id, false),
1006
1006
  implements: node.implements == null ? undefined : node.implements.map(transform.ClassImplements),
1007
1007
  superClass: superClass == null ? null : superClass.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(superClass.id) : transform.Identifier(superClass.id, false),
1008
- superTypeParameters: (superClass == null ? void 0 : superClass.typeParameters) == null ? undefined : transform.TypeParameterInstantiation(superClass.typeParameters),
1008
+ superTypeParameters: transform.TypeParameterInstantiation(superClass == null ? void 0 : superClass.typeParameters),
1009
1009
  typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters) // TODO - mixins??
1010
1010
 
1011
1011
  };
@@ -2855,7 +2855,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2855
2855
  type: 'TSTypeReference',
2856
2856
  loc: DUMMY_LOC,
2857
2857
  typeName: node.id.type === 'Identifier' ? transform.Identifier(node.id, false) : transform.QualifiedTypeIdentifier(node.id),
2858
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
2858
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
2859
2859
  };
2860
2860
  },
2861
2861
 
@@ -2964,7 +2964,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2964
2964
  type: 'TSInterfaceHeritage',
2965
2965
  loc: DUMMY_LOC,
2966
2966
  expression: node.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(node.id) : transform.Identifier(node.id, false),
2967
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
2967
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
2968
2968
  };
2969
2969
  },
2970
2970
 
@@ -2981,7 +2981,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2981
2981
  loc: DUMMY_LOC,
2982
2982
  // Bug: ex.id can be qualified
2983
2983
  typeName: transform.Identifier(ex.id, false),
2984
- typeParameters: ex.typeParameters == null ? undefined : transform.TypeParameterInstantiation(ex.typeParameters)
2984
+ typeParameters: transform.TypeParameterInstantiation(ex.typeParameters)
2985
2985
  })), transform.ObjectTypeAnnotation(node.body)]
2986
2986
  };
2987
2987
  }
@@ -3697,6 +3697,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3697
3697
  },
3698
3698
 
3699
3699
  TypeParameterInstantiation(node) {
3700
+ // Empty parameters in Flow are valid, but TS requires at least one parameter.
3701
+ // This ensures empty type parameters are not created in TS
3702
+ if (node == null || node.params.length === 0) {
3703
+ return undefined;
3704
+ }
3705
+
3700
3706
  return {
3701
3707
  type: 'TSTypeParameterInstantiation',
3702
3708
  loc: DUMMY_LOC,
@@ -3831,10 +3837,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3831
3837
  transform[key] = (node, ...args) => {
3832
3838
  const result = originalFn(node, ...args);
3833
3839
 
3834
- if (Array.isArray(result)) {
3835
- cloneJSDocCommentsToNewNode(node, result[0]);
3836
- } else {
3837
- cloneJSDocCommentsToNewNode(node, result);
3840
+ if (node != null && result != null) {
3841
+ if (Array.isArray(result)) {
3842
+ cloneJSDocCommentsToNewNode(node, result[0]);
3843
+ } else {
3844
+ cloneJSDocCommentsToNewNode(node, result);
3845
+ }
3838
3846
  }
3839
3847
 
3840
3848
  return result;
@@ -63,7 +63,7 @@ let shouldAddReactImport: boolean | null = null;
63
63
 
64
64
  // Returns appropriate Identifier for `React` import.
65
65
  // If a global is in use, set a flag to indicate that we should add the import.
66
- function getReactIdentifier(hasReactImport: boolean) {
66
+ function getReactIdentifier(hasReactImport: boolean): TSESTree.EntityName {
67
67
  if (shouldAddReactImport !== false) {
68
68
  shouldAddReactImport = !hasReactImport;
69
69
  }
@@ -411,7 +411,7 @@ const getTransforms = (
411
411
  }
412
412
  }
413
413
 
414
- const bodyRepresentationType =
414
+ const bodyRepresentationType: TSESTree.TypeNode =
415
415
  body.type === 'EnumNumberBody'
416
416
  ? {type: 'TSNumberKeyword', loc: DUMMY_LOC}
417
417
  : {type: 'TSStringKeyword', loc: DUMMY_LOC};
@@ -854,10 +854,9 @@ const getTransforms = (
854
854
  type: 'TSClassImplements',
855
855
  loc: DUMMY_LOC,
856
856
  expression: transform.Identifier(node.id, false),
857
- typeParameters:
858
- node.typeParameters == null
859
- ? undefined
860
- : transform.TypeParameterInstantiation(node.typeParameters),
857
+ typeParameters: transform.TypeParameterInstantiation(
858
+ node.typeParameters,
859
+ ),
861
860
  };
862
861
  },
863
862
  DeclareClass(
@@ -931,7 +930,7 @@ const getTransforms = (
931
930
  cloneJSDocCommentsToNewNode(member, newNode);
932
931
  classMembers.push(newNode);
933
932
  } else {
934
- const [key, computed] = (() => {
933
+ const [key, computed] = ((): [TSESTree.PropertyName, boolean] => {
935
934
  const _key = member.key;
936
935
  if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
937
936
  const name = _key.name.slice(2);
@@ -1068,10 +1067,9 @@ const getTransforms = (
1068
1067
  : superClass.id.type === 'QualifiedTypeIdentifier'
1069
1068
  ? transform.QualifiedTypeIdentifier(superClass.id)
1070
1069
  : transform.Identifier((superClass.id: $FlowFixMe), false),
1071
- superTypeParameters:
1072
- superClass?.typeParameters == null
1073
- ? undefined
1074
- : transform.TypeParameterInstantiation(superClass.typeParameters),
1070
+ superTypeParameters: transform.TypeParameterInstantiation(
1071
+ superClass?.typeParameters,
1072
+ ),
1075
1073
  typeParameters:
1076
1074
  node.typeParameters == null
1077
1075
  ? undefined
@@ -1375,7 +1373,10 @@ const getTransforms = (
1375
1373
  }: TSESTree.ExportNamedDeclarationWithoutSourceWithMultiple);
1376
1374
  }
1377
1375
 
1378
- const declarations = (() => {
1376
+ const declarations = ((): Array<{
1377
+ declaration: TSESTree.NamedExportDeclarations,
1378
+ exportKind: TSESTree.ExportKind,
1379
+ }> => {
1379
1380
  switch (node.declaration.type) {
1380
1381
  case 'DeclareClass':
1381
1382
  return [
@@ -1954,7 +1955,7 @@ const getTransforms = (
1954
1955
  })();
1955
1956
 
1956
1957
  const mainExport = {
1957
- type: 'ExportNamedDeclaration',
1958
+ type: ('ExportNamedDeclaration': 'ExportNamedDeclaration'),
1958
1959
  loc: DUMMY_LOC,
1959
1960
  assertions: [],
1960
1961
  declaration: exportedDeclaration,
@@ -2901,7 +2902,7 @@ const getTransforms = (
2901
2902
  );
2902
2903
  }
2903
2904
 
2904
- const newParams = (() => {
2905
+ const newParams = ((): $ReadOnlyArray<TSESTree.TypeNode> => {
2905
2906
  if (params.length === 1) {
2906
2907
  return assertHasExactlyNTypeParameters(1);
2907
2908
  }
@@ -3081,10 +3082,9 @@ const getTransforms = (
3081
3082
  node.id.type === 'Identifier'
3082
3083
  ? transform.Identifier(node.id, false)
3083
3084
  : transform.QualifiedTypeIdentifier(node.id),
3084
- typeParameters:
3085
- node.typeParameters == null
3086
- ? undefined
3087
- : transform.TypeParameterInstantiation(node.typeParameters),
3085
+ typeParameters: transform.TypeParameterInstantiation(
3086
+ node.typeParameters,
3087
+ ),
3088
3088
  };
3089
3089
  },
3090
3090
  Identifier(
@@ -3135,12 +3135,12 @@ const getTransforms = (
3135
3135
  ): Array<DeclarationOrUnsupported<TSESTree.ImportDeclaration>> {
3136
3136
  const importKind = node.importKind;
3137
3137
 
3138
- const specifiers = [];
3138
+ const specifiers: Array<TSESTree.ImportClause> = [];
3139
3139
  const unsupportedSpecifiers: Array<TSESTree.TSTypeAliasDeclaration> = [];
3140
3140
  node.specifiers.forEach(spec => {
3141
3141
  let id = (() => {
3142
3142
  if (node.importKind === 'typeof' || spec.importKind === 'typeof') {
3143
- const id = {
3143
+ const id: TSESTree.Identifier = {
3144
3144
  type: 'Identifier',
3145
3145
  loc: DUMMY_LOC,
3146
3146
  name: getPlaceholderNameForTypeofImport(),
@@ -3221,10 +3221,9 @@ const getTransforms = (
3221
3221
  node.id.type === 'QualifiedTypeIdentifier'
3222
3222
  ? transform.QualifiedTypeIdentifier(node.id)
3223
3223
  : transform.Identifier(node.id, false),
3224
- typeParameters:
3225
- node.typeParameters == null
3226
- ? undefined
3227
- : transform.TypeParameterInstantiation(node.typeParameters),
3224
+ typeParameters: transform.TypeParameterInstantiation(
3225
+ node.typeParameters,
3226
+ ),
3228
3227
  };
3229
3228
  },
3230
3229
  InterfaceTypeAnnotation(
@@ -3243,10 +3242,9 @@ const getTransforms = (
3243
3242
  loc: DUMMY_LOC,
3244
3243
  // Bug: ex.id can be qualified
3245
3244
  typeName: transform.Identifier((ex.id: $FlowFixMe), false),
3246
- typeParameters:
3247
- ex.typeParameters == null
3248
- ? undefined
3249
- : transform.TypeParameterInstantiation(ex.typeParameters),
3245
+ typeParameters: transform.TypeParameterInstantiation(
3246
+ ex.typeParameters,
3247
+ ),
3250
3248
  })),
3251
3249
  transform.ObjectTypeAnnotation(node.body),
3252
3250
  ],
@@ -3569,7 +3567,7 @@ const getTransforms = (
3569
3567
  const tsBody = members
3570
3568
  .sort((a, b) => a.start - b.start)
3571
3569
  .map(({node}) => node);
3572
- const objectType = {
3570
+ const objectType: TSESTree.TypeNode = {
3573
3571
  type: 'TSTypeLiteral',
3574
3572
  loc: DUMMY_LOC,
3575
3573
  members: tsBody,
@@ -3889,7 +3887,7 @@ const getTransforms = (
3889
3887
  element.variance != null &&
3890
3888
  element.variance.kind === 'plus',
3891
3889
  );
3892
- const elems = node.types.map(element => {
3890
+ const elems = node.types.map((element): TSESTree.TypeNode => {
3893
3891
  switch (element.type) {
3894
3892
  case 'TupleTypeLabeledElement':
3895
3893
  if (!allReadOnly && element.variance != null) {
@@ -3927,7 +3925,7 @@ const getTransforms = (
3927
3925
  }
3928
3926
  });
3929
3927
 
3930
- const elementTypes = node.inexact
3928
+ const elementTypes: Array<TSESTree.TypeNode> = node.inexact
3931
3929
  ? [
3932
3930
  ...elems,
3933
3931
  {
@@ -4043,8 +4041,14 @@ const getTransforms = (
4043
4041
  };
4044
4042
  },
4045
4043
  TypeParameterInstantiation(
4046
- node: FlowESTree.TypeParameterInstantiation,
4047
- ): TSESTree.TSTypeParameterInstantiation {
4044
+ node: ?FlowESTree.TypeParameterInstantiation,
4045
+ ): TSESTree.TSTypeParameterInstantiation | void {
4046
+ // Empty parameters in Flow are valid, but TS requires at least one parameter.
4047
+ // This ensures empty type parameters are not created in TS
4048
+ if (node == null || node.params.length === 0) {
4049
+ return undefined;
4050
+ }
4051
+
4048
4052
  return {
4049
4053
  type: 'TSTypeParameterInstantiation',
4050
4054
  loc: DUMMY_LOC,
@@ -4150,7 +4154,7 @@ const getTransforms = (
4150
4154
 
4151
4155
  // TS cannot support `renderType` so we always use ReactNode as the return type.
4152
4156
  const hasReactImport = isReactImport(node, 'React');
4153
- const returnType = {
4157
+ const returnType: TSESTree.TSTypeAnnotation = {
4154
4158
  type: 'TSTypeAnnotation',
4155
4159
  loc: DUMMY_LOC,
4156
4160
  // If no rendersType we assume its ReactNode type.
@@ -4189,10 +4193,12 @@ const getTransforms = (
4189
4193
  // $FlowExpectedError[missing-local-annot]
4190
4194
  transform[key] = (node, ...args) => {
4191
4195
  const result = originalFn(node, ...args);
4192
- if (Array.isArray(result)) {
4193
- cloneJSDocCommentsToNewNode(node, result[0]);
4194
- } else {
4195
- cloneJSDocCommentsToNewNode(node, result);
4196
+ if (node != null && result != null) {
4197
+ if (Array.isArray(result)) {
4198
+ cloneJSDocCommentsToNewNode(node, result[0]);
4199
+ } else {
4200
+ cloneJSDocCommentsToNewNode(node, result);
4201
+ }
4196
4202
  }
4197
4203
  return result;
4198
4204
  };
@@ -897,25 +897,25 @@ function convertClassMember(member, context) {
897
897
 
898
898
  if (((_member$value = member.value) == null ? void 0 : _member$value.type) === 'ArrowFunctionExpression' && member.typeAnnotation == null) {
899
899
  const [resultTypeAnnotation, deps] = convertAFunction(member.value, context);
900
- return [_hermesTransform.t.ObjectTypePropertySignature({
900
+ return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
901
901
  // $FlowFixMe[incompatible-call]
902
902
  key: (0, _hermesTransform.asDetachedNode)(member.key),
903
903
  value: resultTypeAnnotation,
904
904
  optional: member.optional,
905
905
  static: member.static,
906
906
  variance: member.variance
907
- }), deps];
907
+ })), deps];
908
908
  }
909
909
 
910
910
  const [resultTypeAnnotation, deps] = convertTypeAnnotation(member.typeAnnotation, member, context);
911
- return [_hermesTransform.t.ObjectTypePropertySignature({
911
+ return [inheritComments(member, _hermesTransform.t.ObjectTypePropertySignature({
912
912
  // $FlowFixMe[incompatible-call]
913
913
  key: (0, _hermesTransform.asDetachedNode)(member.key),
914
914
  value: resultTypeAnnotation,
915
915
  optional: member.optional,
916
916
  static: member.static,
917
917
  variance: member.variance
918
- }), deps];
918
+ })), deps];
919
919
  }
920
920
 
921
921
  case 'MethodDefinition':
@@ -937,21 +937,21 @@ function convertClassMember(member, context) {
937
937
  if (member.kind === 'get' || member.kind === 'set') {
938
938
  // accessors are methods - but flow accessor signatures are properties
939
939
  const kind = member.kind;
940
- return [_hermesTransform.t.ObjectTypeAccessorSignature({
940
+ return [inheritComments(member, _hermesTransform.t.ObjectTypeAccessorSignature({
941
941
  // $FlowFixMe[incompatible-call]
942
942
  key: (0, _hermesTransform.asDetachedNode)(newKey),
943
943
  value: resultValue,
944
944
  static: member.static,
945
945
  kind
946
- }), deps];
946
+ })), deps];
947
947
  }
948
948
 
949
- return [_hermesTransform.t.ObjectTypeMethodSignature({
949
+ return [inheritComments(member, _hermesTransform.t.ObjectTypeMethodSignature({
950
950
  // $FlowFixMe[incompatible-call]
951
951
  key: (0, _hermesTransform.asDetachedNode)(newKey),
952
952
  value: resultValue,
953
953
  static: member.static
954
- }), deps];
954
+ })), deps];
955
955
  }
956
956
 
957
957
  default:
@@ -1198,16 +1198,19 @@ function convertClassMember(
1198
1198
  );
1199
1199
 
1200
1200
  return [
1201
- t.ObjectTypePropertySignature({
1202
- // $FlowFixMe[incompatible-call]
1203
- key: asDetachedNode<
1204
- ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1205
- >(member.key),
1206
- value: resultTypeAnnotation,
1207
- optional: member.optional,
1208
- static: member.static,
1209
- variance: member.variance,
1210
- }),
1201
+ inheritComments(
1202
+ member,
1203
+ t.ObjectTypePropertySignature({
1204
+ // $FlowFixMe[incompatible-call]
1205
+ key: asDetachedNode<
1206
+ ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1207
+ >(member.key),
1208
+ value: resultTypeAnnotation,
1209
+ optional: member.optional,
1210
+ static: member.static,
1211
+ variance: member.variance,
1212
+ }),
1213
+ ),
1211
1214
  deps,
1212
1215
  ];
1213
1216
  }
@@ -1219,16 +1222,19 @@ function convertClassMember(
1219
1222
  );
1220
1223
 
1221
1224
  return [
1222
- t.ObjectTypePropertySignature({
1223
- // $FlowFixMe[incompatible-call]
1224
- key: asDetachedNode<
1225
- ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1226
- >(member.key),
1227
- value: resultTypeAnnotation,
1228
- optional: member.optional,
1229
- static: member.static,
1230
- variance: member.variance,
1231
- }),
1225
+ inheritComments(
1226
+ member,
1227
+ t.ObjectTypePropertySignature({
1228
+ // $FlowFixMe[incompatible-call]
1229
+ key: asDetachedNode<
1230
+ ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1231
+ >(member.key),
1232
+ value: resultTypeAnnotation,
1233
+ optional: member.optional,
1234
+ static: member.static,
1235
+ variance: member.variance,
1236
+ }),
1237
+ ),
1232
1238
  deps,
1233
1239
  ];
1234
1240
  }
@@ -1269,28 +1275,34 @@ function convertClassMember(
1269
1275
  const kind = member.kind;
1270
1276
 
1271
1277
  return [
1272
- t.ObjectTypeAccessorSignature({
1278
+ inheritComments(
1279
+ member,
1280
+ t.ObjectTypeAccessorSignature({
1281
+ // $FlowFixMe[incompatible-call]
1282
+ key: asDetachedNode<
1283
+ ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1284
+ >(newKey),
1285
+ value: resultValue,
1286
+ static: member.static,
1287
+ kind,
1288
+ }),
1289
+ ),
1290
+ deps,
1291
+ ];
1292
+ }
1293
+
1294
+ return [
1295
+ inheritComments(
1296
+ member,
1297
+ t.ObjectTypeMethodSignature({
1273
1298
  // $FlowFixMe[incompatible-call]
1274
1299
  key: asDetachedNode<
1275
1300
  ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1276
1301
  >(newKey),
1277
1302
  value: resultValue,
1278
1303
  static: member.static,
1279
- kind,
1280
1304
  }),
1281
- deps,
1282
- ];
1283
- }
1284
-
1285
- return [
1286
- t.ObjectTypeMethodSignature({
1287
- // $FlowFixMe[incompatible-call]
1288
- key: asDetachedNode<
1289
- ClassPropertyNameComputed | ClassPropertyNameNonComputed,
1290
- >(newKey),
1291
- value: resultValue,
1292
- static: member.static,
1293
- }),
1305
+ ),
1294
1306
  deps,
1295
1307
  ];
1296
1308
  }