flow-parser 0.318.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 +6 -4
@@ -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
+ 'use strict';
12
+
13
+ import type {Program as ESTreeProgram} from 'flow-estree';
14
+ import type {ParserOptions} from './ParserOptions';
15
+ import type {BabelFile} from './babel/TransformESTreeToBabel';
16
+
17
+ import * as FlowParser from './FlowParser';
18
+ import FlowVisitorKeys from './generated/ESTreeVisitorKeys';
19
+ import * as TransformComponentSyntax from './estree/TransformComponentSyntax';
20
+ import * as TransformEnumSyntax from './estree/TransformEnumSyntax';
21
+ import * as TransformMatchSyntax from './estree/TransformMatchSyntax';
22
+ import * as TransformRecordSyntax from './estree/TransformRecordSyntax';
23
+ import * as StripFlowTypesForBabel from './estree/StripFlowTypesForBabel';
24
+ import * as TransformESTreeToBabel from './babel/TransformESTreeToBabel';
25
+ import * as StripFlowTypes from './estree/StripFlowTypes';
26
+
27
+ const DEFAULTS: {flow: 'detect'} = {
28
+ flow: 'detect',
29
+ };
30
+
31
+ function getOptions(opts?: ParserOptions): ParserOptions {
32
+ // Always build a fresh object so we never mutate the caller's input.
33
+ // Repeated calls with the same `opts` reference must produce identical
34
+ // results — earlier versions wrote normalized fields back onto `opts`,
35
+ // poisoning subsequent calls.
36
+ const options: ParserOptions = {...DEFAULTS, ...(opts ?? {})};
37
+
38
+ // Default to detecting whether to parse Flow syntax by the presence
39
+ // of an @flow pragma.
40
+ if (options.flow !== 'all' && options.flow !== 'detect') {
41
+ throw new Error('flow option must be "all" or "detect"');
42
+ }
43
+
44
+ if (options.sourceType === 'unambiguous') {
45
+ // Clear source type so that it will be detected from the contents of the file
46
+ delete options.sourceType;
47
+ } else if (
48
+ options.sourceType != null &&
49
+ options.sourceType !== 'script' &&
50
+ options.sourceType !== 'module'
51
+ ) {
52
+ throw new Error(
53
+ 'sourceType option must be "script", "module", or "unambiguous" if set',
54
+ );
55
+ }
56
+
57
+ if (options.enableExperimentalComponentSyntax == null) {
58
+ options.enableExperimentalComponentSyntax = true; // Enable by default
59
+ }
60
+
61
+ if (options.enableExperimentalFlowMatchSyntax == null) {
62
+ options.enableExperimentalFlowMatchSyntax = true; // Enable by default
63
+ }
64
+
65
+ // Record syntax: upstream defaults `enableExperimentalFlowRecordSyntax` to
66
+ // `true`. The fork's WASM cwrap (FlowParser.js) reads `options.enableRecords`
67
+ // — a separate, OCaml-aligned key intentionally mirroring
68
+ // `default_parse_options`. To preserve the upstream-compatible API surface
69
+ // on the outside while keeping the OCaml-aligned key on the WASM boundary,
70
+ // bridge: default the upstream key, then mirror it onto the cwrap key. The
71
+ // mirroring belongs here (the public-API layer) and not at the wasm
72
+ // boundary, because the wasm fixture runner under
73
+ // `__tests__/runWasmFixtures.js` calls `FlowParser.parse` directly and
74
+ // expects OCaml's strict defaults to apply.
75
+ if (options.enableExperimentalFlowRecordSyntax == null) {
76
+ options.enableExperimentalFlowRecordSyntax = true; // Enable by default
77
+ }
78
+ options.enableRecords = options.enableExperimentalFlowRecordSyntax;
79
+
80
+ // Enum syntax: upstream hermes-parser always parses Flow `enum` syntax. The
81
+ // fork's WASM cwrap reads `options.enableEnums` — an OCaml-aligned key that
82
+ // defaults to false in `default_parse_options`. Default `enableEnums` to
83
+ // true so unflagged hermes-parser-style callers can parse `enum X { A }`
84
+ // without opt-in. Same wasm-fixture-runner consideration as `enableRecords`.
85
+ if (options.enableEnums == null) {
86
+ options.enableEnums = true;
87
+ }
88
+
89
+ // Decorators: upstream hermes-parser supports stage-1 decorators by default.
90
+ // The fork's WASM cwrap reads `enableExperimentalDecorators`, which
91
+ // defaults to false in OCaml's `default_parse_options`. Default true on the
92
+ // JS surface to match upstream. Same wasm-fixture-runner consideration as
93
+ // `enableRecords`.
94
+ if (options.enableExperimentalDecorators == null) {
95
+ options.enableExperimentalDecorators = true;
96
+ }
97
+
98
+ options.tokens = options.tokens === true;
99
+ options.allowReturnOutsideFunction =
100
+ options.allowReturnOutsideFunction === true;
101
+
102
+ return options;
103
+ }
104
+
105
+ declare function parse(
106
+ code: string,
107
+ opts: Readonly<{...ParserOptions, babel: true}>,
108
+ ): BabelFile;
109
+ // eslint-disable-next-line no-redeclare
110
+ declare function parse(
111
+ code: string,
112
+ opts?:
113
+ | Readonly<{...ParserOptions, babel?: false | void}>
114
+ | Readonly<{...ParserOptions, babel: false}>,
115
+ ): ESTreeProgram;
116
+
117
+ // eslint-disable-next-line no-redeclare
118
+ export function parse(
119
+ code: string,
120
+ opts?: ParserOptions,
121
+ ): BabelFile | ESTreeProgram {
122
+ const options = getOptions(opts);
123
+
124
+ // Resolve `flow: 'all' | 'detect'` to the Rust parser's option set.
125
+ // `flow: 'all'` always parses Flow type syntax (`enableTypes: true`).
126
+ // `flow: 'detect'` opts the Rust parser into docblock-pragma scanning
127
+ // (`enableTypesPragmaDetection: true`); the Rust side mirrors C++
128
+ // hermes-parser by parsing all Flow syntax with `@flow`, and only
129
+ // unambiguous Flow syntax without it.
130
+ if (options.flow === 'all') {
131
+ options.enableTypes = true;
132
+ } else {
133
+ options.enableTypesPragmaDetection = true;
134
+ }
135
+ // Flow Rust parser outputs ESTree-compatible AST directly. The wire→ESTree
136
+ // loc/range normalization (source-filename assignment, `node.range` synthesis
137
+ // from `loc.rangeStart`/`rangeEnd`, then cleanup of those wire-only loc
138
+ // fields) is done at the parser level inside `FlowParser.parse` so callers
139
+ // that bypass this hermes-parser-compatibility wrapper still get canonical
140
+ // ESTree loc/range. Optional-chain ESTree wrapping is emitted by the WASM
141
+ // serializer, mirroring upstream `HermesToESTreeAdapter.mapChainExpression`.
142
+ // See FlowParser.js.
143
+ const estreeAST = FlowParser.parse(code, options);
144
+
145
+ if (options.babel !== true) {
146
+ return estreeAST;
147
+ }
148
+
149
+ // `babel: true` overload — reduce the ESTree AST through the same syntax
150
+ // lowering pipeline upstream uses (Enum/Match/Component/Record syntax,
151
+ // strip Flow types), then convert ESTree → Babel AST shape.
152
+ const loweredESTreeAST = [
153
+ options.transformOptions?.TransformEnumSyntax?.enable
154
+ ? TransformEnumSyntax.transformProgram
155
+ : null,
156
+ TransformMatchSyntax.transformProgram,
157
+ TransformComponentSyntax.transformProgram,
158
+ TransformRecordSyntax.transformProgram,
159
+ StripFlowTypesForBabel.transformProgram,
160
+ ].reduce((ast, transform) => transform?.(ast, options) ?? ast, estreeAST);
161
+
162
+ return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
163
+ }
164
+
165
+ export type {ParserOptions} from './ParserOptions';
166
+ export * from './ParserOptions';
167
+ export * from './traverse/SimpleTraverser';
168
+ export * from './transform/SimpleTransform';
169
+ export * from './traverse/getVisitorKeys';
170
+ export {FlowVisitorKeys};
171
+ export * as astArrayMutationHelpers from './transform/astArrayMutationHelpers';
172
+ export * as astNodeMutationHelpers from './transform/astNodeMutationHelpers';
173
+ export {default as mutateESTreeASTForPrettier} from './utils/mutateESTreeASTForPrettier';
174
+
175
+ const Transforms = {
176
+ transformEnumSyntax: TransformEnumSyntax.transformProgram,
177
+ transformMatchSyntax: TransformMatchSyntax.transformProgram,
178
+ transformComponentSyntax: TransformComponentSyntax.transformProgram,
179
+ transformRecordSyntax: TransformRecordSyntax.transformProgram,
180
+ stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
181
+ stripFlowTypes: StripFlowTypes.transformProgram,
182
+ };
183
+ export {Transforms};
@@ -0,0 +1,249 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parse = parse;
7
+
8
+ var _FlowParserDeserializer = _interopRequireDefault(require("./FlowParserDeserializer"));
9
+
10
+ var _FlowParserWASM = _interopRequireDefault(require("./FlowParserWASM"));
11
+
12
+ var _getModuleDocblock = require("./getModuleDocblock");
13
+
14
+ function _interopRequireDefault(obj) {
15
+ return obj && obj.__esModule ? obj : {
16
+ default: obj
17
+ };
18
+ }
19
+
20
+ let FlowParserWASM;
21
+ let flowParse;
22
+ let flowParseResult_free;
23
+ let flowParseResult_getError;
24
+ let flowParseResult_getErrorLine;
25
+ let flowParseResult_getErrorColumn;
26
+ let flowParseResult_getProgramBuffer;
27
+ let flowParseResult_getPositionBuffer;
28
+ let flowParseResult_getPositionBufferSize;
29
+ let flowParseResult_getStringBuffer;
30
+
31
+ function initFlowParserWASM() {
32
+ if (FlowParserWASM != null) {
33
+ return;
34
+ }
35
+
36
+ FlowParserWASM = (0, _FlowParserWASM.default)({
37
+ quit(_status, toThrow) {
38
+ throw toThrow;
39
+ }
40
+
41
+ });
42
+ flowParse = FlowParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']);
43
+ flowParseResult_free = FlowParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
44
+ flowParseResult_getError = FlowParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
45
+ flowParseResult_getErrorLine = FlowParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
46
+ flowParseResult_getErrorColumn = FlowParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
47
+ flowParseResult_getProgramBuffer = FlowParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
48
+ flowParseResult_getPositionBuffer = FlowParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
49
+ flowParseResult_getPositionBufferSize = FlowParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']);
50
+ flowParseResult_getStringBuffer = FlowParserWASM.cwrap('hermesParseResult_getStringBuffer', 'number', ['number']);
51
+ }
52
+
53
+ function copyToHeap(buffer, addr) {
54
+ FlowParserWASM.HEAP8.set(buffer, addr);
55
+ FlowParserWASM.HEAP8[addr + buffer.length] = 0;
56
+ }
57
+
58
+ function flag(v) {
59
+ return v === true ? 1 : 0;
60
+ }
61
+
62
+ function parse(source, options) {
63
+ initFlowParserWASM();
64
+ const sourceBuffer = Buffer.from(source, 'utf8');
65
+ const filename = typeof options.sourceFilename === 'string' ? options.sourceFilename : '';
66
+ const filenameBuffer = filename.length > 0 ? Buffer.from(filename, 'utf8') : null;
67
+ let sourceAddr = 0;
68
+ let filenameAddr = 0;
69
+ let filenameLen = 0;
70
+ let parseResult = 0;
71
+
72
+ try {
73
+ sourceAddr = FlowParserWASM._malloc(sourceBuffer.length + 1);
74
+
75
+ if (!sourceAddr) {
76
+ throw new Error('Parser out of memory');
77
+ }
78
+
79
+ if (filenameBuffer != null) {
80
+ filenameAddr = FlowParserWASM._malloc(filenameBuffer.length + 1);
81
+
82
+ if (!filenameAddr) {
83
+ throw new Error('Parser out of memory');
84
+ }
85
+
86
+ copyToHeap(filenameBuffer, filenameAddr);
87
+ filenameLen = filenameBuffer.length + 1;
88
+ }
89
+
90
+ copyToHeap(sourceBuffer, sourceAddr);
91
+ const enableTypes = options.enableTypes === false ? 0 : 1;
92
+ const sourceTypeCode = options.sourceType === 'script' ? 1 : options.sourceType === 'module' ? 2 : 0;
93
+ parseResult = flowParse(sourceAddr, sourceBuffer.length + 1, filenameAddr, filenameLen, flag(options.enableExperimentalComponentSyntax), flag(options.enableExperimentalFlowMatchSyntax), flag(options.enableExperimentalDecorators), flag(options.tokens), flag(options.allowReturnOutsideFunction), flag(options.assertOperator), flag(options.enableEnums), flag(options.enableRecords), enableTypes, sourceTypeCode, flag(options.enableTypesPragmaDetection), 0);
94
+ const programBuffer = flowParseResult_getProgramBuffer(parseResult);
95
+
96
+ if (!programBuffer) {
97
+ const err = flowParseResult_getError(parseResult);
98
+ const syntaxError = new SyntaxError(err || 'unknown parse error');
99
+ syntaxError.loc = {
100
+ line: flowParseResult_getErrorLine(parseResult),
101
+ column: flowParseResult_getErrorColumn(parseResult)
102
+ };
103
+ throw syntaxError;
104
+ }
105
+
106
+ const deserializer = new _FlowParserDeserializer.default(programBuffer, flowParseResult_getPositionBuffer(parseResult), flowParseResult_getPositionBufferSize(parseResult), flowParseResult_getStringBuffer(parseResult), FlowParserWASM, options);
107
+ const ast = deserializer.deserialize();
108
+
109
+ if (ast.type !== 'Program') {
110
+ throw new Error(`Expected Program, got ${ast.type}`);
111
+ }
112
+
113
+ const sourceFilename = typeof options.sourceFilename === 'string' ? options.sourceFilename : null;
114
+ const visitedNodes = new WeakSet();
115
+ const locRanges = new WeakMap();
116
+
117
+ function fixLocs(node) {
118
+ if (node == null || typeof node !== 'object') {
119
+ return;
120
+ }
121
+
122
+ const wireNode = node;
123
+
124
+ if (visitedNodes.has(wireNode)) {
125
+ return;
126
+ }
127
+
128
+ visitedNodes.add(wireNode);
129
+
130
+ if (wireNode.loc != null) {
131
+ const loc = wireNode.loc;
132
+ let range = locRanges.get(loc);
133
+
134
+ if (range == null) {
135
+ const rangeStart = loc.rangeStart;
136
+ const rangeEnd = loc.rangeEnd;
137
+
138
+ if (rangeStart == null || rangeEnd == null) {
139
+ throw new Error('Expected serialized source range');
140
+ }
141
+
142
+ loc.source = sourceFilename;
143
+ range = [rangeStart, rangeEnd];
144
+ locRanges.set(loc, range);
145
+ delete loc.rangeStart;
146
+ delete loc.rangeEnd;
147
+ }
148
+
149
+ wireNode.range = range;
150
+ }
151
+
152
+ for (const key of Object.keys(wireNode)) {
153
+ const val = wireNode[key];
154
+
155
+ if (Array.isArray(val)) {
156
+ for (const child of val) {
157
+ fixLocs(child);
158
+ }
159
+ } else {
160
+ fixLocs(val);
161
+ }
162
+ }
163
+ }
164
+
165
+ fixLocs(ast);
166
+
167
+ if (options.throwOnParseErrors !== false) {
168
+ const errors = ast.errors;
169
+
170
+ if (errors != null && errors.length > 0) {
171
+ const first = errors[0];
172
+ const start = first.loc.start;
173
+
174
+ if (start == null) {
175
+ throw new Error('Expected serialized parser error start location');
176
+ }
177
+
178
+ const line = start.line;
179
+ const column = start.column;
180
+ const syntaxError = new SyntaxError(`${first.message} (${line}:${column})`);
181
+ syntaxError.loc = {
182
+ line,
183
+ column
184
+ };
185
+ throw syntaxError;
186
+ }
187
+ }
188
+
189
+ if (options.sourceType === 'script' || options.sourceType === 'module') {
190
+ ast.sourceType = options.sourceType;
191
+ } else {
192
+ ast.sourceType = detectSourceType(ast);
193
+ }
194
+
195
+ ast.docblock = (0, _getModuleDocblock.getModuleDocblock)(ast);
196
+ return asProgram(ast);
197
+ } finally {
198
+ if (parseResult !== 0) {
199
+ flowParseResult_free(parseResult);
200
+ }
201
+
202
+ if (sourceAddr !== 0) {
203
+ FlowParserWASM._free(sourceAddr);
204
+ }
205
+
206
+ if (filenameAddr !== 0) {
207
+ FlowParserWASM._free(filenameAddr);
208
+ }
209
+ }
210
+ }
211
+
212
+ function asProgram(ast) {
213
+ const program = ast;
214
+ return program;
215
+ }
216
+
217
+ function detectSourceType(program) {
218
+ if (!Array.isArray(program.body)) {
219
+ return 'script';
220
+ }
221
+
222
+ for (const stmt of program.body) {
223
+ if (stmt == null) {
224
+ continue;
225
+ }
226
+
227
+ switch (stmt.type) {
228
+ case 'ImportDeclaration':
229
+ if (stmt.importKind === 'value' || stmt.importKind == null) {
230
+ return 'module';
231
+ }
232
+
233
+ break;
234
+
235
+ case 'ExportDefaultDeclaration':
236
+ return 'module';
237
+
238
+ case 'ExportNamedDeclaration':
239
+ case 'ExportAllDeclaration':
240
+ if (stmt.exportKind === 'value' || stmt.exportKind == null) {
241
+ return 'module';
242
+ }
243
+
244
+ break;
245
+ }
246
+ }
247
+
248
+ return 'script';
249
+ }
@@ -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;