hermes-parser 0.28.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/HermesASTAdapter.js +1 -2
- package/dist/HermesASTAdapter.js.flow +2 -5
- package/dist/HermesParserNodeDeserializers.js +3 -2
- package/dist/HermesParserWASM.js +1 -1
- package/dist/HermesToESTreeAdapter.js +12 -0
- package/dist/HermesToESTreeAdapter.js.flow +10 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +21 -19
- package/dist/estree/StripComponentSyntax.js.flow +6 -5
- package/dist/estree/TransformMatchSyntax.js +4 -15
- package/dist/estree/TransformMatchSyntax.js.flow +18 -19
- package/dist/generated/ESTreeVisitorKeys.js +1 -1
- package/dist/generated/ParserVisitorKeys.js +1 -1
- package/dist/index.js.flow +1 -1
- package/dist/src/HermesASTAdapter.js +192 -0
- package/dist/src/HermesParser.js +108 -0
- package/dist/src/HermesParserDecodeUTF8String.js +68 -0
- package/dist/src/HermesParserDeserializer.js +243 -0
- package/dist/src/HermesParserNodeDeserializers.js +2473 -0
- package/dist/src/HermesToESTreeAdapter.js +453 -0
- package/dist/src/ParserOptions.js +18 -0
- package/dist/src/babel/TransformESTreeToBabel.js +1104 -0
- package/dist/src/estree/StripComponentSyntax.js +788 -0
- package/dist/src/estree/StripFlowTypes.js +175 -0
- package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/src/estree/TransformMatchSyntax.js +1005 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
- package/dist/src/generated/ParserVisitorKeys.js +790 -0
- package/dist/src/getModuleDocblock.js +112 -0
- package/dist/src/index.js +153 -0
- package/dist/src/transform/SimpleTransform.js +120 -0
- package/dist/src/transform/astArrayMutationHelpers.js +62 -0
- package/dist/src/transform/astNodeMutationHelpers.js +195 -0
- package/dist/src/traverse/SimpleTraverser.js +137 -0
- package/dist/src/traverse/getVisitorKeys.js +37 -0
- package/dist/src/utils/Builders.js +191 -0
- package/dist/src/utils/GenID.js +41 -0
- package/dist/src/utils/createSyntaxError.js +25 -0
- package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/utils/Builders.js +17 -0
- package/dist/utils/Builders.js.flow +20 -0
- package/package.json +2 -2
|
@@ -116,6 +116,9 @@ class HermesToESTreeAdapter extends _HermesASTAdapter.default {
|
|
|
116
116
|
case 'OptionalCallExpression':
|
|
117
117
|
return this.mapChainExpression(node);
|
|
118
118
|
|
|
119
|
+
case 'BlockStatement':
|
|
120
|
+
return this.mapBlockStatement(node);
|
|
121
|
+
|
|
119
122
|
default:
|
|
120
123
|
return this.mapNodeDefault(node);
|
|
121
124
|
}
|
|
@@ -436,6 +439,15 @@ class HermesToESTreeAdapter extends _HermesASTAdapter.default {
|
|
|
436
439
|
return node;
|
|
437
440
|
}
|
|
438
441
|
|
|
442
|
+
mapBlockStatement(node) {
|
|
443
|
+
if (node.implicit && node.body.length) {
|
|
444
|
+
return this.mapNode(node.body[0]);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
delete node.implicit;
|
|
448
|
+
return this.mapNodeDefault(node);
|
|
449
|
+
}
|
|
450
|
+
|
|
439
451
|
}
|
|
440
452
|
|
|
441
453
|
exports.default = HermesToESTreeAdapter;
|
|
@@ -92,6 +92,8 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
|
|
|
92
92
|
case 'CallExpression':
|
|
93
93
|
case 'OptionalCallExpression':
|
|
94
94
|
return this.mapChainExpression(node);
|
|
95
|
+
case 'BlockStatement':
|
|
96
|
+
return this.mapBlockStatement(node);
|
|
95
97
|
default:
|
|
96
98
|
return this.mapNodeDefault(node);
|
|
97
99
|
}
|
|
@@ -424,4 +426,12 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
|
|
|
424
426
|
node.exported = node.exported ?? null;
|
|
425
427
|
return node;
|
|
426
428
|
}
|
|
429
|
+
|
|
430
|
+
mapBlockStatement(node: HermesNode): HermesNode {
|
|
431
|
+
if (node.implicit && node.body.length) {
|
|
432
|
+
return this.mapNode(node.body[0]);
|
|
433
|
+
}
|
|
434
|
+
delete node.implicit;
|
|
435
|
+
return this.mapNodeDefault(node);
|
|
436
|
+
}
|
|
427
437
|
}
|
|
@@ -406,26 +406,28 @@ function mapProgram(node: Program): BabelFile {
|
|
|
406
406
|
};
|
|
407
407
|
const range = [0, endRange];
|
|
408
408
|
|
|
409
|
-
const babelComments = program.comments.map(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
409
|
+
const babelComments: $ReadOnlyArray<BabelComment> = program.comments.map(
|
|
410
|
+
comment => {
|
|
411
|
+
switch (comment.type) {
|
|
412
|
+
case 'Line': {
|
|
413
|
+
return {
|
|
414
|
+
type: 'CommentLine',
|
|
415
|
+
value: comment.value,
|
|
416
|
+
loc: comment.loc,
|
|
417
|
+
range: comment.range,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
case 'Block': {
|
|
421
|
+
return {
|
|
422
|
+
type: 'CommentBlock',
|
|
423
|
+
value: comment.value,
|
|
424
|
+
loc: comment.loc,
|
|
425
|
+
range: comment.range,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
418
428
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
type: 'CommentBlock',
|
|
422
|
-
value: comment.value,
|
|
423
|
-
loc: comment.loc,
|
|
424
|
-
range: comment.range,
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
+
},
|
|
430
|
+
);
|
|
429
431
|
|
|
430
432
|
// Rename root node to File node and move Program node under program property
|
|
431
433
|
return {
|
|
@@ -32,6 +32,7 @@ import type {
|
|
|
32
32
|
SourceLocation,
|
|
33
33
|
Position,
|
|
34
34
|
ObjectPattern,
|
|
35
|
+
ObjectTypeAnnotation,
|
|
35
36
|
Identifier,
|
|
36
37
|
Range,
|
|
37
38
|
RestElement,
|
|
@@ -139,7 +140,7 @@ function createPropsTypeAnnotation(
|
|
|
139
140
|
typeProperties.unshift(spread);
|
|
140
141
|
}
|
|
141
142
|
|
|
142
|
-
const propTypeObj = {
|
|
143
|
+
const propTypeObj: ObjectTypeAnnotation = {
|
|
143
144
|
type: 'ObjectTypeAnnotation',
|
|
144
145
|
callProperties: [],
|
|
145
146
|
properties: typeProperties,
|
|
@@ -238,7 +239,7 @@ function mapComponentParameters(
|
|
|
238
239
|
|
|
239
240
|
const propsProperties = paramsWithoutRef.flatMap(mapComponentParameter);
|
|
240
241
|
|
|
241
|
-
let props = null;
|
|
242
|
+
let props: null | ObjectPattern | Identifier = null;
|
|
242
243
|
if (propsProperties.length === 0) {
|
|
243
244
|
if (refParam == null) {
|
|
244
245
|
throw new Error(
|
|
@@ -460,7 +461,7 @@ type ForwardRefDetails = {
|
|
|
460
461
|
function createForwardRefWrapper(
|
|
461
462
|
originalComponent: ComponentDeclaration,
|
|
462
463
|
): ForwardRefDetails {
|
|
463
|
-
const internalCompId = {
|
|
464
|
+
const internalCompId: Identifier = {
|
|
464
465
|
type: 'Identifier',
|
|
465
466
|
name: `${originalComponent.id.name}_withRef`,
|
|
466
467
|
optional: false,
|
|
@@ -578,7 +579,7 @@ function mapComponentDeclaration(
|
|
|
578
579
|
forwardRefDetails = createForwardRefWrapper(node);
|
|
579
580
|
}
|
|
580
581
|
|
|
581
|
-
const comp = {
|
|
582
|
+
const comp: FunctionDeclaration = {
|
|
582
583
|
type: 'FunctionDeclaration',
|
|
583
584
|
id:
|
|
584
585
|
forwardRefDetails != null
|
|
@@ -636,7 +637,7 @@ function mapDeclareHook(node: DeclareHook): DeclareFunction {
|
|
|
636
637
|
}
|
|
637
638
|
|
|
638
639
|
function mapHookDeclaration(node: HookDeclaration): FunctionDeclaration {
|
|
639
|
-
const comp = {
|
|
640
|
+
const comp: FunctionDeclaration = {
|
|
640
641
|
type: 'FunctionDeclaration',
|
|
641
642
|
id: node.id && shallowCloneNode(node.id),
|
|
642
643
|
__hookDeclaration: true,
|
|
@@ -551,23 +551,12 @@ function testsOfCondition(root, condition) {
|
|
|
551
551
|
|
|
552
552
|
case 'object':
|
|
553
553
|
{
|
|
554
|
-
// typeof <x> === 'object' && <x> !== null
|
|
554
|
+
// (typeof <x> === 'object' && <x> !== null) || typeof <x> === 'function'
|
|
555
555
|
const {
|
|
556
556
|
key
|
|
557
557
|
} = condition;
|
|
558
|
-
const typeofObject =
|
|
559
|
-
|
|
560
|
-
left: {
|
|
561
|
-
type: 'UnaryExpression',
|
|
562
|
-
operator: 'typeof',
|
|
563
|
-
argument: expressionOfKey(root, key),
|
|
564
|
-
prefix: true,
|
|
565
|
-
...(0, _Builders.etc)()
|
|
566
|
-
},
|
|
567
|
-
right: (0, _Builders.stringLiteral)('object'),
|
|
568
|
-
operator: '===',
|
|
569
|
-
...(0, _Builders.etc)()
|
|
570
|
-
};
|
|
558
|
+
const typeofObject = (0, _Builders.typeofExpression)(expressionOfKey(root, key), 'object');
|
|
559
|
+
const typeofFunction = (0, _Builders.typeofExpression)(expressionOfKey(root, key), 'function');
|
|
571
560
|
const notNull = {
|
|
572
561
|
type: 'BinaryExpression',
|
|
573
562
|
left: expressionOfKey(root, key),
|
|
@@ -575,7 +564,7 @@ function testsOfCondition(root, condition) {
|
|
|
575
564
|
operator: '!==',
|
|
576
565
|
...(0, _Builders.etc)()
|
|
577
566
|
};
|
|
578
|
-
return [typeofObject, notNull];
|
|
567
|
+
return [(0, _Builders.disjunction)([(0, _Builders.conjunction)([typeofObject, notNull]), typeofFunction])];
|
|
579
568
|
}
|
|
580
569
|
|
|
581
570
|
case 'prop-exists':
|
|
@@ -18,6 +18,7 @@ import type {ParserOptions} from '../ParserOptions';
|
|
|
18
18
|
import type {
|
|
19
19
|
BinaryExpression,
|
|
20
20
|
BreakStatement,
|
|
21
|
+
DestructuringObjectProperty,
|
|
21
22
|
ESNode,
|
|
22
23
|
Expression,
|
|
23
24
|
Identifier,
|
|
@@ -53,6 +54,7 @@ import {
|
|
|
53
54
|
numberLiteral,
|
|
54
55
|
stringLiteral,
|
|
55
56
|
throwStatement,
|
|
57
|
+
typeofExpression,
|
|
56
58
|
variableDeclaration,
|
|
57
59
|
} from '../utils/Builders';
|
|
58
60
|
import {createGenID} from '../utils/GenID';
|
|
@@ -279,7 +281,7 @@ function analyzePattern(
|
|
|
279
281
|
const [id, kind] =
|
|
280
282
|
target.type === 'MatchBindingPattern'
|
|
281
283
|
? [target.id, target.kind]
|
|
282
|
-
: [target, 'const'];
|
|
284
|
+
: [target, ('const': 'const')];
|
|
283
285
|
checkDuplicateBindingName(seenBindingNames, pattern, id.name);
|
|
284
286
|
checkBindingKind(pattern, kind);
|
|
285
287
|
const binding: Binding = {type: 'id', key, kind, id};
|
|
@@ -476,29 +478,26 @@ function testsOfCondition(
|
|
|
476
478
|
return [isArray, lengthCheck];
|
|
477
479
|
}
|
|
478
480
|
case 'object': {
|
|
479
|
-
// typeof <x> === 'object' && <x> !== null
|
|
481
|
+
// (typeof <x> === 'object' && <x> !== null) || typeof <x> === 'function'
|
|
480
482
|
const {key} = condition;
|
|
481
|
-
const typeofObject
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
right: stringLiteral('object'),
|
|
491
|
-
operator: '===',
|
|
492
|
-
...etc(),
|
|
493
|
-
};
|
|
494
|
-
const notNull = {
|
|
483
|
+
const typeofObject = typeofExpression(
|
|
484
|
+
expressionOfKey(root, key),
|
|
485
|
+
'object',
|
|
486
|
+
);
|
|
487
|
+
const typeofFunction = typeofExpression(
|
|
488
|
+
expressionOfKey(root, key),
|
|
489
|
+
'function',
|
|
490
|
+
);
|
|
491
|
+
const notNull: BinaryExpression = {
|
|
495
492
|
type: 'BinaryExpression',
|
|
496
493
|
left: expressionOfKey(root, key),
|
|
497
494
|
right: nullLiteral(),
|
|
498
495
|
operator: '!==',
|
|
499
496
|
...etc(),
|
|
500
497
|
};
|
|
501
|
-
return [
|
|
498
|
+
return [
|
|
499
|
+
disjunction([conjunction([typeofObject, notNull]), typeofFunction]),
|
|
500
|
+
];
|
|
502
501
|
}
|
|
503
502
|
case 'prop-exists': {
|
|
504
503
|
// <propName> in <x>
|
|
@@ -563,7 +562,7 @@ function statementsOfBindings(
|
|
|
563
562
|
const destructuring: ObjectPattern = {
|
|
564
563
|
type: 'ObjectPattern',
|
|
565
564
|
properties: exclude
|
|
566
|
-
.map(prop =>
|
|
565
|
+
.map((prop): DestructuringObjectProperty =>
|
|
567
566
|
prop.type === 'Identifier'
|
|
568
567
|
? {
|
|
569
568
|
type: 'Property',
|
|
@@ -737,7 +736,7 @@ function mapMatchExpression(node: MatchExpression): Expression {
|
|
|
737
736
|
// If the original argument is simple, no need for a new variable.
|
|
738
737
|
const statements: Array<Statement> = analyses.map(
|
|
739
738
|
({conditions, bindings, guard, body}) => {
|
|
740
|
-
const returnNode = {
|
|
739
|
+
const returnNode: Statement = {
|
|
741
740
|
type: 'ReturnStatement',
|
|
742
741
|
argument: body,
|
|
743
742
|
...etc(),
|
|
@@ -104,7 +104,7 @@ module.exports = {
|
|
|
104
104
|
ImportAttribute: ['key', 'value'],
|
|
105
105
|
ImportDeclaration: ['specifiers', 'source', 'assertions'],
|
|
106
106
|
ImportDefaultSpecifier: ['local'],
|
|
107
|
-
ImportExpression: ['source', '
|
|
107
|
+
ImportExpression: ['source', 'options'],
|
|
108
108
|
ImportNamespaceSpecifier: ['local'],
|
|
109
109
|
ImportSpecifier: ['imported', 'local'],
|
|
110
110
|
IndexedAccessType: ['objectType', 'indexType'],
|
package/dist/index.js.flow
CHANGED
|
@@ -24,7 +24,7 @@ import * as TransformESTreeToBabel from './babel/TransformESTreeToBabel';
|
|
|
24
24
|
import * as StripFlowTypes from './estree/StripFlowTypes';
|
|
25
25
|
|
|
26
26
|
const DEFAULTS = {
|
|
27
|
-
flow: 'detect',
|
|
27
|
+
flow: ('detect': 'detect'),
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
function getOptions(options?: ParserOptions = {...DEFAULTS}) {
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _ParserVisitorKeys = require("./generated/ParserVisitorKeys");
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
12
|
+
*
|
|
13
|
+
* This source code is licensed under the MIT license found in the
|
|
14
|
+
* LICENSE file in the root directory of this source tree.
|
|
15
|
+
*
|
|
16
|
+
*
|
|
17
|
+
* @format
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The base class for transforming the Hermes AST to the desired output format.
|
|
22
|
+
* Extended by concrete adapters which output an ESTree or Babel AST.
|
|
23
|
+
*/
|
|
24
|
+
class HermesASTAdapter {
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.sourceFilename = void 0;
|
|
27
|
+
this.sourceType = void 0;
|
|
28
|
+
this.sourceFilename = options.sourceFilename;
|
|
29
|
+
this.sourceType = options.sourceType;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Transform the input Hermes AST to the desired output format.
|
|
33
|
+
* This modifies the input AST in place instead of constructing a new AST.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
transform(program) {
|
|
38
|
+
// Comments are not traversed via visitor keys
|
|
39
|
+
const comments = program.comments;
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < comments.length; i++) {
|
|
42
|
+
const comment = comments[i];
|
|
43
|
+
this.fixSourceLocation(comment);
|
|
44
|
+
comments[i] = this.mapComment(comment);
|
|
45
|
+
} // The first comment may be an interpreter directive and is stored directly on the program node
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
|
|
49
|
+
|
|
50
|
+
const tokens = program.tokens;
|
|
51
|
+
|
|
52
|
+
if (tokens) {
|
|
53
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
54
|
+
this.fixSourceLocation(tokens[i]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const resultNode = this.mapNode(program);
|
|
59
|
+
|
|
60
|
+
if (resultNode.type !== 'Program') {
|
|
61
|
+
throw new Error(`HermesToESTreeAdapter: Must return a Program node, instead of "${resultNode.type}". `);
|
|
62
|
+
} // $FlowExpectedError[incompatible-return] We know this is a program at this point.
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
return resultNode;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Transform a Hermes AST node to the output AST format.
|
|
69
|
+
*
|
|
70
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
71
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
mapNode(_node) {
|
|
76
|
+
throw new Error('Implemented in subclasses');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
mapNodeDefault(node) {
|
|
80
|
+
const visitorKeys = _ParserVisitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
|
|
81
|
+
|
|
82
|
+
for (const key in visitorKeys) {
|
|
83
|
+
const childType = visitorKeys[key];
|
|
84
|
+
|
|
85
|
+
if (childType === _ParserVisitorKeys.NODE_CHILD) {
|
|
86
|
+
const child = node[key];
|
|
87
|
+
|
|
88
|
+
if (child != null) {
|
|
89
|
+
node[key] = this.mapNode(child);
|
|
90
|
+
}
|
|
91
|
+
} else if (childType === _ParserVisitorKeys.NODE_LIST_CHILD) {
|
|
92
|
+
const children = node[key];
|
|
93
|
+
|
|
94
|
+
for (let i = 0; i < children.length; i++) {
|
|
95
|
+
const child = children[i];
|
|
96
|
+
|
|
97
|
+
if (child != null) {
|
|
98
|
+
children[i] = this.mapNode(child);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return node;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Update the source location for this node depending on the output AST format.
|
|
108
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
109
|
+
*/
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
fixSourceLocation(_node) {
|
|
113
|
+
throw new Error('Implemented in subclasses');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
getSourceType() {
|
|
117
|
+
var _this$sourceType;
|
|
118
|
+
|
|
119
|
+
return (_this$sourceType = this.sourceType) != null ? _this$sourceType : 'script';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
setModuleSourceType() {
|
|
123
|
+
if (this.sourceType == null) {
|
|
124
|
+
this.sourceType = 'module';
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
mapComment(node) {
|
|
129
|
+
return node;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
mapEmpty(_node) {
|
|
133
|
+
// $FlowExpectedError
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
mapImportDeclaration(node) {
|
|
138
|
+
if (node.importKind === 'value') {
|
|
139
|
+
this.setModuleSourceType();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return this.mapNodeDefault(node);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
mapImportSpecifier(node) {
|
|
146
|
+
if (node.importKind === 'value') {
|
|
147
|
+
node.importKind = null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return this.mapNodeDefault(node);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
mapExportDefaultDeclaration(node) {
|
|
154
|
+
this.setModuleSourceType();
|
|
155
|
+
return this.mapNodeDefault(node);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
mapExportNamedDeclaration(node) {
|
|
159
|
+
if (node.exportKind === 'value') {
|
|
160
|
+
this.setModuleSourceType();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return this.mapNodeDefault(node);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
mapExportAllDeclaration(node) {
|
|
167
|
+
if (node.exportKind === 'value') {
|
|
168
|
+
this.setModuleSourceType();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return this.mapNodeDefault(node);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
formatError(node, message) {
|
|
175
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
getBigIntLiteralValue(bigintString) {
|
|
179
|
+
const bigint = bigintString // estree spec is to not have a trailing `n` on this property
|
|
180
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
181
|
+
.replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
|
|
182
|
+
.replaceAll('_', '');
|
|
183
|
+
return {
|
|
184
|
+
bigint,
|
|
185
|
+
// coerce the string to a bigint value if supported by the environment
|
|
186
|
+
value: typeof BigInt === 'function' ? BigInt(bigint) : null
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
exports.default = HermesASTAdapter;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
exports.parse = parse;
|
|
16
|
+
|
|
17
|
+
var _HermesParserDeserializer = _interopRequireDefault(require("./HermesParserDeserializer"));
|
|
18
|
+
|
|
19
|
+
var _HermesParserWASM = _interopRequireDefault(require("./HermesParserWASM"));
|
|
20
|
+
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
|
|
23
|
+
let HermesParserWASM;
|
|
24
|
+
let hermesParse;
|
|
25
|
+
let hermesParseResult_free;
|
|
26
|
+
let hermesParseResult_getError;
|
|
27
|
+
let hermesParseResult_getErrorLine;
|
|
28
|
+
let hermesParseResult_getErrorColumn;
|
|
29
|
+
let hermesParseResult_getProgramBuffer;
|
|
30
|
+
let hermesParseResult_getPositionBuffer;
|
|
31
|
+
let hermesParseResult_getPositionBufferSize;
|
|
32
|
+
/**
|
|
33
|
+
* Init the WASM wrapper code generated by `emscripten` to preparse the
|
|
34
|
+
* HermesParser WASM code.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
function initHermesParserWASM() {
|
|
38
|
+
if (HermesParserWASM != null) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
HermesParserWASM = (0, _HermesParserWASM.default)({
|
|
43
|
+
/**
|
|
44
|
+
* The emscripten version of `quit` unconditionally assigns the `status` to
|
|
45
|
+
* `process.exitCode` which overrides any pre-existing code that has been
|
|
46
|
+
* set, even if it is non zero. For our use case we never want an
|
|
47
|
+
* `exitCode` to be set so this override removes that functionality.
|
|
48
|
+
*/
|
|
49
|
+
quit(_status, toThrow) {
|
|
50
|
+
throw toThrow;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
});
|
|
54
|
+
hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number']);
|
|
55
|
+
hermesParseResult_free = HermesParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
|
|
56
|
+
hermesParseResult_getError = HermesParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
|
|
57
|
+
hermesParseResult_getErrorLine = HermesParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
|
|
58
|
+
hermesParseResult_getErrorColumn = HermesParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
|
|
59
|
+
hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
|
|
60
|
+
hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
|
|
61
|
+
hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']);
|
|
62
|
+
} // Copy a string into the WASM heap and null-terminate
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
function copyToHeap(buffer, addr) {
|
|
66
|
+
HermesParserWASM.HEAP8.set(buffer, addr);
|
|
67
|
+
HermesParserWASM.HEAP8[addr + buffer.length] = 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parse(source, options) {
|
|
71
|
+
initHermesParserWASM(); // Allocate space on heap for source text
|
|
72
|
+
|
|
73
|
+
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
74
|
+
|
|
75
|
+
const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
|
|
76
|
+
|
|
77
|
+
if (!sourceAddr) {
|
|
78
|
+
throw new Error('Parser out of memory');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
// Copy source text onto WASM heap
|
|
83
|
+
copyToHeap(sourceBuffer, sourceAddr);
|
|
84
|
+
const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.enableExperimentalComponentSyntax, options.enableExperimentalFlowMatchSyntax, options.tokens, options.allowReturnOutsideFunction);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
// Extract and throw error from parse result if parsing failed
|
|
88
|
+
const err = hermesParseResult_getError(parseResult);
|
|
89
|
+
|
|
90
|
+
if (err) {
|
|
91
|
+
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
92
|
+
|
|
93
|
+
syntaxError.loc = {
|
|
94
|
+
line: hermesParseResult_getErrorLine(parseResult),
|
|
95
|
+
column: hermesParseResult_getErrorColumn(parseResult)
|
|
96
|
+
};
|
|
97
|
+
throw syntaxError;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), HermesParserWASM, options);
|
|
101
|
+
return deserializer.deserialize();
|
|
102
|
+
} finally {
|
|
103
|
+
hermesParseResult_free(parseResult);
|
|
104
|
+
}
|
|
105
|
+
} finally {
|
|
106
|
+
HermesParserWASM._free(sourceAddr);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
/**
|
|
12
|
+
* Decode a UTF-8 encoded string from Hermes with a known length.
|
|
13
|
+
* Based on Emscripten's UTF8ToString with the following differences:
|
|
14
|
+
* - Always reads all bytes up to the given length, including null bytes. This
|
|
15
|
+
* means that we can decode strings that contain null bytes in the middle.
|
|
16
|
+
* - Allow UTF-8 encoded code points that are part of a surrogate pair, even though
|
|
17
|
+
* this is technically invalid UTF-8 that UTF8ToString would convert to 0xfffd.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
Object.defineProperty(exports, "__esModule", {
|
|
21
|
+
value: true
|
|
22
|
+
});
|
|
23
|
+
exports.default = HermesParserDecodeUTF8String;
|
|
24
|
+
|
|
25
|
+
function HermesParserDecodeUTF8String(ptrIn, length, heap) {
|
|
26
|
+
let ptr = ptrIn;
|
|
27
|
+
const endPtr = ptr + length;
|
|
28
|
+
let str = '';
|
|
29
|
+
|
|
30
|
+
while (ptr < endPtr) {
|
|
31
|
+
// ASCII characters fit in single byte code point
|
|
32
|
+
let u0 = heap[ptr++];
|
|
33
|
+
|
|
34
|
+
if (!(u0 & 0x80)) {
|
|
35
|
+
str += String.fromCharCode(u0);
|
|
36
|
+
continue;
|
|
37
|
+
} // Two byte code point
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
const u1 = heap[ptr++] & 0x3f;
|
|
41
|
+
|
|
42
|
+
if ((u0 & 0xe0) === 0xc0) {
|
|
43
|
+
str += String.fromCharCode((u0 & 0x1f) << 6 | u1);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const u2 = heap[ptr++] & 0x3f;
|
|
48
|
+
|
|
49
|
+
if ((u0 & 0xf0) === 0xe0) {
|
|
50
|
+
// Three byte code point
|
|
51
|
+
u0 = (u0 & 0x0f) << 12 | u1 << 6 | u2;
|
|
52
|
+
} else {
|
|
53
|
+
// Four byte code point
|
|
54
|
+
u0 = (u0 & 0x07) << 18 | u1 << 12 | u2 << 6 | heap[ptr++] & 0x3f;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (u0 < 0x10000) {
|
|
58
|
+
// Code point fits into a single UTF-16 code unit
|
|
59
|
+
str += String.fromCharCode(u0);
|
|
60
|
+
} else {
|
|
61
|
+
// Code point does not fit into single UTF-16 code unit so convert to surrogate pair
|
|
62
|
+
u0 -= 0x10000;
|
|
63
|
+
str += String.fromCharCode(0xd800 | u0 >> 10, 0xdc00 | u0 & 0x3ff);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return str;
|
|
68
|
+
}
|