hermes-parser 0.4.8 → 0.7.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.
- package/LICENSE +1 -1
- package/dist/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +134 -146
- package/dist/HermesASTAdapter.js.flow +178 -0
- package/dist/HermesParser.js +39 -25
- package/dist/HermesParser.js.flow +123 -0
- package/dist/HermesParserDecodeUTF8String.js +17 -12
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +200 -207
- package/dist/HermesParserDeserializer.js.flow +261 -0
- package/dist/HermesParserNodeDeserializers.js +60 -28
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +2 -2
- package/dist/HermesParserWASM.js.flow +75 -0
- package/dist/HermesToBabelAdapter.js +331 -364
- package/dist/HermesToBabelAdapter.js.flow +383 -0
- package/dist/HermesToESTreeAdapter.js +198 -159
- package/dist/HermesToESTreeAdapter.js.flow +233 -0
- package/dist/ParserOptions.js.flow +18 -0
- package/dist/generated/visitor-keys.js +605 -0
- package/dist/generated/visitor-keys.js.flow +17 -0
- package/dist/index.js +32 -19
- package/dist/index.js.flow +72 -13
- package/package.json +7 -1
- package/dist/HermesParserVisitorKeys.js +0 -729
|
@@ -1,445 +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)
|
|
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
|
-
|
|
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); }
|
|
12
21
|
|
|
13
|
-
|
|
22
|
+
/*
|
|
23
|
+
This class does some very "javascripty" things in the name of
|
|
24
|
+
performance which are ultimately impossible to soundly type.
|
|
14
25
|
|
|
15
|
-
|
|
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;
|
|
16
33
|
|
|
17
|
-
|
|
34
|
+
const loc = node.loc;
|
|
18
35
|
|
|
19
|
-
|
|
36
|
+
if (loc == null) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
20
39
|
|
|
21
|
-
|
|
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
|
+
}
|
|
22
48
|
|
|
23
|
-
|
|
49
|
+
mapNode(node) {
|
|
50
|
+
this.fixSourceLocation(node);
|
|
24
51
|
|
|
25
|
-
|
|
52
|
+
switch (node.type) {
|
|
53
|
+
case 'Program':
|
|
54
|
+
return this.mapProgram(node);
|
|
26
55
|
|
|
27
|
-
|
|
56
|
+
case 'BlockStatement':
|
|
57
|
+
return this.mapNodeWithDirectives(node);
|
|
28
58
|
|
|
29
|
-
|
|
59
|
+
case 'Empty':
|
|
60
|
+
return this.mapEmpty(node);
|
|
30
61
|
|
|
31
|
-
|
|
62
|
+
case 'Identifier':
|
|
63
|
+
return this.mapIdentifier(node);
|
|
32
64
|
|
|
33
|
-
|
|
65
|
+
case 'TemplateElement':
|
|
66
|
+
return this.mapTemplateElement(node);
|
|
34
67
|
|
|
35
|
-
|
|
68
|
+
case 'GenericTypeAnnotation':
|
|
69
|
+
return this.mapGenericTypeAnnotation(node);
|
|
36
70
|
|
|
37
|
-
|
|
71
|
+
case 'SymbolTypeAnnotation':
|
|
72
|
+
return this.mapSymbolTypeAnnotation(node);
|
|
38
73
|
|
|
39
|
-
|
|
74
|
+
case 'Property':
|
|
75
|
+
return this.mapProperty(node);
|
|
40
76
|
|
|
41
|
-
|
|
77
|
+
case 'MethodDefinition':
|
|
78
|
+
return this.mapMethodDefinition(node);
|
|
42
79
|
|
|
43
|
-
|
|
80
|
+
case 'ImportDeclaration':
|
|
81
|
+
return this.mapImportDeclaration(node);
|
|
44
82
|
|
|
45
|
-
|
|
46
|
-
|
|
83
|
+
case 'ImportSpecifier':
|
|
84
|
+
return this.mapImportSpecifier(node);
|
|
47
85
|
|
|
48
|
-
|
|
86
|
+
case 'ExportDefaultDeclaration':
|
|
87
|
+
return this.mapExportDefaultDeclaration(node);
|
|
49
88
|
|
|
50
|
-
|
|
51
|
-
|
|
89
|
+
case 'ExportNamedDeclaration':
|
|
90
|
+
return this.mapExportNamedDeclaration(node);
|
|
52
91
|
|
|
53
|
-
|
|
54
|
-
|
|
92
|
+
case 'ExportAllDeclaration':
|
|
93
|
+
return this.mapExportAllDeclaration(node);
|
|
55
94
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
value: function fixSourceLocation(node) {
|
|
59
|
-
var _this$sourceFilename;
|
|
95
|
+
case 'RestElement':
|
|
96
|
+
return this.mapRestElement(node);
|
|
60
97
|
|
|
61
|
-
|
|
98
|
+
case 'ImportExpression':
|
|
99
|
+
return this.mapImportExpression(node);
|
|
62
100
|
|
|
63
|
-
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
101
|
+
case 'JSXStringLiteral':
|
|
102
|
+
return this.mapJSXStringLiteral(node);
|
|
66
103
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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);
|
|
104
|
+
case 'PrivateName':
|
|
105
|
+
case 'ClassPrivateProperty':
|
|
106
|
+
return this.mapPrivateProperty(node);
|
|
79
107
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
108
|
+
case 'FunctionDeclaration':
|
|
109
|
+
case 'FunctionExpression':
|
|
110
|
+
return this.mapFunction(node);
|
|
83
111
|
|
|
84
|
-
|
|
85
|
-
|
|
112
|
+
case 'IndexedAccessType':
|
|
113
|
+
case 'OptionalIndexedAccessType':
|
|
114
|
+
return this.mapUnsupportedTypeAnnotation(node);
|
|
86
115
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return this.mapIdentifier(node);
|
|
92
|
-
|
|
93
|
-
case 'TemplateElement':
|
|
94
|
-
return this.mapTemplateElement(node);
|
|
95
|
-
|
|
96
|
-
case 'GenericTypeAnnotation':
|
|
97
|
-
return this.mapGenericTypeAnnotation(node);
|
|
116
|
+
default:
|
|
117
|
+
return this.mapNodeDefault(node);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
98
120
|
|
|
99
|
-
|
|
100
|
-
|
|
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
|
|
101
143
|
|
|
102
|
-
case 'Property':
|
|
103
|
-
return this.mapProperty(node);
|
|
104
144
|
|
|
105
|
-
|
|
106
|
-
|
|
145
|
+
return {
|
|
146
|
+
type: 'File',
|
|
147
|
+
loc: program.loc,
|
|
148
|
+
start: program.start,
|
|
149
|
+
end: program.end,
|
|
150
|
+
program,
|
|
151
|
+
comments
|
|
152
|
+
};
|
|
153
|
+
}
|
|
107
154
|
|
|
108
|
-
|
|
109
|
-
|
|
155
|
+
mapNodeWithDirectives(node) {
|
|
156
|
+
const directives = [];
|
|
110
157
|
|
|
111
|
-
|
|
112
|
-
|
|
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
|
|
113
162
|
|
|
114
|
-
|
|
115
|
-
return this.mapExportDefaultDeclaration(node);
|
|
163
|
+
directiveChild.expression.type = 'DirectiveLiteral'; // Construct Directive node with DirectiveLiteral value
|
|
116
164
|
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
177
|
|
|
120
|
-
case 'ExportAllDeclaration':
|
|
121
|
-
return this.mapExportAllDeclaration(node);
|
|
122
178
|
|
|
123
|
-
|
|
124
|
-
return this.mapRestElement(node);
|
|
179
|
+
node.directives = directives;
|
|
125
180
|
|
|
126
|
-
|
|
127
|
-
|
|
181
|
+
if (directives.length !== 0) {
|
|
182
|
+
node.body = node.body.slice(directives.length);
|
|
183
|
+
} // Visit expression statement children
|
|
128
184
|
|
|
129
|
-
case 'PrivateName':
|
|
130
|
-
case 'ClassPrivateProperty':
|
|
131
|
-
return this.mapPrivateProperty(node);
|
|
132
185
|
|
|
133
|
-
|
|
134
|
-
case 'FunctionExpression':
|
|
135
|
-
return this.mapFunction(node);
|
|
186
|
+
const body = node.body;
|
|
136
187
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return this.mapUnsupportedTypeAnnotation(node);
|
|
188
|
+
for (let i = 0; i < body.length; i++) {
|
|
189
|
+
const child = body[i];
|
|
140
190
|
|
|
141
|
-
|
|
142
|
-
|
|
191
|
+
if (child != null) {
|
|
192
|
+
body[i] = this.mapNode(child);
|
|
143
193
|
}
|
|
144
194
|
}
|
|
145
|
-
}, {
|
|
146
|
-
key: "mapProgram",
|
|
147
|
-
value: function mapProgram(node) {
|
|
148
|
-
// Visit child nodes and convert to directives
|
|
149
|
-
var _this$mapNodeWithDire = this.mapNodeWithDirectives(node),
|
|
150
|
-
comments = _this$mapNodeWithDire.comments,
|
|
151
|
-
program = _objectWithoutProperties(_this$mapNodeWithDire, ["comments"]);
|
|
152
|
-
|
|
153
|
-
program.sourceType = this.getSourceType(); // Adjust start loc to beginning of file
|
|
154
|
-
|
|
155
|
-
program.loc.start = {
|
|
156
|
-
line: 1,
|
|
157
|
-
column: 0
|
|
158
|
-
};
|
|
159
|
-
program.start = 0; // Adjust end loc to include last comment if program ends with a comment
|
|
160
|
-
|
|
161
|
-
if (comments.length > 0) {
|
|
162
|
-
var lastComment = comments[comments.length - 1];
|
|
163
|
-
|
|
164
|
-
if (lastComment.end > program.end) {
|
|
165
|
-
program.loc.end = lastComment.loc.end;
|
|
166
|
-
program.end = lastComment.end;
|
|
167
|
-
}
|
|
168
|
-
} // Rename root node to File node and move Program node under program property
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
type: 'File',
|
|
173
|
-
loc: program.loc,
|
|
174
|
-
start: program.start,
|
|
175
|
-
end: program.end,
|
|
176
|
-
program: program,
|
|
177
|
-
comments: comments
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
}, {
|
|
181
|
-
key: "mapNodeWithDirectives",
|
|
182
|
-
value: function mapNodeWithDirectives(node) {
|
|
183
|
-
var directives = [];
|
|
184
|
-
|
|
185
|
-
var _iterator = _createForOfIteratorHelper(node.body),
|
|
186
|
-
_step;
|
|
187
|
-
|
|
188
|
-
try {
|
|
189
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
190
|
-
var _child = _step.value;
|
|
191
|
-
|
|
192
|
-
if (_child.type === 'ExpressionStatement' && _child.directive != null) {
|
|
193
|
-
// Visit directive children
|
|
194
|
-
var directiveChild = this.mapNode(_child); // Modify string literal node to be DirectiveLiteral node
|
|
195
|
-
|
|
196
|
-
directiveChild.expression.type = 'DirectiveLiteral'; // Construct Directive node with DirectiveLiteral value
|
|
197
|
-
|
|
198
|
-
directives.push({
|
|
199
|
-
type: 'Directive',
|
|
200
|
-
loc: directiveChild.loc,
|
|
201
|
-
start: directiveChild.start,
|
|
202
|
-
end: directiveChild.end,
|
|
203
|
-
value: directiveChild.expression
|
|
204
|
-
});
|
|
205
|
-
} else {
|
|
206
|
-
// Once we have found the first non-directive node we know there cannot be any more directives
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
} // Move directives from body to new directives array
|
|
210
|
-
|
|
211
|
-
} catch (err) {
|
|
212
|
-
_iterator.e(err);
|
|
213
|
-
} finally {
|
|
214
|
-
_iterator.f();
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
node.directives = directives;
|
|
218
|
-
|
|
219
|
-
if (directives.length !== 0) {
|
|
220
|
-
node.body = node.body.slice(directives.length);
|
|
221
|
-
} // Visit expression statement children
|
|
222
195
|
|
|
196
|
+
return node;
|
|
197
|
+
}
|
|
223
198
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
199
|
+
mapIdentifier(node) {
|
|
200
|
+
node.loc.identifierName = node.name;
|
|
201
|
+
return this.mapNodeDefault(node);
|
|
202
|
+
}
|
|
228
203
|
|
|
229
|
-
|
|
230
|
-
|
|
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
|
|
231
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
|
|
232
229
|
}
|
|
230
|
+
};
|
|
231
|
+
}
|
|
233
232
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
key: "mapIdentifier",
|
|
238
|
-
value: function mapIdentifier(node) {
|
|
239
|
-
node.loc.identifierName = node.name;
|
|
240
|
-
return this.mapNodeDefault(node);
|
|
241
|
-
}
|
|
242
|
-
}, {
|
|
243
|
-
key: "mapTemplateElement",
|
|
244
|
-
value: function mapTemplateElement(node) {
|
|
245
|
-
// Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
|
|
246
|
-
// otherwise exclude "}" from previous expression.
|
|
247
|
-
var startCharsToExclude = 1; // Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
|
|
248
|
-
// otherwise exclude "${" from next expression.
|
|
249
|
-
|
|
250
|
-
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') {
|
|
251
236
|
return {
|
|
252
|
-
type: '
|
|
253
|
-
loc:
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
column: node.loc.start.column + startCharsToExclude
|
|
257
|
-
},
|
|
258
|
-
end: {
|
|
259
|
-
line: node.loc.end.line,
|
|
260
|
-
column: node.loc.end.column - endCharsToExclude
|
|
261
|
-
}
|
|
262
|
-
},
|
|
263
|
-
start: node.start + startCharsToExclude,
|
|
264
|
-
end: node.end - endCharsToExclude,
|
|
265
|
-
tail: node.tail,
|
|
266
|
-
value: {
|
|
267
|
-
cooked: node.cooked,
|
|
268
|
-
raw: node.raw
|
|
269
|
-
}
|
|
237
|
+
type: 'ThisTypeAnnotation',
|
|
238
|
+
loc: node.loc,
|
|
239
|
+
start: node.start,
|
|
240
|
+
end: node.end
|
|
270
241
|
};
|
|
271
242
|
}
|
|
272
|
-
}, {
|
|
273
|
-
key: "mapGenericTypeAnnotation",
|
|
274
|
-
value: function mapGenericTypeAnnotation(node) {
|
|
275
|
-
// Convert simple `this` generic type to ThisTypeAnnotation
|
|
276
|
-
if (node.typeParameters === null && node.id.type === 'Identifier' && node.id.name === 'this') {
|
|
277
|
-
return {
|
|
278
|
-
type: 'ThisTypeAnnotation',
|
|
279
|
-
loc: node.loc,
|
|
280
|
-
start: node.start,
|
|
281
|
-
end: node.end
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
243
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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',
|
|
292
255
|
loc: node.loc,
|
|
293
256
|
start: node.start,
|
|
294
257
|
end: node.end,
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
generator = value.generator,
|
|
319
|
-
returnType = value.returnType,
|
|
320
|
-
typeParameters = value.typeParameters,
|
|
321
|
-
predicate = value.predicate;
|
|
322
|
-
return {
|
|
323
|
-
type: 'ObjectMethod',
|
|
324
|
-
loc: node.loc,
|
|
325
|
-
start: node.start,
|
|
326
|
-
end: node.end,
|
|
327
|
-
// Non getter or setter methods have `kind = method`
|
|
328
|
-
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
329
|
-
computed: node.computed,
|
|
330
|
-
key: key,
|
|
331
|
-
id: id,
|
|
332
|
-
params: params,
|
|
333
|
-
body: body,
|
|
334
|
-
async: async,
|
|
335
|
-
generator: generator,
|
|
336
|
-
returnType: returnType,
|
|
337
|
-
typeParameters: typeParameters,
|
|
338
|
-
predicate: predicate
|
|
339
|
-
};
|
|
340
|
-
} else {
|
|
341
|
-
// Non-method property nodes should be renamed to ObjectProperty
|
|
342
|
-
node.type = 'ObjectProperty';
|
|
343
|
-
return node;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}, {
|
|
347
|
-
key: "mapMethodDefinition",
|
|
348
|
-
value: function mapMethodDefinition(node) {
|
|
349
|
-
var key = this.mapNode(node.key);
|
|
350
|
-
var value = this.mapNode(node.value); // Properties under the FunctionExpression value that should be moved
|
|
351
|
-
// to the ClassMethod node itself.
|
|
352
|
-
|
|
353
|
-
var id = value.id,
|
|
354
|
-
params = value.params,
|
|
355
|
-
body = value.body,
|
|
356
|
-
async = value.async,
|
|
357
|
-
generator = value.generator,
|
|
358
|
-
returnType = value.returnType,
|
|
359
|
-
typeParameters = value.typeParameters,
|
|
360
|
-
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;
|
|
361
281
|
return {
|
|
362
|
-
type: '
|
|
282
|
+
type: 'ObjectMethod',
|
|
363
283
|
loc: node.loc,
|
|
364
284
|
start: node.start,
|
|
365
285
|
end: node.end,
|
|
366
|
-
kind
|
|
286
|
+
// Non getter or setter methods have `kind = method`
|
|
287
|
+
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
367
288
|
computed: node.computed,
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
predicate: predicate
|
|
289
|
+
key,
|
|
290
|
+
id,
|
|
291
|
+
params,
|
|
292
|
+
body,
|
|
293
|
+
async,
|
|
294
|
+
generator,
|
|
295
|
+
returnType,
|
|
296
|
+
typeParameters,
|
|
297
|
+
predicate
|
|
378
298
|
};
|
|
299
|
+
} else {
|
|
300
|
+
// Non-method property nodes should be renamed to ObjectProperty
|
|
301
|
+
node.type = 'ObjectProperty';
|
|
302
|
+
return node;
|
|
379
303
|
}
|
|
380
|
-
}
|
|
381
|
-
key: "mapRestElement",
|
|
382
|
-
value: function mapRestElement(node) {
|
|
383
|
-
var restElement = this.mapNodeDefault(node); // Hermes puts type annotations on rest elements on the argument node,
|
|
384
|
-
// but Babel expects type annotations on the rest element node itself.
|
|
304
|
+
}
|
|
385
305
|
|
|
386
|
-
|
|
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
|
+
}
|
|
387
340
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
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.
|
|
392
344
|
|
|
393
|
-
|
|
394
|
-
}
|
|
395
|
-
}, {
|
|
396
|
-
key: "mapImportExpression",
|
|
397
|
-
value: function mapImportExpression(node) {
|
|
398
|
-
// Babel expects ImportExpression to be structued as a regular
|
|
399
|
-
// CallExpression where the callee is an Import node.
|
|
400
|
-
return {
|
|
401
|
-
type: 'CallExpression',
|
|
402
|
-
loc: node.loc,
|
|
403
|
-
start: node.start,
|
|
404
|
-
end: node.end,
|
|
405
|
-
callee: {
|
|
406
|
-
type: 'Import',
|
|
407
|
-
loc: node.loc,
|
|
408
|
-
start: node.start,
|
|
409
|
-
end: node.end
|
|
410
|
-
},
|
|
411
|
-
arguments: [this.mapNode(node.source)]
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
}, {
|
|
415
|
-
key: "mapFunction",
|
|
416
|
-
value: function mapFunction(node) {
|
|
417
|
-
// Remove the first parameter if it is a this-type annotation,
|
|
418
|
-
// which is not recognized by Babel.
|
|
419
|
-
if (node.params.length !== 0 && node.params[0].name === 'this') {
|
|
420
|
-
node.params.shift();
|
|
421
|
-
}
|
|
345
|
+
const annotation = restElement.argument.typeAnnotation;
|
|
422
346
|
|
|
423
|
-
|
|
347
|
+
if (annotation != null) {
|
|
348
|
+
restElement.typeAnnotation = annotation;
|
|
349
|
+
restElement.argument.typeAnnotation = null;
|
|
424
350
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
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',
|
|
435
365
|
loc: node.loc,
|
|
436
366
|
start: node.start,
|
|
437
367
|
end: node.end
|
|
438
|
-
}
|
|
368
|
+
},
|
|
369
|
+
arguments: [this.mapNode(node.source)]
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
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();
|
|
439
391
|
}
|
|
440
|
-
}]);
|
|
441
392
|
|
|
442
|
-
|
|
443
|
-
}
|
|
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
|
+
}
|
|
444
411
|
|
|
445
|
-
|
|
412
|
+
exports.default = HermesToBabelAdapter;
|