@ohos-ports/hermes-parser 0.37.0-beta.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 (90) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/dist/HermesAST.js.flow +57 -0
  4. package/dist/HermesASTAdapter.js +192 -0
  5. package/dist/HermesASTAdapter.js.flow +189 -0
  6. package/dist/HermesParser.js +108 -0
  7. package/dist/HermesParser.js.flow +163 -0
  8. package/dist/HermesParserDecodeUTF8String.js +68 -0
  9. package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
  10. package/dist/HermesParserDeserializer.js +242 -0
  11. package/dist/HermesParserDeserializer.js.flow +260 -0
  12. package/dist/HermesParserNodeDeserializers.js +2594 -0
  13. package/dist/HermesParserNodeDeserializers.js.flow +16 -0
  14. package/dist/HermesParserWASM.js +6 -0
  15. package/dist/HermesParserWASM.js.flow +97 -0
  16. package/dist/HermesToESTreeAdapter.js +441 -0
  17. package/dist/HermesToESTreeAdapter.js.flow +423 -0
  18. package/dist/ParserOptions.js +20 -0
  19. package/dist/ParserOptions.js.flow +44 -0
  20. package/dist/babel/TransformESTreeToBabel.js +1197 -0
  21. package/dist/babel/TransformESTreeToBabel.js.flow +1349 -0
  22. package/dist/estree/StripFlowTypes.js +175 -0
  23. package/dist/estree/StripFlowTypes.js.flow +158 -0
  24. package/dist/estree/StripFlowTypesForBabel.js +215 -0
  25. package/dist/estree/StripFlowTypesForBabel.js.flow +216 -0
  26. package/dist/estree/TransformComponentSyntax.js +792 -0
  27. package/dist/estree/TransformComponentSyntax.js.flow +864 -0
  28. package/dist/estree/TransformEnumSyntax.js +106 -0
  29. package/dist/estree/TransformEnumSyntax.js.flow +125 -0
  30. package/dist/estree/TransformMatchSyntax.js +1083 -0
  31. package/dist/estree/TransformMatchSyntax.js.flow +991 -0
  32. package/dist/estree/TransformRecordSyntax.js +296 -0
  33. package/dist/estree/TransformRecordSyntax.js.flow +310 -0
  34. package/dist/generated/ESTreeVisitorKeys.js +233 -0
  35. package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
  36. package/dist/generated/ParserVisitorKeys.js +842 -0
  37. package/dist/generated/ParserVisitorKeys.js.flow +17 -0
  38. package/dist/getModuleDocblock.js +112 -0
  39. package/dist/getModuleDocblock.js.flow +118 -0
  40. package/dist/index.js +170 -0
  41. package/dist/index.js.flow +131 -0
  42. package/dist/src/HermesASTAdapter.js +192 -0
  43. package/dist/src/HermesParser.js +108 -0
  44. package/dist/src/HermesParserDecodeUTF8String.js +68 -0
  45. package/dist/src/HermesParserDeserializer.js +242 -0
  46. package/dist/src/HermesParserNodeDeserializers.js +2594 -0
  47. package/dist/src/HermesToESTreeAdapter.js +441 -0
  48. package/dist/src/ParserOptions.js +20 -0
  49. package/dist/src/babel/TransformESTreeToBabel.js +1197 -0
  50. package/dist/src/estree/StripFlowTypes.js +175 -0
  51. package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
  52. package/dist/src/estree/TransformComponentSyntax.js +792 -0
  53. package/dist/src/estree/TransformEnumSyntax.js +106 -0
  54. package/dist/src/estree/TransformMatchSyntax.js +1083 -0
  55. package/dist/src/estree/TransformRecordSyntax.js +296 -0
  56. package/dist/src/generated/ESTreeVisitorKeys.js +233 -0
  57. package/dist/src/generated/ParserVisitorKeys.js +842 -0
  58. package/dist/src/getModuleDocblock.js +112 -0
  59. package/dist/src/index.js +170 -0
  60. package/dist/src/transform/SimpleTransform.js +145 -0
  61. package/dist/src/transform/astArrayMutationHelpers.js +62 -0
  62. package/dist/src/transform/astNodeMutationHelpers.js +201 -0
  63. package/dist/src/traverse/SimpleTraverser.js +139 -0
  64. package/dist/src/traverse/getVisitorKeys.js +39 -0
  65. package/dist/src/utils/Builders.js +190 -0
  66. package/dist/src/utils/GenID.js +46 -0
  67. package/dist/src/utils/createSyntaxError.js +25 -0
  68. package/dist/src/utils/isReservedWord.js +62 -0
  69. package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
  70. package/dist/transform/SimpleTransform.js +145 -0
  71. package/dist/transform/SimpleTransform.js.flow +168 -0
  72. package/dist/transform/astArrayMutationHelpers.js +62 -0
  73. package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
  74. package/dist/transform/astNodeMutationHelpers.js +201 -0
  75. package/dist/transform/astNodeMutationHelpers.js.flow +252 -0
  76. package/dist/traverse/SimpleTraverser.js +139 -0
  77. package/dist/traverse/SimpleTraverser.js.flow +133 -0
  78. package/dist/traverse/getVisitorKeys.js +39 -0
  79. package/dist/traverse/getVisitorKeys.js.flow +38 -0
  80. package/dist/utils/Builders.js +190 -0
  81. package/dist/utils/Builders.js.flow +218 -0
  82. package/dist/utils/GenID.js +46 -0
  83. package/dist/utils/GenID.js.flow +39 -0
  84. package/dist/utils/createSyntaxError.js +25 -0
  85. package/dist/utils/createSyntaxError.js.flow +24 -0
  86. package/dist/utils/isReservedWord.js +62 -0
  87. package/dist/utils/isReservedWord.js.flow +57 -0
  88. package/dist/utils/mutateESTreeASTForPrettier.js +127 -0
  89. package/dist/utils/mutateESTreeASTForPrettier.js.flow +130 -0
  90. package/package.json +30 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # hermes-parser
2
+ A JavaScript parser built from the Hermes engine's parser compiled to WebAssembly. Can parse ES6, Flow, and JSX syntax.
3
+
4
+ ## API
5
+ The Hermes parser exposes a single `parse(code, [options])` function, where `code` is the source code to parse as a string, and `options` is an optional object that may contain the following properties:
6
+ - **babel**: `boolean`, defaults to `false`. If `true`, output an AST conforming to Babel's AST format. If `false`, output an AST conforming to the ESTree AST format.
7
+ - **allowReturnOutsideFunction**: `boolean`, defaults to `false`. If `true`, do not error on return statements found outside functions.
8
+ - **flow**: `"all"` or `"detect"`, defaults to `"detect"`. If `"detect"`, only parse syntax as Flow syntax where it is ambiguous whether it is a Flow feature or regular JavaScript when the `@flow` pragma is present in the file. Otherwise if `"all"`, always parse ambiguous syntax as Flow syntax regardless of the presence of an `@flow` pragma. For example `foo<T>(x)` in a file without an `@flow` pragma will be parsed as two comparisons if set to `"detect"`, otherwise if set to `"all"` or the `@flow` pragma is included it will be parsed as a call expression with a type argument.
9
+ - **sourceFilename**: `string`, defaults to `null`. The filename corresponding to the code that is to be parsed. If non-null, the filename will be added to all source locations in the output AST.
10
+ - **sourceType**: `"module"`, `"script"`, or `"unambiguous"` (default). If `"unambiguous"`, source type will be automatically detected and set to `"module"` if any ES6 imports or exports are present in the code, otherwise source type will be set to `"script"`.
11
+ - **tokens**: `boolean`, defaults to `false`. If `true`, add all tokens to a `tokens` property on the root node.
12
+ - **transformOptions**: `object`. Options to be supplied to relevant transforms.
13
+ - `TransformEnumSyntax`
14
+ - `enable`: `boolean` - Whether to enable the transform. By default, `false`.
15
+ - `getRuntime` (optional): `() => Expression` - The expression which should be a reference to the Flow Enums runtime. By default, is `require('flow-enums-runtime')`.
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ // flowlint unclear-type:off
12
+
13
+ export type HermesPosition = {
14
+ /** >= 1 */
15
+ readonly line: number,
16
+ /** >= 0 */
17
+ readonly column: number,
18
+ };
19
+ export type HermesSourceLocation = {
20
+ start?: HermesPosition,
21
+ end?: HermesPosition,
22
+ rangeStart?: number,
23
+ rangeEnd?: number,
24
+ };
25
+ export type HermesNode = {
26
+ type: string,
27
+ [string]: any,
28
+ };
29
+ export type HermesToken = {
30
+ type:
31
+ | 'Boolean'
32
+ | 'Identifier'
33
+ | 'Keyword'
34
+ | 'Null'
35
+ | 'Numeric'
36
+ | 'BigInt'
37
+ | 'Punctuator'
38
+ | 'String'
39
+ | 'RegularExpression'
40
+ | 'Template'
41
+ | 'JSXText',
42
+ loc: HermesSourceLocation,
43
+ value: ?string,
44
+ };
45
+ export type HermesComment = {
46
+ type: 'CommentLine' | 'CommentBlock' | 'InterpreterDirective',
47
+ loc: HermesSourceLocation,
48
+ value: ?string,
49
+ };
50
+ export type HermesProgram = {
51
+ type: 'Program',
52
+ loc: HermesSourceLocation,
53
+ body: Array<?HermesNode>,
54
+ comments: Array<HermesComment>,
55
+ tokens?: Array<HermesToken>,
56
+ interpreter?: ?HermesComment,
57
+ };
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _ParserVisitorKeys = require("./generated/ParserVisitorKeys");
9
+
10
+ /**
11
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
12
+ *
13
+ * This source code is licensed under the MIT license found in the
14
+ * LICENSE file in the root directory of this source tree.
15
+ *
16
+ * strict
17
+ * @format
18
+ */
19
+
20
+ /**
21
+ * The base class for transforming the Hermes AST to the desired output format.
22
+ * Extended by concrete adapters which output an ESTree or Babel AST.
23
+ */
24
+ class HermesASTAdapter {
25
+ constructor(options) {
26
+ this.sourceFilename = void 0;
27
+ this.sourceType = void 0;
28
+ this.sourceFilename = options.sourceFilename;
29
+ this.sourceType = options.sourceType;
30
+ }
31
+ /**
32
+ * Transform the input Hermes AST to the desired output format.
33
+ * This modifies the input AST in place instead of constructing a new AST.
34
+ */
35
+
36
+
37
+ transform(program) {
38
+ // Comments are not traversed via visitor keys
39
+ const comments = program.comments;
40
+
41
+ for (let i = 0; i < comments.length; i++) {
42
+ const comment = comments[i];
43
+ this.fixSourceLocation(comment);
44
+ comments[i] = this.mapComment(comment);
45
+ } // The first comment may be an interpreter directive and is stored directly on the program node
46
+
47
+
48
+ program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
49
+
50
+ const tokens = program.tokens;
51
+
52
+ if (tokens) {
53
+ for (let i = 0; i < tokens.length; i++) {
54
+ this.fixSourceLocation(tokens[i]);
55
+ }
56
+ }
57
+
58
+ const resultNode = this.mapNode(program);
59
+
60
+ if (resultNode.type !== 'Program') {
61
+ throw new Error(`HermesToESTreeAdapter: Must return a Program node, instead of "${resultNode.type}". `);
62
+ } // $FlowExpectedError[incompatible-type] We know this is a program at this point.
63
+
64
+
65
+ return resultNode;
66
+ }
67
+ /**
68
+ * Transform a Hermes AST node to the output AST format.
69
+ *
70
+ * This may modify the input node in-place and return that same node, or a completely
71
+ * new node may be constructed and returned. Overriden in child classes.
72
+ */
73
+
74
+
75
+ mapNode(_node) {
76
+ throw new Error('Implemented in subclasses');
77
+ }
78
+
79
+ mapNodeDefault(node) {
80
+ const visitorKeys = _ParserVisitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
81
+
82
+ for (const key in visitorKeys) {
83
+ const childType = visitorKeys[key];
84
+
85
+ if (childType === _ParserVisitorKeys.NODE_CHILD) {
86
+ const child = node[key];
87
+
88
+ if (child != null) {
89
+ node[key] = this.mapNode(child);
90
+ }
91
+ } else if (childType === _ParserVisitorKeys.NODE_LIST_CHILD) {
92
+ const children = node[key];
93
+
94
+ for (let i = 0; i < children.length; i++) {
95
+ const child = children[i];
96
+
97
+ if (child != null) {
98
+ children[i] = this.mapNode(child);
99
+ }
100
+ }
101
+ }
102
+ }
103
+
104
+ return node;
105
+ }
106
+ /**
107
+ * Update the source location for this node depending on the output AST format.
108
+ * This can modify the input node in-place. Overriden in child classes.
109
+ */
110
+
111
+
112
+ fixSourceLocation(_node) {
113
+ throw new Error('Implemented in subclasses');
114
+ }
115
+
116
+ getSourceType() {
117
+ var _this$sourceType;
118
+
119
+ return (_this$sourceType = this.sourceType) != null ? _this$sourceType : 'script';
120
+ }
121
+
122
+ setModuleSourceType() {
123
+ if (this.sourceType == null) {
124
+ this.sourceType = 'module';
125
+ }
126
+ }
127
+
128
+ mapComment(node) {
129
+ return node;
130
+ }
131
+
132
+ mapEmpty(_node) {
133
+ // $FlowExpectedError[incompatible-type]
134
+ return null;
135
+ }
136
+
137
+ mapImportDeclaration(node) {
138
+ if (node.importKind === 'value') {
139
+ this.setModuleSourceType();
140
+ }
141
+
142
+ return this.mapNodeDefault(node);
143
+ }
144
+
145
+ mapImportSpecifier(node) {
146
+ if (node.importKind === 'value') {
147
+ node.importKind = null;
148
+ }
149
+
150
+ return this.mapNodeDefault(node);
151
+ }
152
+
153
+ mapExportDefaultDeclaration(node) {
154
+ this.setModuleSourceType();
155
+ return this.mapNodeDefault(node);
156
+ }
157
+
158
+ mapExportNamedDeclaration(node) {
159
+ if (node.exportKind === 'value') {
160
+ this.setModuleSourceType();
161
+ }
162
+
163
+ return this.mapNodeDefault(node);
164
+ }
165
+
166
+ mapExportAllDeclaration(node) {
167
+ if (node.exportKind === 'value') {
168
+ this.setModuleSourceType();
169
+ }
170
+
171
+ return this.mapNodeDefault(node);
172
+ }
173
+
174
+ formatError(node, message) {
175
+ return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
176
+ }
177
+
178
+ getBigIntLiteralValue(bigintString) {
179
+ const bigint = bigintString // estree spec is to not have a trailing `n` on this property
180
+ // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
181
+ .replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
182
+ .replaceAll('_', '');
183
+ return {
184
+ bigint,
185
+ // coerce the string to a bigint value if supported by the environment
186
+ value: typeof BigInt === 'function' ? BigInt(bigint) : null
187
+ };
188
+ }
189
+
190
+ }
191
+
192
+ exports.default = HermesASTAdapter;
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ * @format
9
+ */
10
+
11
+ import type {Program} from 'hermes-estree';
12
+ import type {HermesNode} from './HermesAST';
13
+ import type {ParserOptions} from './ParserOptions';
14
+
15
+ import {
16
+ HERMES_AST_VISITOR_KEYS,
17
+ NODE_CHILD,
18
+ NODE_LIST_CHILD,
19
+ } from './generated/ParserVisitorKeys';
20
+
21
+ /**
22
+ * The base class for transforming the Hermes AST to the desired output format.
23
+ * Extended by concrete adapters which output an ESTree or Babel AST.
24
+ */
25
+ export default class HermesASTAdapter {
26
+ sourceFilename: ParserOptions['sourceFilename'];
27
+ sourceType: ParserOptions['sourceType'];
28
+
29
+ constructor(options: ParserOptions) {
30
+ this.sourceFilename = options.sourceFilename;
31
+ this.sourceType = options.sourceType;
32
+ }
33
+
34
+ /**
35
+ * Transform the input Hermes AST to the desired output format.
36
+ * This modifies the input AST in place instead of constructing a new AST.
37
+ */
38
+ transform(program: HermesNode): Program {
39
+ // Comments are not traversed via visitor keys
40
+ const comments = program.comments;
41
+ for (let i = 0; i < comments.length; i++) {
42
+ const comment = comments[i];
43
+ this.fixSourceLocation(comment);
44
+ comments[i] = this.mapComment(comment);
45
+ }
46
+
47
+ // The first comment may be an interpreter directive and is stored directly on the program node
48
+ program.interpreter =
49
+ comments.length > 0 && comments[0].type === 'InterpreterDirective'
50
+ ? comments.shift()
51
+ : null;
52
+
53
+ // Tokens are not traversed via visitor keys
54
+ const tokens = program.tokens;
55
+ if (tokens) {
56
+ for (let i = 0; i < tokens.length; i++) {
57
+ this.fixSourceLocation(tokens[i]);
58
+ }
59
+ }
60
+
61
+ const resultNode = this.mapNode(program);
62
+ if (resultNode.type !== 'Program') {
63
+ throw new Error(
64
+ `HermesToESTreeAdapter: Must return a Program node, instead of "${resultNode.type}". `,
65
+ );
66
+ }
67
+
68
+ // $FlowExpectedError[incompatible-type] We know this is a program at this point.
69
+ return resultNode;
70
+ }
71
+
72
+ /**
73
+ * Transform a Hermes AST node to the output AST format.
74
+ *
75
+ * This may modify the input node in-place and return that same node, or a completely
76
+ * new node may be constructed and returned. Overriden in child classes.
77
+ */
78
+ mapNode(_node: HermesNode): HermesNode {
79
+ throw new Error('Implemented in subclasses');
80
+ }
81
+
82
+ mapNodeDefault(node: HermesNode): HermesNode {
83
+ const visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
84
+ for (const key in visitorKeys) {
85
+ const childType = visitorKeys[key];
86
+ if (childType === NODE_CHILD) {
87
+ const child = node[key];
88
+ if (child != null) {
89
+ node[key] = this.mapNode(child);
90
+ }
91
+ } else if (childType === NODE_LIST_CHILD) {
92
+ const children = node[key];
93
+ for (let i = 0; i < children.length; i++) {
94
+ const child = children[i];
95
+ if (child != null) {
96
+ children[i] = this.mapNode(child);
97
+ }
98
+ }
99
+ }
100
+ }
101
+
102
+ return node;
103
+ }
104
+
105
+ /**
106
+ * Update the source location for this node depending on the output AST format.
107
+ * This can modify the input node in-place. Overriden in child classes.
108
+ */
109
+ fixSourceLocation(_node: HermesNode): void {
110
+ throw new Error('Implemented in subclasses');
111
+ }
112
+
113
+ getSourceType(): ParserOptions['sourceType'] {
114
+ return this.sourceType ?? 'script';
115
+ }
116
+
117
+ setModuleSourceType(): void {
118
+ if (this.sourceType == null) {
119
+ this.sourceType = 'module';
120
+ }
121
+ }
122
+
123
+ mapComment(node: HermesNode): HermesNode {
124
+ return node;
125
+ }
126
+
127
+ mapEmpty(_node: HermesNode): HermesNode {
128
+ // $FlowExpectedError[incompatible-type]
129
+ return null;
130
+ }
131
+
132
+ mapImportDeclaration(node: HermesNode): HermesNode {
133
+ if (node.importKind === 'value') {
134
+ this.setModuleSourceType();
135
+ }
136
+
137
+ return this.mapNodeDefault(node);
138
+ }
139
+
140
+ mapImportSpecifier(node: HermesNode): HermesNode {
141
+ if (node.importKind === 'value') {
142
+ node.importKind = null;
143
+ }
144
+
145
+ return this.mapNodeDefault(node);
146
+ }
147
+
148
+ mapExportDefaultDeclaration(node: HermesNode): HermesNode {
149
+ this.setModuleSourceType();
150
+ return this.mapNodeDefault(node);
151
+ }
152
+
153
+ mapExportNamedDeclaration(node: HermesNode): HermesNode {
154
+ if (node.exportKind === 'value') {
155
+ this.setModuleSourceType();
156
+ }
157
+
158
+ return this.mapNodeDefault(node);
159
+ }
160
+
161
+ mapExportAllDeclaration(node: HermesNode): HermesNode {
162
+ if (node.exportKind === 'value') {
163
+ this.setModuleSourceType();
164
+ }
165
+
166
+ return this.mapNodeDefault(node);
167
+ }
168
+
169
+ formatError(node: HermesNode, message: string): string {
170
+ return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
171
+ }
172
+
173
+ getBigIntLiteralValue(bigintString: string): {
174
+ bigint: string,
175
+ value: bigint | null,
176
+ } {
177
+ const bigint = bigintString
178
+ // estree spec is to not have a trailing `n` on this property
179
+ // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
180
+ .replace(/n$/, '')
181
+ // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
182
+ .replaceAll('_', '');
183
+ return {
184
+ bigint,
185
+ // coerce the string to a bigint value if supported by the environment
186
+ value: typeof BigInt === 'function' ? BigInt(bigint) : null,
187
+ };
188
+ }
189
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * strict
8
+ * @format
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.parse = parse;
16
+
17
+ var _HermesParserDeserializer = _interopRequireDefault(require("./HermesParserDeserializer"));
18
+
19
+ var _HermesParserWASM = _interopRequireDefault(require("./HermesParserWASM"));
20
+
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
+
23
+ let HermesParserWASM;
24
+ let hermesParse;
25
+ let hermesParseResult_free;
26
+ let hermesParseResult_getError;
27
+ let hermesParseResult_getErrorLine;
28
+ let hermesParseResult_getErrorColumn;
29
+ let hermesParseResult_getProgramBuffer;
30
+ let hermesParseResult_getPositionBuffer;
31
+ let hermesParseResult_getPositionBufferSize;
32
+ /**
33
+ * Init the WASM wrapper code generated by `emscripten` to preparse the
34
+ * HermesParser WASM code.
35
+ */
36
+
37
+ function initHermesParserWASM() {
38
+ if (HermesParserWASM != null) {
39
+ return;
40
+ }
41
+
42
+ HermesParserWASM = (0, _HermesParserWASM.default)({
43
+ /**
44
+ * The emscripten version of `quit` unconditionally assigns the `status` to
45
+ * `process.exitCode` which overrides any pre-existing code that has been
46
+ * set, even if it is non zero. For our use case we never want an
47
+ * `exitCode` to be set so this override removes that functionality.
48
+ */
49
+ quit(_status, toThrow) {
50
+ throw toThrow;
51
+ }
52
+
53
+ });
54
+ hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']);
55
+ hermesParseResult_free = HermesParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
56
+ hermesParseResult_getError = HermesParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
57
+ hermesParseResult_getErrorLine = HermesParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
58
+ hermesParseResult_getErrorColumn = HermesParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
59
+ hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
60
+ hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
61
+ hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']);
62
+ } // Copy a string into the WASM heap and null-terminate
63
+
64
+
65
+ function copyToHeap(buffer, addr) {
66
+ HermesParserWASM.HEAP8.set(buffer, addr);
67
+ HermesParserWASM.HEAP8[addr + buffer.length] = 0;
68
+ }
69
+
70
+ function parse(source, options) {
71
+ initHermesParserWASM(); // Allocate space on heap for source text
72
+
73
+ const sourceBuffer = Buffer.from(source, 'utf8');
74
+
75
+ const sourceAddr = HermesParserWASM._malloc(sourceBuffer.length + 1);
76
+
77
+ if (!sourceAddr) {
78
+ throw new Error('Parser out of memory');
79
+ }
80
+
81
+ try {
82
+ // Copy source text onto WASM heap
83
+ copyToHeap(sourceBuffer, sourceAddr);
84
+ const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.enableExperimentalComponentSyntax, options.enableExperimentalFlowMatchSyntax, options.enableExperimentalFlowRecordSyntax, options.tokens, options.allowReturnOutsideFunction);
85
+
86
+ try {
87
+ // Extract and throw error from parse result if parsing failed
88
+ const err = hermesParseResult_getError(parseResult);
89
+
90
+ if (err) {
91
+ const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
92
+
93
+ syntaxError.loc = {
94
+ line: hermesParseResult_getErrorLine(parseResult),
95
+ column: hermesParseResult_getErrorColumn(parseResult)
96
+ };
97
+ throw syntaxError;
98
+ }
99
+
100
+ const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), HermesParserWASM, options);
101
+ return deserializer.deserialize();
102
+ } finally {
103
+ hermesParseResult_free(parseResult);
104
+ }
105
+ } finally {
106
+ HermesParserWASM._free(sourceAddr);
107
+ }
108
+ }