hermes-parser 0.28.1 → 0.29.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 (41) hide show
  1. package/dist/HermesASTAdapter.js +1 -2
  2. package/dist/HermesASTAdapter.js.flow +2 -5
  3. package/dist/HermesParserNodeDeserializers.js +3 -2
  4. package/dist/HermesParserWASM.js +1 -1
  5. package/dist/HermesToESTreeAdapter.js +12 -0
  6. package/dist/HermesToESTreeAdapter.js.flow +10 -0
  7. package/dist/babel/TransformESTreeToBabel.js.flow +21 -19
  8. package/dist/estree/StripComponentSyntax.js.flow +6 -5
  9. package/dist/estree/TransformMatchSyntax.js +4 -15
  10. package/dist/estree/TransformMatchSyntax.js.flow +18 -19
  11. package/dist/generated/ESTreeVisitorKeys.js +1 -1
  12. package/dist/generated/ParserVisitorKeys.js +1 -1
  13. package/dist/index.js.flow +1 -1
  14. package/dist/src/HermesASTAdapter.js +192 -0
  15. package/dist/src/HermesParser.js +108 -0
  16. package/dist/src/HermesParserDecodeUTF8String.js +68 -0
  17. package/dist/src/HermesParserDeserializer.js +243 -0
  18. package/dist/src/HermesParserNodeDeserializers.js +2473 -0
  19. package/dist/src/HermesToESTreeAdapter.js +453 -0
  20. package/dist/src/ParserOptions.js +18 -0
  21. package/dist/src/babel/TransformESTreeToBabel.js +1104 -0
  22. package/dist/src/estree/StripComponentSyntax.js +788 -0
  23. package/dist/src/estree/StripFlowTypes.js +175 -0
  24. package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
  25. package/dist/src/estree/TransformMatchSyntax.js +1005 -0
  26. package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
  27. package/dist/src/generated/ParserVisitorKeys.js +790 -0
  28. package/dist/src/getModuleDocblock.js +112 -0
  29. package/dist/src/index.js +153 -0
  30. package/dist/src/transform/SimpleTransform.js +120 -0
  31. package/dist/src/transform/astArrayMutationHelpers.js +62 -0
  32. package/dist/src/transform/astNodeMutationHelpers.js +195 -0
  33. package/dist/src/traverse/SimpleTraverser.js +137 -0
  34. package/dist/src/traverse/getVisitorKeys.js +37 -0
  35. package/dist/src/utils/Builders.js +191 -0
  36. package/dist/src/utils/GenID.js +41 -0
  37. package/dist/src/utils/createSyntaxError.js +25 -0
  38. package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
  39. package/dist/utils/Builders.js +17 -0
  40. package/dist/utils/Builders.js.flow +20 -0
  41. package/package.json +2 -2
@@ -0,0 +1,175 @@
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
+ *
8
+ * @format
9
+ */
10
+
11
+ /**
12
+ * This transform strips all Flow types.
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
+ 'use strict';
19
+
20
+ Object.defineProperty(exports, "__esModule", {
21
+ value: true
22
+ });
23
+ exports.transformProgram = transformProgram;
24
+
25
+ var _SimpleTransform = require("../transform/SimpleTransform");
26
+
27
+ const nodeWith = _SimpleTransform.SimpleTransform.nodeWith;
28
+
29
+ function transformProgram(program, _options) {
30
+ return _SimpleTransform.SimpleTransform.transformProgram(program, {
31
+ transform(node) {
32
+ switch (node.type) {
33
+ case 'AsExpression':
34
+ case 'AsConstExpression':
35
+ case 'TypeCastExpression':
36
+ {
37
+ return node.expression;
38
+ }
39
+
40
+ case 'CallExpression':
41
+ case 'NewExpression':
42
+ {
43
+ if (node.typeArguments != null) {
44
+ return nodeWith(node, {
45
+ typeArguments: null
46
+ });
47
+ }
48
+
49
+ return node;
50
+ }
51
+
52
+ case 'ObjectPattern':
53
+ case 'ArrayPattern':
54
+ case 'Identifier':
55
+ {
56
+ if (node.typeAnnotation != null) {
57
+ return nodeWith(node, {
58
+ typeAnnotation: null
59
+ });
60
+ }
61
+
62
+ return node;
63
+ }
64
+
65
+ case 'DeclareClass':
66
+ case 'DeclareFunction':
67
+ case 'DeclareInterface':
68
+ case 'DeclareModule':
69
+ case 'DeclareModuleExports':
70
+ case 'DeclareNamespace':
71
+ case 'DeclareOpaqueType':
72
+ case 'DeclareTypeAlias':
73
+ case 'DeclareVariable':
74
+ case 'InterfaceDeclaration':
75
+ case 'OpaqueType':
76
+ case 'TypeAlias':
77
+ {
78
+ return null;
79
+ }
80
+
81
+ case 'FunctionDeclaration':
82
+ case 'ArrowFunctionExpression':
83
+ case 'FunctionExpression':
84
+ {
85
+ const newParams = [];
86
+
87
+ for (let i = 0; i < node.params.length; i++) {
88
+ if (i === 0 && node.params[0].type === 'Identifier' && node.params[0].name === 'this') {
89
+ continue;
90
+ }
91
+
92
+ let param = node.params[i];
93
+
94
+ if (param.type === 'AssignmentPattern') {
95
+ param = param.left;
96
+ }
97
+
98
+ if (param.optional === true) {
99
+ param = nodeWith(param, {
100
+ optional: false
101
+ });
102
+ }
103
+
104
+ newParams.push(param);
105
+ }
106
+
107
+ return nodeWith(node, {
108
+ params: newParams,
109
+ returnType: null,
110
+ typeParameters: null,
111
+ predicate: null
112
+ });
113
+ }
114
+
115
+ case 'ClassDeclaration':
116
+ case 'ClassExpression':
117
+ {
118
+ return nodeWith(node, {
119
+ typeParameters: null,
120
+ superTypeParameters: null,
121
+ implements: [],
122
+ decorators: []
123
+ });
124
+ }
125
+
126
+ case 'PropertyDefinition':
127
+ {
128
+ return nodeWith(node, {
129
+ typeAnnotation: null,
130
+ variance: null,
131
+ declare: false,
132
+ optional: false
133
+ });
134
+ }
135
+
136
+ case 'ImportDeclaration':
137
+ {
138
+ if (node.importKind === 'type' || node.importKind === 'typeof') {
139
+ return null;
140
+ }
141
+
142
+ const nonTypeSpecifiers = node.specifiers.filter(s => s.type !== 'ImportSpecifier' || s.importKind !== 'type' && s.importKind !== 'typeof');
143
+
144
+ if (nonTypeSpecifiers.length === 0) {
145
+ return null;
146
+ }
147
+
148
+ if (nonTypeSpecifiers.length === node.specifiers.length) {
149
+ return node;
150
+ }
151
+
152
+ return nodeWith(node, {
153
+ specifiers: nonTypeSpecifiers
154
+ });
155
+ }
156
+
157
+ case 'ExportAllDeclaration':
158
+ case 'ExportNamedDeclaration':
159
+ {
160
+ if (node.exportKind === 'type') {
161
+ return null;
162
+ }
163
+
164
+ return node;
165
+ }
166
+
167
+ default:
168
+ {
169
+ return node;
170
+ }
171
+ }
172
+ }
173
+
174
+ });
175
+ }
@@ -0,0 +1,215 @@
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
+ *
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
+ 'use strict';
19
+
20
+ Object.defineProperty(exports, "__esModule", {
21
+ value: true
22
+ });
23
+ exports.transformProgram = transformProgram;
24
+
25
+ var _SimpleTransform = require("../transform/SimpleTransform");
26
+
27
+ var _createSyntaxError = require("../utils/createSyntaxError");
28
+
29
+ const nodeWith = _SimpleTransform.SimpleTransform.nodeWith; // Rely on the mapper to fix up parent relationships.
30
+
31
+ const EMPTY_PARENT = null;
32
+
33
+ function createSimpleGenericTypeAnnotation(name, nodeForLoc) {
34
+ return {
35
+ type: 'GenericTypeAnnotation',
36
+ id: {
37
+ type: 'Identifier',
38
+ name,
39
+ optional: false,
40
+ typeAnnotation: null,
41
+ loc: nodeForLoc.loc,
42
+ range: nodeForLoc.range,
43
+ parent: EMPTY_PARENT
44
+ },
45
+ typeParameters: null,
46
+ loc: nodeForLoc.loc,
47
+ range: nodeForLoc.range,
48
+ parent: nodeForLoc.parent
49
+ };
50
+ }
51
+
52
+ function createAnyTypeAnnotation(node) {
53
+ return {
54
+ type: 'AnyTypeAnnotation',
55
+ loc: node.loc,
56
+ range: node.range,
57
+ parent: node.parent
58
+ };
59
+ }
60
+ /**
61
+ * Convert DeclareEnum nodes to DeclareVariable
62
+ */
63
+
64
+
65
+ function mapDeclareEnum(node) {
66
+ return {
67
+ type: 'DeclareVariable',
68
+ kind: 'const',
69
+ id: nodeWith(node.id, {
70
+ typeAnnotation: {
71
+ type: 'TypeAnnotation',
72
+ typeAnnotation: createAnyTypeAnnotation(node.body),
73
+ loc: node.body.loc,
74
+ range: node.body.range,
75
+ parent: EMPTY_PARENT
76
+ }
77
+ }),
78
+ loc: node.loc,
79
+ range: node.range,
80
+ parent: node.parent
81
+ };
82
+ }
83
+ /**
84
+ * Convert DeclareNamespace nodes to DeclareVariable
85
+ */
86
+
87
+
88
+ function mapDeclareNamespace(node) {
89
+ return {
90
+ type: 'DeclareVariable',
91
+ kind: 'const',
92
+ id: nodeWith(node.id, {
93
+ typeAnnotation: {
94
+ type: 'TypeAnnotation',
95
+ typeAnnotation: createAnyTypeAnnotation(node.body),
96
+ loc: node.body.loc,
97
+ range: node.body.range,
98
+ parent: EMPTY_PARENT
99
+ }
100
+ }),
101
+ loc: node.loc,
102
+ range: node.range,
103
+ parent: node.parent
104
+ };
105
+ }
106
+ /**
107
+ * Remove `this` param from functions.
108
+ */
109
+
110
+
111
+ function mapFunction(node) {
112
+ // Remove the first parameter if it is a this-type annotation,
113
+ // which is not recognized by Babel.
114
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
115
+ return nodeWith(node, {
116
+ params: node.params.slice(1)
117
+ });
118
+ }
119
+
120
+ return node;
121
+ }
122
+ /**
123
+ * Convert to QualifiedTypeIdentifier
124
+ */
125
+
126
+
127
+ function mapQualifiedTypeofIdentifier(node) {
128
+ return {
129
+ type: 'QualifiedTypeIdentifier',
130
+ qualification: node.qualification.type === 'QualifiedTypeofIdentifier' ? mapQualifiedTypeofIdentifier(node.qualification) : node.qualification,
131
+ id: node.id,
132
+ loc: node.loc,
133
+ range: node.range,
134
+ parent: node.parent
135
+ };
136
+ }
137
+
138
+ function transformProgram(program, _options) {
139
+ return _SimpleTransform.SimpleTransform.transformProgram(program, {
140
+ transform(node) {
141
+ switch (node.type) {
142
+ case 'SymbolTypeAnnotation':
143
+ {
144
+ // Convert to simple generic type annotation
145
+ return createSimpleGenericTypeAnnotation('symbol', node);
146
+ }
147
+
148
+ case 'BigIntTypeAnnotation':
149
+ {
150
+ // Convert to simple generic type annotation
151
+ return createSimpleGenericTypeAnnotation('bigint', node);
152
+ }
153
+
154
+ case 'ObjectTypeAnnotation':
155
+ {
156
+ const shouldStrip = node.properties.some(prop => prop.type === 'ObjectTypeMappedTypeProperty');
157
+
158
+ if (shouldStrip) {
159
+ return createAnyTypeAnnotation(node);
160
+ }
161
+
162
+ return node;
163
+ }
164
+
165
+ case 'ObjectTypeMappedTypeProperty':
166
+ {
167
+ throw (0, _createSyntaxError.createSyntaxError)(node, `Invalid AST structure, ObjectTypeMappedTypeProperty found outside of an ObjectTypeAnnotation`);
168
+ }
169
+
170
+ case 'IndexedAccessType':
171
+ case 'OptionalIndexedAccessType':
172
+ case 'KeyofTypeAnnotation':
173
+ case 'ConditionalTypeAnnotation':
174
+ case 'InferTypeAnnotation':
175
+ case 'TupleTypeLabeledElement':
176
+ case 'TupleTypeSpreadElement':
177
+ case 'ComponentTypeAnnotation':
178
+ case 'HookTypeAnnotation':
179
+ case 'TypeOperator':
180
+ case 'TypePredicate':
181
+ {
182
+ // Babel does not support these generic types, so convert to any
183
+ return createAnyTypeAnnotation(node);
184
+ }
185
+
186
+ case 'QualifiedTypeofIdentifier':
187
+ {
188
+ return mapQualifiedTypeofIdentifier(node);
189
+ }
190
+
191
+ case 'DeclareEnum':
192
+ {
193
+ return mapDeclareEnum(node);
194
+ }
195
+
196
+ case 'DeclareNamespace':
197
+ {
198
+ return mapDeclareNamespace(node);
199
+ }
200
+
201
+ case 'FunctionDeclaration':
202
+ case 'FunctionExpression':
203
+ {
204
+ return mapFunction(node);
205
+ }
206
+
207
+ default:
208
+ {
209
+ return node;
210
+ }
211
+ }
212
+ }
213
+
214
+ });
215
+ }