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