flow-api-translator 0.28.0 → 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.
@@ -32,11 +32,15 @@ const DUMMY_COMMON = {
32
32
  parent: DUMMY_PARENT,
33
33
  };
34
34
 
35
+ type LooseOmit<O: interface {}, K: $Keys<$FlowFixMe>> = Pick<
36
+ O,
37
+ Exclude<$Keys<O>, K>,
38
+ >;
35
39
  function constructFlowNode<T: FlowESTree.BaseNode>(
36
- node: $Diff<T, FlowESTree.BaseNode>,
40
+ node: LooseOmit<NoInfer<T>, 'parent'>,
37
41
  ): T {
38
42
  return {
39
- ...node,
43
+ ...(node: $FlowFixMe),
40
44
  ...DUMMY_COMMON,
41
45
  };
42
46
  }
@@ -92,7 +96,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
92
96
  return buildCodeFrame(node, message, code, false);
93
97
  }
94
98
  function addErrorComment(node: FlowESTree.ESNode, message: string): void {
95
- const comment = {
99
+ const comment: TSESTree.Comment = {
96
100
  type: 'Block',
97
101
  loc: DUMMY_LOC,
98
102
  value: `*${EOL} * ${message.replace(
@@ -42,7 +42,6 @@ const DUMMY_LOC = {
42
42
  };
43
43
 
44
44
  function constructFlowNode(node) {
45
- // $FlowFixMe[prop-missing]
46
45
  return node;
47
46
  }
48
47
 
@@ -812,7 +811,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
812
811
  type: 'TSClassImplements',
813
812
  loc: DUMMY_LOC,
814
813
  expression: transform.Identifier(node.id, false),
815
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
814
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
816
815
  };
817
816
  },
818
817
 
@@ -1006,7 +1005,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1006
1005
  id: transform.Identifier(node.id, false),
1007
1006
  implements: node.implements == null ? undefined : node.implements.map(transform.ClassImplements),
1008
1007
  superClass: superClass == null ? null : superClass.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(superClass.id) : transform.Identifier(superClass.id, false),
1009
- superTypeParameters: (superClass == null ? void 0 : superClass.typeParameters) == null ? undefined : transform.TypeParameterInstantiation(superClass.typeParameters),
1008
+ superTypeParameters: transform.TypeParameterInstantiation(superClass == null ? void 0 : superClass.typeParameters),
1010
1009
  typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterDeclaration(node.typeParameters) // TODO - mixins??
1011
1010
 
1012
1011
  };
@@ -2856,7 +2855,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2856
2855
  type: 'TSTypeReference',
2857
2856
  loc: DUMMY_LOC,
2858
2857
  typeName: node.id.type === 'Identifier' ? transform.Identifier(node.id, false) : transform.QualifiedTypeIdentifier(node.id),
2859
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
2858
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
2860
2859
  };
2861
2860
  },
2862
2861
 
@@ -2965,7 +2964,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2965
2964
  type: 'TSInterfaceHeritage',
2966
2965
  loc: DUMMY_LOC,
2967
2966
  expression: node.id.type === 'QualifiedTypeIdentifier' ? transform.QualifiedTypeIdentifier(node.id) : transform.Identifier(node.id, false),
2968
- typeParameters: node.typeParameters == null ? undefined : transform.TypeParameterInstantiation(node.typeParameters)
2967
+ typeParameters: transform.TypeParameterInstantiation(node.typeParameters)
2969
2968
  };
2970
2969
  },
2971
2970
 
@@ -2982,7 +2981,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2982
2981
  loc: DUMMY_LOC,
2983
2982
  // Bug: ex.id can be qualified
2984
2983
  typeName: transform.Identifier(ex.id, false),
2985
- typeParameters: ex.typeParameters == null ? undefined : transform.TypeParameterInstantiation(ex.typeParameters)
2984
+ typeParameters: transform.TypeParameterInstantiation(ex.typeParameters)
2986
2985
  })), transform.ObjectTypeAnnotation(node.body)]
2987
2986
  };
2988
2987
  }
@@ -3698,6 +3697,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3698
3697
  },
3699
3698
 
3700
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
+
3701
3706
  return {
3702
3707
  type: 'TSTypeParameterInstantiation',
3703
3708
  loc: DUMMY_LOC,
@@ -3832,10 +3837,12 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3832
3837
  transform[key] = (node, ...args) => {
3833
3838
  const result = originalFn(node, ...args);
3834
3839
 
3835
- if (Array.isArray(result)) {
3836
- cloneJSDocCommentsToNewNode(node, result[0]);
3837
- } else {
3838
- 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
+ }
3839
3846
  }
3840
3847
 
3841
3848
  return result;
@@ -35,11 +35,14 @@ const DUMMY_LOC: FlowESTree.SourceLocation = {
35
35
  end: {line: 1, column: 0},
36
36
  };
37
37
 
38
+ type LooseOmit<O: interface {}, K: $Keys<$FlowFixMe>> = Pick<
39
+ O,
40
+ Exclude<$Keys<O>, K>,
41
+ >;
38
42
  function constructFlowNode<T: FlowESTree.BaseNode>(
39
- node: $Diff<T, FlowESTree.BaseNode>,
43
+ node: LooseOmit<NoInfer<T>, 'parent'>,
40
44
  ): T {
41
- // $FlowFixMe[prop-missing]
42
- return node;
45
+ return (node: $FlowFixMe);
43
46
  }
44
47
 
45
48
  const cloneJSDocCommentsToNewNode =
@@ -60,7 +63,7 @@ let shouldAddReactImport: boolean | null = null;
60
63
 
61
64
  // Returns appropriate Identifier for `React` import.
62
65
  // If a global is in use, set a flag to indicate that we should add the import.
63
- function getReactIdentifier(hasReactImport: boolean) {
66
+ function getReactIdentifier(hasReactImport: boolean): TSESTree.EntityName {
64
67
  if (shouldAddReactImport !== false) {
65
68
  shouldAddReactImport = !hasReactImport;
66
69
  }
@@ -408,7 +411,7 @@ const getTransforms = (
408
411
  }
409
412
  }
410
413
 
411
- const bodyRepresentationType =
414
+ const bodyRepresentationType: TSESTree.TypeNode =
412
415
  body.type === 'EnumNumberBody'
413
416
  ? {type: 'TSNumberKeyword', loc: DUMMY_LOC}
414
417
  : {type: 'TSStringKeyword', loc: DUMMY_LOC};
@@ -851,10 +854,9 @@ const getTransforms = (
851
854
  type: 'TSClassImplements',
852
855
  loc: DUMMY_LOC,
853
856
  expression: transform.Identifier(node.id, false),
854
- typeParameters:
855
- node.typeParameters == null
856
- ? undefined
857
- : transform.TypeParameterInstantiation(node.typeParameters),
857
+ typeParameters: transform.TypeParameterInstantiation(
858
+ node.typeParameters,
859
+ ),
858
860
  };
859
861
  },
860
862
  DeclareClass(
@@ -928,7 +930,7 @@ const getTransforms = (
928
930
  cloneJSDocCommentsToNewNode(member, newNode);
929
931
  classMembers.push(newNode);
930
932
  } else {
931
- const [key, computed] = (() => {
933
+ const [key, computed] = ((): [TSESTree.PropertyName, boolean] => {
932
934
  const _key = member.key;
933
935
  if (_key.type === 'Identifier' && _key.name.startsWith('@@')) {
934
936
  const name = _key.name.slice(2);
@@ -1065,10 +1067,9 @@ const getTransforms = (
1065
1067
  : superClass.id.type === 'QualifiedTypeIdentifier'
1066
1068
  ? transform.QualifiedTypeIdentifier(superClass.id)
1067
1069
  : transform.Identifier((superClass.id: $FlowFixMe), false),
1068
- superTypeParameters:
1069
- superClass?.typeParameters == null
1070
- ? undefined
1071
- : transform.TypeParameterInstantiation(superClass.typeParameters),
1070
+ superTypeParameters: transform.TypeParameterInstantiation(
1071
+ superClass?.typeParameters,
1072
+ ),
1072
1073
  typeParameters:
1073
1074
  node.typeParameters == null
1074
1075
  ? undefined
@@ -1372,7 +1373,10 @@ const getTransforms = (
1372
1373
  }: TSESTree.ExportNamedDeclarationWithoutSourceWithMultiple);
1373
1374
  }
1374
1375
 
1375
- const declarations = (() => {
1376
+ const declarations = ((): Array<{
1377
+ declaration: TSESTree.NamedExportDeclarations,
1378
+ exportKind: TSESTree.ExportKind,
1379
+ }> => {
1376
1380
  switch (node.declaration.type) {
1377
1381
  case 'DeclareClass':
1378
1382
  return [
@@ -1951,7 +1955,7 @@ const getTransforms = (
1951
1955
  })();
1952
1956
 
1953
1957
  const mainExport = {
1954
- type: 'ExportNamedDeclaration',
1958
+ type: ('ExportNamedDeclaration': 'ExportNamedDeclaration'),
1955
1959
  loc: DUMMY_LOC,
1956
1960
  assertions: [],
1957
1961
  declaration: exportedDeclaration,
@@ -2898,7 +2902,7 @@ const getTransforms = (
2898
2902
  );
2899
2903
  }
2900
2904
 
2901
- const newParams = (() => {
2905
+ const newParams = ((): $ReadOnlyArray<TSESTree.TypeNode> => {
2902
2906
  if (params.length === 1) {
2903
2907
  return assertHasExactlyNTypeParameters(1);
2904
2908
  }
@@ -3078,10 +3082,9 @@ const getTransforms = (
3078
3082
  node.id.type === 'Identifier'
3079
3083
  ? transform.Identifier(node.id, false)
3080
3084
  : transform.QualifiedTypeIdentifier(node.id),
3081
- typeParameters:
3082
- node.typeParameters == null
3083
- ? undefined
3084
- : transform.TypeParameterInstantiation(node.typeParameters),
3085
+ typeParameters: transform.TypeParameterInstantiation(
3086
+ node.typeParameters,
3087
+ ),
3085
3088
  };
3086
3089
  },
3087
3090
  Identifier(
@@ -3132,12 +3135,12 @@ const getTransforms = (
3132
3135
  ): Array<DeclarationOrUnsupported<TSESTree.ImportDeclaration>> {
3133
3136
  const importKind = node.importKind;
3134
3137
 
3135
- const specifiers = [];
3138
+ const specifiers: Array<TSESTree.ImportClause> = [];
3136
3139
  const unsupportedSpecifiers: Array<TSESTree.TSTypeAliasDeclaration> = [];
3137
3140
  node.specifiers.forEach(spec => {
3138
3141
  let id = (() => {
3139
3142
  if (node.importKind === 'typeof' || spec.importKind === 'typeof') {
3140
- const id = {
3143
+ const id: TSESTree.Identifier = {
3141
3144
  type: 'Identifier',
3142
3145
  loc: DUMMY_LOC,
3143
3146
  name: getPlaceholderNameForTypeofImport(),
@@ -3218,10 +3221,9 @@ const getTransforms = (
3218
3221
  node.id.type === 'QualifiedTypeIdentifier'
3219
3222
  ? transform.QualifiedTypeIdentifier(node.id)
3220
3223
  : transform.Identifier(node.id, false),
3221
- typeParameters:
3222
- node.typeParameters == null
3223
- ? undefined
3224
- : transform.TypeParameterInstantiation(node.typeParameters),
3224
+ typeParameters: transform.TypeParameterInstantiation(
3225
+ node.typeParameters,
3226
+ ),
3225
3227
  };
3226
3228
  },
3227
3229
  InterfaceTypeAnnotation(
@@ -3240,10 +3242,9 @@ const getTransforms = (
3240
3242
  loc: DUMMY_LOC,
3241
3243
  // Bug: ex.id can be qualified
3242
3244
  typeName: transform.Identifier((ex.id: $FlowFixMe), false),
3243
- typeParameters:
3244
- ex.typeParameters == null
3245
- ? undefined
3246
- : transform.TypeParameterInstantiation(ex.typeParameters),
3245
+ typeParameters: transform.TypeParameterInstantiation(
3246
+ ex.typeParameters,
3247
+ ),
3247
3248
  })),
3248
3249
  transform.ObjectTypeAnnotation(node.body),
3249
3250
  ],
@@ -3566,7 +3567,7 @@ const getTransforms = (
3566
3567
  const tsBody = members
3567
3568
  .sort((a, b) => a.start - b.start)
3568
3569
  .map(({node}) => node);
3569
- const objectType = {
3570
+ const objectType: TSESTree.TypeNode = {
3570
3571
  type: 'TSTypeLiteral',
3571
3572
  loc: DUMMY_LOC,
3572
3573
  members: tsBody,
@@ -3886,7 +3887,7 @@ const getTransforms = (
3886
3887
  element.variance != null &&
3887
3888
  element.variance.kind === 'plus',
3888
3889
  );
3889
- const elems = node.types.map(element => {
3890
+ const elems = node.types.map((element): TSESTree.TypeNode => {
3890
3891
  switch (element.type) {
3891
3892
  case 'TupleTypeLabeledElement':
3892
3893
  if (!allReadOnly && element.variance != null) {
@@ -3924,7 +3925,7 @@ const getTransforms = (
3924
3925
  }
3925
3926
  });
3926
3927
 
3927
- const elementTypes = node.inexact
3928
+ const elementTypes: Array<TSESTree.TypeNode> = node.inexact
3928
3929
  ? [
3929
3930
  ...elems,
3930
3931
  {
@@ -4040,8 +4041,14 @@ const getTransforms = (
4040
4041
  };
4041
4042
  },
4042
4043
  TypeParameterInstantiation(
4043
- node: FlowESTree.TypeParameterInstantiation,
4044
- ): 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
+
4045
4052
  return {
4046
4053
  type: 'TSTypeParameterInstantiation',
4047
4054
  loc: DUMMY_LOC,
@@ -4147,7 +4154,7 @@ const getTransforms = (
4147
4154
 
4148
4155
  // TS cannot support `renderType` so we always use ReactNode as the return type.
4149
4156
  const hasReactImport = isReactImport(node, 'React');
4150
- const returnType = {
4157
+ const returnType: TSESTree.TSTypeAnnotation = {
4151
4158
  type: 'TSTypeAnnotation',
4152
4159
  loc: DUMMY_LOC,
4153
4160
  // If no rendersType we assume its ReactNode type.
@@ -4186,10 +4193,12 @@ const getTransforms = (
4186
4193
  // $FlowExpectedError[missing-local-annot]
4187
4194
  transform[key] = (node, ...args) => {
4188
4195
  const result = originalFn(node, ...args);
4189
- if (Array.isArray(result)) {
4190
- cloneJSDocCommentsToNewNode(node, result[0]);
4191
- } else {
4192
- 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
+ }
4193
4202
  }
4194
4203
  return result;
4195
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
  }