flow-parser 0.317.0 → 0.319.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.
Files changed (102) hide show
  1. package/oxidized/FlowParser.js +249 -0
  2. package/oxidized/FlowParser.js.flow +457 -0
  3. package/oxidized/FlowParserDeserializer.js +209 -0
  4. package/oxidized/FlowParserDeserializer.js.flow +323 -0
  5. package/oxidized/FlowParserNodeDeserializers.js +1767 -0
  6. package/oxidized/FlowParserNodeDeserializers.js.flow +18 -0
  7. package/oxidized/FlowParserWASM.js +1 -0
  8. package/oxidized/FlowParserWASM.js.flow +119 -0
  9. package/oxidized/HermesAST.js.flow +57 -0
  10. package/oxidized/HermesParserDecodeUTF8String.js +45 -0
  11. package/oxidized/HermesParserDecodeUTF8String.js.flow +65 -0
  12. package/oxidized/ParserOptions.js +8 -0
  13. package/oxidized/ParserOptions.js.flow +55 -0
  14. package/oxidized/babel/TransformESTreeToBabel.js +1055 -0
  15. package/oxidized/babel/TransformESTreeToBabel.js.flow +1349 -0
  16. package/oxidized/estree/StripFlowTypes.js +158 -0
  17. package/oxidized/estree/StripFlowTypes.js.flow +158 -0
  18. package/oxidized/estree/StripFlowTypesForBabel.js +176 -0
  19. package/oxidized/estree/StripFlowTypesForBabel.js.flow +216 -0
  20. package/oxidized/estree/TransformComponentSyntax.js +751 -0
  21. package/oxidized/estree/TransformComponentSyntax.js.flow +864 -0
  22. package/oxidized/estree/TransformEnumSyntax.js +92 -0
  23. package/oxidized/estree/TransformEnumSyntax.js.flow +125 -0
  24. package/oxidized/estree/TransformMatchSyntax.js +990 -0
  25. package/oxidized/estree/TransformMatchSyntax.js.flow +991 -0
  26. package/oxidized/estree/TransformRecordSyntax.js +283 -0
  27. package/oxidized/estree/TransformRecordSyntax.js.flow +310 -0
  28. package/oxidized/generated/ESTreeVisitorKeys.js +234 -0
  29. package/oxidized/generated/ESTreeVisitorKeys.js.flow +22 -0
  30. package/oxidized/getModuleDocblock.js +87 -0
  31. package/oxidized/getModuleDocblock.js.flow +119 -0
  32. package/oxidized/index.js +220 -0
  33. package/oxidized/index.js.flow +183 -0
  34. package/oxidized/oxidized-src/FlowParser.js +249 -0
  35. package/oxidized/oxidized-src/FlowParserDeserializer.js +209 -0
  36. package/oxidized/oxidized-src/FlowParserNodeDeserializers.js +1767 -0
  37. package/oxidized/oxidized-src/HermesParserDecodeUTF8String.js +45 -0
  38. package/oxidized/oxidized-src/ParserOptions.js +8 -0
  39. package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +1055 -0
  40. package/oxidized/oxidized-src/estree/StripFlowTypes.js +158 -0
  41. package/oxidized/oxidized-src/estree/StripFlowTypesForBabel.js +176 -0
  42. package/oxidized/oxidized-src/estree/TransformComponentSyntax.js +751 -0
  43. package/oxidized/oxidized-src/estree/TransformEnumSyntax.js +92 -0
  44. package/oxidized/oxidized-src/estree/TransformMatchSyntax.js +990 -0
  45. package/oxidized/oxidized-src/estree/TransformRecordSyntax.js +283 -0
  46. package/oxidized/oxidized-src/generated/ESTreeVisitorKeys.js +234 -0
  47. package/oxidized/oxidized-src/getModuleDocblock.js +87 -0
  48. package/oxidized/oxidized-src/index.js +220 -0
  49. package/oxidized/oxidized-src/transform/SimpleTransform.js +95 -0
  50. package/oxidized/oxidized-src/transform/astArrayMutationHelpers.js +52 -0
  51. package/oxidized/oxidized-src/transform/astNodeMutationHelpers.js +142 -0
  52. package/oxidized/oxidized-src/transform/print/comments/comments.js +245 -0
  53. package/oxidized/oxidized-src/transform/print/comments/prettier/common/util.js +235 -0
  54. package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/comments.js +663 -0
  55. package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/loc.js +30 -0
  56. package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
  57. package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/utils.js +89 -0
  58. package/oxidized/oxidized-src/transform/print/comments/prettier/main/comments.js +374 -0
  59. package/oxidized/oxidized-src/transform/print/comments/prettier/utils/get-last.js +5 -0
  60. package/oxidized/oxidized-src/transform/print/detachedNodeTypes.js +1 -0
  61. package/oxidized/oxidized-src/transform/print/print.js +68 -0
  62. package/oxidized/oxidized-src/traverse/SimpleTraverser.js +90 -0
  63. package/oxidized/oxidized-src/traverse/getVisitorKeys.js +29 -0
  64. package/oxidized/oxidized-src/utils/Builders.js +181 -0
  65. package/oxidized/oxidized-src/utils/GenID.js +37 -0
  66. package/oxidized/oxidized-src/utils/createSyntaxError.js +15 -0
  67. package/oxidized/oxidized-src/utils/isReservedWord.js +53 -0
  68. package/oxidized/oxidized-src/utils/mutateESTreeASTForPrettier.js +85 -0
  69. package/oxidized/transform/SimpleTransform.js +95 -0
  70. package/oxidized/transform/SimpleTransform.js.flow +168 -0
  71. package/oxidized/transform/astArrayMutationHelpers.js +52 -0
  72. package/oxidized/transform/astArrayMutationHelpers.js.flow +71 -0
  73. package/oxidized/transform/astNodeMutationHelpers.js +142 -0
  74. package/oxidized/transform/astNodeMutationHelpers.js.flow +252 -0
  75. package/oxidized/transform/print/comments/comments.js +245 -0
  76. package/oxidized/transform/print/comments/comments.js.flow +330 -0
  77. package/oxidized/transform/print/comments/prettier/common/util.js +235 -0
  78. package/oxidized/transform/print/comments/prettier/language-js/comments.js +663 -0
  79. package/oxidized/transform/print/comments/prettier/language-js/loc.js +30 -0
  80. package/oxidized/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
  81. package/oxidized/transform/print/comments/prettier/language-js/utils.js +89 -0
  82. package/oxidized/transform/print/comments/prettier/main/comments.js +374 -0
  83. package/oxidized/transform/print/comments/prettier/utils/get-last.js +5 -0
  84. package/oxidized/transform/print/detachedNodeTypes.js +1 -0
  85. package/oxidized/transform/print/detachedNodeTypes.js.flow +24 -0
  86. package/oxidized/transform/print/print.js +68 -0
  87. package/oxidized/transform/print/print.js.flow +87 -0
  88. package/oxidized/traverse/SimpleTraverser.js +90 -0
  89. package/oxidized/traverse/SimpleTraverser.js.flow +133 -0
  90. package/oxidized/traverse/getVisitorKeys.js +29 -0
  91. package/oxidized/traverse/getVisitorKeys.js.flow +36 -0
  92. package/oxidized/utils/Builders.js +181 -0
  93. package/oxidized/utils/Builders.js.flow +218 -0
  94. package/oxidized/utils/GenID.js +37 -0
  95. package/oxidized/utils/GenID.js.flow +39 -0
  96. package/oxidized/utils/createSyntaxError.js +15 -0
  97. package/oxidized/utils/createSyntaxError.js.flow +24 -0
  98. package/oxidized/utils/isReservedWord.js +53 -0
  99. package/oxidized/utils/isReservedWord.js.flow +57 -0
  100. package/oxidized/utils/mutateESTreeASTForPrettier.js +85 -0
  101. package/oxidized/utils/mutateESTreeASTForPrettier.js.flow +130 -0
  102. package/package.json +16 -6
@@ -0,0 +1,209 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _HermesParserDecodeUTF8String = _interopRequireDefault(require("./HermesParserDecodeUTF8String"));
9
+
10
+ var _FlowParserNodeDeserializers = _interopRequireDefault(require("./FlowParserNodeDeserializers"));
11
+
12
+ function _interopRequireDefault(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+
18
+ class FlowParserDeserializer {
19
+ constructor(programBuffer, positionBuffer, positionBufferSize, stringBufferBase, wasmParser, options) {
20
+ this.programBufferIdx = void 0;
21
+ this.positionBufferIdx = void 0;
22
+ this.positionBufferSize = void 0;
23
+ this.stringBufferBase = void 0;
24
+ this.locMap = void 0;
25
+ this.HEAPU8 = void 0;
26
+ this.HEAPU32 = void 0;
27
+ this.HEAPF64 = void 0;
28
+ this.options = void 0;
29
+ this.commentTypes = ['Block', 'Line'];
30
+ this.tokenTypes = ['Boolean', 'Identifier', 'Keyword', 'Null', 'Numeric', 'BigInt', 'Punctuator', 'String', 'RegularExpression', 'Template', 'JSXText'];
31
+ this.programBufferIdx = programBuffer / 4;
32
+ this.positionBufferIdx = positionBuffer / 4;
33
+ this.positionBufferSize = positionBufferSize;
34
+ this.stringBufferBase = stringBufferBase;
35
+ this.locMap = {};
36
+ this.HEAPU8 = wasmParser.HEAPU8;
37
+ this.HEAPU32 = wasmParser.HEAPU32;
38
+ this.HEAPF64 = wasmParser.HEAPF64;
39
+ this.options = options;
40
+ }
41
+
42
+ next() {
43
+ const num = this.HEAPU32[this.programBufferIdx++];
44
+ return num;
45
+ }
46
+
47
+ deserialize() {
48
+ const program = {
49
+ type: 'Program',
50
+ loc: this.addEmptyLoc(),
51
+ body: this.deserializeNodeList(),
52
+ comments: this.deserializeComments()
53
+ };
54
+ program.interpreter = this.deserializeNode();
55
+
56
+ if (this.options.tokens === true) {
57
+ program.tokens = this.deserializeTokens();
58
+ } else {
59
+ this.deserializeTokens();
60
+ }
61
+
62
+ program.errors = this.deserializeErrors();
63
+ this.fillLocs();
64
+ return program;
65
+ }
66
+
67
+ deserializeErrors() {
68
+ const size = this.next();
69
+ const errors = [];
70
+
71
+ for (let i = 0; i < size; i++) {
72
+ const loc = this.addEmptyLoc();
73
+ const message = this.deserializeString();
74
+
75
+ if (message == null) {
76
+ throw new Error('Expected serialized parser error message');
77
+ }
78
+
79
+ errors.push({
80
+ loc,
81
+ message
82
+ });
83
+ }
84
+
85
+ return errors;
86
+ }
87
+
88
+ deserializeBoolean() {
89
+ return Boolean(this.next());
90
+ }
91
+
92
+ deserializeNumber() {
93
+ let floatIdx;
94
+
95
+ if (this.programBufferIdx % 2 === 0) {
96
+ floatIdx = this.programBufferIdx / 2;
97
+ this.programBufferIdx += 2;
98
+ } else {
99
+ floatIdx = (this.programBufferIdx + 1) / 2;
100
+ this.programBufferIdx += 3;
101
+ }
102
+
103
+ return this.HEAPF64[floatIdx];
104
+ }
105
+
106
+ deserializeString() {
107
+ const offsetPlusOne = this.next();
108
+
109
+ if (offsetPlusOne === 0) {
110
+ return null;
111
+ }
112
+
113
+ const size = this.next();
114
+ return (0, _HermesParserDecodeUTF8String.default)(this.stringBufferBase + offsetPlusOne - 1, size, this.HEAPU8);
115
+ }
116
+
117
+ deserializeNode() {
118
+ const nodeType = this.next();
119
+
120
+ if (nodeType === 0) {
121
+ return null;
122
+ }
123
+
124
+ const nodeDeserializer = _FlowParserNodeDeserializers.default[nodeType - 1].bind(this);
125
+
126
+ return nodeDeserializer();
127
+ }
128
+
129
+ deserializeNodeList() {
130
+ const size = this.next();
131
+ const nodeList = [];
132
+
133
+ for (let i = 0; i < size; i++) {
134
+ nodeList.push(this.deserializeNode());
135
+ }
136
+
137
+ return nodeList;
138
+ }
139
+
140
+ deserializeComments() {
141
+ const size = this.next();
142
+ const comments = [];
143
+
144
+ for (let i = 0; i < size; i++) {
145
+ const commentType = this.commentTypes[this.next()];
146
+ const loc = this.addEmptyLoc();
147
+ const value = this.deserializeString();
148
+ comments.push({
149
+ type: commentType,
150
+ loc,
151
+ value
152
+ });
153
+ }
154
+
155
+ return comments;
156
+ }
157
+
158
+ deserializeTokens() {
159
+ const size = this.next();
160
+ const tokens = [];
161
+
162
+ for (let i = 0; i < size; i++) {
163
+ const tokenType = this.tokenTypes[this.next()];
164
+ const loc = this.addEmptyLoc();
165
+ const value = this.deserializeString();
166
+ tokens.push({
167
+ type: tokenType,
168
+ loc,
169
+ value
170
+ });
171
+ }
172
+
173
+ return tokens;
174
+ }
175
+
176
+ addEmptyLoc() {
177
+ const loc = {};
178
+ this.locMap[this.next()] = loc;
179
+ return loc;
180
+ }
181
+
182
+ fillLocs() {
183
+ for (let i = 0; i < this.positionBufferSize; i++) {
184
+ const locId = this.HEAPU32[this.positionBufferIdx++];
185
+ const kind = this.HEAPU32[this.positionBufferIdx++];
186
+ const line = this.HEAPU32[this.positionBufferIdx++];
187
+ const column = this.HEAPU32[this.positionBufferIdx++];
188
+ const offset = this.HEAPU32[this.positionBufferIdx++];
189
+ const loc = this.locMap[locId];
190
+
191
+ if (kind === 0) {
192
+ loc.start = {
193
+ line,
194
+ column
195
+ };
196
+ loc.rangeStart = offset;
197
+ } else {
198
+ loc.end = {
199
+ line,
200
+ column
201
+ };
202
+ loc.rangeEnd = offset;
203
+ }
204
+ }
205
+ }
206
+
207
+ }
208
+
209
+ exports.default = FlowParserDeserializer;
@@ -0,0 +1,323 @@
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 {HermesSourceLocation, HermesNode, HermesToken} from './HermesAST';
14
+ import type {FlowParserWASM} from './FlowParserWASM';
15
+ import type {ParserOptions} from './ParserOptions';
16
+
17
+ import HermesParserDecodeUTF8String from './HermesParserDecodeUTF8String';
18
+ import NODE_DESERIALIZERS from './FlowParserNodeDeserializers';
19
+
20
+ type FlowComment = {
21
+ type: 'Block' | 'Line',
22
+ loc: HermesSourceLocation,
23
+ value: ?string,
24
+ };
25
+
26
+ export type FlowParserProgram = {
27
+ type: 'Program',
28
+ loc: HermesSourceLocation,
29
+ body: Array<?HermesNode>,
30
+ comments: Array<FlowComment>,
31
+ interpreter?: ?HermesNode,
32
+ tokens?: Array<HermesToken>,
33
+ errors?: Array<{loc: HermesSourceLocation, message: string}>,
34
+ sourceType?: 'script' | 'module',
35
+ docblock?: unknown,
36
+ [string]: unknown,
37
+ };
38
+
39
+ /**
40
+ * Deserializer for the Flow Rust parser's binary protocol.
41
+ *
42
+ * This is structurally identical to HermesParserDeserializer, but uses
43
+ * FlowParserNodeDeserializers (matching the Rust serializer's node kind
44
+ * numbering) and produces ESTree-compatible output directly.
45
+ */
46
+ export default class FlowParserDeserializer {
47
+ programBufferIdx: number;
48
+ positionBufferIdx: number;
49
+ +positionBufferSize: number;
50
+ +stringBufferBase: number;
51
+ +locMap: {[number]: HermesSourceLocation};
52
+ +HEAPU8: FlowParserWASM['HEAPU8'];
53
+ +HEAPU32: FlowParserWASM['HEAPU32'];
54
+ +HEAPF64: FlowParserWASM['HEAPF64'];
55
+ +options: ParserOptions;
56
+
57
+ // Comment types: Flow uses ESTree-standard names
58
+ // Matches CommentKind enum in ast.rs: Block = 0, Line = 1
59
+ +commentTypes: ReadonlyArray<FlowComment['type']> = ['Block', 'Line'];
60
+
61
+ // Matches TokenType enum (same as Hermes for compatibility)
62
+ +tokenTypes: ReadonlyArray<HermesToken['type']> = [
63
+ 'Boolean',
64
+ 'Identifier',
65
+ 'Keyword',
66
+ 'Null',
67
+ 'Numeric',
68
+ 'BigInt',
69
+ 'Punctuator',
70
+ 'String',
71
+ 'RegularExpression',
72
+ 'Template',
73
+ 'JSXText',
74
+ ];
75
+
76
+ constructor(
77
+ programBuffer: number,
78
+ positionBuffer: number,
79
+ positionBufferSize: number,
80
+ stringBufferBase: number,
81
+ wasmParser: FlowParserWASM,
82
+ options: ParserOptions,
83
+ ) {
84
+ // Program and position buffer are memory addresses, so we must convert
85
+ // into indices into HEAPU32 (an array of 4-byte integers).
86
+ this.programBufferIdx = programBuffer / 4;
87
+ this.positionBufferIdx = positionBuffer / 4;
88
+
89
+ this.positionBufferSize = positionBufferSize;
90
+ // Base byte address into the WASM heap for the side string buffer.
91
+ // Each non-null string is encoded in the program buffer as
92
+ // `(offset+1, len)`; we decode UTF-8 starting at `stringBufferBase +
93
+ // offset`. The +1 disambiguates an empty string at offset 0 from a
94
+ // null pointer (encoded as `(0,)` with no length word).
95
+ this.stringBufferBase = stringBufferBase;
96
+ this.locMap = {};
97
+
98
+ this.HEAPU8 = wasmParser.HEAPU8;
99
+ this.HEAPU32 = wasmParser.HEAPU32;
100
+ this.HEAPF64 = wasmParser.HEAPF64;
101
+
102
+ this.options = options;
103
+ }
104
+
105
+ /**
106
+ * Consume and return the next 4 bytes in the program buffer.
107
+ */
108
+ next(): number {
109
+ const num = this.HEAPU32[this.programBufferIdx++];
110
+ return num;
111
+ }
112
+
113
+ deserialize(): FlowParserProgram {
114
+ const program: FlowParserProgram = {
115
+ type: 'Program',
116
+ loc: this.addEmptyLoc(),
117
+ body: this.deserializeNodeList(),
118
+ comments: this.deserializeComments(),
119
+ };
120
+
121
+ // Interpreter directive (OCaml `program` estree_translator.ml:118-123).
122
+ // The serializer writes a Node slot here: an `InterpreterDirective` node
123
+ // when `#!shebang` is present, otherwise null. Always attach the slot so
124
+ // the public Program shape carries `interpreter: InterpreterDirective | null`
125
+ // — matches upstream hermes-parser, which exposes the slot uniformly.
126
+ program.interpreter = this.deserializeNode();
127
+
128
+ if (this.options.tokens === true) {
129
+ program.tokens = this.deserializeTokens();
130
+ } else {
131
+ // Tokens slot is always written by the serializer; consume the count
132
+ // even when callers didn't ask for tokens.
133
+ this.deserializeTokens();
134
+ }
135
+
136
+ program.errors = this.deserializeErrors();
137
+
138
+ this.fillLocs();
139
+
140
+ return program;
141
+ }
142
+
143
+ /**
144
+ * Errors are serialized as a 4-byte count followed by (locId, message)
145
+ * pairs. Each entry becomes `{loc, message}` matching the OCaml/Hermes
146
+ * shape consumed by the hardcoded fixture comparator.
147
+ */
148
+ deserializeErrors(): Array<{loc: HermesSourceLocation, message: string}> {
149
+ const size = this.next();
150
+ const errors = [];
151
+ for (let i = 0; i < size; i++) {
152
+ const loc = this.addEmptyLoc();
153
+ const message = this.deserializeString();
154
+ if (message == null) {
155
+ throw new Error('Expected serialized parser error message');
156
+ }
157
+ errors.push({loc, message});
158
+ }
159
+ return errors;
160
+ }
161
+
162
+ /**
163
+ * Booleans are serialized as a single 4-byte integer.
164
+ */
165
+ deserializeBoolean(): boolean {
166
+ return Boolean(this.next());
167
+ }
168
+
169
+ /**
170
+ * Numbers are serialized directly into program buffer, taking up 8 bytes
171
+ * preceded by 4 bytes of alignment padding if necessary.
172
+ */
173
+ deserializeNumber(): number {
174
+ let floatIdx;
175
+
176
+ // Numbers are aligned on 8-byte boundaries, so skip padding if we are at
177
+ // an odd index into the 4-byte aligned program buffer.
178
+ if (this.programBufferIdx % 2 === 0) {
179
+ floatIdx = this.programBufferIdx / 2;
180
+ this.programBufferIdx += 2;
181
+ } else {
182
+ floatIdx = (this.programBufferIdx + 1) / 2;
183
+ this.programBufferIdx += 3;
184
+ }
185
+
186
+ return this.HEAPF64[floatIdx];
187
+ }
188
+
189
+ /**
190
+ * Strings are serialized as `(offset+1, len)` where `offset` is the byte
191
+ * position into the side `string_buffer`. A 0 in the first slot means the
192
+ * string is null and no length word follows. The `+1` lets an empty
193
+ * string at offset 0 stay distinguishable from null.
194
+ */
195
+ deserializeString(): ?string {
196
+ const offsetPlusOne = this.next();
197
+ if (offsetPlusOne === 0) {
198
+ return null;
199
+ }
200
+
201
+ const size = this.next();
202
+
203
+ return HermesParserDecodeUTF8String(
204
+ this.stringBufferBase + offsetPlusOne - 1,
205
+ size,
206
+ this.HEAPU8,
207
+ );
208
+ }
209
+
210
+ /**
211
+ * Nodes are serialized as a 4-byte integer denoting their node kind,
212
+ * followed by a 4-byte loc ID, followed by serialized node properties.
213
+ *
214
+ * If the node kind is 0 the node is null, otherwise the node kind - 1 is an
215
+ * index into the array of node deserialization functions.
216
+ */
217
+ deserializeNode(): ?HermesNode {
218
+ const nodeType = this.next();
219
+ if (nodeType === 0) {
220
+ return null;
221
+ }
222
+
223
+ const nodeDeserializer = NODE_DESERIALIZERS[nodeType - 1].bind(this);
224
+ return nodeDeserializer();
225
+ }
226
+
227
+ /**
228
+ * Node lists are serialized as a 4-byte integer denoting the number of
229
+ * elements in the list, followed by the serialized elements.
230
+ */
231
+ deserializeNodeList(): Array<?HermesNode> {
232
+ const size = this.next();
233
+ const nodeList = [];
234
+
235
+ for (let i = 0; i < size; i++) {
236
+ nodeList.push(this.deserializeNode());
237
+ }
238
+
239
+ return nodeList;
240
+ }
241
+
242
+ /**
243
+ * Comments are serialized as a node list, where each comment is serialized
244
+ * as a 4-byte integer denoting comment type, followed by a 4-byte value
245
+ * denoting the loc ID, followed by a serialized string for the comment value.
246
+ */
247
+ deserializeComments(): Array<FlowComment> {
248
+ const size = this.next();
249
+ const comments = [];
250
+
251
+ for (let i = 0; i < size; i++) {
252
+ const commentType = this.commentTypes[this.next()];
253
+ const loc = this.addEmptyLoc();
254
+ const value = this.deserializeString();
255
+ comments.push({
256
+ type: commentType,
257
+ loc,
258
+ value,
259
+ });
260
+ }
261
+
262
+ return comments;
263
+ }
264
+
265
+ deserializeTokens(): Array<HermesToken> {
266
+ const size = this.next();
267
+ const tokens = [];
268
+
269
+ for (let i = 0; i < size; i++) {
270
+ const tokenType = this.tokenTypes[this.next()];
271
+ const loc = this.addEmptyLoc();
272
+ const value = this.deserializeString();
273
+ tokens.push({
274
+ type: tokenType,
275
+ loc,
276
+ value,
277
+ });
278
+ }
279
+
280
+ return tokens;
281
+ }
282
+
283
+ /**
284
+ * While deserializing the AST locations are represented by
285
+ * a 4-byte loc ID. This is used to create a map of loc IDs to empty loc
286
+ * objects that are filled after the AST has been deserialized.
287
+ */
288
+ addEmptyLoc(): HermesSourceLocation {
289
+ const loc: HermesSourceLocation = {};
290
+ this.locMap[this.next()] = loc;
291
+ return loc;
292
+ }
293
+
294
+ /**
295
+ * Positions are serialized as a loc ID which denotes which loc it is
296
+ * associated with, followed by kind which denotes whether it is a start
297
+ * or end position, followed by line, column, and offset (4-bytes each).
298
+ */
299
+ fillLocs(): void {
300
+ for (let i = 0; i < this.positionBufferSize; i++) {
301
+ const locId = this.HEAPU32[this.positionBufferIdx++];
302
+ const kind = this.HEAPU32[this.positionBufferIdx++];
303
+ const line = this.HEAPU32[this.positionBufferIdx++];
304
+ const column = this.HEAPU32[this.positionBufferIdx++];
305
+ const offset = this.HEAPU32[this.positionBufferIdx++];
306
+
307
+ const loc = this.locMap[locId];
308
+ if (kind === 0) {
309
+ loc.start = {
310
+ line,
311
+ column,
312
+ };
313
+ loc.rangeStart = offset;
314
+ } else {
315
+ loc.end = {
316
+ line,
317
+ column,
318
+ };
319
+ loc.rangeEnd = offset;
320
+ }
321
+ }
322
+ }
323
+ }