hermes-estree 0.32.0 → 0.32.1

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 (82) hide show
  1. package/dist/HermesAST.js.flow +57 -0
  2. package/dist/HermesASTAdapter.js +192 -0
  3. package/dist/HermesASTAdapter.js.flow +189 -0
  4. package/dist/HermesParser.js +108 -0
  5. package/dist/HermesParser.js.flow +161 -0
  6. package/dist/HermesParserDecodeUTF8String.js +68 -0
  7. package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
  8. package/dist/HermesParserDeserializer.js +242 -0
  9. package/dist/HermesParserDeserializer.js.flow +260 -0
  10. package/dist/HermesParserNodeDeserializers.js +2477 -0
  11. package/dist/HermesParserNodeDeserializers.js.flow +16 -0
  12. package/dist/HermesParserWASM.js +6 -0
  13. package/dist/HermesParserWASM.js.flow +87 -0
  14. package/dist/HermesToESTreeAdapter.js +439 -0
  15. package/dist/HermesToESTreeAdapter.js.flow +421 -0
  16. package/dist/ParserOptions.js +18 -0
  17. package/dist/ParserOptions.js.flow +41 -0
  18. package/dist/babel/TransformESTreeToBabel.js +1123 -0
  19. package/dist/babel/TransformESTreeToBabel.js.flow +1277 -0
  20. package/dist/estree/StripFlowTypes.js +175 -0
  21. package/dist/estree/StripFlowTypes.js.flow +158 -0
  22. package/dist/estree/StripFlowTypesForBabel.js +215 -0
  23. package/dist/estree/StripFlowTypesForBabel.js.flow +216 -0
  24. package/dist/estree/TransformComponentSyntax.js +788 -0
  25. package/dist/estree/TransformComponentSyntax.js.flow +864 -0
  26. package/dist/estree/TransformEnumSyntax.js +106 -0
  27. package/dist/estree/TransformEnumSyntax.js.flow +125 -0
  28. package/dist/estree/TransformMatchSyntax.js +1006 -0
  29. package/dist/estree/TransformMatchSyntax.js.flow +912 -0
  30. package/dist/generated/ESTreeVisitorKeys.js +220 -0
  31. package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
  32. package/dist/generated/ParserVisitorKeys.js +794 -0
  33. package/dist/generated/ParserVisitorKeys.js.flow +17 -0
  34. package/dist/getModuleDocblock.js +112 -0
  35. package/dist/getModuleDocblock.js.flow +118 -0
  36. package/dist/predicates.js +1 -0
  37. package/dist/predicates.js.flow +1 -0
  38. package/dist/src/HermesASTAdapter.js +192 -0
  39. package/dist/src/HermesParser.js +108 -0
  40. package/dist/src/HermesParserDecodeUTF8String.js +68 -0
  41. package/dist/src/HermesParserDeserializer.js +242 -0
  42. package/dist/src/HermesParserNodeDeserializers.js +2477 -0
  43. package/dist/src/HermesToESTreeAdapter.js +439 -0
  44. package/dist/src/ParserOptions.js +18 -0
  45. package/dist/src/babel/TransformESTreeToBabel.js +1123 -0
  46. package/dist/src/estree/StripFlowTypes.js +175 -0
  47. package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
  48. package/dist/src/estree/TransformComponentSyntax.js +788 -0
  49. package/dist/src/estree/TransformEnumSyntax.js +106 -0
  50. package/dist/src/estree/TransformMatchSyntax.js +1006 -0
  51. package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
  52. package/dist/src/generated/ParserVisitorKeys.js +794 -0
  53. package/dist/src/getModuleDocblock.js +112 -0
  54. package/dist/src/predicates.js +1 -0
  55. package/dist/src/transform/SimpleTransform.js +136 -0
  56. package/dist/src/transform/astArrayMutationHelpers.js +62 -0
  57. package/dist/src/transform/astNodeMutationHelpers.js +200 -0
  58. package/dist/src/traverse/SimpleTraverser.js +137 -0
  59. package/dist/src/traverse/getVisitorKeys.js +37 -0
  60. package/dist/src/utils/Builders.js +191 -0
  61. package/dist/src/utils/GenID.js +41 -0
  62. package/dist/src/utils/createSyntaxError.js +25 -0
  63. package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
  64. package/dist/transform/SimpleTransform.js +136 -0
  65. package/dist/transform/SimpleTransform.js.flow +169 -0
  66. package/dist/transform/astArrayMutationHelpers.js +62 -0
  67. package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
  68. package/dist/transform/astNodeMutationHelpers.js +200 -0
  69. package/dist/transform/astNodeMutationHelpers.js.flow +246 -0
  70. package/dist/traverse/SimpleTraverser.js +137 -0
  71. package/dist/traverse/SimpleTraverser.js.flow +133 -0
  72. package/dist/traverse/getVisitorKeys.js +37 -0
  73. package/dist/traverse/getVisitorKeys.js.flow +36 -0
  74. package/dist/utils/Builders.js +191 -0
  75. package/dist/utils/Builders.js.flow +218 -0
  76. package/dist/utils/GenID.js +41 -0
  77. package/dist/utils/GenID.js.flow +38 -0
  78. package/dist/utils/createSyntaxError.js +25 -0
  79. package/dist/utils/createSyntaxError.js.flow +24 -0
  80. package/dist/utils/mutateESTreeASTForPrettier.js +127 -0
  81. package/dist/utils/mutateESTreeASTForPrettier.js.flow +130 -0
  82. package/package.json +1 -1
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ /**
12
+ * This transform strips Flow types that are not supported past Babel 7.
13
+ *
14
+ * It is expected that all transforms create valid ESTree AST output. If
15
+ * the transform requires outputting Babel specific AST nodes then it
16
+ * should live in `ConvertESTreeToBabel.js`
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ import type {ParserOptions} from '../ParserOptions';
22
+ import type {
23
+ Program,
24
+ ESNode,
25
+ DeclareEnum,
26
+ DeclareNamespace,
27
+ DeclareVariable,
28
+ AnyTypeAnnotation,
29
+ GenericTypeAnnotation,
30
+ QualifiedTypeIdentifier,
31
+ QualifiedTypeofIdentifier,
32
+ AFunction,
33
+ } from 'hermes-estree';
34
+
35
+ import {SimpleTransform} from '../transform/SimpleTransform';
36
+ import {createSyntaxError} from '../utils/createSyntaxError';
37
+
38
+ const nodeWith = SimpleTransform.nodeWith;
39
+
40
+ // Rely on the mapper to fix up parent relationships.
41
+ const EMPTY_PARENT: $FlowFixMe = null;
42
+
43
+ function createSimpleGenericTypeAnnotation(
44
+ name: string,
45
+ nodeForLoc: ESNode,
46
+ ): GenericTypeAnnotation {
47
+ return {
48
+ type: 'GenericTypeAnnotation',
49
+ id: {
50
+ type: 'Identifier',
51
+ name,
52
+ optional: false,
53
+ typeAnnotation: null,
54
+ loc: nodeForLoc.loc,
55
+ range: nodeForLoc.range,
56
+ parent: EMPTY_PARENT,
57
+ },
58
+ typeParameters: null,
59
+ loc: nodeForLoc.loc,
60
+ range: nodeForLoc.range,
61
+ parent: nodeForLoc.parent,
62
+ };
63
+ }
64
+
65
+ function createAnyTypeAnnotation(node: ESNode): AnyTypeAnnotation {
66
+ return {
67
+ type: 'AnyTypeAnnotation',
68
+ loc: node.loc,
69
+ range: node.range,
70
+ parent: node.parent,
71
+ };
72
+ }
73
+
74
+ /**
75
+ * Convert DeclareEnum nodes to DeclareVariable
76
+ */
77
+ function mapDeclareEnum(node: DeclareEnum): DeclareVariable {
78
+ return {
79
+ type: 'DeclareVariable',
80
+ kind: 'const',
81
+ id: nodeWith(node.id, {
82
+ typeAnnotation: {
83
+ type: 'TypeAnnotation',
84
+ typeAnnotation: createAnyTypeAnnotation(node.body),
85
+ loc: node.body.loc,
86
+ range: node.body.range,
87
+ parent: EMPTY_PARENT,
88
+ },
89
+ }),
90
+ loc: node.loc,
91
+ range: node.range,
92
+ parent: node.parent,
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Convert DeclareNamespace nodes to DeclareVariable
98
+ */
99
+ function mapDeclareNamespace(node: DeclareNamespace): DeclareVariable {
100
+ return {
101
+ type: 'DeclareVariable',
102
+ kind: 'const',
103
+ id: nodeWith(node.id, {
104
+ typeAnnotation: {
105
+ type: 'TypeAnnotation',
106
+ typeAnnotation: createAnyTypeAnnotation(node.body),
107
+ loc: node.body.loc,
108
+ range: node.body.range,
109
+ parent: EMPTY_PARENT,
110
+ },
111
+ }),
112
+ loc: node.loc,
113
+ range: node.range,
114
+ parent: node.parent,
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Remove `this` param from functions.
120
+ */
121
+ function mapFunction(node: AFunction): AFunction {
122
+ // Remove the first parameter if it is a this-type annotation,
123
+ // which is not recognized by Babel.
124
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
125
+ return nodeWith(node, {
126
+ params: node.params.slice(1),
127
+ });
128
+ }
129
+
130
+ return node;
131
+ }
132
+
133
+ /**
134
+ * Convert to QualifiedTypeIdentifier
135
+ */
136
+ function mapQualifiedTypeofIdentifier(
137
+ node: QualifiedTypeofIdentifier,
138
+ ): QualifiedTypeIdentifier {
139
+ return {
140
+ type: 'QualifiedTypeIdentifier',
141
+ qualification:
142
+ node.qualification.type === 'QualifiedTypeofIdentifier'
143
+ ? mapQualifiedTypeofIdentifier(node.qualification)
144
+ : node.qualification,
145
+ id: node.id,
146
+ loc: node.loc,
147
+ range: node.range,
148
+ parent: node.parent,
149
+ };
150
+ }
151
+
152
+ export function transformProgram(
153
+ program: Program,
154
+ _options: ParserOptions,
155
+ ): Program {
156
+ return SimpleTransform.transformProgram(program, {
157
+ transform(node: ESNode) {
158
+ switch (node.type) {
159
+ case 'SymbolTypeAnnotation': {
160
+ // Convert to simple generic type annotation
161
+ return createSimpleGenericTypeAnnotation('symbol', node);
162
+ }
163
+ case 'BigIntTypeAnnotation': {
164
+ // Convert to simple generic type annotation
165
+ return createSimpleGenericTypeAnnotation('bigint', node);
166
+ }
167
+ case 'ObjectTypeAnnotation': {
168
+ const shouldStrip = node.properties.some(
169
+ prop => prop.type === 'ObjectTypeMappedTypeProperty',
170
+ );
171
+ if (shouldStrip) {
172
+ return createAnyTypeAnnotation(node);
173
+ }
174
+
175
+ return node;
176
+ }
177
+ case 'ObjectTypeMappedTypeProperty': {
178
+ throw createSyntaxError(
179
+ node,
180
+ `Invalid AST structure, ObjectTypeMappedTypeProperty found outside of an ObjectTypeAnnotation`,
181
+ );
182
+ }
183
+ case 'IndexedAccessType':
184
+ case 'OptionalIndexedAccessType':
185
+ case 'KeyofTypeAnnotation':
186
+ case 'ConditionalTypeAnnotation':
187
+ case 'InferTypeAnnotation':
188
+ case 'TupleTypeLabeledElement':
189
+ case 'TupleTypeSpreadElement':
190
+ case 'ComponentTypeAnnotation':
191
+ case 'HookTypeAnnotation':
192
+ case 'TypeOperator':
193
+ case 'TypePredicate': {
194
+ // Babel does not support these generic types, so convert to any
195
+ return createAnyTypeAnnotation(node);
196
+ }
197
+ case 'QualifiedTypeofIdentifier': {
198
+ return mapQualifiedTypeofIdentifier(node);
199
+ }
200
+ case 'DeclareEnum': {
201
+ return mapDeclareEnum(node);
202
+ }
203
+ case 'DeclareNamespace': {
204
+ return mapDeclareNamespace(node);
205
+ }
206
+ case 'FunctionDeclaration':
207
+ case 'FunctionExpression': {
208
+ return mapFunction(node);
209
+ }
210
+ default: {
211
+ return node;
212
+ }
213
+ }
214
+ },
215
+ });
216
+ }