flow-api-translator 0.36.1 → 0.327.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.
Files changed (37) hide show
  1. package/dist/TSDefToFlowDef.js +66 -327
  2. package/dist/TSDefToFlowDef.js.flow +70 -35
  3. package/dist/flowDefToTSDef.js +134 -825
  4. package/dist/flowDefToTSDef.js.flow +196 -100
  5. package/dist/flowImportTo.js +5 -25
  6. package/dist/flowImportTo.js.flow +4 -4
  7. package/dist/flowToFlowDef.js +190 -379
  8. package/dist/flowToFlowDef.js.flow +109 -27
  9. package/dist/flowToJS.js +3 -19
  10. package/dist/flowToJS.js.flow +5 -5
  11. package/dist/index.js +12 -50
  12. package/dist/index.js.flow +1 -1
  13. package/dist/src/TSDefToFlowDef.js +66 -327
  14. package/dist/src/flowDefToTSDef.js +134 -825
  15. package/dist/src/flowImportTo.js +5 -25
  16. package/dist/src/flowToFlowDef.js +190 -379
  17. package/dist/src/flowToJS.js +3 -19
  18. package/dist/src/index.js +12 -50
  19. package/dist/src/utils/DocblockUtils.js +4 -14
  20. package/dist/src/utils/ErrorUtils.js +4 -40
  21. package/dist/src/utils/FlowAnalyze.js +8 -28
  22. package/dist/src/utils/TSNodeUtils.js +83 -0
  23. package/dist/src/utils/TranslationUtils.js +0 -13
  24. package/dist/src/utils/ts-estree-ast-types.js +0 -29
  25. package/dist/utils/DocblockUtils.js +4 -14
  26. package/dist/utils/DocblockUtils.js.flow +1 -1
  27. package/dist/utils/ErrorUtils.js +4 -40
  28. package/dist/utils/ErrorUtils.js.flow +6 -6
  29. package/dist/utils/FlowAnalyze.js +8 -28
  30. package/dist/utils/FlowAnalyze.js.flow +9 -4
  31. package/dist/utils/TSNodeUtils.js +83 -0
  32. package/dist/utils/TSNodeUtils.js.flow +108 -0
  33. package/dist/utils/TranslationUtils.js +0 -13
  34. package/dist/utils/TranslationUtils.js.flow +2 -2
  35. package/dist/utils/ts-estree-ast-types.js +0 -29
  36. package/dist/utils/ts-estree-ast-types.js.flow +697 -740
  37. package/package.json +10 -9
@@ -10,12 +10,12 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {ObjectWithLoc} from 'hermes-estree';
13
+ import type {ObjectWithLoc} from 'flow-estree';
14
14
  import type {TranslationOptions} from './utils/TranslationUtils';
15
15
 
16
- import * as FlowESTree from 'hermes-estree';
16
+ import * as FlowESTree from 'flow-estree';
17
17
  import * as TSESTree from './utils/ts-estree-ast-types';
18
- import {makeCommentOwnLine as makeCommentOwnLineOriginal} from 'hermes-transform';
18
+ import {makeCommentOwnLine as makeCommentOwnLineOriginal} from 'flow-transform';
19
19
  import {
20
20
  buildCodeFrame,
21
21
  translationError as translationErrorBase,
@@ -23,7 +23,7 @@ import {
23
23
  } from './utils/ErrorUtils';
24
24
  import {EOL} from 'os';
25
25
 
26
- const DUMMY_LOC: FlowESTree.SourceLocation = (null: $FlowFixMe);
26
+ const DUMMY_LOC: FlowESTree.SourceLocation = null as $FlowFixMe;
27
27
  const DUMMY_RANGE: [number, number] = [0, 0];
28
28
  const DUMMY_PARENT: $FlowFixMe = null;
29
29
  const DUMMY_COMMON = {
@@ -32,22 +32,22 @@ const DUMMY_COMMON = {
32
32
  parent: DUMMY_PARENT,
33
33
  };
34
34
 
35
- type LooseOmit<O: interface {}, K: $Keys<$FlowFixMe>> = Pick<
35
+ type LooseOmit<O extends interface {}, K extends keyof $FlowFixMe> = Pick<
36
36
  O,
37
- Exclude<$Keys<O>, K>,
37
+ Exclude<keyof O, K>,
38
38
  >;
39
- function constructFlowNode<T: FlowESTree.BaseNode>(
39
+ function constructFlowNode<T extends FlowESTree.BaseNode>(
40
40
  node: LooseOmit<NoInfer<T>, 'parent'>,
41
41
  ): T {
42
42
  return {
43
- ...(node: $FlowFixMe),
43
+ ...(node as $FlowFixMe),
44
44
  ...DUMMY_COMMON,
45
45
  };
46
46
  }
47
47
 
48
48
  const makeCommentOwnLine =
49
49
  // $FlowExpectedError[incompatible-type] - trust me this re-type is 100% safe
50
- (makeCommentOwnLineOriginal: (string, mixed) => string);
50
+ makeCommentOwnLineOriginal as (string, unknown) => string;
51
51
 
52
52
  export function TSDefToFlowDef(
53
53
  originalCode: string,
@@ -113,7 +113,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
113
113
  // $FlowExpectedError[cannot-write]
114
114
  node.comments ??= [];
115
115
  // $FlowExpectedError[incompatible-type]
116
- (node.comments: Array<TSESTree.Comment>).push(comment);
116
+ (node.comments as Array<TSESTree.Comment>).push(comment);
117
117
  }
118
118
  function unsupportedAnnotation(
119
119
  node: ObjectWithLoc,
@@ -330,13 +330,13 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
330
330
  case 'ClassDeclaration':
331
331
  declaration = Transform.ClassDeclarationWithName(
332
332
  // possibly missing id
333
- (node.declaration: $FlowFixMe),
333
+ node.declaration as $FlowFixMe,
334
334
  );
335
335
  break;
336
336
  case 'FunctionDeclaration':
337
337
  declaration = Transform.FunctionDeclarationWithName(
338
338
  // possibly missing id
339
- (node.declaration: $FlowFixMe),
339
+ node.declaration as $FlowFixMe,
340
340
  );
341
341
  break;
342
342
  case 'TSDeclareFunction':
@@ -369,7 +369,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
369
369
  | FlowESTree.ExportNamedDeclaration
370
370
  | FlowESTree.DeclareExportDeclarationNamedWithDeclaration
371
371
  | FlowESTree.DeclareExportDeclarationNamedWithSpecifiers
372
- | $ReadOnlyArray<
372
+ | ReadonlyArray<
373
373
  | FlowESTree.DeclareExportDeclarationNamedWithDeclaration
374
374
  | FlowESTree.TypeAlias
375
375
  | FlowESTree.DeclareNamespace
@@ -379,7 +379,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
379
379
  if (node.declaration == null) {
380
380
  const source: FlowESTree.StringLiteral =
381
381
  node.source == null
382
- ? (null: $FlowFixMe)
382
+ ? (null as $FlowFixMe)
383
383
  : constructFlowNode<FlowESTree.StringLiteral>({
384
384
  type: 'Literal',
385
385
  literalType: 'string',
@@ -410,7 +410,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
410
410
  type: 'DeclareExportDeclaration',
411
411
  declaration: Transform.ClassDeclarationWithName(
412
412
  // possibly missing id
413
- (node.declaration: $FlowFixMe),
413
+ node.declaration as $FlowFixMe,
414
414
  ),
415
415
  default: false,
416
416
  source: null,
@@ -423,7 +423,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
423
423
  type: 'DeclareExportDeclaration',
424
424
  declaration: Transform.FunctionDeclarationWithName(
425
425
  // possibly missing id
426
- (node.declaration: $FlowFixMe),
426
+ node.declaration as $FlowFixMe,
427
427
  ),
428
428
  default: false,
429
429
  source: null,
@@ -546,6 +546,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
546
546
  },
547
547
  optional: false,
548
548
  },
549
+ implicitDeclare: false,
549
550
  predicate: null,
550
551
  });
551
552
  }
@@ -660,7 +661,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
660
661
  type: 'Literal',
661
662
  literalType: 'regexp',
662
663
  value: node.value,
663
- regex: (null: $FlowFixMe),
664
+ regex: null as $FlowFixMe,
664
665
  raw: node.raw,
665
666
  });
666
667
  } else if (node.value == null) {
@@ -715,7 +716,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
715
716
  case 'regexp':
716
717
  return unsupportedAnnotation(node, 'regexp literal type');
717
718
  default:
718
- (literal: empty);
719
+ literal as empty;
719
720
  throw 'unreachable';
720
721
  }
721
722
  }
@@ -725,7 +726,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
725
726
  ):
726
727
  | FlowESTree.Statement
727
728
  | FlowESTree.ModuleDeclaration
728
- | $ReadOnlyArray<FlowESTree.Statement | FlowESTree.ModuleDeclaration> {
729
+ | ReadonlyArray<FlowESTree.Statement | FlowESTree.ModuleDeclaration> {
729
730
  switch (node.type) {
730
731
  case 'BlockStatement':
731
732
  return Transform.BlockStatement(node);
@@ -789,8 +790,8 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
789
790
 
790
791
  static Statement(
791
792
  node: TSESTree.Statement,
792
- ): FlowESTree.Statement | $ReadOnlyArray<FlowESTree.Statement> {
793
- return (Transform.AllStatement(node): $FlowFixMe);
793
+ ): FlowESTree.Statement | ReadonlyArray<FlowESTree.Statement> {
794
+ return Transform.AllStatement(node) as $FlowFixMe;
794
795
  }
795
796
 
796
797
  static TSAnyType(): FlowESTree.AnyTypeAnnotation {
@@ -873,6 +874,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
873
874
  },
874
875
  optional: false,
875
876
  },
877
+ implicitDeclare: false,
876
878
  predicate: null,
877
879
  });
878
880
  }
@@ -935,7 +937,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
935
937
  }
936
938
 
937
939
  static _partitionAndTranslateTSFunctionParams(
938
- tsParams: $ReadOnlyArray<TSESTree.Parameter>,
940
+ tsParams: ReadonlyArray<TSESTree.Parameter>,
939
941
  ): {
940
942
  thisParam: FlowESTree.FunctionTypeParam | null,
941
943
  restParam: FlowESTree.FunctionTypeParam | null,
@@ -1027,6 +1029,19 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1027
1029
  static TSImportType(
1028
1030
  node: TSESTree.TSImportType,
1029
1031
  ): FlowESTree.TypeAnnotationType {
1032
+ const source =
1033
+ node.argument ??
1034
+ (node.source == null
1035
+ ? null
1036
+ : ({
1037
+ type: 'TSLiteralType',
1038
+ loc: node.source.loc,
1039
+ literal: node.source,
1040
+ } as TSESTree.TSLiteralType));
1041
+ if (source == null) {
1042
+ return unsupportedAnnotation(node, 'import types without a source');
1043
+ }
1044
+
1030
1045
  let base: FlowESTree.TypeAnnotationType =
1031
1046
  constructFlowNode<FlowESTree.GenericTypeAnnotation>({
1032
1047
  type: 'GenericTypeAnnotation',
@@ -1039,15 +1054,15 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1039
1054
  typeParameters:
1040
1055
  constructFlowNode<FlowESTree.TypeParameterInstantiation>({
1041
1056
  type: 'TypeParameterInstantiation',
1042
- params: [Transform.TSTypeAnnotation(node.argument)],
1057
+ params: [Transform.TSTypeAnnotation(source)],
1043
1058
  }),
1044
1059
  });
1060
+ if (node.typeArguments != null) {
1061
+ return unsupportedAnnotation(node, 'import types with type arguments');
1062
+ }
1045
1063
  if (node.qualifier == null) {
1046
1064
  return base;
1047
1065
  }
1048
- if (node.typeParameters != null) {
1049
- return unsupportedAnnotation(node, 'import types with type parameters');
1050
- }
1051
1066
  let qualifier = Transform.EntityNameToTypeIdentifier(node.qualifier);
1052
1067
  const namesRev: Array<string> = [];
1053
1068
  while (qualifier.type !== 'Identifier') {
@@ -1243,8 +1258,8 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1243
1258
  node: TSESTree.TSMappedType,
1244
1259
  ): FlowESTree.ObjectTypeAnnotation {
1245
1260
  const keyTparam = Transform.TSTypeParameter(node.typeParameter);
1246
- const sourceType: FlowESTree.TypeAnnotationType = (keyTparam.bound
1247
- ?.typeAnnotation: $FlowFixMe);
1261
+ const sourceType: FlowESTree.TypeAnnotationType = keyTparam.bound
1262
+ ?.typeAnnotation as $FlowFixMe;
1248
1263
  // $FlowFixMe[cannot-write]
1249
1264
  keyTparam.bound = null;
1250
1265
  const prop = constructFlowNode<FlowESTree.ObjectTypeMappedTypeProperty>({
@@ -1303,7 +1318,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1303
1318
  if (node.id.type === 'Literal') {
1304
1319
  return constructFlowNode<FlowESTree.DeclareModule>({
1305
1320
  type: 'DeclareModule',
1306
- id: (Transform.Literal(node.id): $FlowFixMe),
1321
+ id: Transform.Literal(node.id) as $FlowFixMe,
1307
1322
  body,
1308
1323
  });
1309
1324
  }
@@ -1312,8 +1327,11 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1312
1327
  }
1313
1328
  return constructFlowNode<FlowESTree.DeclareNamespace>({
1314
1329
  type: 'DeclareNamespace',
1330
+ global: false,
1315
1331
  id: Transform.Identifier(node.id),
1316
1332
  body,
1333
+ implicitDeclare: false,
1334
+ keyword: 'namespace',
1317
1335
  });
1318
1336
  }
1319
1337
 
@@ -1712,7 +1730,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1712
1730
  ? (constructFlowNode<FlowESTree.PrivateIdentifier>({
1713
1731
  type: 'PrivateIdentifier',
1714
1732
  name: key.name,
1715
- }): $FlowFixMe)
1733
+ }) as $FlowFixMe)
1716
1734
  : constructFlowNode<FlowESTree.StringLiteral>({
1717
1735
  type: 'Literal',
1718
1736
  literalType: 'string',
@@ -1767,7 +1785,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1767
1785
  ? (constructFlowNode<FlowESTree.PrivateIdentifier>({
1768
1786
  type: 'PrivateIdentifier',
1769
1787
  name: originalKey.name,
1770
- }): $FlowFixMe)
1788
+ }) as $FlowFixMe)
1771
1789
  : constructFlowNode<FlowESTree.StringLiteral>({
1772
1790
  type: 'Literal',
1773
1791
  literalType: 'string',
@@ -1960,12 +1978,22 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
1960
1978
 
1961
1979
  static TSTypeQuery(
1962
1980
  node: TSESTree.TSTypeQuery,
1963
- ): FlowESTree.TypeofTypeAnnotation {
1981
+ ): FlowESTree.TypeAnnotationType {
1982
+ if (node.exprName.type === 'TSImportType') {
1983
+ if (node.typeArguments != null) {
1984
+ return unsupportedAnnotation(
1985
+ node,
1986
+ 'type query imports with type arguments',
1987
+ );
1988
+ }
1989
+ return Transform.TSImportType(node.exprName);
1990
+ }
1991
+
1964
1992
  return constructFlowNode<FlowESTree.TypeofTypeAnnotation>({
1965
1993
  type: 'TypeofTypeAnnotation',
1966
1994
  argument: Transform.EntityNameToTypeofIdentifier(node.exprName),
1967
1995
  typeArguments:
1968
- Transform.TSTypeParameterInstantiationOpt(node.typeParameters) ??
1996
+ Transform.TSTypeParameterInstantiationOpt(node.typeArguments) ??
1969
1997
  undefined,
1970
1998
  });
1971
1999
  }
@@ -2005,7 +2033,7 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
2005
2033
 
2006
2034
  static VariableDeclaration(
2007
2035
  node: TSESTree.VariableDeclaration,
2008
- ): $ReadOnlyArray<FlowESTree.DeclareVariable> {
2036
+ ): ReadonlyArray<FlowESTree.DeclareVariable> {
2009
2037
  return node.declarations.map(decl => {
2010
2038
  if (decl.id.type !== 'Identifier') {
2011
2039
  throw translationError(
@@ -2025,8 +2053,15 @@ const getTransforms = (originalCode: string, opts: TranslationOptions) => {
2025
2053
  }
2026
2054
  return constructFlowNode<FlowESTree.DeclareVariable>({
2027
2055
  type: 'DeclareVariable',
2028
- id,
2056
+ declarations: [
2057
+ constructFlowNode<FlowESTree.VariableDeclarator>({
2058
+ type: 'VariableDeclarator',
2059
+ id,
2060
+ init: null,
2061
+ }),
2062
+ ],
2029
2063
  kind: node.kind,
2064
+ implicitDeclare: false,
2030
2065
  });
2031
2066
  });
2032
2067
  }