hermes-parser 0.15.0 → 0.16.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/dist/HermesASTAdapter.js +8 -1
- package/dist/HermesASTAdapter.js.flow +11 -2
- package/dist/HermesParserNodeDeserializers.js +62 -1
- package/dist/HermesParserWASM.js +1 -1
- package/dist/HermesToESTreeAdapter.js +4 -2
- package/dist/HermesToESTreeAdapter.js.flow +7 -2
- package/dist/babel/TransformESTreeToBabel.js +1078 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +1243 -0
- package/dist/estree/StripComponentSyntax.js +623 -0
- package/dist/estree/StripComponentSyntax.js.flow +671 -0
- package/dist/estree/StripFlowTypes.js +172 -0
- package/dist/estree/StripFlowTypes.js.flow +155 -0
- package/dist/estree/StripFlowTypesForBabel.js +169 -0
- package/dist/estree/StripFlowTypesForBabel.js.flow +173 -0
- package/dist/generated/ESTreeVisitorKeys.js +1 -0
- package/dist/generated/ParserVisitorKeys.js +6 -0
- package/dist/index.js +31 -11
- package/dist/index.js.flow +31 -13
- package/dist/transform/SimpleTransform.js +32 -5
- package/dist/transform/SimpleTransform.js.flow +45 -7
- package/dist/transform/astNodeMutationHelpers.js +26 -19
- package/dist/transform/astNodeMutationHelpers.js.flow +52 -16
- package/package.json +2 -2
- package/dist/HermesToBabelAdapter.js +0 -787
- package/dist/HermesToBabelAdapter.js.flow +0 -785
|
@@ -1,787 +0,0 @@
|
|
|
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
|
-
|
|
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
|
-
function createDefaultPosition() {
|
|
22
|
-
return {
|
|
23
|
-
line: 1,
|
|
24
|
-
column: 0
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function createSyntaxError(node, err) {
|
|
29
|
-
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
30
|
-
|
|
31
|
-
syntaxError.loc = {
|
|
32
|
-
line: node.loc.start.line,
|
|
33
|
-
column: node.loc.start.column
|
|
34
|
-
};
|
|
35
|
-
return syntaxError;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
class HermesToBabelAdapter extends _HermesASTAdapter.default {
|
|
39
|
-
fixSourceLocation(node) {
|
|
40
|
-
var _this$sourceFilename;
|
|
41
|
-
|
|
42
|
-
const loc = node.loc;
|
|
43
|
-
|
|
44
|
-
if (loc == null) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
node.loc = {
|
|
49
|
-
source: (_this$sourceFilename = this.sourceFilename) != null ? _this$sourceFilename : null,
|
|
50
|
-
start: loc.start,
|
|
51
|
-
end: loc.end
|
|
52
|
-
};
|
|
53
|
-
node.start = loc.rangeStart;
|
|
54
|
-
node.end = loc.rangeEnd;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
mapNode(node) {
|
|
58
|
-
this.fixSourceLocation(node);
|
|
59
|
-
|
|
60
|
-
switch (node.type) {
|
|
61
|
-
case 'Program':
|
|
62
|
-
return this.mapProgram(node);
|
|
63
|
-
|
|
64
|
-
case 'BlockStatement':
|
|
65
|
-
return this.mapNodeWithDirectives(node);
|
|
66
|
-
|
|
67
|
-
case 'Empty':
|
|
68
|
-
return this.mapEmpty(node);
|
|
69
|
-
|
|
70
|
-
case 'Identifier':
|
|
71
|
-
return this.mapIdentifier(node);
|
|
72
|
-
|
|
73
|
-
case 'TemplateElement':
|
|
74
|
-
return this.mapTemplateElement(node);
|
|
75
|
-
|
|
76
|
-
case 'GenericTypeAnnotation':
|
|
77
|
-
return this.mapGenericTypeAnnotation(node);
|
|
78
|
-
|
|
79
|
-
case 'SymbolTypeAnnotation':
|
|
80
|
-
return this.mapSymbolTypeAnnotation(node);
|
|
81
|
-
|
|
82
|
-
case 'Property':
|
|
83
|
-
return this.mapProperty(node);
|
|
84
|
-
|
|
85
|
-
case 'MethodDefinition':
|
|
86
|
-
return this.mapMethodDefinition(node);
|
|
87
|
-
|
|
88
|
-
case 'ImportDeclaration':
|
|
89
|
-
return this.mapImportDeclaration(node);
|
|
90
|
-
|
|
91
|
-
case 'ImportSpecifier':
|
|
92
|
-
return this.mapImportSpecifier(node);
|
|
93
|
-
|
|
94
|
-
case 'ExportDefaultDeclaration':
|
|
95
|
-
return this.mapExportDefaultDeclaration(node);
|
|
96
|
-
|
|
97
|
-
case 'ExportNamedDeclaration':
|
|
98
|
-
return this.mapExportNamedDeclaration(node);
|
|
99
|
-
|
|
100
|
-
case 'ExportNamespaceSpecifier':
|
|
101
|
-
return this.mapExportNamespaceSpecifier(node);
|
|
102
|
-
|
|
103
|
-
case 'ExportAllDeclaration':
|
|
104
|
-
return this.mapExportAllDeclaration(node);
|
|
105
|
-
|
|
106
|
-
case 'RestElement':
|
|
107
|
-
return this.mapRestElement(node);
|
|
108
|
-
|
|
109
|
-
case 'ImportExpression':
|
|
110
|
-
return this.mapImportExpression(node);
|
|
111
|
-
|
|
112
|
-
case 'JSXStringLiteral':
|
|
113
|
-
return this.mapJSXStringLiteral(node);
|
|
114
|
-
|
|
115
|
-
case 'PrivateName':
|
|
116
|
-
return this.mapPrivateName(node);
|
|
117
|
-
|
|
118
|
-
case 'ClassPrivateProperty':
|
|
119
|
-
return this.mapPrivateProperty(node);
|
|
120
|
-
|
|
121
|
-
case 'FunctionDeclaration':
|
|
122
|
-
case 'FunctionExpression':
|
|
123
|
-
return this.mapFunction(node);
|
|
124
|
-
|
|
125
|
-
case 'IndexedAccessType':
|
|
126
|
-
case 'OptionalIndexedAccessType':
|
|
127
|
-
case 'KeyofTypeAnnotation':
|
|
128
|
-
case 'ConditionalTypeAnnotation':
|
|
129
|
-
case 'InferTypeAnnotation':
|
|
130
|
-
case 'TupleTypeLabeledElement':
|
|
131
|
-
case 'TupleTypeSpreadElement':
|
|
132
|
-
case 'ObjectTypeMappedTypeProperty':
|
|
133
|
-
case 'ComponentTypeAnnotation':
|
|
134
|
-
case 'TypePredicate':
|
|
135
|
-
return this.mapUnsupportedTypeAnnotation(node);
|
|
136
|
-
|
|
137
|
-
case 'BigIntLiteral':
|
|
138
|
-
return this.mapBigIntLiteral(node);
|
|
139
|
-
|
|
140
|
-
case 'BigIntLiteralTypeAnnotation':
|
|
141
|
-
return this.mapBigIntLiteralTypeAnnotation(node);
|
|
142
|
-
|
|
143
|
-
case 'BigIntTypeAnnotation':
|
|
144
|
-
return this.mapBigIntTypeAnnotation(node);
|
|
145
|
-
|
|
146
|
-
case 'TypeofTypeAnnotation':
|
|
147
|
-
return this.mapTypeofTypeAnnotation(node);
|
|
148
|
-
|
|
149
|
-
case 'QualifiedTypeofIdentifier':
|
|
150
|
-
return this.mapQualifiedTypeofIdentifier(node);
|
|
151
|
-
|
|
152
|
-
case 'DeclareVariable':
|
|
153
|
-
return this.mapDeclareVariable(node);
|
|
154
|
-
|
|
155
|
-
case 'DeclareEnum':
|
|
156
|
-
return this.mapDeclareEnum(node);
|
|
157
|
-
|
|
158
|
-
case 'DeclareComponent':
|
|
159
|
-
return this.mapDeclareComponent(node);
|
|
160
|
-
|
|
161
|
-
case 'JSXElement':
|
|
162
|
-
return this.mapJSXElement(node);
|
|
163
|
-
|
|
164
|
-
case 'ComponentDeclaration':
|
|
165
|
-
return this.mapComponentDeclaration(node);
|
|
166
|
-
|
|
167
|
-
default:
|
|
168
|
-
return this.mapNodeDefault(node);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
mapProgram(node) {
|
|
173
|
-
// Visit child nodes and convert to directives
|
|
174
|
-
const {
|
|
175
|
-
comments,
|
|
176
|
-
...program
|
|
177
|
-
} = this.mapNodeWithDirectives(node);
|
|
178
|
-
program.sourceType = this.getSourceType(); // Adjust start loc to beginning of file
|
|
179
|
-
|
|
180
|
-
program.loc.start = {
|
|
181
|
-
line: 1,
|
|
182
|
-
column: 0
|
|
183
|
-
};
|
|
184
|
-
program.start = 0; // Adjust end loc to include last comment if program ends with a comment
|
|
185
|
-
|
|
186
|
-
if (comments.length > 0) {
|
|
187
|
-
const lastComment = comments[comments.length - 1];
|
|
188
|
-
|
|
189
|
-
if (lastComment.end > program.end) {
|
|
190
|
-
program.loc.end = lastComment.loc.end;
|
|
191
|
-
program.end = lastComment.end;
|
|
192
|
-
}
|
|
193
|
-
} // Rename root node to File node and move Program node under program property
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
type: 'File',
|
|
198
|
-
loc: program.loc,
|
|
199
|
-
start: program.start,
|
|
200
|
-
end: program.end,
|
|
201
|
-
program,
|
|
202
|
-
comments
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
mapNodeWithDirectives(node) {
|
|
207
|
-
const directives = [];
|
|
208
|
-
|
|
209
|
-
for (const child of node.body) {
|
|
210
|
-
if (child.type === 'ExpressionStatement' && child.directive != null) {
|
|
211
|
-
// Visit directive children
|
|
212
|
-
const directiveChild = this.mapNode(child); // Modify string literal node to be DirectiveLiteral node
|
|
213
|
-
|
|
214
|
-
directiveChild.expression.type = 'DirectiveLiteral'; // Construct Directive node with DirectiveLiteral value
|
|
215
|
-
|
|
216
|
-
directives.push({
|
|
217
|
-
type: 'Directive',
|
|
218
|
-
loc: directiveChild.loc,
|
|
219
|
-
start: directiveChild.start,
|
|
220
|
-
end: directiveChild.end,
|
|
221
|
-
value: directiveChild.expression
|
|
222
|
-
});
|
|
223
|
-
} else {
|
|
224
|
-
// Once we have found the first non-directive node we know there cannot be any more directives
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
} // Move directives from body to new directives array
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
node.directives = directives;
|
|
231
|
-
|
|
232
|
-
if (directives.length !== 0) {
|
|
233
|
-
node.body = node.body.slice(directives.length);
|
|
234
|
-
} // Visit expression statement children
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const body = node.body;
|
|
238
|
-
|
|
239
|
-
for (let i = 0; i < body.length; i++) {
|
|
240
|
-
const child = body[i];
|
|
241
|
-
|
|
242
|
-
if (child != null) {
|
|
243
|
-
body[i] = this.mapNode(child);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return node;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
mapIdentifier(node) {
|
|
251
|
-
node.loc.identifierName = node.name;
|
|
252
|
-
return this.mapNodeDefault(node);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
mapTemplateElement(node) {
|
|
256
|
-
// Adjust start loc to exclude "`" at beginning of template literal if this is the first quasi,
|
|
257
|
-
// otherwise exclude "}" from previous expression.
|
|
258
|
-
const startCharsToExclude = 1; // Adjust end loc to exclude "`" at end of template literal if this is the last quasi,
|
|
259
|
-
// otherwise exclude "${" from next expression.
|
|
260
|
-
|
|
261
|
-
const endCharsToExclude = node.tail ? 1 : 2;
|
|
262
|
-
return {
|
|
263
|
-
type: 'TemplateElement',
|
|
264
|
-
loc: {
|
|
265
|
-
start: {
|
|
266
|
-
line: node.loc.start.line,
|
|
267
|
-
column: node.loc.start.column + startCharsToExclude
|
|
268
|
-
},
|
|
269
|
-
end: {
|
|
270
|
-
line: node.loc.end.line,
|
|
271
|
-
column: node.loc.end.column - endCharsToExclude
|
|
272
|
-
}
|
|
273
|
-
},
|
|
274
|
-
start: node.start + startCharsToExclude,
|
|
275
|
-
end: node.end - endCharsToExclude,
|
|
276
|
-
tail: node.tail,
|
|
277
|
-
value: {
|
|
278
|
-
cooked: node.cooked,
|
|
279
|
-
raw: node.raw
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
mapGenericTypeAnnotation(node) {
|
|
285
|
-
// Convert simple `this` generic type to ThisTypeAnnotation
|
|
286
|
-
if (node.typeParameters == null && node.id.type === 'Identifier' && node.id.name === 'this') {
|
|
287
|
-
return {
|
|
288
|
-
type: 'ThisTypeAnnotation',
|
|
289
|
-
loc: node.loc,
|
|
290
|
-
start: node.start,
|
|
291
|
-
end: node.end
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return this.mapNodeDefault(node);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
mapSymbolTypeAnnotation(node) {
|
|
299
|
-
return {
|
|
300
|
-
type: 'GenericTypeAnnotation',
|
|
301
|
-
loc: node.loc,
|
|
302
|
-
start: node.start,
|
|
303
|
-
end: node.end,
|
|
304
|
-
id: {
|
|
305
|
-
type: 'Identifier',
|
|
306
|
-
loc: node.loc,
|
|
307
|
-
start: node.start,
|
|
308
|
-
end: node.end,
|
|
309
|
-
name: 'symbol'
|
|
310
|
-
},
|
|
311
|
-
typeParameters: null
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
mapProperty(node) {
|
|
316
|
-
const key = this.mapNode(node.key);
|
|
317
|
-
const value = this.mapNode(node.value); // Convert methods, getters, and setters to ObjectMethod nodes
|
|
318
|
-
|
|
319
|
-
if (node.method || node.kind !== 'init') {
|
|
320
|
-
// Properties under the FunctionExpression value that should be moved
|
|
321
|
-
// to the ObjectMethod node itself.
|
|
322
|
-
const {
|
|
323
|
-
id,
|
|
324
|
-
params,
|
|
325
|
-
body,
|
|
326
|
-
async,
|
|
327
|
-
generator,
|
|
328
|
-
returnType,
|
|
329
|
-
typeParameters,
|
|
330
|
-
predicate
|
|
331
|
-
} = value;
|
|
332
|
-
const newNode = {
|
|
333
|
-
type: 'ObjectMethod',
|
|
334
|
-
loc: node.loc,
|
|
335
|
-
start: node.start,
|
|
336
|
-
end: node.end,
|
|
337
|
-
// Non getter or setter methods have `kind = method`
|
|
338
|
-
kind: node.kind === 'init' ? 'method' : node.kind,
|
|
339
|
-
method: node.kind === 'init' ? true : false,
|
|
340
|
-
computed: node.computed,
|
|
341
|
-
key,
|
|
342
|
-
id,
|
|
343
|
-
params,
|
|
344
|
-
body,
|
|
345
|
-
async,
|
|
346
|
-
generator,
|
|
347
|
-
returnType,
|
|
348
|
-
typeParameters,
|
|
349
|
-
predicate
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
if (node.kind !== 'init') {
|
|
353
|
-
// babel emits an empty variance property on accessors for some reason
|
|
354
|
-
newNode.variance = null;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
return newNode;
|
|
358
|
-
} else {
|
|
359
|
-
// Non-method property nodes should be renamed to ObjectProperty
|
|
360
|
-
node.type = 'ObjectProperty';
|
|
361
|
-
return node;
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
mapMethodDefinition(node) {
|
|
366
|
-
const key = this.mapNode(node.key);
|
|
367
|
-
const value = this.mapNode(node.value); // Properties under the FunctionExpression value that should be moved
|
|
368
|
-
// to the ClassMethod node itself.
|
|
369
|
-
|
|
370
|
-
const {
|
|
371
|
-
id,
|
|
372
|
-
params,
|
|
373
|
-
body,
|
|
374
|
-
async,
|
|
375
|
-
generator,
|
|
376
|
-
returnType,
|
|
377
|
-
typeParameters,
|
|
378
|
-
predicate
|
|
379
|
-
} = value;
|
|
380
|
-
return {
|
|
381
|
-
type: key.type === 'PrivateName' ? 'ClassPrivateMethod' : 'ClassMethod',
|
|
382
|
-
loc: node.loc,
|
|
383
|
-
start: node.start,
|
|
384
|
-
end: node.end,
|
|
385
|
-
kind: node.kind,
|
|
386
|
-
computed: node.computed,
|
|
387
|
-
static: node.static,
|
|
388
|
-
key,
|
|
389
|
-
id,
|
|
390
|
-
params,
|
|
391
|
-
body,
|
|
392
|
-
async,
|
|
393
|
-
generator,
|
|
394
|
-
returnType,
|
|
395
|
-
typeParameters,
|
|
396
|
-
predicate
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
mapRestElement(node) {
|
|
401
|
-
const restElement = this.mapNodeDefault(node); // Hermes puts type annotations on rest elements on the argument node,
|
|
402
|
-
// but Babel expects type annotations on the rest element node itself.
|
|
403
|
-
|
|
404
|
-
const annotation = restElement.argument.typeAnnotation;
|
|
405
|
-
|
|
406
|
-
if (annotation != null) {
|
|
407
|
-
restElement.typeAnnotation = annotation;
|
|
408
|
-
restElement.argument.typeAnnotation = null; // Unfortunately there's no way for us to recover the end location of
|
|
409
|
-
// the argument for the general case
|
|
410
|
-
|
|
411
|
-
if (restElement.argument.type === 'Identifier') {
|
|
412
|
-
restElement.argument.end = restElement.argument.start + restElement.argument.name.length;
|
|
413
|
-
restElement.argument.loc.end = { ...restElement.argument.loc.start,
|
|
414
|
-
column: restElement.argument.loc.start.column + restElement.argument.name.length
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
return restElement;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
mapImportExpression(node) {
|
|
423
|
-
// Babel expects ImportExpression to be structued as a regular
|
|
424
|
-
// CallExpression where the callee is an Import node.
|
|
425
|
-
return {
|
|
426
|
-
type: 'CallExpression',
|
|
427
|
-
loc: node.loc,
|
|
428
|
-
start: node.start,
|
|
429
|
-
end: node.end,
|
|
430
|
-
callee: {
|
|
431
|
-
type: 'Import',
|
|
432
|
-
loc: { ...node.loc,
|
|
433
|
-
end: { ...node.loc.start,
|
|
434
|
-
column: node.loc.start.column + 'import'.length
|
|
435
|
-
}
|
|
436
|
-
},
|
|
437
|
-
start: node.start,
|
|
438
|
-
end: node.start + 'import'.length
|
|
439
|
-
},
|
|
440
|
-
arguments: [this.mapNode(node.source)]
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
mapJSXStringLiteral(node) {
|
|
445
|
-
// Babel expects StringLiterals in JSX,
|
|
446
|
-
// but Hermes uses JSXStringLiteral to attach the raw value without
|
|
447
|
-
// having to internally attach it to every single string literal.
|
|
448
|
-
return {
|
|
449
|
-
type: 'StringLiteral',
|
|
450
|
-
loc: node.loc,
|
|
451
|
-
start: node.start,
|
|
452
|
-
end: node.end,
|
|
453
|
-
value: node.value
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
mapFunction(node) {
|
|
458
|
-
// Remove the first parameter if it is a this-type annotation,
|
|
459
|
-
// which is not recognized by Babel.
|
|
460
|
-
if (node.params.length !== 0 && node.params[0].name === 'this') {
|
|
461
|
-
node.params.shift();
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
return this.mapNodeDefault(node);
|
|
465
|
-
}
|
|
466
|
-
/**
|
|
467
|
-
* If Babel (the version we target) does not support a type annotation we
|
|
468
|
-
* parse, we need to return some other valid type annotation in its place.
|
|
469
|
-
*/
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
mapUnsupportedTypeAnnotation(node) {
|
|
473
|
-
return {
|
|
474
|
-
type: 'AnyTypeAnnotation',
|
|
475
|
-
loc: node.loc,
|
|
476
|
-
start: node.start,
|
|
477
|
-
end: node.end
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
mapBigIntLiteral(node) {
|
|
482
|
-
node.value = this.getBigIntLiteralValue(node.bigint).value;
|
|
483
|
-
return node;
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
mapBigIntLiteralTypeAnnotation(node) {
|
|
487
|
-
node.value = this.getBigIntLiteralValue(node.raw).value;
|
|
488
|
-
return node;
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Babel does not parse the bigint keyword type as the keyword node.
|
|
492
|
-
* So we need to down-level the AST to a plain GenericTypeAnnotation
|
|
493
|
-
*/
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
mapBigIntTypeAnnotation(node) {
|
|
497
|
-
return {
|
|
498
|
-
type: 'GenericTypeAnnotation',
|
|
499
|
-
id: {
|
|
500
|
-
type: 'Identifier',
|
|
501
|
-
name: 'bigint',
|
|
502
|
-
loc: node.loc,
|
|
503
|
-
start: node.start,
|
|
504
|
-
end: node.end
|
|
505
|
-
},
|
|
506
|
-
typeParameters: null,
|
|
507
|
-
loc: node.loc,
|
|
508
|
-
start: node.start,
|
|
509
|
-
end: node.end
|
|
510
|
-
};
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
mapPrivateProperty(nodeUnprocessed) {
|
|
514
|
-
const node = this.mapNodeDefault(nodeUnprocessed);
|
|
515
|
-
node.key = {
|
|
516
|
-
type: 'PrivateName',
|
|
517
|
-
id: { ...node.key,
|
|
518
|
-
// babel doesn't include the hash in the identifier
|
|
519
|
-
start: node.key.start + 1,
|
|
520
|
-
loc: { ...node.key.loc,
|
|
521
|
-
start: { ...node.key.loc.start,
|
|
522
|
-
column: node.key.loc.start.column + 1
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
},
|
|
526
|
-
start: node.key.start,
|
|
527
|
-
end: node.key.end,
|
|
528
|
-
loc: node.key.loc
|
|
529
|
-
};
|
|
530
|
-
return node;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
mapPrivateName(node) {
|
|
534
|
-
// babel doesn't include the hash in the identifier
|
|
535
|
-
node.id.start += 1;
|
|
536
|
-
node.id.loc.start.column += 1;
|
|
537
|
-
return node;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
mapExportNamespaceSpecifier(nodeUnprocessed) {
|
|
541
|
-
const node = this.mapNodeDefault(nodeUnprocessed); // the hermes AST emits the location as the location of the entire export
|
|
542
|
-
// but babel emits the location as *just* the "* as id" bit
|
|
543
|
-
// the end will always align with the end of the identifier (ezpz)
|
|
544
|
-
// but the start will align with the "*" token - which we can't recover from just the AST
|
|
545
|
-
// so we just fudge the start location a bit to get it "good enough"
|
|
546
|
-
// it will be wrong if the AST is anything like "export * as x from 'y'"... but oh well
|
|
547
|
-
|
|
548
|
-
node.start = node.start + 'export '.length;
|
|
549
|
-
node.loc.start.column = node.loc.start.column + 'export '.length;
|
|
550
|
-
node.end = node.exported.end;
|
|
551
|
-
node.loc.end = {
|
|
552
|
-
column: node.exported.loc.end.column,
|
|
553
|
-
line: node.exported.loc.end.line
|
|
554
|
-
};
|
|
555
|
-
return node;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
mapTypeofTypeAnnotation(nodeUnprocessed) {
|
|
559
|
-
nodeUnprocessed.argument = {
|
|
560
|
-
type: 'GenericTypeAnnotation',
|
|
561
|
-
id: nodeUnprocessed.argument,
|
|
562
|
-
typeParameters: null,
|
|
563
|
-
loc: nodeUnprocessed.argument.loc
|
|
564
|
-
};
|
|
565
|
-
return this.mapNodeDefault(nodeUnprocessed);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
mapQualifiedTypeofIdentifier(nodeUnprocessed) {
|
|
569
|
-
nodeUnprocessed.type = 'QualifiedTypeIdentifier';
|
|
570
|
-
return this.mapNodeDefault(nodeUnprocessed);
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
mapDeclareVariable(nodeUnprocessed) {
|
|
574
|
-
delete nodeUnprocessed.kind;
|
|
575
|
-
return this.mapNodeDefault(nodeUnprocessed);
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
mapDeclareEnum(nodeUnprocessed) {
|
|
579
|
-
nodeUnprocessed.id.typeAnnotation = this.mapUnsupportedTypeAnnotation(nodeUnprocessed.body);
|
|
580
|
-
delete nodeUnprocessed.body;
|
|
581
|
-
nodeUnprocessed.type = 'DeclareVariable';
|
|
582
|
-
return this.mapDeclareVariable(nodeUnprocessed);
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
mapDeclareComponent(nodeUnprocessed) {
|
|
586
|
-
nodeUnprocessed.id.typeAnnotation = this.mapUnsupportedTypeAnnotation(nodeUnprocessed);
|
|
587
|
-
delete nodeUnprocessed.params;
|
|
588
|
-
delete nodeUnprocessed.rest;
|
|
589
|
-
delete nodeUnprocessed.typeParameters;
|
|
590
|
-
delete nodeUnprocessed.rendersType;
|
|
591
|
-
nodeUnprocessed.type = 'DeclareVariable';
|
|
592
|
-
return this.mapDeclareVariable(nodeUnprocessed);
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
mapJSXElement(nodeUnprocessed) {
|
|
596
|
-
delete nodeUnprocessed.openingElement.typeArguments;
|
|
597
|
-
return this.mapNodeDefault(nodeUnprocessed);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
mapComponentDeclaration(nodeUnprocessed) {
|
|
601
|
-
let rendersType = nodeUnprocessed.rendersType;
|
|
602
|
-
|
|
603
|
-
if (rendersType == null) {
|
|
604
|
-
// Create empty loc for return type annotation nodes
|
|
605
|
-
const createRendersTypeLoc = () => ({
|
|
606
|
-
loc: {
|
|
607
|
-
start: { ...nodeUnprocessed.body.loc.end
|
|
608
|
-
},
|
|
609
|
-
end: { ...nodeUnprocessed.body.loc.end
|
|
610
|
-
},
|
|
611
|
-
rangeStart: nodeUnprocessed.body.loc.rangeStart,
|
|
612
|
-
rangeEnd: nodeUnprocessed.body.loc.rangeEnd
|
|
613
|
-
}
|
|
614
|
-
});
|
|
615
|
-
|
|
616
|
-
rendersType = {
|
|
617
|
-
type: 'TypeAnnotation',
|
|
618
|
-
typeAnnotation: {
|
|
619
|
-
type: 'GenericTypeAnnotation',
|
|
620
|
-
id: {
|
|
621
|
-
type: 'QualifiedTypeIdentifier',
|
|
622
|
-
qualification: {
|
|
623
|
-
type: 'Identifier',
|
|
624
|
-
name: 'React',
|
|
625
|
-
...createRendersTypeLoc()
|
|
626
|
-
},
|
|
627
|
-
id: {
|
|
628
|
-
type: 'Identifier',
|
|
629
|
-
name: 'Node',
|
|
630
|
-
...createRendersTypeLoc()
|
|
631
|
-
}
|
|
632
|
-
},
|
|
633
|
-
typeParameters: null,
|
|
634
|
-
...createRendersTypeLoc()
|
|
635
|
-
},
|
|
636
|
-
...createRendersTypeLoc()
|
|
637
|
-
};
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
function getParamName(paramName) {
|
|
641
|
-
switch (paramName.type) {
|
|
642
|
-
case 'Identifier':
|
|
643
|
-
return paramName.name;
|
|
644
|
-
|
|
645
|
-
case 'StringLiteral':
|
|
646
|
-
return paramName.value;
|
|
647
|
-
|
|
648
|
-
default:
|
|
649
|
-
throw createSyntaxError(paramName, `Unknown Component parameter name type of "${paramName.type}"`);
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
function createPropsTypeAnnotation(loc) {
|
|
654
|
-
// Create empty loc for type annotation nodes
|
|
655
|
-
const createParamsTypeLoc = () => ({
|
|
656
|
-
loc: {
|
|
657
|
-
start: loc.start != null ? { ...loc.start
|
|
658
|
-
} : createDefaultPosition(),
|
|
659
|
-
end: loc.end != null ? { ...loc.end
|
|
660
|
-
} : createDefaultPosition(),
|
|
661
|
-
rangeStart: loc.rangeStart,
|
|
662
|
-
rangeEnd: loc.rangeEnd
|
|
663
|
-
}
|
|
664
|
-
});
|
|
665
|
-
|
|
666
|
-
return {
|
|
667
|
-
type: 'TypeAnnotation',
|
|
668
|
-
typeAnnotation: {
|
|
669
|
-
type: 'GenericTypeAnnotation',
|
|
670
|
-
id: {
|
|
671
|
-
type: 'Identifier',
|
|
672
|
-
name: '$ReadOnly',
|
|
673
|
-
...createParamsTypeLoc()
|
|
674
|
-
},
|
|
675
|
-
typeParameters: {
|
|
676
|
-
type: 'TypeParameterInstantiation',
|
|
677
|
-
params: [{
|
|
678
|
-
type: 'ObjectTypeAnnotation',
|
|
679
|
-
callProperties: [],
|
|
680
|
-
properties: [],
|
|
681
|
-
indexers: [],
|
|
682
|
-
internalSlots: [],
|
|
683
|
-
exact: false,
|
|
684
|
-
inexact: true,
|
|
685
|
-
...createParamsTypeLoc()
|
|
686
|
-
}],
|
|
687
|
-
...createParamsTypeLoc()
|
|
688
|
-
},
|
|
689
|
-
...createParamsTypeLoc()
|
|
690
|
-
},
|
|
691
|
-
...createParamsTypeLoc()
|
|
692
|
-
};
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
const params = (() => {
|
|
696
|
-
if (nodeUnprocessed.params.length === 0) {
|
|
697
|
-
return [];
|
|
698
|
-
} // Optimize `component Foo(...props: Props) {}` to `function Foo(props: Props) {}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
if (nodeUnprocessed.params.length === 1 && nodeUnprocessed.params[0].type === 'RestElement') {
|
|
702
|
-
const restElement = nodeUnprocessed.params[0];
|
|
703
|
-
return [{ ...restElement.argument,
|
|
704
|
-
typeAnnotation: createPropsTypeAnnotation(restElement.argument.typeAnnotation.loc)
|
|
705
|
-
}];
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
const properties = nodeUnprocessed.params.map(param => {
|
|
709
|
-
switch (param.type) {
|
|
710
|
-
case 'RestElement':
|
|
711
|
-
{
|
|
712
|
-
delete param.typeAnnotation;
|
|
713
|
-
return param;
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
case 'ComponentParameter':
|
|
717
|
-
{
|
|
718
|
-
if (getParamName(param.name) === 'ref') {
|
|
719
|
-
throw createSyntaxError(param, 'Component parameters named "ref" are currently not supported');
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
if (param.name.type === 'Identifier') {
|
|
723
|
-
delete param.name.typeAnnotation;
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
if (param.local.type === 'AssignmentPattern') {
|
|
727
|
-
delete param.local.left.typeAnnotation;
|
|
728
|
-
delete param.local.left.optional;
|
|
729
|
-
} else {
|
|
730
|
-
delete param.local.typeAnnotation;
|
|
731
|
-
delete param.local.optional;
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
return {
|
|
735
|
-
type: 'ObjectProperty',
|
|
736
|
-
key: param.name,
|
|
737
|
-
value: param.local,
|
|
738
|
-
method: false,
|
|
739
|
-
shorthand: param.shorthand,
|
|
740
|
-
computed: false,
|
|
741
|
-
loc: param.loc,
|
|
742
|
-
start: param.start,
|
|
743
|
-
end: param.end
|
|
744
|
-
};
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
default:
|
|
748
|
-
{
|
|
749
|
-
throw createSyntaxError(param, `Unknown Component parameter type of "${param.type}"`);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
});
|
|
753
|
-
const paramsLoc = {
|
|
754
|
-
start: properties[0].loc.start,
|
|
755
|
-
end: properties[properties.length - 1].loc.end,
|
|
756
|
-
rangeStart: properties[0].loc.rangeStart,
|
|
757
|
-
rangeEnd: properties[properties.length - 1].loc.rangeEnd
|
|
758
|
-
};
|
|
759
|
-
return [{
|
|
760
|
-
type: 'ObjectPattern',
|
|
761
|
-
properties,
|
|
762
|
-
typeAnnotation: createPropsTypeAnnotation({ ...paramsLoc,
|
|
763
|
-
start: paramsLoc.end,
|
|
764
|
-
rangeStart: paramsLoc.rangeEnd
|
|
765
|
-
}),
|
|
766
|
-
loc: paramsLoc
|
|
767
|
-
}];
|
|
768
|
-
})();
|
|
769
|
-
|
|
770
|
-
const functionComponent = {
|
|
771
|
-
type: 'FunctionDeclaration',
|
|
772
|
-
id: nodeUnprocessed.id,
|
|
773
|
-
typeParameters: nodeUnprocessed.typeParameters,
|
|
774
|
-
params,
|
|
775
|
-
returnType: rendersType,
|
|
776
|
-
body: nodeUnprocessed.body,
|
|
777
|
-
async: false,
|
|
778
|
-
generator: false,
|
|
779
|
-
predicate: null,
|
|
780
|
-
loc: nodeUnprocessed.loc
|
|
781
|
-
};
|
|
782
|
-
return this.mapNodeDefault(functionComponent);
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
exports.default = HermesToBabelAdapter;
|