flow-api-translator 0.37.0 → 0.328.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.
@@ -10,23 +10,26 @@
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
- import type {ScopeManager} from 'hermes-eslint';
15
+ import type {ScopeManager} from 'flow-eslint';
16
16
 
17
- import * as FlowESTree from 'hermes-estree';
17
+ import * as FlowESTree from 'flow-estree';
18
18
  import * as TSESTree from './utils/ts-estree-ast-types';
19
19
  import {
20
20
  cloneJSDocCommentsToNewNode as cloneJSDocCommentsToNewNodeOriginal,
21
21
  makeCommentOwnLine as makeCommentOwnLineOriginal,
22
- } from 'hermes-transform';
22
+ } from 'flow-transform';
23
23
  import {
24
24
  buildCodeFrame,
25
25
  translationError as translationErrorBase,
26
26
  unexpectedTranslationError as unexpectedTranslationErrorBase,
27
27
  } from './utils/ErrorUtils';
28
28
  import {removeAtFlowFromDocblock} from './utils/DocblockUtils';
29
- import {extractPropertyKeyLiterals} from './utils/TSNodeUtils';
29
+ import {
30
+ ensureTypeIncludesUndefined,
31
+ extractPropertyKeyLiterals,
32
+ } from './utils/TSNodeUtils';
30
33
  import {EOL} from 'os';
31
34
 
32
35
  type DeclarationOrUnsupported<T> = T | TSESTree.TSTypeAliasDeclaration;
@@ -54,6 +57,65 @@ const makeCommentOwnLine =
54
57
  // $FlowExpectedError[incompatible-type] - trust me this re-type is 100% safe
55
58
  makeCommentOwnLineOriginal as (string, unknown) => string;
56
59
 
60
+ /**
61
+ * `DeclareComponent` reaches us in one of two shapes: parsed nodes carry
62
+ * `ComponentParameter`/`RestElement` in `params` with a null `rest` (the
63
+ * `ComponentDeclaration` shape the Flow parser reuses here), while detached
64
+ * nodes built with `flow-transform` use `ComponentTypeParameter` + `rest`.
65
+ * Normalize both onto the latter so the shared `ComponentTypeParameters`
66
+ * translation — which `ComponentTypeAnnotation` also uses — sees one shape.
67
+ */
68
+ function normalizeDeclareComponentParams(
69
+ params: FlowESTree.DeclareComponent['params'],
70
+ rest: FlowESTree.DeclareComponent['rest'],
71
+ onUnexpected: (node: ObjectWithLoc, message: string) => Error,
72
+ ): [
73
+ ReadonlyArray<FlowESTree.ComponentTypeParameter>,
74
+ FlowESTree.ComponentTypeParameter | null,
75
+ ] {
76
+ const normalizedParams: Array<FlowESTree.ComponentTypeParameter> = [];
77
+ let normalizedRest: FlowESTree.ComponentTypeParameter | null = rest;
78
+
79
+ const toComponentTypeParameter = (
80
+ name: FlowESTree.ComponentTypeParameter['name'],
81
+ local: FlowESTree.ESNode,
82
+ source: FlowESTree.BaseToken,
83
+ ): FlowESTree.ComponentTypeParameter => {
84
+ if (local.type !== 'Identifier' || local.typeAnnotation == null) {
85
+ throw onUnexpected(
86
+ source,
87
+ `DeclareComponent: expected an annotated identifier parameter, got "${local.type}"`,
88
+ );
89
+ }
90
+ return constructFlowNode<FlowESTree.ComponentTypeParameter>({
91
+ type: 'ComponentTypeParameter',
92
+ name,
93
+ typeAnnotation: local.typeAnnotation.typeAnnotation,
94
+ optional: local.optional,
95
+ loc: source.loc,
96
+ range: source.range,
97
+ });
98
+ };
99
+
100
+ for (const param of params) {
101
+ switch (param.type) {
102
+ case 'ComponentTypeParameter':
103
+ normalizedParams.push(param);
104
+ break;
105
+ case 'ComponentParameter':
106
+ normalizedParams.push(
107
+ toComponentTypeParameter(param.name, param.local, param),
108
+ );
109
+ break;
110
+ case 'RestElement':
111
+ normalizedRest = toComponentTypeParameter(null, param.argument, param);
112
+ break;
113
+ }
114
+ }
115
+
116
+ return [normalizedParams, normalizedRest];
117
+ }
118
+
57
119
  const VALID_REACT_IMPORTS = new Set<string>(['React', 'react']);
58
120
 
59
121
  function isValidReactImportOrGlobal(id: FlowESTree.Identifier): boolean {
@@ -1562,7 +1624,15 @@ const getTransforms = (
1562
1624
  ? undefined
1563
1625
  : transform.TypeParameterDeclaration(node.typeParameters);
1564
1626
 
1565
- const params = transform.ComponentTypeParameters(node.params, node.rest);
1627
+ const [declaredParams, declaredRest] = normalizeDeclareComponentParams(
1628
+ node.params,
1629
+ node.rest,
1630
+ unexpectedTranslationError,
1631
+ );
1632
+ const params = transform.ComponentTypeParameters(
1633
+ declaredParams,
1634
+ declaredRest,
1635
+ );
1566
1636
 
1567
1637
  // TS cannot support `renderType` so we always use ReactNode as the return type.
1568
1638
  const hasReactImport = isReactImport(node, 'React');
@@ -1861,15 +1931,21 @@ const getTransforms = (
1861
1931
  type: 'VariableDeclaration',
1862
1932
  loc: DUMMY_LOC,
1863
1933
  declare: true,
1864
- declarations: [
1865
- {
1934
+ declarations: node.declarations.map(declaration => {
1935
+ if (declaration.id.type !== 'Identifier') {
1936
+ throw translationError(
1937
+ declaration.id,
1938
+ `DeclareVariable: unsupported declarator of type "${declaration.id.type}"`,
1939
+ );
1940
+ }
1941
+ return {
1866
1942
  type: 'VariableDeclarator',
1867
1943
  loc: DUMMY_LOC,
1868
1944
  declare: true,
1869
- id: transform.Identifier(node.id, true),
1945
+ id: transform.Identifier(declaration.id, true),
1870
1946
  init: null,
1871
- },
1872
- ],
1947
+ };
1948
+ }),
1873
1949
  kind: node.kind,
1874
1950
  };
1875
1951
  },
@@ -3851,7 +3927,12 @@ const getTransforms = (
3851
3927
  typeAnnotation: {
3852
3928
  type: 'TSTypeAnnotation',
3853
3929
  loc: DUMMY_LOC,
3854
- typeAnnotation: transformTypeAnnotationType(node.value),
3930
+ typeAnnotation:
3931
+ node.optional === true
3932
+ ? ensureTypeIncludesUndefined(
3933
+ transformTypeAnnotationType(node.value),
3934
+ )
3935
+ : transformTypeAnnotationType(node.value),
3855
3936
  },
3856
3937
  };
3857
3938
  },
@@ -4,61 +4,50 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.flowImportTo = flowImportTo;
7
-
8
- var _hermesParser = require("hermes-parser");
9
-
7
+ var _flowParser = require("flow-parser");
10
8
  function flowImportTo(ast, _code, _scopeManager, opts) {
11
9
  function mapSource(source) {
12
10
  const resultValue = opts.sourceMapper({
13
11
  module: source.value
14
12
  });
15
-
16
13
  if (resultValue === source.value) {
17
14
  return source;
18
15
  }
19
-
20
- return _hermesParser.SimpleTransform.nodeWith(source, {
16
+ return _flowParser.SimpleTransform.nodeWith(source, {
21
17
  value: resultValue,
22
18
  raw: `"${resultValue}"`
23
19
  });
24
20
  }
25
-
26
- const result = _hermesParser.SimpleTransform.transform(ast, {
21
+ const result = _flowParser.SimpleTransform.transform(ast, {
27
22
  transform(node) {
28
23
  switch (node.type) {
29
24
  case 'ImportDeclaration':
30
25
  {
31
- return _hermesParser.SimpleTransform.nodeWith(node, {
26
+ return _flowParser.SimpleTransform.nodeWith(node, {
32
27
  source: mapSource(node.source)
33
28
  });
34
29
  }
35
-
36
30
  case 'DeclareExportAllDeclaration':
37
31
  case 'ExportAllDeclaration':
38
32
  case 'DeclareExportDeclaration':
39
33
  case 'ExportNamedDeclaration':
40
34
  {
41
35
  if (node.source != null) {
42
- return _hermesParser.SimpleTransform.nodeWith(node, {
36
+ return _flowParser.SimpleTransform.nodeWith(node, {
43
37
  source: mapSource(node.source)
44
38
  });
45
39
  }
46
-
47
40
  return node;
48
41
  }
49
-
50
42
  default:
51
43
  {
52
44
  return node;
53
45
  }
54
46
  }
55
47
  }
56
-
57
48
  });
58
-
59
49
  if (result == null || result.type !== 'Program') {
60
50
  throw new Error('flowImportTo: Unexpected transform result.');
61
51
  }
62
-
63
52
  return result;
64
53
  }
@@ -10,10 +10,10 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {ScopeManager} from 'hermes-eslint';
14
- import type {Program, ESNode, StringLiteral} from 'hermes-estree';
13
+ import type {ScopeManager} from 'flow-eslint';
14
+ import type {Program, ESNode, StringLiteral} from 'flow-estree';
15
15
 
16
- import {SimpleTransform} from 'hermes-parser';
16
+ import {SimpleTransform} from 'flow-parser';
17
17
 
18
18
  export type SourceMapperParams = {
19
19
  module: string,