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.
@@ -11,144 +11,24 @@
11
11
  'use strict';
12
12
 
13
13
  import type {ScopeManager} from 'hermes-eslint';
14
- import type {ESNode, Program} from 'hermes-estree';
14
+ import type {Program} from 'hermes-estree';
15
+ import type {ParserOptions} from 'hermes-parser';
15
16
 
17
+ import {Transforms} from 'hermes-parser';
18
+ import {astNodeMutationHelpers} from 'hermes-parser';
16
19
  import {removeAtFlowFromDocblock} from './utils/DocblockUtils';
17
20
 
18
- import {SimpleTransform, astNodeMutationHelpers} from 'hermes-parser';
19
21
  const {nodeWith} = astNodeMutationHelpers;
20
22
 
21
- function transform(node: ESNode): ESNode | null {
22
- switch (node.type) {
23
- case 'Program': {
24
- const docblock = node.docblock;
25
- if (docblock == null) {
26
- return node;
27
- }
28
-
29
- return nodeWith(node, {
30
- docblock: removeAtFlowFromDocblock(docblock),
31
- });
32
- }
33
-
34
- case 'TypeCastExpression': {
35
- return node.expression;
36
- }
37
-
38
- case 'CallExpression':
39
- case 'NewExpression': {
40
- if (node.typeArguments != null) {
41
- return nodeWith(node, {typeArguments: null});
42
- }
43
- return node;
44
- }
45
-
46
- case 'ObjectPattern':
47
- case 'ArrayPattern':
48
- case 'Identifier': {
49
- if (node.typeAnnotation != null) {
50
- return nodeWith(node, {typeAnnotation: null});
51
- }
52
- return node;
53
- }
54
-
55
- case 'DeclareClass':
56
- case 'DeclareFunction':
57
- case 'DeclareInterface':
58
- case 'DeclareModule':
59
- case 'DeclareModuleExports':
60
- case 'DeclareOpaqueType':
61
- case 'DeclareTypeAlias':
62
- case 'DeclareVariable':
63
- case 'InterfaceDeclaration':
64
- case 'OpaqueType':
65
- case 'TypeAlias': {
66
- return null;
67
- }
68
-
69
- case 'FunctionDeclaration':
70
- case 'ArrowFunctionExpression':
71
- case 'FunctionExpression': {
72
- const newParams = [];
73
- for (let i = 0; i < node.params.length; i++) {
74
- if (
75
- i === 0 &&
76
- node.params[0].type === 'Identifier' &&
77
- node.params[0].name === 'this'
78
- ) {
79
- continue;
80
- }
81
-
82
- let param = node.params[i];
83
- if (param.type === 'AssignmentPattern') {
84
- param = param.left;
85
- }
86
- if (param.optional === true) {
87
- param = nodeWith(param, {optional: false});
88
- }
89
- newParams.push(param);
90
- }
91
-
92
- return nodeWith(node, {
93
- params: newParams,
94
- returnType: null,
95
- typeParameters: null,
96
- predicate: null,
97
- });
98
- }
99
-
100
- case 'ClassDeclaration':
101
- case 'ClassExpression': {
102
- return nodeWith(node, {
103
- typeParameters: null,
104
- superTypeParameters: null,
105
- implements: [],
106
- decorators: [],
107
- });
108
- }
109
-
110
- case 'PropertyDefinition': {
111
- return nodeWith(node, {
112
- typeAnnotation: null,
113
- variance: null,
114
- declare: false,
115
- optional: false,
116
- });
117
- }
118
-
119
- case 'ImportDeclaration': {
120
- if (node.importKind === 'type' || node.importKind === 'typeof') {
121
- return null;
122
- }
123
- const nonTypeSpecifiers = node.specifiers.filter(
124
- s =>
125
- s.type !== 'ImportSpecifier' ||
126
- (s.importKind !== 'type' && s.importKind !== 'typeof'),
127
- );
128
- if (nonTypeSpecifiers.length === 0) {
129
- return null;
130
- }
131
- if (nonTypeSpecifiers.length === node.specifiers.length) {
132
- return node;
133
- }
134
-
135
- return nodeWith(node, {
136
- specifiers: nonTypeSpecifiers,
137
- });
138
- }
139
-
140
- case 'ExportAllDeclaration':
141
- case 'ExportNamedDeclaration': {
142
- if (node.exportKind === 'type') {
143
- return null;
144
- }
145
- return node;
146
- }
147
-
148
- default: {
149
- return node;
150
- }
23
+ function stripAtFlow(ast: Program, _options: ParserOptions): Program {
24
+ const docblock = ast.docblock;
25
+ if (docblock == null) {
26
+ return ast;
151
27
  }
28
+
29
+ return nodeWith(ast, {
30
+ docblock: removeAtFlowFromDocblock(docblock),
31
+ });
152
32
  }
153
33
 
154
34
  export function flowToJS(
@@ -156,11 +36,9 @@ export function flowToJS(
156
36
  _code: string,
157
37
  _scopeManager: ScopeManager,
158
38
  ): Program {
159
- const result = SimpleTransform.transform(sourceAST, {
160
- transform,
161
- });
162
- if (result == null || result.type !== 'Program') {
163
- throw new Error('flowToJS: Unexpected transform result.');
164
- }
165
- return result;
39
+ return [
40
+ Transforms.stripComponentSyntax,
41
+ Transforms.stripFlowTypes,
42
+ stripAtFlow,
43
+ ].reduce((ast, transform) => transform(ast, {}), sourceAST);
166
44
  }
@@ -95,7 +95,7 @@ export interface AwaitExpression extends BaseNode {
95
95
  }
96
96
  export interface BigIntLiteral extends LiteralBase {
97
97
  +type: 'Literal';
98
- +value: /*bigint |*/ null;
98
+ +value: bigint;
99
99
  +bigint: string;
100
100
  }
101
101
  export interface BinaryExpression extends BaseNode {
@@ -784,7 +784,7 @@ export type Literal =
784
784
  interface LiteralBase extends BaseNode {
785
785
  +type: 'Literal';
786
786
  +raw: string;
787
- +value: RegExp | /*bigint |*/ boolean | number | string | null;
787
+ +value: RegExp | bigint | boolean | number | string | null;
788
788
  }
789
789
  export type LiteralExpression = Literal | TemplateLiteral;
790
790
  export interface LogicalExpression extends BaseNode {
@@ -1589,7 +1589,7 @@ export interface TSImportEqualsDeclaration extends BaseNode {
1589
1589
  export interface TSImportType extends BaseNode {
1590
1590
  +type: 'TSImportType';
1591
1591
  +isTypeOf: boolean;
1592
- +parameter: TypeNode;
1592
+ +argument: TypeNode;
1593
1593
  +qualifier: EntityName | null;
1594
1594
  +typeParameters: TSTypeParameterInstantiation | null;
1595
1595
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-api-translator",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Toolkit for creating Flow and TypeScript compatible libraries from Flow source code.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -12,10 +12,10 @@
12
12
  "@babel/code-frame": "^7.16.0",
13
13
  "@typescript-eslint/visitor-keys": "^5.42.0",
14
14
  "flow-enums-runtime": "^0.0.6",
15
- "hermes-eslint": "0.15.0",
16
- "hermes-estree": "0.15.0",
17
- "hermes-parser": "0.15.0",
18
- "hermes-transform": "0.15.0"
15
+ "hermes-eslint": "0.16.0",
16
+ "hermes-estree": "0.16.0",
17
+ "hermes-parser": "0.16.0",
18
+ "hermes-transform": "0.16.0"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "prettier": "^3.0.0 || ^2.7.1"