flow-api-translator 0.15.0 → 0.16.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.
@@ -265,9 +265,15 @@ function stripUnusedDefs(detachedStmt, usedDeps, context) {
265
265
 
266
266
  function convertStatement(stmt, context) {
267
267
  switch (stmt.type) {
268
+ case 'ComponentDeclaration':
269
+ {
270
+ const [result, deps] = convertComponentDeclaration(stmt, context);
271
+ return [result, deps];
272
+ }
273
+
268
274
  case 'FunctionDeclaration':
269
275
  {
270
- const [result, deps] = convertFunctionDeclation(stmt, context);
276
+ const [result, deps] = convertFunctionDeclaration(stmt, context);
271
277
  return [result, deps];
272
278
  }
273
279
 
@@ -491,9 +497,19 @@ function convertLiteral(expr, context) {
491
497
 
492
498
  function convertExportDeclaration(decl, opts, context) {
493
499
  switch (decl.type) {
500
+ case 'ComponentDeclaration':
501
+ {
502
+ const [declDecl, deps] = convertComponentDeclaration(decl, context);
503
+ return [opts.default ? _hermesTransform.t.DeclareExportDefaultDeclaration({
504
+ declaration: declDecl
505
+ }) : _hermesTransform.t.DeclareExportDeclarationNamedWithDeclaration({
506
+ declaration: declDecl
507
+ }), deps];
508
+ }
509
+
494
510
  case 'FunctionDeclaration':
495
511
  {
496
- const [declDecl, deps] = convertFunctionDeclation(decl, context);
512
+ const [declDecl, deps] = convertFunctionDeclaration(decl, context);
497
513
  return [opts.default ? _hermesTransform.t.DeclareExportDefaultDeclaration({
498
514
  declaration: declDecl
499
515
  }) : _hermesTransform.t.DeclareExportDeclarationNamedWithDeclaration({
@@ -588,6 +604,20 @@ function convertExportDeclaration(decl, opts, context) {
588
604
  }
589
605
 
590
606
  function convertExportDefaultDeclaration(stmt, context) {
607
+ const expr = stmt.declaration;
608
+
609
+ if ((0, _hermesEstree.isExpression)(expr) && expr.type === 'Identifier') {
610
+ const name = expr.name;
611
+ const [declDecl, deps] = [_hermesTransform.t.TypeofTypeAnnotation({
612
+ argument: _hermesTransform.t.Identifier({
613
+ name
614
+ })
615
+ }), (0, _FlowAnalyze.analyzeTypeDependencies)(expr, context)];
616
+ return [_hermesTransform.t.DeclareExportDefaultDeclaration({
617
+ declaration: declDecl
618
+ }), deps];
619
+ }
620
+
591
621
  return convertExportDeclaration(stmt.declaration, {
592
622
  default: true
593
623
  }, context);
@@ -637,6 +667,14 @@ function convertVariableDeclaration(stmt, context) {
637
667
  return [(0, _ErrorUtils.flowFixMeOrError)(first, `VariableDeclaration: Type annotation missing`, context), []];
638
668
  }
639
669
 
670
+ if (init.type === 'Identifier') {
671
+ return [_hermesTransform.t.TypeofTypeAnnotation({
672
+ argument: _hermesTransform.t.Identifier({
673
+ name: init.name
674
+ })
675
+ }), (0, _FlowAnalyze.analyzeTypeDependencies)(init, context)];
676
+ }
677
+
640
678
  return convertExpressionToTypeAnnotation(init, context);
641
679
  })();
642
680
 
@@ -783,7 +821,86 @@ function convertClassMember(member, context) {
783
821
  }
784
822
  }
785
823
 
786
- function convertFunctionDeclation(func, context) {
824
+ function convertComponentDeclaration(comp, context) {
825
+ const [resultTypeParams, typeParamsDeps] = convertTypeParameterDeclarationOrNull(comp.typeParameters, context);
826
+ const [resultParams, resultRestParam, paramsAndRestDeps] = convertComponentParameters(comp.params, context);
827
+
828
+ const [resultRendersType, rendersTypeDeps] = (() => {
829
+ const rendersType = comp.rendersType;
830
+
831
+ if (rendersType == null) {
832
+ return EMPTY_TRANSLATION_RESULT;
833
+ }
834
+
835
+ return [(0, _hermesTransform.asDetachedNode)(rendersType), (0, _FlowAnalyze.analyzeTypeDependencies)(rendersType, context)];
836
+ })();
837
+
838
+ return [_hermesTransform.t.DeclareComponent({
839
+ id: comp.id,
840
+ params: resultParams,
841
+ rest: resultRestParam,
842
+ typeParameters: resultTypeParams,
843
+ rendersType: resultRendersType
844
+ }), [...typeParamsDeps, ...paramsAndRestDeps, ...rendersTypeDeps]];
845
+ }
846
+
847
+ function convertComponentParameters(params, context) {
848
+ return params.reduce(([resultParams, restParam, paramsDeps], param) => {
849
+ switch (param.type) {
850
+ case 'ComponentParameter':
851
+ {
852
+ let optional = false;
853
+ let local = param.local;
854
+
855
+ if (local.type === 'AssignmentPattern') {
856
+ local = local.left;
857
+ optional = true;
858
+ }
859
+
860
+ if (!optional && local.type === 'Identifier') {
861
+ optional = local.optional;
862
+ }
863
+
864
+ const [typeAnnotationType, typeDeps] = convertTypeAnnotation(local.typeAnnotation, param, context);
865
+
866
+ const resultParam = _hermesTransform.t.ComponentTypeParameter({
867
+ name: (0, _hermesTransform.asDetachedNode)(param.name),
868
+ typeAnnotation: typeAnnotationType,
869
+ optional
870
+ });
871
+
872
+ return [[...resultParams, resultParam], restParam, [...paramsDeps, ...typeDeps]];
873
+ }
874
+
875
+ case 'RestElement':
876
+ {
877
+ if (restParam != null) {
878
+ throw (0, _ErrorUtils.translationError)(param, `ComponentParameter: Multiple rest elements found`, context);
879
+ }
880
+
881
+ const argument = param.argument;
882
+
883
+ if (argument.type === 'AssignmentPattern' || argument.type === 'ArrayPattern' || argument.type === 'RestElement') {
884
+ throw (0, _ErrorUtils.translationError)(param, `ComponentParameter: Invalid RestElement usage`, context);
885
+ }
886
+
887
+ const [typeAnnotationType, typeDeps] = convertTypeAnnotation(argument.typeAnnotation, argument, context);
888
+
889
+ const resultRestParam = _hermesTransform.t.ComponentTypeParameter({
890
+ name: _hermesTransform.t.Identifier({
891
+ name: argument.type === 'Identifier' ? argument.name : 'rest'
892
+ }),
893
+ typeAnnotation: typeAnnotationType,
894
+ optional: argument.type === 'Identifier' ? argument.optional : false
895
+ });
896
+
897
+ return [resultParams, resultRestParam, [...paramsDeps, ...typeDeps]];
898
+ }
899
+ }
900
+ }, [[], null, []]);
901
+ }
902
+
903
+ function convertFunctionDeclaration(func, context) {
787
904
  const id = func.id;
788
905
 
789
906
  if (id == null) {
@@ -18,7 +18,11 @@ import type {
18
18
  ClassMember,
19
19
  ClassPropertyNameComputed,
20
20
  ClassPropertyNameNonComputed,
21
+ ComponentDeclaration,
22
+ ComponentParameter,
23
+ ComponentTypeParameter,
21
24
  DeclareClass,
25
+ DeclareComponent,
22
26
  DeclareFunction,
23
27
  DeclareOpaqueType,
24
28
  DeclareVariable,
@@ -43,6 +47,7 @@ import type {
43
47
  ObjectTypeProperty,
44
48
  OpaqueType,
45
49
  Program,
50
+ RestElement,
46
51
  Statement,
47
52
  StringLiteral,
48
53
  TypeAlias,
@@ -356,8 +361,12 @@ function convertStatement(
356
361
  context: TranslationContext,
357
362
  ): TranslatedResult<ProgramStatement> {
358
363
  switch (stmt.type) {
364
+ case 'ComponentDeclaration': {
365
+ const [result, deps] = convertComponentDeclaration(stmt, context);
366
+ return [result, deps];
367
+ }
359
368
  case 'FunctionDeclaration': {
360
- const [result, deps] = convertFunctionDeclation(stmt, context);
369
+ const [result, deps] = convertFunctionDeclaration(stmt, context);
361
370
  return [result, deps];
362
371
  }
363
372
  case 'ClassDeclaration': {
@@ -608,8 +617,21 @@ function convertExportDeclaration(
608
617
  context: TranslationContext,
609
618
  ): TranslatedResult<ProgramStatement> {
610
619
  switch (decl.type) {
620
+ case 'ComponentDeclaration': {
621
+ const [declDecl, deps] = convertComponentDeclaration(decl, context);
622
+ return [
623
+ opts.default
624
+ ? t.DeclareExportDefaultDeclaration({
625
+ declaration: declDecl,
626
+ })
627
+ : t.DeclareExportDeclarationNamedWithDeclaration({
628
+ declaration: declDecl,
629
+ }),
630
+ deps,
631
+ ];
632
+ }
611
633
  case 'FunctionDeclaration': {
612
- const [declDecl, deps] = convertFunctionDeclation(decl, context);
634
+ const [declDecl, deps] = convertFunctionDeclaration(decl, context);
613
635
  return [
614
636
  opts.default
615
637
  ? t.DeclareExportDefaultDeclaration({
@@ -744,6 +766,20 @@ function convertExportDefaultDeclaration(
744
766
  stmt: ExportDefaultDeclaration,
745
767
  context: TranslationContext,
746
768
  ): TranslatedResult<ProgramStatement> {
769
+ const expr = stmt.declaration;
770
+ if (isExpression(expr) && (expr: Expression).type === 'Identifier') {
771
+ const name = ((expr: $FlowFixMe): Identifier).name;
772
+ const [declDecl, deps] = [
773
+ t.TypeofTypeAnnotation({argument: t.Identifier({name})}),
774
+ analyzeTypeDependencies(expr, context),
775
+ ];
776
+ return [
777
+ t.DeclareExportDefaultDeclaration({
778
+ declaration: declDecl,
779
+ }),
780
+ deps,
781
+ ];
782
+ }
747
783
  return convertExportDeclaration(stmt.declaration, {default: true}, context);
748
784
  }
749
785
 
@@ -814,6 +850,13 @@ function convertVariableDeclaration(
814
850
  ];
815
851
  }
816
852
 
853
+ if (init.type === 'Identifier') {
854
+ return [
855
+ t.TypeofTypeAnnotation({argument: t.Identifier({name: init.name})}),
856
+ analyzeTypeDependencies(init, context),
857
+ ];
858
+ }
859
+
817
860
  return convertExpressionToTypeAnnotation(init, context);
818
861
  })();
819
862
 
@@ -1048,8 +1091,126 @@ function convertClassMember(
1048
1091
  }
1049
1092
  }
1050
1093
  }
1094
+ function convertComponentDeclaration(
1095
+ comp: ComponentDeclaration,
1096
+ context: TranslationContext,
1097
+ ): TranslatedResult<DeclareComponent> {
1098
+ const [resultTypeParams, typeParamsDeps] =
1099
+ convertTypeParameterDeclarationOrNull(comp.typeParameters, context);
1100
+
1101
+ const [resultParams, resultRestParam, paramsAndRestDeps] =
1102
+ convertComponentParameters(comp.params, context);
1103
+
1104
+ const [resultRendersType, rendersTypeDeps] = (() => {
1105
+ const rendersType = comp.rendersType;
1106
+ if (rendersType == null) {
1107
+ return EMPTY_TRANSLATION_RESULT;
1108
+ }
1109
+
1110
+ return [
1111
+ asDetachedNode<TypeAnnotation>(rendersType),
1112
+ analyzeTypeDependencies(rendersType, context),
1113
+ ];
1114
+ })();
1115
+
1116
+ return [
1117
+ t.DeclareComponent({
1118
+ id: comp.id,
1119
+ params: resultParams,
1120
+ rest: resultRestParam,
1121
+ typeParameters: resultTypeParams,
1122
+ rendersType: resultRendersType,
1123
+ }),
1124
+ [...typeParamsDeps, ...paramsAndRestDeps, ...rendersTypeDeps],
1125
+ ];
1126
+ }
1127
+
1128
+ type TranslatedComponentParametersResults = [
1129
+ $ReadOnlyArray<DetachedNode<ComponentTypeParameter>>,
1130
+ ?DetachedNode<ComponentTypeParameter>,
1131
+ TranslatedDeps,
1132
+ ];
1133
+
1134
+ function convertComponentParameters(
1135
+ params: $ReadOnlyArray<ComponentParameter | RestElement>,
1136
+ context: TranslationContext,
1137
+ ): TranslatedComponentParametersResults {
1138
+ return params.reduce<TranslatedComponentParametersResults>(
1139
+ ([resultParams, restParam, paramsDeps], param) => {
1140
+ switch (param.type) {
1141
+ case 'ComponentParameter': {
1142
+ let optional = false;
1143
+ let local = param.local;
1144
+ if (local.type === 'AssignmentPattern') {
1145
+ local = local.left;
1146
+ optional = true;
1147
+ }
1148
+ if (!optional && local.type === 'Identifier') {
1149
+ optional = local.optional;
1150
+ }
1151
+
1152
+ const [typeAnnotationType, typeDeps] = convertTypeAnnotation(
1153
+ local.typeAnnotation,
1154
+ param,
1155
+ context,
1156
+ );
1157
+
1158
+ const resultParam = t.ComponentTypeParameter({
1159
+ name: asDetachedNode(param.name),
1160
+ typeAnnotation: typeAnnotationType,
1161
+ optional,
1162
+ });
1163
+
1164
+ return [
1165
+ [...resultParams, resultParam],
1166
+ restParam,
1167
+ [...paramsDeps, ...typeDeps],
1168
+ ];
1169
+ }
1170
+ case 'RestElement': {
1171
+ if (restParam != null) {
1172
+ throw translationError(
1173
+ param,
1174
+ `ComponentParameter: Multiple rest elements found`,
1175
+ context,
1176
+ );
1177
+ }
1178
+ const argument = param.argument;
1179
+ if (
1180
+ argument.type === 'AssignmentPattern' ||
1181
+ argument.type === 'ArrayPattern' ||
1182
+ argument.type === 'RestElement'
1183
+ ) {
1184
+ throw translationError(
1185
+ param,
1186
+ `ComponentParameter: Invalid RestElement usage`,
1187
+ context,
1188
+ );
1189
+ }
1190
+ const [typeAnnotationType, typeDeps] = convertTypeAnnotation(
1191
+ argument.typeAnnotation,
1192
+ argument,
1193
+ context,
1194
+ );
1195
+
1196
+ const resultRestParam = t.ComponentTypeParameter({
1197
+ name: t.Identifier({
1198
+ name: argument.type === 'Identifier' ? argument.name : 'rest',
1199
+ }),
1200
+ typeAnnotation: typeAnnotationType,
1201
+ optional:
1202
+ argument.type === 'Identifier' ? argument.optional : false,
1203
+ });
1204
+
1205
+ return [resultParams, resultRestParam, [...paramsDeps, ...typeDeps]];
1206
+ }
1207
+ }
1208
+ },
1209
+ [[], null, []],
1210
+ );
1211
+ }
1051
1212
 
1052
- function convertFunctionDeclation(
1213
+ function convertFunctionDeclaration(
1053
1214
  func: FunctionDeclaration,
1054
1215
  context: TranslationContext,
1055
1216
  ): TranslatedResult<DeclareFunction> {
package/dist/flowToJS.js CHANGED
@@ -14,175 +14,26 @@ Object.defineProperty(exports, "__esModule", {
14
14
  });
15
15
  exports.flowToJS = flowToJS;
16
16
 
17
- var _DocblockUtils = require("./utils/DocblockUtils");
18
-
19
17
  var _hermesParser = require("hermes-parser");
20
18
 
19
+ var _DocblockUtils = require("./utils/DocblockUtils");
20
+
21
21
  const {
22
22
  nodeWith
23
23
  } = _hermesParser.astNodeMutationHelpers;
24
24
 
25
- function transform(node) {
26
- switch (node.type) {
27
- case 'Program':
28
- {
29
- const docblock = node.docblock;
30
-
31
- if (docblock == null) {
32
- return node;
33
- }
34
-
35
- return nodeWith(node, {
36
- docblock: (0, _DocblockUtils.removeAtFlowFromDocblock)(docblock)
37
- });
38
- }
39
-
40
- case 'TypeCastExpression':
41
- {
42
- return node.expression;
43
- }
44
-
45
- case 'CallExpression':
46
- case 'NewExpression':
47
- {
48
- if (node.typeArguments != null) {
49
- return nodeWith(node, {
50
- typeArguments: null
51
- });
52
- }
53
-
54
- return node;
55
- }
56
-
57
- case 'ObjectPattern':
58
- case 'ArrayPattern':
59
- case 'Identifier':
60
- {
61
- if (node.typeAnnotation != null) {
62
- return nodeWith(node, {
63
- typeAnnotation: null
64
- });
65
- }
66
-
67
- return node;
68
- }
69
-
70
- case 'DeclareClass':
71
- case 'DeclareFunction':
72
- case 'DeclareInterface':
73
- case 'DeclareModule':
74
- case 'DeclareModuleExports':
75
- case 'DeclareOpaqueType':
76
- case 'DeclareTypeAlias':
77
- case 'DeclareVariable':
78
- case 'InterfaceDeclaration':
79
- case 'OpaqueType':
80
- case 'TypeAlias':
81
- {
82
- return null;
83
- }
84
-
85
- case 'FunctionDeclaration':
86
- case 'ArrowFunctionExpression':
87
- case 'FunctionExpression':
88
- {
89
- const newParams = [];
90
-
91
- for (let i = 0; i < node.params.length; i++) {
92
- if (i === 0 && node.params[0].type === 'Identifier' && node.params[0].name === 'this') {
93
- continue;
94
- }
95
-
96
- let param = node.params[i];
97
-
98
- if (param.type === 'AssignmentPattern') {
99
- param = param.left;
100
- }
101
-
102
- if (param.optional === true) {
103
- param = nodeWith(param, {
104
- optional: false
105
- });
106
- }
25
+ function stripAtFlow(ast, _options) {
26
+ const docblock = ast.docblock;
107
27
 
108
- newParams.push(param);
109
- }
110
-
111
- return nodeWith(node, {
112
- params: newParams,
113
- returnType: null,
114
- typeParameters: null,
115
- predicate: null
116
- });
117
- }
118
-
119
- case 'ClassDeclaration':
120
- case 'ClassExpression':
121
- {
122
- return nodeWith(node, {
123
- typeParameters: null,
124
- superTypeParameters: null,
125
- implements: [],
126
- decorators: []
127
- });
128
- }
129
-
130
- case 'PropertyDefinition':
131
- {
132
- return nodeWith(node, {
133
- typeAnnotation: null,
134
- variance: null,
135
- declare: false,
136
- optional: false
137
- });
138
- }
139
-
140
- case 'ImportDeclaration':
141
- {
142
- if (node.importKind === 'type' || node.importKind === 'typeof') {
143
- return null;
144
- }
145
-
146
- const nonTypeSpecifiers = node.specifiers.filter(s => s.type !== 'ImportSpecifier' || s.importKind !== 'type' && s.importKind !== 'typeof');
147
-
148
- if (nonTypeSpecifiers.length === 0) {
149
- return null;
150
- }
151
-
152
- if (nonTypeSpecifiers.length === node.specifiers.length) {
153
- return node;
154
- }
155
-
156
- return nodeWith(node, {
157
- specifiers: nonTypeSpecifiers
158
- });
159
- }
160
-
161
- case 'ExportAllDeclaration':
162
- case 'ExportNamedDeclaration':
163
- {
164
- if (node.exportKind === 'type') {
165
- return null;
166
- }
167
-
168
- return node;
169
- }
170
-
171
- default:
172
- {
173
- return node;
174
- }
28
+ if (docblock == null) {
29
+ return ast;
175
30
  }
176
- }
177
31
 
178
- function flowToJS(sourceAST, _code, _scopeManager) {
179
- const result = _hermesParser.SimpleTransform.transform(sourceAST, {
180
- transform
32
+ return nodeWith(ast, {
33
+ docblock: (0, _DocblockUtils.removeAtFlowFromDocblock)(docblock)
181
34
  });
35
+ }
182
36
 
183
- if (result == null || result.type !== 'Program') {
184
- throw new Error('flowToJS: Unexpected transform result.');
185
- }
186
-
187
- return result;
37
+ function flowToJS(sourceAST, _code, _scopeManager) {
38
+ return [_hermesParser.Transforms.stripComponentSyntax, _hermesParser.Transforms.stripFlowTypes, stripAtFlow].reduce((ast, transform) => transform(ast, {}), sourceAST);
188
39
  }