hermes-parser 0.5.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/HermesAST.js.flow +2 -1
- package/dist/HermesASTAdapter.js +129 -144
- package/dist/HermesASTAdapter.js.flow +1 -7
- package/dist/HermesParser.js +19 -19
- package/dist/HermesParser.js.flow +1 -1
- package/dist/HermesParserDecodeUTF8String.js +10 -10
- package/dist/HermesParserDecodeUTF8String.js.flow +3 -3
- package/dist/HermesParserDeserializer.js +187 -221
- package/dist/HermesParserDeserializer.js.flow +2 -1
- package/dist/HermesParserNodeDeserializers.js +58 -27
- package/dist/HermesParserNodeDeserializers.js.flow +1 -1
- package/dist/HermesParserWASM.js +2 -2
- package/dist/HermesParserWASM.js.flow +1 -1
- package/dist/HermesToBabelAdapter.js +415 -370
- package/dist/HermesToBabelAdapter.js.flow +109 -5
- package/dist/HermesToESTreeAdapter.js +393 -156
- package/dist/HermesToESTreeAdapter.js.flow +282 -15
- package/dist/ParserOptions.js.flow +1 -1
- package/dist/generated/visitor-keys.js +56 -41
- package/dist/generated/visitor-keys.js.flow +1 -1
- package/dist/getModuleDocblock.js +111 -0
- package/dist/getModuleDocblock.js.flow +116 -0
- package/dist/index.js +12 -21
- package/dist/index.js.flow +2 -2
- package/package.json +7 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -12,48 +12,28 @@
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", {
|
|
13
13
|
value: true
|
|
14
14
|
});
|
|
15
|
-
exports
|
|
15
|
+
exports.default = void 0;
|
|
16
16
|
|
|
17
17
|
var _HermesParserDecodeUTF8String = _interopRequireDefault(require("./HermesParserDecodeUTF8String"));
|
|
18
18
|
|
|
19
19
|
var _HermesParserNodeDeserializers = _interopRequireDefault(require("./HermesParserNodeDeserializers"));
|
|
20
20
|
|
|
21
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
26
|
-
|
|
27
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
28
|
-
|
|
29
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
30
|
-
|
|
31
|
-
var HermesParserDeserializer = /*#__PURE__*/function () {
|
|
23
|
+
class HermesParserDeserializer {
|
|
32
24
|
// Matches StoredComment::Kind enum in JSLexer.h
|
|
33
25
|
// Matches TokenType enum in HermesParserJSSerializer.h
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
_defineProperty(this, "HEAPU8", void 0);
|
|
46
|
-
|
|
47
|
-
_defineProperty(this, "HEAPU32", void 0);
|
|
48
|
-
|
|
49
|
-
_defineProperty(this, "HEAPF64", void 0);
|
|
50
|
-
|
|
51
|
-
_defineProperty(this, "options", void 0);
|
|
52
|
-
|
|
53
|
-
_defineProperty(this, "commentTypes", ['CommentLine', 'CommentBlock', 'InterpreterDirective']);
|
|
54
|
-
|
|
55
|
-
_defineProperty(this, "tokenTypes", ['Boolean', 'Identifier', 'Keyword', 'Null', 'Numeric', 'Punctuator', 'String', 'RegularExpression', 'Template', 'JSXText']);
|
|
56
|
-
|
|
26
|
+
constructor(programBuffer, positionBuffer, positionBufferSize, wasmParser, options) {
|
|
27
|
+
this.programBufferIdx = void 0;
|
|
28
|
+
this.positionBufferIdx = void 0;
|
|
29
|
+
this.positionBufferSize = void 0;
|
|
30
|
+
this.locMap = void 0;
|
|
31
|
+
this.HEAPU8 = void 0;
|
|
32
|
+
this.HEAPU32 = void 0;
|
|
33
|
+
this.HEAPF64 = void 0;
|
|
34
|
+
this.options = void 0;
|
|
35
|
+
this.commentTypes = ['CommentLine', 'CommentBlock', 'InterpreterDirective'];
|
|
36
|
+
this.tokenTypes = ['Boolean', 'Identifier', 'Keyword', 'Null', 'Numeric', 'BigInt', 'Punctuator', 'String', 'RegularExpression', 'Template', 'JSXText'];
|
|
57
37
|
// Program and position buffer are memory addresses, so we must convert
|
|
58
38
|
// into indices into HEAPU32 (an array of 4-byte integers).
|
|
59
39
|
this.programBufferIdx = programBuffer / 4;
|
|
@@ -70,208 +50,194 @@ var HermesParserDeserializer = /*#__PURE__*/function () {
|
|
|
70
50
|
*/
|
|
71
51
|
|
|
72
52
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return num;
|
|
78
|
-
}
|
|
79
|
-
}, {
|
|
80
|
-
key: "deserialize",
|
|
81
|
-
value: function deserialize() {
|
|
82
|
-
var program = {
|
|
83
|
-
type: 'Program',
|
|
84
|
-
loc: this.addEmptyLoc(),
|
|
85
|
-
body: this.deserializeNodeList(),
|
|
86
|
-
comments: this.deserializeComments()
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
if (this.options.tokens === true) {
|
|
90
|
-
program.tokens = this.deserializeTokens();
|
|
91
|
-
}
|
|
53
|
+
next() {
|
|
54
|
+
const num = this.HEAPU32[this.programBufferIdx++];
|
|
55
|
+
return num;
|
|
56
|
+
}
|
|
92
57
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}, {
|
|
101
|
-
key: "deserializeBoolean",
|
|
102
|
-
value: function deserializeBoolean() {
|
|
103
|
-
return Boolean(this.next());
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Numbers are serialized directly into program buffer, taking up 8 bytes
|
|
107
|
-
* preceded by 4 bytes of alignment padding if necessary.
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
}, {
|
|
111
|
-
key: "deserializeNumber",
|
|
112
|
-
value: function deserializeNumber() {
|
|
113
|
-
var floatIdx; // Numbers are aligned on 8-byte boundaries, so skip padding if we are at
|
|
114
|
-
// an odd index into the 4-byte aligned program buffer.
|
|
115
|
-
|
|
116
|
-
if (this.programBufferIdx % 2 === 0) {
|
|
117
|
-
floatIdx = this.programBufferIdx / 2;
|
|
118
|
-
this.programBufferIdx += 2;
|
|
119
|
-
} else {
|
|
120
|
-
floatIdx = (this.programBufferIdx + 1) / 2;
|
|
121
|
-
this.programBufferIdx += 3;
|
|
122
|
-
}
|
|
58
|
+
deserialize() {
|
|
59
|
+
const program = {
|
|
60
|
+
type: 'Program',
|
|
61
|
+
loc: this.addEmptyLoc(),
|
|
62
|
+
body: this.deserializeNodeList(),
|
|
63
|
+
comments: this.deserializeComments()
|
|
64
|
+
};
|
|
123
65
|
|
|
124
|
-
|
|
66
|
+
if (this.options.tokens === true) {
|
|
67
|
+
program.tokens = this.deserializeTokens();
|
|
125
68
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Strings are serialized as a 4-byte pointer into the heap, followed
|
|
128
|
-
* by their size as a 4-byte integer. The size is only present if the
|
|
129
|
-
* pointer is non-null.
|
|
130
|
-
*/
|
|
131
|
-
|
|
132
|
-
}, {
|
|
133
|
-
key: "deserializeString",
|
|
134
|
-
value: function deserializeString() {
|
|
135
|
-
var ptr = this.next();
|
|
136
|
-
|
|
137
|
-
if (ptr === 0) {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
69
|
|
|
141
|
-
|
|
142
|
-
|
|
70
|
+
this.fillLocs();
|
|
71
|
+
return program;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Booleans are serialized as a single 4-byte integer.
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
deserializeBoolean() {
|
|
79
|
+
return Boolean(this.next());
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Numbers are serialized directly into program buffer, taking up 8 bytes
|
|
83
|
+
* preceded by 4 bytes of alignment padding if necessary.
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
deserializeNumber() {
|
|
88
|
+
let floatIdx; // Numbers are aligned on 8-byte boundaries, so skip padding if we are at
|
|
89
|
+
// an odd index into the 4-byte aligned program buffer.
|
|
90
|
+
|
|
91
|
+
if (this.programBufferIdx % 2 === 0) {
|
|
92
|
+
floatIdx = this.programBufferIdx / 2;
|
|
93
|
+
this.programBufferIdx += 2;
|
|
94
|
+
} else {
|
|
95
|
+
floatIdx = (this.programBufferIdx + 1) / 2;
|
|
96
|
+
this.programBufferIdx += 3;
|
|
143
97
|
}
|
|
144
|
-
/**
|
|
145
|
-
* Nodes are serialized as a 4-byte integer denoting their node kind,
|
|
146
|
-
* followed by a 4-byte loc ID, followed by serialized node properties.
|
|
147
|
-
*
|
|
148
|
-
* If the node kind is 0 the node is null, otherwise the node kind - 1 is an
|
|
149
|
-
* index into the array of node deserialization functions.
|
|
150
|
-
*/
|
|
151
|
-
|
|
152
|
-
}, {
|
|
153
|
-
key: "deserializeNode",
|
|
154
|
-
value: function deserializeNode() {
|
|
155
|
-
var nodeType = this.next();
|
|
156
|
-
|
|
157
|
-
if (nodeType === 0) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
98
|
|
|
161
|
-
|
|
99
|
+
return this.HEAPF64[floatIdx];
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Strings are serialized as a 4-byte pointer into the heap, followed
|
|
103
|
+
* by their size as a 4-byte integer. The size is only present if the
|
|
104
|
+
* pointer is non-null.
|
|
105
|
+
*/
|
|
106
|
+
|
|
162
107
|
|
|
163
|
-
|
|
108
|
+
deserializeString() {
|
|
109
|
+
const ptr = this.next();
|
|
110
|
+
|
|
111
|
+
if (ptr === 0) {
|
|
112
|
+
return null;
|
|
164
113
|
}
|
|
165
|
-
/**
|
|
166
|
-
* Node lists are serialized as a 4-byte integer denoting the number of
|
|
167
|
-
* elements in the list, followed by the serialized elements.
|
|
168
|
-
*/
|
|
169
|
-
|
|
170
|
-
}, {
|
|
171
|
-
key: "deserializeNodeList",
|
|
172
|
-
value: function deserializeNodeList() {
|
|
173
|
-
var size = this.next();
|
|
174
|
-
var nodeList = [];
|
|
175
|
-
|
|
176
|
-
for (var i = 0; i < size; i++) {
|
|
177
|
-
nodeList.push(this.deserializeNode());
|
|
178
|
-
}
|
|
179
114
|
|
|
180
|
-
|
|
115
|
+
const size = this.next();
|
|
116
|
+
return (0, _HermesParserDecodeUTF8String.default)(ptr, size, this.HEAPU8);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Nodes are serialized as a 4-byte integer denoting their node kind,
|
|
120
|
+
* followed by a 4-byte loc ID, followed by serialized node properties.
|
|
121
|
+
*
|
|
122
|
+
* If the node kind is 0 the node is null, otherwise the node kind - 1 is an
|
|
123
|
+
* index into the array of node deserialization functions.
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
deserializeNode() {
|
|
128
|
+
const nodeType = this.next();
|
|
129
|
+
|
|
130
|
+
if (nodeType === 0) {
|
|
131
|
+
return null;
|
|
181
132
|
}
|
|
182
|
-
/**
|
|
183
|
-
* Comments are serialized as a node list, where each comment is serialized
|
|
184
|
-
* as a 4-byte integer denoting comment type, followed by a 4-byte value
|
|
185
|
-
* denoting the loc ID, followed by a serialized string for the comment value.
|
|
186
|
-
*/
|
|
187
|
-
|
|
188
|
-
}, {
|
|
189
|
-
key: "deserializeComments",
|
|
190
|
-
value: function deserializeComments() {
|
|
191
|
-
var size = this.next();
|
|
192
|
-
var comments = [];
|
|
193
|
-
|
|
194
|
-
for (var i = 0; i < size; i++) {
|
|
195
|
-
var commentType = this.commentTypes[this.next()];
|
|
196
|
-
var loc = this.addEmptyLoc();
|
|
197
|
-
var value = this.deserializeString();
|
|
198
|
-
comments.push({
|
|
199
|
-
type: commentType,
|
|
200
|
-
loc: loc,
|
|
201
|
-
value: value
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
133
|
|
|
205
|
-
|
|
134
|
+
const nodeDeserializer = _HermesParserNodeDeserializers.default[nodeType - 1].bind(this);
|
|
135
|
+
|
|
136
|
+
return nodeDeserializer();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Node lists are serialized as a 4-byte integer denoting the number of
|
|
140
|
+
* elements in the list, followed by the serialized elements.
|
|
141
|
+
*/
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
deserializeNodeList() {
|
|
145
|
+
const size = this.next();
|
|
146
|
+
const nodeList = [];
|
|
147
|
+
|
|
148
|
+
for (let i = 0; i < size; i++) {
|
|
149
|
+
nodeList.push(this.deserializeNode());
|
|
206
150
|
}
|
|
207
|
-
}, {
|
|
208
|
-
key: "deserializeTokens",
|
|
209
|
-
value: function deserializeTokens() {
|
|
210
|
-
var size = this.next();
|
|
211
|
-
var tokens = [];
|
|
212
|
-
|
|
213
|
-
for (var i = 0; i < size; i++) {
|
|
214
|
-
var tokenType = this.tokenTypes[this.next()];
|
|
215
|
-
var loc = this.addEmptyLoc();
|
|
216
|
-
var value = this.deserializeString();
|
|
217
|
-
tokens.push({
|
|
218
|
-
type: tokenType,
|
|
219
|
-
loc: loc,
|
|
220
|
-
value: value
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
151
|
|
|
224
|
-
|
|
152
|
+
return nodeList;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Comments are serialized as a node list, where each comment is serialized
|
|
156
|
+
* as a 4-byte integer denoting comment type, followed by a 4-byte value
|
|
157
|
+
* denoting the loc ID, followed by a serialized string for the comment value.
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
deserializeComments() {
|
|
162
|
+
const size = this.next();
|
|
163
|
+
const comments = [];
|
|
164
|
+
|
|
165
|
+
for (let i = 0; i < size; i++) {
|
|
166
|
+
const commentType = this.commentTypes[this.next()];
|
|
167
|
+
const loc = this.addEmptyLoc();
|
|
168
|
+
const value = this.deserializeString();
|
|
169
|
+
comments.push({
|
|
170
|
+
type: commentType,
|
|
171
|
+
loc,
|
|
172
|
+
value
|
|
173
|
+
});
|
|
225
174
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
this.
|
|
238
|
-
|
|
175
|
+
|
|
176
|
+
return comments;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
deserializeTokens() {
|
|
180
|
+
const size = this.next();
|
|
181
|
+
const tokens = [];
|
|
182
|
+
|
|
183
|
+
for (let i = 0; i < size; i++) {
|
|
184
|
+
const tokenType = this.tokenTypes[this.next()];
|
|
185
|
+
const loc = this.addEmptyLoc();
|
|
186
|
+
const value = this.deserializeString();
|
|
187
|
+
tokens.push({
|
|
188
|
+
type: tokenType,
|
|
189
|
+
loc,
|
|
190
|
+
value
|
|
191
|
+
});
|
|
239
192
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
193
|
+
|
|
194
|
+
return tokens;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* While deserializing the AST locations are represented by
|
|
198
|
+
* a 4-byte loc ID. This is used to create a map of loc IDs to empty loc
|
|
199
|
+
* objects that are filled after the AST has been deserialized.
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
addEmptyLoc() {
|
|
204
|
+
// $FlowExpectedError
|
|
205
|
+
const loc = {};
|
|
206
|
+
this.locMap[this.next()] = loc;
|
|
207
|
+
return loc;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Positions are serialized as a loc ID which denotes which loc it is associated with,
|
|
211
|
+
* followed by kind which denotes whether it is a start or end position,
|
|
212
|
+
* followed by line, column, and offset (4-bytes each).
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
fillLocs() {
|
|
217
|
+
for (let i = 0; i < this.positionBufferSize; i++) {
|
|
218
|
+
const locId = this.HEAPU32[this.positionBufferIdx++];
|
|
219
|
+
const kind = this.HEAPU32[this.positionBufferIdx++];
|
|
220
|
+
const line = this.HEAPU32[this.positionBufferIdx++];
|
|
221
|
+
const column = this.HEAPU32[this.positionBufferIdx++];
|
|
222
|
+
const offset = this.HEAPU32[this.positionBufferIdx++];
|
|
223
|
+
const loc = this.locMap[locId];
|
|
224
|
+
|
|
225
|
+
if (kind === 0) {
|
|
226
|
+
loc.start = {
|
|
227
|
+
line,
|
|
228
|
+
column
|
|
229
|
+
};
|
|
230
|
+
loc.rangeStart = offset;
|
|
231
|
+
} else {
|
|
232
|
+
loc.end = {
|
|
233
|
+
line,
|
|
234
|
+
column
|
|
235
|
+
};
|
|
236
|
+
loc.rangeEnd = offset;
|
|
270
237
|
}
|
|
271
238
|
}
|
|
272
|
-
}
|
|
239
|
+
}
|
|
273
240
|
|
|
274
|
-
|
|
275
|
-
}();
|
|
241
|
+
}
|
|
276
242
|
|
|
277
|
-
exports
|
|
243
|
+
exports.default = HermesParserDeserializer;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -46,6 +46,7 @@ export default class HermesParserDeserializer {
|
|
|
46
46
|
'Keyword',
|
|
47
47
|
'Null',
|
|
48
48
|
'Numeric',
|
|
49
|
+
'BigInt',
|
|
49
50
|
'Punctuator',
|
|
50
51
|
'String',
|
|
51
52
|
'RegularExpression',
|