hermes-parser 0.4.6 → 0.6.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.
@@ -1,426 +1,412 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _HermesASTAdapter = _interopRequireDefault(require("./HermesASTAdapter"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
1
12
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
14
  *
4
15
  * This source code is licensed under the MIT license found in the
5
16
  * LICENSE file in the root directory of this source tree.
6
17
  *
18
+ *
7
19
  * @format
8
20
  */
9
- 'use strict';
10
21
 
11
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
22
+ /*
23
+ This class does some very "javascripty" things in the name of
24
+ performance which are ultimately impossible to soundly type.
12
25
 
13
- function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
26
+ So instead of adding strict types and a large number of suppression
27
+ comments, instead it is left untyped and subclasses are strictly
28
+ typed via a separate flow declaration file.
29
+ */
30
+ class HermesToBabelAdapter extends _HermesASTAdapter.default {
31
+ fixSourceLocation(node) {
32
+ var _this$sourceFilename;
14
33
 
15
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
34
+ const loc = node.loc;
16
35
 
17
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
36
+ if (loc == null) {
37
+ return;
38
+ }
18
39
 
19
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
40
+ node.loc = {
41
+ source: (_this$sourceFilename = this.sourceFilename) != null ? _this$sourceFilename : null,
42
+ start: loc.start,
43
+ end: loc.end
44
+ };
45
+ node.start = loc.rangeStart;
46
+ node.end = loc.rangeEnd;
47
+ }
20
48
 
21
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
49
+ mapNode(node) {
50
+ this.fixSourceLocation(node);
22
51
 
23
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
52
+ switch (node.type) {
53
+ case 'Program':
54
+ return this.mapProgram(node);
24
55
 
25
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
56
+ case 'BlockStatement':
57
+ return this.mapNodeWithDirectives(node);
26
58
 
27
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
59
+ case 'Empty':
60
+ return this.mapEmpty(node);
28
61
 
29
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
62
+ case 'Identifier':
63
+ return this.mapIdentifier(node);
30
64
 
31
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
65
+ case 'TemplateElement':
66
+ return this.mapTemplateElement(node);
32
67
 
33
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
68
+ case 'GenericTypeAnnotation':
69
+ return this.mapGenericTypeAnnotation(node);
34
70
 
35
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
71
+ case 'SymbolTypeAnnotation':
72
+ return this.mapSymbolTypeAnnotation(node);
36
73
 
37
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
74
+ case 'Property':
75
+ return this.mapProperty(node);
38
76
 
39
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
77
+ case 'MethodDefinition':
78
+ return this.mapMethodDefinition(node);
40
79
 
41
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
80
+ case 'ImportDeclaration':
81
+ return this.mapImportDeclaration(node);
42
82
 
43
- var HermesASTAdapter = require('./HermesASTAdapter');
83
+ case 'ImportSpecifier':
84
+ return this.mapImportSpecifier(node);
44
85
 
45
- var HermesToBabelAdapter = /*#__PURE__*/function (_HermesASTAdapter) {
46
- _inherits(HermesToBabelAdapter, _HermesASTAdapter);
86
+ case 'ExportDefaultDeclaration':
87
+ return this.mapExportDefaultDeclaration(node);
47
88
 
48
- var _super = _createSuper(HermesToBabelAdapter);
89
+ case 'ExportNamedDeclaration':
90
+ return this.mapExportNamedDeclaration(node);
49
91
 
50
- function HermesToBabelAdapter() {
51
- _classCallCheck(this, HermesToBabelAdapter);
92
+ case 'ExportAllDeclaration':
93
+ return this.mapExportAllDeclaration(node);
52
94
 
53
- return _super.apply(this, arguments);
54
- }
95
+ case 'RestElement':
96
+ return this.mapRestElement(node);
55
97
 
56
- _createClass(HermesToBabelAdapter, [{
57
- key: "fixSourceLocation",
58
- value: function fixSourceLocation(node) {
59
- var _this$sourceFilename;
98
+ case 'ImportExpression':
99
+ return this.mapImportExpression(node);
60
100
 
61
- var loc = node.loc;
101
+ case 'JSXStringLiteral':
102
+ return this.mapJSXStringLiteral(node);
62
103
 
63
- if (loc == null) {
64
- return;
65
- }
104
+ case 'PrivateName':
105
+ case 'ClassPrivateProperty':
106
+ return this.mapPrivateProperty(node);
66
107
 
67
- node.loc = {
68
- source: (_this$sourceFilename = this.sourceFilename) !== null && _this$sourceFilename !== void 0 ? _this$sourceFilename : null,
69
- start: loc.start,
70
- end: loc.end
71
- };
72
- node.start = loc.rangeStart;
73
- node.end = loc.rangeEnd;
74
- }
75
- }, {
76
- key: "mapNode",
77
- value: function mapNode(node) {
78
- this.fixSourceLocation(node);
108
+ case 'FunctionDeclaration':
109
+ case 'FunctionExpression':
110
+ return this.mapFunction(node);
79
111
 
80
- switch (node.type) {
81
- case 'Program':
82
- return this.mapProgram(node);
112
+ case 'IndexedAccessType':
113
+ case 'OptionalIndexedAccessType':
114
+ return this.mapUnsupportedTypeAnnotation(node);
83
115
 
84
- case 'BlockStatement':
85
- return this.mapNodeWithDirectives(node);
86
-
87
- case 'Empty':
88
- return this.mapEmpty(node);
89
-
90
- case 'Identifier':
91
- return this.mapIdentifier(node);
92
-
93
- case 'TemplateElement':
94
- return this.mapTemplateElement(node);
116
+ default:
117
+ return this.mapNodeDefault(node);
118
+ }
119
+ }
95
120
 
96
- case 'GenericTypeAnnotation':
97
- return this.mapGenericTypeAnnotation(node);
121
+ mapProgram(node) {
122
+ // Visit child nodes and convert to directives
123
+ const {
124
+ comments,
125
+ ...program
126
+ } = this.mapNodeWithDirectives(node);
127
+ program.sourceType = this.getSourceType(); // Adjust start loc to beginning of file
128
+
129
+ program.loc.start = {
130
+ line: 1,
131
+ column: 0
132
+ };
133
+ program.start = 0; // Adjust end loc to include last comment if program ends with a comment
134
+
135
+ if (comments.length > 0) {
136
+ const lastComment = comments[comments.length - 1];
137
+
138
+ if (lastComment.end > program.end) {
139
+ program.loc.end = lastComment.loc.end;
140
+ program.end = lastComment.end;
141
+ }
142
+ } // Rename root node to File node and move Program node under program property
98
143
 
99
- case 'SymbolTypeAnnotation':
100
- return this.mapSymbolTypeAnnotation(node);
101
144
 
102
- case 'Property':
103
- return this.mapProperty(node);
145
+ return {
146
+ type: 'File',
147
+ loc: program.loc,
148
+ start: program.start,
149
+ end: program.end,
150
+ program,
151
+ comments
152
+ };
153
+ }
104
154
 
105
- case 'MethodDefinition':
106
- return this.mapMethodDefinition(node);
155
+ mapNodeWithDirectives(node) {
156
+ const directives = [];
107
157
 
108
- case 'ImportDeclaration':
109
- return this.mapImportDeclaration(node);
158
+ for (const child of node.body) {
159
+ if (child.type === 'ExpressionStatement' && child.directive != null) {
160
+ // Visit directive children
161
+ const directiveChild = this.mapNode(child); // Modify string literal node to be DirectiveLiteral node
110
162
 
111
- case 'ImportSpecifier':
112
- return this.mapImportSpecifier(node);
163
+ directiveChild.expression.type = 'DirectiveLiteral'; // Construct Directive node with DirectiveLiteral value
113
164
 
114
- case 'ExportDefaultDeclaration':
115
- return this.mapExportDefaultDeclaration(node);
165
+ directives.push({
166
+ type: 'Directive',
167
+ loc: directiveChild.loc,
168
+ start: directiveChild.start,
169
+ end: directiveChild.end,
170
+ value: directiveChild.expression
171
+ });
172
+ } else {
173
+ // Once we have found the first non-directive node we know there cannot be any more directives
174
+ break;
175
+ }
176
+ } // Move directives from body to new directives array
116
177
 
117
- case 'ExportNamedDeclaration':
118
- return this.mapExportNamedDeclaration(node);
119
178
 
120
- case 'ExportAllDeclaration':
121
- return this.mapExportAllDeclaration(node);
179
+ node.directives = directives;
122
180
 
123
- case 'RestElement':
124
- return this.mapRestElement(node);
181
+ if (directives.length !== 0) {
182
+ node.body = node.body.slice(directives.length);
183
+ } // Visit expression statement children
125
184
 
126
- case 'ImportExpression':
127
- return this.mapImportExpression(node);
128
185
 
129
- case 'PrivateName':
130
- case 'ClassPrivateProperty':
131
- return this.mapPrivateProperty(node);
186
+ const body = node.body;
132
187
 
133
- case 'FunctionDeclaration':
134
- case 'FunctionExpression':
135
- return this.mapFunction(node);
188
+ for (let i = 0; i < body.length; i++) {
189
+ const child = body[i];
136
190
 
137
- default:
138
- return this.mapNodeDefault(node);
191
+ if (child != null) {
192
+ body[i] = this.mapNode(child);
139
193
  }
140
194
  }
141
- }, {
142
- key: "mapProgram",
143
- value: function mapProgram(node) {
144
- // Visit child nodes and convert to directives
145
- var _this$mapNodeWithDire = this.mapNodeWithDirectives(node),
146
- comments = _this$mapNodeWithDire.comments,
147
- program = _objectWithoutProperties(_this$mapNodeWithDire, ["comments"]);
148
-
149
- program.sourceType = this.getSourceType(); // Adjust start loc to beginning of file
150
-
151
- program.loc.start = {
152
- line: 1,
153
- column: 0
154
- };
155
- program.start = 0; // Adjust end loc to include last comment if program ends with a comment
156
-
157
- if (comments.length > 0) {
158
- var lastComment = comments[comments.length - 1];
159
-
160
- if (lastComment.end > program.end) {
161
- program.loc.end = lastComment.loc.end;
162
- program.end = lastComment.end;
163
- }
164
- } // Rename root node to File node and move Program node under program property
165
-
166
-
167
- return {
168
- type: 'File',
169
- loc: program.loc,
170
- start: program.start,
171
- end: program.end,
172
- program: program,
173
- comments: comments
174
- };
175
- }
176
- }, {
177
- key: "mapNodeWithDirectives",
178
- value: function mapNodeWithDirectives(node) {
179
- var directives = [];
180
-
181
- var _iterator = _createForOfIteratorHelper(node.body),
182
- _step;
183
-
184
- try {
185
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
186
- var _child = _step.value;
187
-
188
- if (_child.type === 'ExpressionStatement' && _child.directive != null) {
189
- // Visit directive children
190
- var directiveChild = this.mapNode(_child); // Modify string literal node to be DirectiveLiteral node
191
-
192
- directiveChild.expression.type = 'DirectiveLiteral'; // Construct Directive node with DirectiveLiteral value
193
-
194
- directives.push({
195
- type: 'Directive',
196
- loc: directiveChild.loc,
197
- start: directiveChild.start,
198
- end: directiveChild.end,
199
- value: directiveChild.expression
200
- });
201
- } else {
202
- // Once we have found the first non-directive node we know there cannot be any more directives
203
- break;
204
- }
205
- } // Move directives from body to new directives array
206
-
207
- } catch (err) {
208
- _iterator.e(err);
209
- } finally {
210
- _iterator.f();
211
- }
212
195
 
213
- node.directives = directives;
214
-
215
- if (directives.length !== 0) {
216
- node.body = node.body.slice(directives.length);
217
- } // Visit expression statement children
218
-
219
-
220
- var body = node.body;
196
+ return node;
197
+ }
221
198
 
222
- for (var i = 0; i < body.length; i++) {
223
- var child = body[i];
199
+ mapIdentifier(node) {
200
+ node.loc.identifierName = node.name;
201
+ return this.mapNodeDefault(node);
202
+ }
224
203
 
225
- if (child != null) {
226
- body[i] = this.mapNode(child);
204
+ mapTemplateElement(node) {
205
+ // Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
206
+ // otherwise exclude "}" from previous expression.
207
+ const startCharsToExclude = 1; // Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
208
+ // otherwise exclude "${" from next expression.
209
+
210
+ const endCharsToExclude = node.tail ? 1 : 2;
211
+ return {
212
+ type: 'TemplateElement',
213
+ loc: {
214
+ start: {
215
+ line: node.loc.start.line,
216
+ column: node.loc.start.column + startCharsToExclude
217
+ },
218
+ end: {
219
+ line: node.loc.end.line,
220
+ column: node.loc.end.column - endCharsToExclude
227
221
  }
222
+ },
223
+ start: node.start + startCharsToExclude,
224
+ end: node.end - endCharsToExclude,
225
+ tail: node.tail,
226
+ value: {
227
+ cooked: node.cooked,
228
+ raw: node.raw
228
229
  }
230
+ };
231
+ }
229
232
 
230
- return node;
231
- }
232
- }, {
233
- key: "mapIdentifier",
234
- value: function mapIdentifier(node) {
235
- node.loc.identifierName = node.name;
236
- return this.mapNodeDefault(node);
237
- }
238
- }, {
239
- key: "mapTemplateElement",
240
- value: function mapTemplateElement(node) {
241
- // Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
242
- // otherwise exclude "}" from previous expression.
243
- var startCharsToExclude = 1; // Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
244
- // otherwise exclude "${" from next expression.
245
-
246
- var endCharsToExclude = node.tail ? 1 : 2;
233
+ mapGenericTypeAnnotation(node) {
234
+ // Convert simple `this` generic type to ThisTypeAnnotation
235
+ if (node.typeParameters == null && node.id.type === 'Identifier' && node.id.name === 'this') {
247
236
  return {
248
- type: 'TemplateElement',
249
- loc: {
250
- start: {
251
- line: node.loc.start.line,
252
- column: node.loc.start.column + startCharsToExclude
253
- },
254
- end: {
255
- line: node.loc.end.line,
256
- column: node.loc.end.column - endCharsToExclude
257
- }
258
- },
259
- start: node.start + startCharsToExclude,
260
- end: node.end - endCharsToExclude,
261
- tail: node.tail,
262
- value: {
263
- cooked: node.cooked,
264
- raw: node.raw
265
- }
237
+ type: 'ThisTypeAnnotation',
238
+ loc: node.loc,
239
+ start: node.start,
240
+ end: node.end
266
241
  };
267
242
  }
268
- }, {
269
- key: "mapGenericTypeAnnotation",
270
- value: function mapGenericTypeAnnotation(node) {
271
- // Convert simple `this` generic type to ThisTypeAnnotation
272
- if (node.typeParameters === null && node.id.type === 'Identifier' && node.id.name === 'this') {
273
- return {
274
- type: 'ThisTypeAnnotation',
275
- loc: node.loc,
276
- start: node.start,
277
- end: node.end
278
- };
279
- }
280
243
 
281
- return this.mapNodeDefault(node);
282
- }
283
- }, {
284
- key: "mapSymbolTypeAnnotation",
285
- value: function mapSymbolTypeAnnotation(node) {
286
- return {
287
- type: 'GenericTypeAnnotation',
244
+ return this.mapNodeDefault(node);
245
+ }
246
+
247
+ mapSymbolTypeAnnotation(node) {
248
+ return {
249
+ type: 'GenericTypeAnnotation',
250
+ loc: node.loc,
251
+ start: node.start,
252
+ end: node.end,
253
+ id: {
254
+ type: 'Identifier',
288
255
  loc: node.loc,
289
256
  start: node.start,
290
257
  end: node.end,
291
- id: {
292
- type: 'Identifier',
293
- loc: node.loc,
294
- start: node.start,
295
- end: node.end,
296
- name: 'symbol'
297
- },
298
- typeParameters: null
299
- };
300
- }
301
- }, {
302
- key: "mapProperty",
303
- value: function mapProperty(node) {
304
- var key = this.mapNode(node.key);
305
- var value = this.mapNode(node.value); // Convert methods, getters, and setters to ObjectMethod nodes
306
-
307
- if (node.method || node.kind !== 'init') {
308
- // Properties under the FunctionExpression value that should be moved
309
- // to the ObjectMethod node itself.
310
- var id = value.id,
311
- params = value.params,
312
- body = value.body,
313
- async = value.async,
314
- generator = value.generator,
315
- returnType = value.returnType,
316
- typeParameters = value.typeParameters,
317
- predicate = value.predicate;
318
- return {
319
- type: 'ObjectMethod',
320
- loc: node.loc,
321
- start: node.start,
322
- end: node.end,
323
- // Non getter or setter methods have `kind = method`
324
- kind: node.kind === 'init' ? 'method' : node.kind,
325
- computed: node.computed,
326
- key: key,
327
- id: id,
328
- params: params,
329
- body: body,
330
- async: async,
331
- generator: generator,
332
- returnType: returnType,
333
- typeParameters: typeParameters,
334
- predicate: predicate
335
- };
336
- } else {
337
- // Non-method property nodes should be renamed to ObjectProperty
338
- node.type = 'ObjectProperty';
339
- return node;
340
- }
341
- }
342
- }, {
343
- key: "mapMethodDefinition",
344
- value: function mapMethodDefinition(node) {
345
- var key = this.mapNode(node.key);
346
- var value = this.mapNode(node.value); // Properties under the FunctionExpression value that should be moved
347
- // to the ClassMethod node itself.
348
-
349
- var id = value.id,
350
- params = value.params,
351
- body = value.body,
352
- async = value.async,
353
- generator = value.generator,
354
- returnType = value.returnType,
355
- typeParameters = value.typeParameters,
356
- predicate = value.predicate;
258
+ name: 'symbol'
259
+ },
260
+ typeParameters: null
261
+ };
262
+ }
263
+
264
+ mapProperty(node) {
265
+ const key = this.mapNode(node.key);
266
+ const value = this.mapNode(node.value); // Convert methods, getters, and setters to ObjectMethod nodes
267
+
268
+ if (node.method || node.kind !== 'init') {
269
+ // Properties under the FunctionExpression value that should be moved
270
+ // to the ObjectMethod node itself.
271
+ const {
272
+ id,
273
+ params,
274
+ body,
275
+ async,
276
+ generator,
277
+ returnType,
278
+ typeParameters,
279
+ predicate
280
+ } = value;
357
281
  return {
358
- type: 'ClassMethod',
282
+ type: 'ObjectMethod',
359
283
  loc: node.loc,
360
284
  start: node.start,
361
285
  end: node.end,
362
- kind: node.kind,
286
+ // Non getter or setter methods have `kind = method`
287
+ kind: node.kind === 'init' ? 'method' : node.kind,
363
288
  computed: node.computed,
364
- "static": node["static"],
365
- key: key,
366
- id: id,
367
- params: params,
368
- body: body,
369
- async: async,
370
- generator: generator,
371
- returnType: returnType,
372
- typeParameters: typeParameters,
373
- predicate: predicate
289
+ key,
290
+ id,
291
+ params,
292
+ body,
293
+ async,
294
+ generator,
295
+ returnType,
296
+ typeParameters,
297
+ predicate
374
298
  };
299
+ } else {
300
+ // Non-method property nodes should be renamed to ObjectProperty
301
+ node.type = 'ObjectProperty';
302
+ return node;
375
303
  }
376
- }, {
377
- key: "mapRestElement",
378
- value: function mapRestElement(node) {
379
- var restElement = this.mapNodeDefault(node); // Hermes puts type annotations on rest elements on the argument node,
380
- // but Babel expects type annotations on the rest element node itself.
304
+ }
381
305
 
382
- var annotation = restElement.argument.typeAnnotation;
306
+ mapMethodDefinition(node) {
307
+ const key = this.mapNode(node.key);
308
+ const value = this.mapNode(node.value); // Properties under the FunctionExpression value that should be moved
309
+ // to the ClassMethod node itself.
310
+
311
+ const {
312
+ id,
313
+ params,
314
+ body,
315
+ async,
316
+ generator,
317
+ returnType,
318
+ typeParameters,
319
+ predicate
320
+ } = value;
321
+ return {
322
+ type: 'ClassMethod',
323
+ loc: node.loc,
324
+ start: node.start,
325
+ end: node.end,
326
+ kind: node.kind,
327
+ computed: node.computed,
328
+ static: node.static,
329
+ key,
330
+ id,
331
+ params,
332
+ body,
333
+ async,
334
+ generator,
335
+ returnType,
336
+ typeParameters,
337
+ predicate
338
+ };
339
+ }
383
340
 
384
- if (annotation != null) {
385
- restElement.typeAnnotation = annotation;
386
- restElement.argument.typeAnnotation = null;
387
- }
341
+ mapRestElement(node) {
342
+ const restElement = this.mapNodeDefault(node); // Hermes puts type annotations on rest elements on the argument node,
343
+ // but Babel expects type annotations on the rest element node itself.
388
344
 
389
- return restElement;
345
+ const annotation = restElement.argument.typeAnnotation;
346
+
347
+ if (annotation != null) {
348
+ restElement.typeAnnotation = annotation;
349
+ restElement.argument.typeAnnotation = null;
390
350
  }
391
- }, {
392
- key: "mapImportExpression",
393
- value: function mapImportExpression(node) {
394
- // Babel expects ImportExpression to be structued as a regular
395
- // CallExpression where the callee is an Import node.
396
- return {
397
- type: 'CallExpression',
351
+
352
+ return restElement;
353
+ }
354
+
355
+ mapImportExpression(node) {
356
+ // Babel expects ImportExpression to be structued as a regular
357
+ // CallExpression where the callee is an Import node.
358
+ return {
359
+ type: 'CallExpression',
360
+ loc: node.loc,
361
+ start: node.start,
362
+ end: node.end,
363
+ callee: {
364
+ type: 'Import',
398
365
  loc: node.loc,
399
366
  start: node.start,
400
- end: node.end,
401
- callee: {
402
- type: 'Import',
403
- loc: node.loc,
404
- start: node.start,
405
- end: node.end
406
- },
407
- arguments: [this.mapNode(node.source)]
408
- };
409
- }
410
- }, {
411
- key: "mapFunction",
412
- value: function mapFunction(node) {
413
- // Remove the first parameter if it is a this-type annotation,
414
- // which is not recognized by Babel.
415
- if (node.params.length !== 0 && node.params[0].name === 'this') {
416
- node.params.shift();
417
- }
367
+ end: node.end
368
+ },
369
+ arguments: [this.mapNode(node.source)]
370
+ };
371
+ }
418
372
 
419
- return this.mapNodeDefault(node);
373
+ mapJSXStringLiteral(node) {
374
+ // Babel expects StringLiterals in JSX,
375
+ // but Hermes uses JSXStringLiteral to attach the raw value without
376
+ // having to internally attach it to every single string literal.
377
+ return {
378
+ type: 'StringLiteral',
379
+ loc: node.loc,
380
+ start: node.start,
381
+ end: node.end,
382
+ value: node.value
383
+ };
384
+ }
385
+
386
+ mapFunction(node) {
387
+ // Remove the first parameter if it is a this-type annotation,
388
+ // which is not recognized by Babel.
389
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
390
+ node.params.shift();
420
391
  }
421
- }]);
422
392
 
423
- return HermesToBabelAdapter;
424
- }(HermesASTAdapter);
393
+ return this.mapNodeDefault(node);
394
+ }
395
+ /**
396
+ * If Babel (the version we target) does not support a type annotation we
397
+ * parse, we need to return some other valid type annotation in its place.
398
+ */
399
+
400
+
401
+ mapUnsupportedTypeAnnotation(node) {
402
+ return {
403
+ type: 'AnyTypeAnnotation',
404
+ loc: node.loc,
405
+ start: node.start,
406
+ end: node.end
407
+ };
408
+ }
409
+
410
+ }
425
411
 
426
- module.exports = HermesToBabelAdapter;
412
+ exports.default = HermesToBabelAdapter;