hermes-estree 0.31.2 → 0.32.1
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/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +192 -0
- package/dist/HermesASTAdapter.js.flow +189 -0
- package/dist/HermesParser.js +108 -0
- package/dist/HermesParser.js.flow +161 -0
- package/dist/HermesParserDecodeUTF8String.js +68 -0
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +242 -0
- package/dist/HermesParserDeserializer.js.flow +260 -0
- package/dist/HermesParserNodeDeserializers.js +2477 -0
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +6 -0
- package/dist/HermesParserWASM.js.flow +87 -0
- package/dist/HermesToESTreeAdapter.js +439 -0
- package/dist/HermesToESTreeAdapter.js.flow +421 -0
- package/dist/ParserOptions.js +18 -0
- package/dist/ParserOptions.js.flow +41 -0
- package/dist/babel/TransformESTreeToBabel.js +1123 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +1277 -0
- package/dist/estree/StripFlowTypes.js +175 -0
- package/dist/estree/StripFlowTypes.js.flow +158 -0
- package/dist/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/estree/StripFlowTypesForBabel.js.flow +216 -0
- package/dist/estree/TransformComponentSyntax.js +788 -0
- package/dist/estree/TransformComponentSyntax.js.flow +864 -0
- package/dist/estree/TransformEnumSyntax.js +106 -0
- package/dist/estree/TransformEnumSyntax.js.flow +125 -0
- package/dist/estree/TransformMatchSyntax.js +1006 -0
- package/dist/estree/TransformMatchSyntax.js.flow +912 -0
- package/dist/generated/ESTreeVisitorKeys.js +220 -0
- package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
- package/dist/generated/ParserVisitorKeys.js +794 -0
- package/dist/generated/ParserVisitorKeys.js.flow +17 -0
- package/dist/getModuleDocblock.js +112 -0
- package/dist/getModuleDocblock.js.flow +118 -0
- package/dist/predicates.js +1 -0
- package/dist/predicates.js.flow +1 -0
- 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 +242 -0
- package/dist/src/HermesParserNodeDeserializers.js +2477 -0
- package/dist/src/HermesToESTreeAdapter.js +439 -0
- package/dist/src/ParserOptions.js +18 -0
- package/dist/src/babel/TransformESTreeToBabel.js +1123 -0
- package/dist/src/estree/StripFlowTypes.js +175 -0
- package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/src/estree/TransformComponentSyntax.js +788 -0
- package/dist/src/estree/TransformEnumSyntax.js +106 -0
- package/dist/src/estree/TransformMatchSyntax.js +1006 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
- package/dist/src/generated/ParserVisitorKeys.js +794 -0
- package/dist/src/getModuleDocblock.js +112 -0
- package/dist/src/predicates.js +1 -0
- package/dist/src/transform/SimpleTransform.js +136 -0
- package/dist/src/transform/astArrayMutationHelpers.js +62 -0
- package/dist/src/transform/astNodeMutationHelpers.js +200 -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/transform/SimpleTransform.js +136 -0
- package/dist/transform/SimpleTransform.js.flow +169 -0
- package/dist/transform/astArrayMutationHelpers.js +62 -0
- package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
- package/dist/transform/astNodeMutationHelpers.js +200 -0
- package/dist/transform/astNodeMutationHelpers.js.flow +246 -0
- package/dist/traverse/SimpleTraverser.js +137 -0
- package/dist/traverse/SimpleTraverser.js.flow +133 -0
- package/dist/traverse/getVisitorKeys.js +37 -0
- package/dist/traverse/getVisitorKeys.js.flow +36 -0
- package/dist/utils/Builders.js +191 -0
- package/dist/utils/Builders.js.flow +218 -0
- package/dist/utils/GenID.js +41 -0
- package/dist/utils/GenID.js.flow +38 -0
- package/dist/utils/createSyntaxError.js +25 -0
- package/dist/utils/createSyntaxError.js.flow +24 -0
- package/dist/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +130 -0
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// flowlint unclear-type:off
|
|
12
|
+
|
|
13
|
+
export type HermesPosition = {
|
|
14
|
+
/** >= 1 */
|
|
15
|
+
+line: number,
|
|
16
|
+
/** >= 0 */
|
|
17
|
+
+column: number,
|
|
18
|
+
};
|
|
19
|
+
export type HermesSourceLocation = {
|
|
20
|
+
start?: HermesPosition,
|
|
21
|
+
end?: HermesPosition,
|
|
22
|
+
rangeStart?: number,
|
|
23
|
+
rangeEnd?: number,
|
|
24
|
+
};
|
|
25
|
+
export type HermesNode = {
|
|
26
|
+
type: string,
|
|
27
|
+
[string]: any,
|
|
28
|
+
};
|
|
29
|
+
export type HermesToken = {
|
|
30
|
+
type:
|
|
31
|
+
| 'Boolean'
|
|
32
|
+
| 'Identifier'
|
|
33
|
+
| 'Keyword'
|
|
34
|
+
| 'Null'
|
|
35
|
+
| 'Numeric'
|
|
36
|
+
| 'BigInt'
|
|
37
|
+
| 'Punctuator'
|
|
38
|
+
| 'String'
|
|
39
|
+
| 'RegularExpression'
|
|
40
|
+
| 'Template'
|
|
41
|
+
| 'JSXText',
|
|
42
|
+
loc: HermesSourceLocation,
|
|
43
|
+
value: ?string,
|
|
44
|
+
};
|
|
45
|
+
export type HermesComment = {
|
|
46
|
+
type: 'CommentLine' | 'CommentBlock' | 'InterpreterDirective',
|
|
47
|
+
loc: HermesSourceLocation,
|
|
48
|
+
value: ?string,
|
|
49
|
+
};
|
|
50
|
+
export type HermesProgram = {
|
|
51
|
+
type: 'Program',
|
|
52
|
+
loc: HermesSourceLocation,
|
|
53
|
+
body: Array<?HermesNode>,
|
|
54
|
+
comments: Array<HermesComment>,
|
|
55
|
+
tokens?: Array<HermesToken>,
|
|
56
|
+
interpreter?: ?HermesComment,
|
|
57
|
+
};
|
|
@@ -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-type] 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[incompatible-type]
|
|
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,189 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {Program} from 'hermes-estree';
|
|
12
|
+
import type {HermesNode} from './HermesAST';
|
|
13
|
+
import type {ParserOptions} from './ParserOptions';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
HERMES_AST_VISITOR_KEYS,
|
|
17
|
+
NODE_CHILD,
|
|
18
|
+
NODE_LIST_CHILD,
|
|
19
|
+
} from './generated/ParserVisitorKeys';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The base class for transforming the Hermes AST to the desired output format.
|
|
23
|
+
* Extended by concrete adapters which output an ESTree or Babel AST.
|
|
24
|
+
*/
|
|
25
|
+
export default class HermesASTAdapter {
|
|
26
|
+
sourceFilename: ParserOptions['sourceFilename'];
|
|
27
|
+
sourceType: ParserOptions['sourceType'];
|
|
28
|
+
|
|
29
|
+
constructor(options: ParserOptions) {
|
|
30
|
+
this.sourceFilename = options.sourceFilename;
|
|
31
|
+
this.sourceType = options.sourceType;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Transform the input Hermes AST to the desired output format.
|
|
36
|
+
* This modifies the input AST in place instead of constructing a new AST.
|
|
37
|
+
*/
|
|
38
|
+
transform(program: HermesNode): Program {
|
|
39
|
+
// Comments are not traversed via visitor keys
|
|
40
|
+
const comments = program.comments;
|
|
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
|
+
}
|
|
46
|
+
|
|
47
|
+
// The first comment may be an interpreter directive and is stored directly on the program node
|
|
48
|
+
program.interpreter =
|
|
49
|
+
comments.length > 0 && comments[0].type === 'InterpreterDirective'
|
|
50
|
+
? comments.shift()
|
|
51
|
+
: null;
|
|
52
|
+
|
|
53
|
+
// Tokens are not traversed via visitor keys
|
|
54
|
+
const tokens = program.tokens;
|
|
55
|
+
if (tokens) {
|
|
56
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
57
|
+
this.fixSourceLocation(tokens[i]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const resultNode = this.mapNode(program);
|
|
62
|
+
if (resultNode.type !== 'Program') {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`HermesToESTreeAdapter: Must return a Program node, instead of "${resultNode.type}". `,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// $FlowExpectedError[incompatible-type] We know this is a program at this point.
|
|
69
|
+
return resultNode;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Transform a Hermes AST node to the output AST format.
|
|
74
|
+
*
|
|
75
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
76
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
77
|
+
*/
|
|
78
|
+
mapNode(_node: HermesNode): HermesNode {
|
|
79
|
+
throw new Error('Implemented in subclasses');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
mapNodeDefault(node: HermesNode): HermesNode {
|
|
83
|
+
const visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
|
|
84
|
+
for (const key in visitorKeys) {
|
|
85
|
+
const childType = visitorKeys[key];
|
|
86
|
+
if (childType === NODE_CHILD) {
|
|
87
|
+
const child = node[key];
|
|
88
|
+
if (child != null) {
|
|
89
|
+
node[key] = this.mapNode(child);
|
|
90
|
+
}
|
|
91
|
+
} else if (childType === NODE_LIST_CHILD) {
|
|
92
|
+
const children = node[key];
|
|
93
|
+
for (let i = 0; i < children.length; i++) {
|
|
94
|
+
const child = children[i];
|
|
95
|
+
if (child != null) {
|
|
96
|
+
children[i] = this.mapNode(child);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return node;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Update the source location for this node depending on the output AST format.
|
|
107
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
108
|
+
*/
|
|
109
|
+
fixSourceLocation(_node: HermesNode): void {
|
|
110
|
+
throw new Error('Implemented in subclasses');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getSourceType(): ParserOptions['sourceType'] {
|
|
114
|
+
return this.sourceType ?? 'script';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setModuleSourceType(): void {
|
|
118
|
+
if (this.sourceType == null) {
|
|
119
|
+
this.sourceType = 'module';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mapComment(node: HermesNode): HermesNode {
|
|
124
|
+
return node;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
mapEmpty(_node: HermesNode): HermesNode {
|
|
128
|
+
// $FlowExpectedError[incompatible-type]
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
mapImportDeclaration(node: HermesNode): HermesNode {
|
|
133
|
+
if (node.importKind === 'value') {
|
|
134
|
+
this.setModuleSourceType();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return this.mapNodeDefault(node);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
mapImportSpecifier(node: HermesNode): HermesNode {
|
|
141
|
+
if (node.importKind === 'value') {
|
|
142
|
+
node.importKind = null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return this.mapNodeDefault(node);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
mapExportDefaultDeclaration(node: HermesNode): HermesNode {
|
|
149
|
+
this.setModuleSourceType();
|
|
150
|
+
return this.mapNodeDefault(node);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
mapExportNamedDeclaration(node: HermesNode): HermesNode {
|
|
154
|
+
if (node.exportKind === 'value') {
|
|
155
|
+
this.setModuleSourceType();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this.mapNodeDefault(node);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
mapExportAllDeclaration(node: HermesNode): HermesNode {
|
|
162
|
+
if (node.exportKind === 'value') {
|
|
163
|
+
this.setModuleSourceType();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return this.mapNodeDefault(node);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
formatError(node: HermesNode, message: string): string {
|
|
170
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getBigIntLiteralValue(bigintString: string): {
|
|
174
|
+
bigint: string,
|
|
175
|
+
value: bigint | null,
|
|
176
|
+
} {
|
|
177
|
+
const bigint = bigintString
|
|
178
|
+
// estree spec is to not have a trailing `n` on this property
|
|
179
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
180
|
+
.replace(/n$/, '')
|
|
181
|
+
// `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
|
+
}
|
|
@@ -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,161 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
import type {HermesNode} from './HermesAST';
|
|
14
|
+
import type {ParserOptions} from './ParserOptions';
|
|
15
|
+
|
|
16
|
+
import HermesParserDeserializer from './HermesParserDeserializer';
|
|
17
|
+
import HermesParserWASMModule from './HermesParserWASM';
|
|
18
|
+
|
|
19
|
+
let HermesParserWASM;
|
|
20
|
+
let hermesParse;
|
|
21
|
+
let hermesParseResult_free;
|
|
22
|
+
let hermesParseResult_getError;
|
|
23
|
+
let hermesParseResult_getErrorLine;
|
|
24
|
+
let hermesParseResult_getErrorColumn;
|
|
25
|
+
let hermesParseResult_getProgramBuffer;
|
|
26
|
+
let hermesParseResult_getPositionBuffer;
|
|
27
|
+
let hermesParseResult_getPositionBufferSize;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Init the WASM wrapper code generated by `emscripten` to preparse the
|
|
31
|
+
* HermesParser WASM code.
|
|
32
|
+
*/
|
|
33
|
+
function initHermesParserWASM() {
|
|
34
|
+
if (HermesParserWASM != null) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
HermesParserWASM = HermesParserWASMModule({
|
|
39
|
+
/**
|
|
40
|
+
* The emscripten version of `quit` unconditionally assigns the `status` to
|
|
41
|
+
* `process.exitCode` which overrides any pre-existing code that has been
|
|
42
|
+
* set, even if it is non zero. For our use case we never want an
|
|
43
|
+
* `exitCode` to be set so this override removes that functionality.
|
|
44
|
+
*/
|
|
45
|
+
quit(_status: number, toThrow: Error) {
|
|
46
|
+
throw toThrow;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', [
|
|
51
|
+
'number',
|
|
52
|
+
'number',
|
|
53
|
+
'number',
|
|
54
|
+
'number',
|
|
55
|
+
'number',
|
|
56
|
+
'number',
|
|
57
|
+
'number',
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
hermesParseResult_free = HermesParserWASM.cwrap(
|
|
61
|
+
'hermesParseResult_free',
|
|
62
|
+
'void',
|
|
63
|
+
['number'],
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
hermesParseResult_getError = HermesParserWASM.cwrap(
|
|
67
|
+
'hermesParseResult_getError',
|
|
68
|
+
'string',
|
|
69
|
+
['number'],
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
hermesParseResult_getErrorLine = HermesParserWASM.cwrap(
|
|
73
|
+
'hermesParseResult_getErrorLine',
|
|
74
|
+
'number',
|
|
75
|
+
['number'],
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
hermesParseResult_getErrorColumn = HermesParserWASM.cwrap(
|
|
79
|
+
'hermesParseResult_getErrorColumn',
|
|
80
|
+
'number',
|
|
81
|
+
['number'],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap(
|
|
85
|
+
'hermesParseResult_getProgramBuffer',
|
|
86
|
+
'number',
|
|
87
|
+
['number'],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap(
|
|
91
|
+
'hermesParseResult_getPositionBuffer',
|
|
92
|
+
'number',
|
|
93
|
+
['number'],
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap(
|
|
97
|
+
'hermesParseResult_getPositionBufferSize',
|
|
98
|
+
'number',
|
|
99
|
+
['number'],
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Copy a string into the WASM heap and null-terminate
|
|
104
|
+
function copyToHeap(buffer: Buffer, addr: number) {
|
|
105
|
+
HermesParserWASM.HEAP8.set(buffer, addr);
|
|
106
|
+
HermesParserWASM.HEAP8[addr + buffer.length] = 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function parse(source: string, options: ParserOptions): HermesNode {
|
|
110
|
+
initHermesParserWASM();
|
|
111
|
+
|
|
112
|
+
// Allocate space on heap for source text
|
|
113
|
+
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
114
|
+
const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
|
|
115
|
+
if (!sourceAddr) {
|
|
116
|
+
throw new Error('Parser out of memory');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
// Copy source text onto WASM heap
|
|
121
|
+
copyToHeap(sourceBuffer, sourceAddr);
|
|
122
|
+
|
|
123
|
+
const parseResult = hermesParse(
|
|
124
|
+
sourceAddr,
|
|
125
|
+
sourceBuffer.length + 1,
|
|
126
|
+
options.flow === 'detect',
|
|
127
|
+
options.enableExperimentalComponentSyntax,
|
|
128
|
+
options.enableExperimentalFlowMatchSyntax,
|
|
129
|
+
options.tokens,
|
|
130
|
+
options.allowReturnOutsideFunction,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
// Extract and throw error from parse result if parsing failed
|
|
135
|
+
const err = hermesParseResult_getError(parseResult);
|
|
136
|
+
if (err) {
|
|
137
|
+
const syntaxError = new SyntaxError(err);
|
|
138
|
+
// $FlowExpectedError[prop-missing]
|
|
139
|
+
syntaxError.loc = {
|
|
140
|
+
line: hermesParseResult_getErrorLine(parseResult),
|
|
141
|
+
column: hermesParseResult_getErrorColumn(parseResult),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
throw syntaxError;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const deserializer = new HermesParserDeserializer(
|
|
148
|
+
hermesParseResult_getProgramBuffer(parseResult),
|
|
149
|
+
hermesParseResult_getPositionBuffer(parseResult),
|
|
150
|
+
hermesParseResult_getPositionBufferSize(parseResult),
|
|
151
|
+
HermesParserWASM,
|
|
152
|
+
options,
|
|
153
|
+
);
|
|
154
|
+
return deserializer.deserialize();
|
|
155
|
+
} finally {
|
|
156
|
+
hermesParseResult_free(parseResult);
|
|
157
|
+
}
|
|
158
|
+
} finally {
|
|
159
|
+
HermesParserWASM._free(sourceAddr);
|
|
160
|
+
}
|
|
161
|
+
}
|