hermes-estree 0.9.0 → 0.10.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 +186 -0
- package/dist/HermesASTAdapter.js.flow +183 -0
- package/dist/HermesParser.js +83 -0
- package/dist/HermesParser.js.flow +123 -0
- package/dist/HermesParserDecodeUTF8String.js +68 -0
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +243 -0
- package/dist/HermesParserDeserializer.js.flow +261 -0
- package/dist/HermesParserNodeDeserializers.js +2008 -0
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +6 -0
- package/dist/HermesParserWASM.js.flow +75 -0
- package/dist/HermesToBabelAdapter.js +514 -0
- package/dist/HermesToBabelAdapter.js.flow +490 -0
- package/dist/HermesToESTreeAdapter.js +439 -0
- package/dist/HermesToESTreeAdapter.js.flow +422 -0
- package/dist/ParserOptions.js.flow +18 -0
- package/dist/generated/ESTreeVisitorKeys.js +180 -0
- package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
- package/dist/generated/HermesESTreeSelectorTypes.js.flow +20 -1
- package/dist/generated/ParserVisitorKeys.js +622 -0
- package/dist/generated/ParserVisitorKeys.js.flow +17 -0
- package/dist/generated/predicates.js +24 -12
- package/dist/generated/predicates.js.flow +34 -23
- package/dist/getModuleDocblock.js +112 -0
- package/dist/getModuleDocblock.js.flow +118 -0
- package/dist/predicates.js +13 -1
- package/dist/predicates.js.flow +125 -2
- package/dist/transform/SimpleTransform.js +92 -0
- package/dist/transform/SimpleTransform.js.flow +104 -0
- package/dist/transform/astArrayMutationHelpers.js +62 -0
- package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
- package/dist/transform/astNodeMutationHelpers.js +186 -0
- package/dist/transform/astNodeMutationHelpers.js.flow +205 -0
- package/dist/traverse/SimpleTraverser.js +138 -0
- package/dist/traverse/SimpleTraverser.js.flow +132 -0
- package/dist/traverse/getVisitorKeys.js +35 -0
- package/dist/traverse/getVisitorKeys.js.flow +36 -0
- package/dist/types.js.flow +128 -30
- 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 interface 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,186 @@
|
|
|
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
|
+
return this.mapNode(program);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Transform a Hermes AST node to the output AST format.
|
|
62
|
+
*
|
|
63
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
64
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
mapNode(_node) {
|
|
69
|
+
throw new Error('Implemented in subclasses');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
mapNodeDefault(node) {
|
|
73
|
+
const visitorKeys = _ParserVisitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
|
|
74
|
+
|
|
75
|
+
for (const key in visitorKeys) {
|
|
76
|
+
const childType = visitorKeys[key];
|
|
77
|
+
|
|
78
|
+
if (childType === _ParserVisitorKeys.NODE_CHILD) {
|
|
79
|
+
const child = node[key];
|
|
80
|
+
|
|
81
|
+
if (child != null) {
|
|
82
|
+
node[key] = this.mapNode(child);
|
|
83
|
+
}
|
|
84
|
+
} else if (childType === _ParserVisitorKeys.NODE_LIST_CHILD) {
|
|
85
|
+
const children = node[key];
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < children.length; i++) {
|
|
88
|
+
const child = children[i];
|
|
89
|
+
|
|
90
|
+
if (child != null) {
|
|
91
|
+
children[i] = this.mapNode(child);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return node;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Update the source location for this node depending on the output AST format.
|
|
101
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
fixSourceLocation(_node) {
|
|
106
|
+
throw new Error('Implemented in subclasses');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getSourceType() {
|
|
110
|
+
var _this$sourceType;
|
|
111
|
+
|
|
112
|
+
return (_this$sourceType = this.sourceType) != null ? _this$sourceType : 'script';
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
setModuleSourceType() {
|
|
116
|
+
if (this.sourceType == null) {
|
|
117
|
+
this.sourceType = 'module';
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
mapComment(node) {
|
|
122
|
+
return node;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
mapEmpty(_node) {
|
|
126
|
+
// $FlowExpectedError
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
mapImportDeclaration(node) {
|
|
131
|
+
if (node.importKind === 'value') {
|
|
132
|
+
this.setModuleSourceType();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return this.mapNodeDefault(node);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
mapImportSpecifier(node) {
|
|
139
|
+
if (node.importKind === 'value') {
|
|
140
|
+
node.importKind = null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return this.mapNodeDefault(node);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
mapExportDefaultDeclaration(node) {
|
|
147
|
+
this.setModuleSourceType();
|
|
148
|
+
return this.mapNodeDefault(node);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
mapExportNamedDeclaration(node) {
|
|
152
|
+
if (node.exportKind === 'value') {
|
|
153
|
+
this.setModuleSourceType();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return this.mapNodeDefault(node);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
mapExportAllDeclaration(node) {
|
|
160
|
+
if (node.exportKind === 'value') {
|
|
161
|
+
this.setModuleSourceType();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return this.mapNodeDefault(node);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
formatError(node, message) {
|
|
168
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
getBigIntLiteralValue(bigintString) {
|
|
172
|
+
// TODO - once we update flow we can remove this
|
|
173
|
+
const bigint = bigintString // estree spec is to not have a trailing `n` on this property
|
|
174
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
175
|
+
.replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
|
|
176
|
+
.replace(/_/, '');
|
|
177
|
+
return {
|
|
178
|
+
bigint,
|
|
179
|
+
// coerce the string to a bigint value if supported by the environment
|
|
180
|
+
value: typeof BigInt === 'function' ? BigInt(bigint) : null
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
exports.default = HermesASTAdapter;
|
|
@@ -0,0 +1,183 @@
|
|
|
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 {HermesNode} from './HermesAST';
|
|
12
|
+
import type {ParserOptions} from './ParserOptions';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
HERMES_AST_VISITOR_KEYS,
|
|
16
|
+
NODE_CHILD,
|
|
17
|
+
NODE_LIST_CHILD,
|
|
18
|
+
} from './generated/ParserVisitorKeys';
|
|
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
|
+
export default class HermesASTAdapter {
|
|
25
|
+
sourceFilename: ParserOptions['sourceFilename'];
|
|
26
|
+
sourceType: ParserOptions['sourceType'];
|
|
27
|
+
|
|
28
|
+
constructor(options: ParserOptions) {
|
|
29
|
+
this.sourceFilename = options.sourceFilename;
|
|
30
|
+
this.sourceType = options.sourceType;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Transform the input Hermes AST to the desired output format.
|
|
35
|
+
* This modifies the input AST in place instead of constructing a new AST.
|
|
36
|
+
*/
|
|
37
|
+
transform(program: HermesNode): ?HermesNode {
|
|
38
|
+
// Comments are not traversed via visitor keys
|
|
39
|
+
const comments = program.comments;
|
|
40
|
+
for (let i = 0; i < comments.length; i++) {
|
|
41
|
+
const comment = comments[i];
|
|
42
|
+
this.fixSourceLocation(comment);
|
|
43
|
+
comments[i] = this.mapComment(comment);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The first comment may be an interpreter directive and is stored directly on the program node
|
|
47
|
+
program.interpreter =
|
|
48
|
+
comments.length > 0 && comments[0].type === 'InterpreterDirective'
|
|
49
|
+
? comments.shift()
|
|
50
|
+
: null;
|
|
51
|
+
|
|
52
|
+
// Tokens are not traversed via visitor keys
|
|
53
|
+
const tokens = program.tokens;
|
|
54
|
+
if (tokens) {
|
|
55
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
56
|
+
this.fixSourceLocation(tokens[i]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return this.mapNode(program);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Transform a Hermes AST node to the output AST format.
|
|
65
|
+
*
|
|
66
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
67
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
68
|
+
*/
|
|
69
|
+
mapNode(_node: HermesNode): HermesNode {
|
|
70
|
+
throw new Error('Implemented in subclasses');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
mapNodeDefault(node: HermesNode): HermesNode {
|
|
74
|
+
const visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
|
|
75
|
+
for (const key in visitorKeys) {
|
|
76
|
+
const childType = visitorKeys[key];
|
|
77
|
+
if (childType === NODE_CHILD) {
|
|
78
|
+
const child = node[key];
|
|
79
|
+
if (child != null) {
|
|
80
|
+
node[key] = this.mapNode(child);
|
|
81
|
+
}
|
|
82
|
+
} else if (childType === NODE_LIST_CHILD) {
|
|
83
|
+
const children = node[key];
|
|
84
|
+
for (let i = 0; i < children.length; i++) {
|
|
85
|
+
const child = children[i];
|
|
86
|
+
if (child != null) {
|
|
87
|
+
children[i] = this.mapNode(child);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Update the source location for this node depending on the output AST format.
|
|
98
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
99
|
+
*/
|
|
100
|
+
fixSourceLocation(_node: HermesNode): void {
|
|
101
|
+
throw new Error('Implemented in subclasses');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getSourceType(): ParserOptions['sourceType'] {
|
|
105
|
+
return this.sourceType ?? 'script';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setModuleSourceType(): void {
|
|
109
|
+
if (this.sourceType == null) {
|
|
110
|
+
this.sourceType = 'module';
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
mapComment(node: HermesNode): HermesNode {
|
|
115
|
+
return node;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
mapEmpty(_node: HermesNode): HermesNode {
|
|
119
|
+
// $FlowExpectedError
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mapImportDeclaration(node: HermesNode): HermesNode {
|
|
124
|
+
if (node.importKind === 'value') {
|
|
125
|
+
this.setModuleSourceType();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return this.mapNodeDefault(node);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
mapImportSpecifier(node: HermesNode): HermesNode {
|
|
132
|
+
if (node.importKind === 'value') {
|
|
133
|
+
node.importKind = null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return this.mapNodeDefault(node);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
mapExportDefaultDeclaration(node: HermesNode): HermesNode {
|
|
140
|
+
this.setModuleSourceType();
|
|
141
|
+
return this.mapNodeDefault(node);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
mapExportNamedDeclaration(node: HermesNode): HermesNode {
|
|
145
|
+
if (node.exportKind === 'value') {
|
|
146
|
+
this.setModuleSourceType();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return this.mapNodeDefault(node);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
mapExportAllDeclaration(node: HermesNode): HermesNode {
|
|
153
|
+
if (node.exportKind === 'value') {
|
|
154
|
+
this.setModuleSourceType();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return this.mapNodeDefault(node);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
formatError(node: HermesNode, message: string): string {
|
|
161
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
getBigIntLiteralValue(bigintString: string): {
|
|
165
|
+
bigint: string,
|
|
166
|
+
value: $FlowFixMe /* bigint */,
|
|
167
|
+
} {
|
|
168
|
+
// TODO - once we update flow we can remove this
|
|
169
|
+
declare var BigInt: ?(value: $FlowFixMe) => mixed;
|
|
170
|
+
|
|
171
|
+
const bigint = bigintString
|
|
172
|
+
// estree spec is to not have a trailing `n` on this property
|
|
173
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
174
|
+
.replace(/n$/, '')
|
|
175
|
+
// `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
|
|
176
|
+
.replace(/_/, '');
|
|
177
|
+
return {
|
|
178
|
+
bigint,
|
|
179
|
+
// coerce the string to a bigint value if supported by the environment
|
|
180
|
+
value: typeof BigInt === 'function' ? BigInt(bigint) : null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
const hermesParse = _HermesParserWASM.default.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
|
|
24
|
+
|
|
25
|
+
const hermesParseResult_free = _HermesParserWASM.default.cwrap('hermesParseResult_free', 'void', ['number']);
|
|
26
|
+
|
|
27
|
+
const hermesParseResult_getError = _HermesParserWASM.default.cwrap('hermesParseResult_getError', 'string', ['number']);
|
|
28
|
+
|
|
29
|
+
const hermesParseResult_getErrorLine = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
|
|
30
|
+
|
|
31
|
+
const hermesParseResult_getErrorColumn = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
|
|
32
|
+
|
|
33
|
+
const hermesParseResult_getProgramBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
|
|
34
|
+
|
|
35
|
+
const hermesParseResult_getPositionBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
|
|
36
|
+
|
|
37
|
+
const hermesParseResult_getPositionBufferSize = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function copyToHeap(buffer, addr) {
|
|
41
|
+
_HermesParserWASM.default.HEAP8.set(buffer, addr);
|
|
42
|
+
|
|
43
|
+
_HermesParserWASM.default.HEAP8[addr + buffer.length] = 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parse(source, options) {
|
|
47
|
+
// Allocate space on heap for source text
|
|
48
|
+
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
49
|
+
|
|
50
|
+
const sourceAddr = _HermesParserWASM.default._malloc(sourceBuffer.length + 1);
|
|
51
|
+
|
|
52
|
+
if (!sourceAddr) {
|
|
53
|
+
throw new Error('Parser out of memory');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
// Copy source text onto WASM heap
|
|
58
|
+
copyToHeap(sourceBuffer, sourceAddr);
|
|
59
|
+
const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.tokens, options.allowReturnOutsideFunction);
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
// Extract and throw error from parse result if parsing failed
|
|
63
|
+
const err = hermesParseResult_getError(parseResult);
|
|
64
|
+
|
|
65
|
+
if (err) {
|
|
66
|
+
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
67
|
+
|
|
68
|
+
syntaxError.loc = {
|
|
69
|
+
line: hermesParseResult_getErrorLine(parseResult),
|
|
70
|
+
column: hermesParseResult_getErrorColumn(parseResult)
|
|
71
|
+
};
|
|
72
|
+
throw syntaxError;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), _HermesParserWASM.default, options);
|
|
76
|
+
return deserializer.deserialize();
|
|
77
|
+
} finally {
|
|
78
|
+
hermesParseResult_free(parseResult);
|
|
79
|
+
}
|
|
80
|
+
} finally {
|
|
81
|
+
_HermesParserWASM.default._free(sourceAddr);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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 HermesParserWASM from './HermesParserWASM';
|
|
18
|
+
|
|
19
|
+
const hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', [
|
|
20
|
+
'number',
|
|
21
|
+
'number',
|
|
22
|
+
'number',
|
|
23
|
+
'number',
|
|
24
|
+
'number',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
const hermesParseResult_free = HermesParserWASM.cwrap(
|
|
28
|
+
'hermesParseResult_free',
|
|
29
|
+
'void',
|
|
30
|
+
['number'],
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const hermesParseResult_getError = HermesParserWASM.cwrap(
|
|
34
|
+
'hermesParseResult_getError',
|
|
35
|
+
'string',
|
|
36
|
+
['number'],
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const hermesParseResult_getErrorLine = HermesParserWASM.cwrap(
|
|
40
|
+
'hermesParseResult_getErrorLine',
|
|
41
|
+
'number',
|
|
42
|
+
['number'],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const hermesParseResult_getErrorColumn = HermesParserWASM.cwrap(
|
|
46
|
+
'hermesParseResult_getErrorColumn',
|
|
47
|
+
'number',
|
|
48
|
+
['number'],
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap(
|
|
52
|
+
'hermesParseResult_getProgramBuffer',
|
|
53
|
+
'number',
|
|
54
|
+
['number'],
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap(
|
|
58
|
+
'hermesParseResult_getPositionBuffer',
|
|
59
|
+
'number',
|
|
60
|
+
['number'],
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap(
|
|
64
|
+
'hermesParseResult_getPositionBufferSize',
|
|
65
|
+
'number',
|
|
66
|
+
['number'],
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Copy a string into the WASM heap and null-terminate
|
|
70
|
+
function copyToHeap(buffer: Buffer, addr: number) {
|
|
71
|
+
HermesParserWASM.HEAP8.set(buffer, addr);
|
|
72
|
+
HermesParserWASM.HEAP8[addr + buffer.length] = 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function parse(source: string, options: ParserOptions): HermesNode {
|
|
76
|
+
// Allocate space on heap for source text
|
|
77
|
+
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
78
|
+
const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
|
|
79
|
+
if (!sourceAddr) {
|
|
80
|
+
throw new Error('Parser out of memory');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
// Copy source text onto WASM heap
|
|
85
|
+
copyToHeap(sourceBuffer, sourceAddr);
|
|
86
|
+
|
|
87
|
+
const parseResult = hermesParse(
|
|
88
|
+
sourceAddr,
|
|
89
|
+
sourceBuffer.length + 1,
|
|
90
|
+
options.flow === 'detect',
|
|
91
|
+
options.tokens,
|
|
92
|
+
options.allowReturnOutsideFunction,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
try {
|
|
96
|
+
// Extract and throw error from parse result if parsing failed
|
|
97
|
+
const err = hermesParseResult_getError(parseResult);
|
|
98
|
+
if (err) {
|
|
99
|
+
const syntaxError = new SyntaxError(err);
|
|
100
|
+
// $FlowExpectedError[prop-missing]
|
|
101
|
+
syntaxError.loc = {
|
|
102
|
+
line: hermesParseResult_getErrorLine(parseResult),
|
|
103
|
+
column: hermesParseResult_getErrorColumn(parseResult),
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
throw syntaxError;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const deserializer = new HermesParserDeserializer(
|
|
110
|
+
hermesParseResult_getProgramBuffer(parseResult),
|
|
111
|
+
hermesParseResult_getPositionBuffer(parseResult),
|
|
112
|
+
hermesParseResult_getPositionBufferSize(parseResult),
|
|
113
|
+
HermesParserWASM,
|
|
114
|
+
options,
|
|
115
|
+
);
|
|
116
|
+
return deserializer.deserialize();
|
|
117
|
+
} finally {
|
|
118
|
+
hermesParseResult_free(parseResult);
|
|
119
|
+
}
|
|
120
|
+
} finally {
|
|
121
|
+
HermesParserWASM._free(sourceAddr);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -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
|
+
}
|