hermes-estree 0.32.1 → 0.33.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/dist/generated/HermesESTreeSelectorTypes.js.flow +262 -27
  2. package/dist/generated/predicates.js +120 -0
  3. package/dist/generated/predicates.js.flow +72 -0
  4. package/dist/predicates.js +2 -4
  5. package/dist/predicates.js.flow +5 -4
  6. package/dist/src/generated/predicates.js +120 -0
  7. package/dist/src/predicates.js +2 -4
  8. package/dist/types.js.flow +103 -11
  9. package/package.json +1 -1
  10. package/dist/HermesAST.js.flow +0 -57
  11. package/dist/HermesASTAdapter.js +0 -192
  12. package/dist/HermesASTAdapter.js.flow +0 -189
  13. package/dist/HermesParser.js +0 -108
  14. package/dist/HermesParser.js.flow +0 -161
  15. package/dist/HermesParserDecodeUTF8String.js +0 -68
  16. package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
  17. package/dist/HermesParserDeserializer.js +0 -242
  18. package/dist/HermesParserDeserializer.js.flow +0 -260
  19. package/dist/HermesParserNodeDeserializers.js +0 -2477
  20. package/dist/HermesParserNodeDeserializers.js.flow +0 -16
  21. package/dist/HermesParserWASM.js +0 -6
  22. package/dist/HermesParserWASM.js.flow +0 -87
  23. package/dist/HermesToESTreeAdapter.js +0 -439
  24. package/dist/HermesToESTreeAdapter.js.flow +0 -421
  25. package/dist/ParserOptions.js +0 -18
  26. package/dist/ParserOptions.js.flow +0 -41
  27. package/dist/babel/TransformESTreeToBabel.js +0 -1123
  28. package/dist/babel/TransformESTreeToBabel.js.flow +0 -1277
  29. package/dist/estree/StripFlowTypes.js +0 -175
  30. package/dist/estree/StripFlowTypes.js.flow +0 -158
  31. package/dist/estree/StripFlowTypesForBabel.js +0 -215
  32. package/dist/estree/StripFlowTypesForBabel.js.flow +0 -216
  33. package/dist/estree/TransformComponentSyntax.js +0 -788
  34. package/dist/estree/TransformComponentSyntax.js.flow +0 -864
  35. package/dist/estree/TransformEnumSyntax.js +0 -106
  36. package/dist/estree/TransformEnumSyntax.js.flow +0 -125
  37. package/dist/estree/TransformMatchSyntax.js +0 -1006
  38. package/dist/estree/TransformMatchSyntax.js.flow +0 -912
  39. package/dist/generated/ESTreeVisitorKeys.js +0 -220
  40. package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
  41. package/dist/generated/ParserVisitorKeys.js +0 -794
  42. package/dist/generated/ParserVisitorKeys.js.flow +0 -17
  43. package/dist/getModuleDocblock.js +0 -112
  44. package/dist/getModuleDocblock.js.flow +0 -118
  45. package/dist/src/HermesASTAdapter.js +0 -192
  46. package/dist/src/HermesParser.js +0 -108
  47. package/dist/src/HermesParserDecodeUTF8String.js +0 -68
  48. package/dist/src/HermesParserDeserializer.js +0 -242
  49. package/dist/src/HermesParserNodeDeserializers.js +0 -2477
  50. package/dist/src/HermesToESTreeAdapter.js +0 -439
  51. package/dist/src/ParserOptions.js +0 -18
  52. package/dist/src/babel/TransformESTreeToBabel.js +0 -1123
  53. package/dist/src/estree/StripFlowTypes.js +0 -175
  54. package/dist/src/estree/StripFlowTypesForBabel.js +0 -215
  55. package/dist/src/estree/TransformComponentSyntax.js +0 -788
  56. package/dist/src/estree/TransformEnumSyntax.js +0 -106
  57. package/dist/src/estree/TransformMatchSyntax.js +0 -1006
  58. package/dist/src/generated/ESTreeVisitorKeys.js +0 -220
  59. package/dist/src/generated/ParserVisitorKeys.js +0 -794
  60. package/dist/src/getModuleDocblock.js +0 -112
  61. package/dist/src/transform/SimpleTransform.js +0 -136
  62. package/dist/src/transform/astArrayMutationHelpers.js +0 -62
  63. package/dist/src/transform/astNodeMutationHelpers.js +0 -200
  64. package/dist/src/traverse/SimpleTraverser.js +0 -137
  65. package/dist/src/traverse/getVisitorKeys.js +0 -37
  66. package/dist/src/utils/Builders.js +0 -191
  67. package/dist/src/utils/GenID.js +0 -41
  68. package/dist/src/utils/createSyntaxError.js +0 -25
  69. package/dist/src/utils/mutateESTreeASTForPrettier.js +0 -127
  70. package/dist/transform/SimpleTransform.js +0 -136
  71. package/dist/transform/SimpleTransform.js.flow +0 -169
  72. package/dist/transform/astArrayMutationHelpers.js +0 -62
  73. package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
  74. package/dist/transform/astNodeMutationHelpers.js +0 -200
  75. package/dist/transform/astNodeMutationHelpers.js.flow +0 -246
  76. package/dist/traverse/SimpleTraverser.js +0 -137
  77. package/dist/traverse/SimpleTraverser.js.flow +0 -133
  78. package/dist/traverse/getVisitorKeys.js +0 -37
  79. package/dist/traverse/getVisitorKeys.js.flow +0 -36
  80. package/dist/utils/Builders.js +0 -191
  81. package/dist/utils/Builders.js.flow +0 -218
  82. package/dist/utils/GenID.js +0 -41
  83. package/dist/utils/GenID.js.flow +0 -38
  84. package/dist/utils/createSyntaxError.js +0 -25
  85. package/dist/utils/createSyntaxError.js.flow +0 -24
  86. package/dist/utils/mutateESTreeASTForPrettier.js +0 -127
  87. package/dist/utils/mutateESTreeASTForPrettier.js.flow +0 -130
@@ -207,8 +207,17 @@ export type ESNode =
207
207
  | MatchPattern
208
208
  | MatchRestPattern
209
209
  | MatchObjectPatternProperty
210
+ | MatchInstanceObjectPattern
210
211
  | MatchExpressionCase
211
212
  | MatchStatementCase
213
+ // Records
214
+ | RecordDeclaration
215
+ | RecordDeclarationImplements
216
+ | RecordDeclarationBody
217
+ | RecordDeclarationProperty
218
+ | RecordDeclarationStaticProperty
219
+ | RecordExpression
220
+ | RecordExpressionProperties
212
221
  // JSX
213
222
  | JSXNode;
214
223
 
@@ -276,7 +285,8 @@ export type Statement =
276
285
  | VariableDeclaration
277
286
  | WhileStatement
278
287
  | WithStatement
279
- | MatchStatement;
288
+ | MatchStatement
289
+ | RecordDeclaration;
280
290
 
281
291
  // nodes that can be the direct parent of a statement
282
292
  export type StatementParentSingle =
@@ -486,6 +496,7 @@ export type Expression =
486
496
  | AsExpression
487
497
  | AsConstExpression
488
498
  | MatchExpression
499
+ | RecordExpression
489
500
  | JSXFragment
490
501
  | JSXElement;
491
502
 
@@ -510,19 +521,25 @@ export interface ObjectExpression extends BaseNode {
510
521
  // and object desturcturing properties.
511
522
  export type Property = ObjectProperty | DestructuringObjectProperty;
512
523
 
524
+ export type ObjectPropertyKey =
525
+ | Identifier
526
+ | StringLiteral
527
+ | NumericLiteral
528
+ | BigIntLiteral;
529
+
513
530
  export type ObjectProperty =
514
531
  | ObjectPropertyWithNonShorthandStaticName
515
532
  | ObjectPropertyWithShorthandStaticName
516
533
  | ObjectPropertyWithComputedName;
517
534
  interface ObjectPropertyBase extends BaseNode {
518
- +parent: ObjectExpression | ObjectPattern;
535
+ +parent: ObjectExpression | ObjectPattern | RecordExpressionProperties;
519
536
  }
520
537
  export interface ObjectPropertyWithNonShorthandStaticName
521
538
  extends ObjectPropertyBase {
522
539
  +type: 'Property';
523
540
  +computed: false;
524
541
  // non-computed, non-shorthand names are constrained significantly
525
- +key: Identifier | StringLiteral | NumericLiteral;
542
+ +key: ObjectPropertyKey;
526
543
  +value: Expression;
527
544
  +kind: 'init' | 'get' | 'set';
528
545
  +method: boolean;
@@ -568,7 +585,7 @@ export interface DestructuringObjectPropertyWithNonShorthandStaticName
568
585
  +type: 'Property';
569
586
  +computed: false;
570
587
  // non-computed, non-shorthand names are constrained significantly
571
- +key: Identifier | StringLiteral | NumericLiteral;
588
+ +key: ObjectPropertyKey;
572
589
  // destructuring properties cannot have any value
573
590
  +value: DestructuringPattern;
574
591
  +shorthand: false;
@@ -914,7 +931,7 @@ interface BaseClass extends BaseNode {
914
931
  +body: ClassBody;
915
932
 
916
933
  +typeParameters: null | TypeParameterDeclaration;
917
- +superTypeParameters: null | TypeParameterInstantiation;
934
+ +superTypeArguments: null | TypeParameterInstantiation;
918
935
  +implements: $ReadOnlyArray<ClassImplements>;
919
936
  +decorators: $ReadOnlyArray<Decorator>;
920
937
  }
@@ -925,8 +942,7 @@ export type PropertyName =
925
942
  export type ClassPropertyNameComputed = Expression;
926
943
  export type ClassPropertyNameNonComputed =
927
944
  | PrivateIdentifier
928
- | Identifier
929
- | StringLiteral;
945
+ | ObjectPropertyKey;
930
946
 
931
947
  export type ClassMember = PropertyDefinition | MethodDefinition | StaticBlock;
932
948
  export type ClassMemberWithNonComputedName =
@@ -1090,7 +1106,8 @@ export type NamedDeclaration =
1090
1106
  | TypeAlias
1091
1107
  | OpaqueType
1092
1108
  | InterfaceDeclaration
1093
- | EnumDeclaration;
1109
+ | EnumDeclaration
1110
+ | RecordDeclaration;
1094
1111
 
1095
1112
  interface ExportNamedDeclarationBase extends BaseNode {
1096
1113
  +type: 'ExportNamedDeclaration';
@@ -1156,6 +1173,9 @@ export type TypeAnnotationType =
1156
1173
  | ThisTypeAnnotation
1157
1174
  | MixedTypeAnnotation
1158
1175
  | VoidTypeAnnotation
1176
+ | UnknownTypeAnnotation
1177
+ | NeverTypeAnnotation
1178
+ | UndefinedTypeAnnotation
1159
1179
  | StringLiteralTypeAnnotation
1160
1180
  | NumberLiteralTypeAnnotation
1161
1181
  | BigIntLiteralTypeAnnotation
@@ -1250,6 +1270,15 @@ export interface MixedTypeAnnotation extends BaseNode {
1250
1270
  export interface VoidTypeAnnotation extends BaseNode {
1251
1271
  +type: 'VoidTypeAnnotation';
1252
1272
  }
1273
+ export interface UnknownTypeAnnotation extends BaseNode {
1274
+ +type: 'UnknownTypeAnnotation';
1275
+ }
1276
+ export interface NeverTypeAnnotation extends BaseNode {
1277
+ +type: 'NeverTypeAnnotation';
1278
+ }
1279
+ export interface UndefinedTypeAnnotation extends BaseNode {
1280
+ +type: 'UndefinedTypeAnnotation';
1281
+ }
1253
1282
  export interface StringLiteralTypeAnnotation extends BaseNode {
1254
1283
  +type: 'StringLiteralTypeAnnotation';
1255
1284
  +value: string;
@@ -1308,7 +1337,7 @@ export interface KeyofTypeAnnotation extends BaseNode {
1308
1337
  }
1309
1338
  export interface TupleTypeAnnotation extends BaseNode {
1310
1339
  +type: 'TupleTypeAnnotation';
1311
- +types: $ReadOnlyArray<TypeAnnotationType>;
1340
+ +elementTypes: $ReadOnlyArray<TypeAnnotationType>;
1312
1341
  +inexact: boolean;
1313
1342
  }
1314
1343
  export interface TupleTypeSpreadElement extends BaseNode {
@@ -1457,7 +1486,7 @@ export interface ObjectTypeAnnotation extends BaseNode {
1457
1486
  }
1458
1487
  interface ObjectTypePropertyBase extends BaseNode {
1459
1488
  +type: 'ObjectTypeProperty';
1460
- +key: Identifier | StringLiteral;
1489
+ +key: ObjectPropertyKey;
1461
1490
  +value: TypeAnnotationType;
1462
1491
  +method: boolean;
1463
1492
  +optional: boolean;
@@ -2054,6 +2083,7 @@ export type MatchPattern =
2054
2083
  | MatchMemberPattern
2055
2084
  | MatchBindingPattern
2056
2085
  | MatchObjectPattern
2086
+ | MatchInstancePattern
2057
2087
  | MatchArrayPattern;
2058
2088
 
2059
2089
  export interface MatchOrPattern extends BaseNode {
@@ -2098,10 +2128,20 @@ export interface MatchObjectPattern extends BaseNode {
2098
2128
  }
2099
2129
  export interface MatchObjectPatternProperty extends BaseNode {
2100
2130
  +type: 'MatchObjectPatternProperty';
2101
- +key: Identifier | StringLiteral | NumericLiteral | BigIntLiteral;
2131
+ +key: ObjectPropertyKey;
2102
2132
  +pattern: MatchPattern;
2103
2133
  +shorthand: boolean;
2104
2134
  }
2135
+ export interface MatchInstancePattern extends BaseNode {
2136
+ +type: 'MatchInstancePattern';
2137
+ +targetConstructor: MatchIdentifierPattern | MatchMemberPattern;
2138
+ +properties: MatchInstanceObjectPattern;
2139
+ }
2140
+ export interface MatchInstanceObjectPattern extends BaseNode {
2141
+ +type: 'MatchInstanceObjectPattern';
2142
+ +properties: $ReadOnlyArray<MatchObjectPatternProperty>;
2143
+ +rest: MatchRestPattern | null;
2144
+ }
2105
2145
  export interface MatchArrayPattern extends BaseNode {
2106
2146
  +type: 'MatchArrayPattern';
2107
2147
  +elements: $ReadOnlyArray<MatchPattern>;
@@ -2112,6 +2152,58 @@ export interface MatchRestPattern extends BaseNode {
2112
2152
  +argument: MatchBindingPattern | null;
2113
2153
  }
2114
2154
 
2155
+ /***********
2156
+ * Records *
2157
+ ***********/
2158
+ export interface RecordDeclaration extends BaseNode {
2159
+ +type: 'RecordDeclaration';
2160
+ +id: Identifier;
2161
+ +typeParameters: TypeParameterDeclaration | null;
2162
+ +implements: $ReadOnlyArray<RecordDeclarationImplements>;
2163
+ +body: RecordDeclarationBody;
2164
+ }
2165
+
2166
+ export interface RecordDeclarationImplements extends BaseNode {
2167
+ +type: 'RecordDeclarationImplements';
2168
+ +id: Identifier;
2169
+ +typeArguments: TypeParameterInstantiation | null;
2170
+ }
2171
+
2172
+ export interface RecordDeclarationBody extends BaseNode {
2173
+ +type: 'RecordDeclarationBody';
2174
+ +elements: $ReadOnlyArray<
2175
+ | RecordDeclarationProperty
2176
+ | RecordDeclarationStaticProperty
2177
+ | MethodDefinition,
2178
+ >;
2179
+ }
2180
+
2181
+ export interface RecordDeclarationProperty extends BaseNode {
2182
+ +type: 'RecordDeclarationProperty';
2183
+ +key: ObjectPropertyKey;
2184
+ +typeAnnotation: TypeAnnotation;
2185
+ +defaultValue: Expression | null;
2186
+ }
2187
+
2188
+ export interface RecordDeclarationStaticProperty extends BaseNode {
2189
+ +type: 'RecordDeclarationStaticProperty';
2190
+ +key: ObjectPropertyKey;
2191
+ +typeAnnotation: TypeAnnotation;
2192
+ +value: Expression;
2193
+ }
2194
+
2195
+ export interface RecordExpression extends BaseNode {
2196
+ +type: 'RecordExpression';
2197
+ +recordConstructor: Expression;
2198
+ +typeArguments: TypeParameterInstantiation | null;
2199
+ +properties: RecordExpressionProperties;
2200
+ }
2201
+
2202
+ export interface RecordExpressionProperties extends BaseNode {
2203
+ +type: 'RecordExpressionProperties';
2204
+ +properties: $ReadOnlyArray<ObjectProperty | SpreadElement>;
2205
+ }
2206
+
2115
2207
  /******************************************************
2116
2208
  * Deprecated spec nodes awaiting migration by Hermes *
2117
2209
  ******************************************************/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-estree",
3
- "version": "0.32.1",
3
+ "version": "0.33.1",
4
4
  "description": "Flow types for the Flow-ESTree spec produced by the hermes parser",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -1,57 +0,0 @@
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
- +line: number,
16
- /** >= 0 */
17
- +column: number,
18
- };
19
- export type HermesSourceLocation = {
20
- start?: HermesPosition,
21
- end?: HermesPosition,
22
- rangeStart?: number,
23
- rangeEnd?: number,
24
- };
25
- export type HermesNode = {
26
- type: string,
27
- [string]: any,
28
- };
29
- export type HermesToken = {
30
- type:
31
- | 'Boolean'
32
- | 'Identifier'
33
- | 'Keyword'
34
- | 'Null'
35
- | 'Numeric'
36
- | 'BigInt'
37
- | 'Punctuator'
38
- | 'String'
39
- | 'RegularExpression'
40
- | 'Template'
41
- | 'JSXText',
42
- loc: HermesSourceLocation,
43
- value: ?string,
44
- };
45
- export type HermesComment = {
46
- type: 'CommentLine' | 'CommentBlock' | 'InterpreterDirective',
47
- loc: HermesSourceLocation,
48
- value: ?string,
49
- };
50
- export type HermesProgram = {
51
- type: 'Program',
52
- loc: HermesSourceLocation,
53
- body: Array<?HermesNode>,
54
- comments: Array<HermesComment>,
55
- tokens?: Array<HermesToken>,
56
- interpreter?: ?HermesComment,
57
- };
@@ -1,192 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _ParserVisitorKeys = require("./generated/ParserVisitorKeys");
9
-
10
- /**
11
- * Copyright (c) Meta Platforms, Inc. and affiliates.
12
- *
13
- * This source code is licensed under the MIT license found in the
14
- * LICENSE file in the root directory of this source tree.
15
- *
16
- *
17
- * @format
18
- */
19
-
20
- /**
21
- * The base class for transforming the Hermes AST to the desired output format.
22
- * Extended by concrete adapters which output an ESTree or Babel AST.
23
- */
24
- class HermesASTAdapter {
25
- constructor(options) {
26
- this.sourceFilename = void 0;
27
- this.sourceType = void 0;
28
- this.sourceFilename = options.sourceFilename;
29
- this.sourceType = options.sourceType;
30
- }
31
- /**
32
- * Transform the input Hermes AST to the desired output format.
33
- * This modifies the input AST in place instead of constructing a new AST.
34
- */
35
-
36
-
37
- transform(program) {
38
- // Comments are not traversed via visitor keys
39
- const comments = program.comments;
40
-
41
- for (let i = 0; i < comments.length; i++) {
42
- const comment = comments[i];
43
- this.fixSourceLocation(comment);
44
- comments[i] = this.mapComment(comment);
45
- } // The first comment may be an interpreter directive and is stored directly on the program node
46
-
47
-
48
- program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
49
-
50
- const tokens = program.tokens;
51
-
52
- if (tokens) {
53
- for (let i = 0; i < tokens.length; i++) {
54
- this.fixSourceLocation(tokens[i]);
55
- }
56
- }
57
-
58
- 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;
@@ -1,189 +0,0 @@
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
- }