hermes-parser 0.4.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +134 -146
- package/dist/HermesASTAdapter.js.flow +178 -0
- package/dist/HermesParser.js +39 -25
- package/dist/HermesParser.js.flow +123 -0
- package/dist/HermesParserDecodeUTF8String.js +17 -12
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +200 -207
- package/dist/HermesParserDeserializer.js.flow +261 -0
- package/dist/HermesParserNodeDeserializers.js +60 -28
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +2 -2
- package/dist/HermesParserWASM.js.flow +75 -0
- package/dist/HermesToBabelAdapter.js +331 -364
- package/dist/HermesToBabelAdapter.js.flow +383 -0
- package/dist/HermesToESTreeAdapter.js +198 -159
- package/dist/HermesToESTreeAdapter.js.flow +233 -0
- package/dist/ParserOptions.js.flow +18 -0
- package/dist/generated/visitor-keys.js +605 -0
- package/dist/generated/visitor-keys.js.flow +17 -0
- package/dist/index.js +32 -19
- package/dist/index.js.flow +72 -13
- package/package.json +7 -1
- package/dist/HermesParserVisitorKeys.js +0 -729
|
@@ -1,212 +1,251 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _HermesASTAdapter = _interopRequireDefault(require("./HermesASTAdapter"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
1
12
|
/**
|
|
2
|
-
* Copyright (c)
|
|
13
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
14
|
*
|
|
4
15
|
* This source code is licensed under the MIT license found in the
|
|
5
16
|
* LICENSE file in the root directory of this source tree.
|
|
6
17
|
*
|
|
18
|
+
*
|
|
7
19
|
* @format
|
|
8
20
|
*/
|
|
9
|
-
'use strict';
|
|
10
|
-
|
|
11
|
-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
12
21
|
|
|
13
|
-
|
|
22
|
+
/*
|
|
23
|
+
This class does some very "javascripty" things in the name of
|
|
24
|
+
performance which are ultimately impossible to soundly type.
|
|
25
|
+
|
|
26
|
+
So instead of adding strict types and a large number of suppression
|
|
27
|
+
comments, instead it is left untyped and subclasses are strictly
|
|
28
|
+
typed via a separate flow declaration file.
|
|
29
|
+
*/
|
|
30
|
+
class HermesToESTreeAdapter extends _HermesASTAdapter.default {
|
|
31
|
+
constructor(options, code) {
|
|
32
|
+
super(options);
|
|
33
|
+
this.code = void 0;
|
|
34
|
+
this.code = code;
|
|
35
|
+
}
|
|
14
36
|
|
|
15
|
-
|
|
37
|
+
fixSourceLocation(node) {
|
|
38
|
+
var _this$sourceFilename;
|
|
16
39
|
|
|
17
|
-
|
|
40
|
+
const loc = node.loc;
|
|
18
41
|
|
|
19
|
-
|
|
42
|
+
if (loc == null) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
20
45
|
|
|
21
|
-
|
|
46
|
+
node.loc = {
|
|
47
|
+
source: (_this$sourceFilename = this.sourceFilename) != null ? _this$sourceFilename : null,
|
|
48
|
+
start: loc.start,
|
|
49
|
+
end: loc.end
|
|
50
|
+
};
|
|
51
|
+
node.range = [loc.rangeStart, loc.rangeEnd];
|
|
52
|
+
}
|
|
22
53
|
|
|
23
|
-
|
|
54
|
+
mapNode(node) {
|
|
55
|
+
this.fixSourceLocation(node);
|
|
24
56
|
|
|
25
|
-
|
|
57
|
+
switch (node.type) {
|
|
58
|
+
case 'Program':
|
|
59
|
+
return this.mapProgram(node);
|
|
26
60
|
|
|
27
|
-
|
|
61
|
+
case 'NullLiteral':
|
|
62
|
+
return this.mapNullLiteral(node);
|
|
28
63
|
|
|
29
|
-
|
|
64
|
+
case 'BooleanLiteral':
|
|
65
|
+
case 'StringLiteral':
|
|
66
|
+
case 'NumericLiteral':
|
|
67
|
+
case 'JSXStringLiteral':
|
|
68
|
+
return this.mapSimpleLiteral(node);
|
|
30
69
|
|
|
31
|
-
|
|
70
|
+
case 'BigIntLiteral':
|
|
71
|
+
return this.mapBigIntLiteral(node);
|
|
32
72
|
|
|
33
|
-
|
|
73
|
+
case 'RegExpLiteral':
|
|
74
|
+
return this.mapRegExpLiteral(node);
|
|
34
75
|
|
|
35
|
-
|
|
36
|
-
|
|
76
|
+
case 'Empty':
|
|
77
|
+
return this.mapEmpty(node);
|
|
37
78
|
|
|
38
|
-
|
|
79
|
+
case 'TemplateElement':
|
|
80
|
+
return this.mapTemplateElement(node);
|
|
39
81
|
|
|
40
|
-
|
|
41
|
-
|
|
82
|
+
case 'BigIntLiteralTypeAnnotation':
|
|
83
|
+
return this.mapBigIntLiteralTypeAnnotation(node);
|
|
42
84
|
|
|
43
|
-
|
|
85
|
+
case 'GenericTypeAnnotation':
|
|
86
|
+
return this.mapGenericTypeAnnotation(node);
|
|
44
87
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return _this;
|
|
48
|
-
}
|
|
88
|
+
case 'ImportDeclaration':
|
|
89
|
+
return this.mapImportDeclaration(node);
|
|
49
90
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
value: function fixSourceLocation(node) {
|
|
53
|
-
var _this$sourceFilename;
|
|
91
|
+
case 'ImportSpecifier':
|
|
92
|
+
return this.mapImportSpecifier(node);
|
|
54
93
|
|
|
55
|
-
|
|
94
|
+
case 'ExportDefaultDeclaration':
|
|
95
|
+
return this.mapExportDefaultDeclaration(node);
|
|
56
96
|
|
|
57
|
-
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
97
|
+
case 'ExportNamedDeclaration':
|
|
98
|
+
return this.mapExportNamedDeclaration(node);
|
|
60
99
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
start: loc.start,
|
|
64
|
-
end: loc.end
|
|
65
|
-
};
|
|
66
|
-
node.range = [loc.rangeStart, loc.rangeEnd];
|
|
67
|
-
}
|
|
68
|
-
}, {
|
|
69
|
-
key: "mapNode",
|
|
70
|
-
value: function mapNode(node) {
|
|
71
|
-
this.fixSourceLocation(node);
|
|
100
|
+
case 'ExportAllDeclaration':
|
|
101
|
+
return this.mapExportAllDeclaration(node);
|
|
72
102
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
103
|
+
case 'PrivateName':
|
|
104
|
+
case 'ClassPrivateProperty':
|
|
105
|
+
return this.mapPrivateProperty(node);
|
|
76
106
|
|
|
77
|
-
|
|
78
|
-
|
|
107
|
+
case 'Property':
|
|
108
|
+
return this.mapProperty(node);
|
|
79
109
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
110
|
+
default:
|
|
111
|
+
return this.mapNodeDefault(node);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
84
114
|
|
|
85
|
-
|
|
86
|
-
|
|
115
|
+
mapProgram(node) {
|
|
116
|
+
node = this.mapNodeDefault(node);
|
|
117
|
+
node.sourceType = this.getSourceType();
|
|
118
|
+
return node;
|
|
119
|
+
}
|
|
87
120
|
|
|
88
|
-
|
|
89
|
-
|
|
121
|
+
mapSimpleLiteral(node) {
|
|
122
|
+
return {
|
|
123
|
+
type: 'Literal',
|
|
124
|
+
loc: node.loc,
|
|
125
|
+
range: node.range,
|
|
126
|
+
value: node.value,
|
|
127
|
+
raw: this.code.slice(node.range[0], node.range[1]),
|
|
128
|
+
literalType: (() => {
|
|
129
|
+
switch (node.type) {
|
|
130
|
+
case 'NullLiteral':
|
|
131
|
+
return 'null';
|
|
132
|
+
|
|
133
|
+
case 'BooleanLiteral':
|
|
134
|
+
return 'boolean';
|
|
135
|
+
|
|
136
|
+
case 'StringLiteral':
|
|
137
|
+
case 'JSXStringLiteral':
|
|
138
|
+
return 'string';
|
|
139
|
+
|
|
140
|
+
case 'NumericLiteral':
|
|
141
|
+
return 'numeric';
|
|
142
|
+
|
|
143
|
+
case 'BigIntLiteral':
|
|
144
|
+
return 'bigint';
|
|
145
|
+
|
|
146
|
+
case 'RegExpLiteral':
|
|
147
|
+
return 'regexp';
|
|
148
|
+
}
|
|
90
149
|
|
|
91
|
-
|
|
92
|
-
|
|
150
|
+
return null;
|
|
151
|
+
})()
|
|
152
|
+
};
|
|
153
|
+
}
|
|
93
154
|
|
|
94
|
-
|
|
95
|
-
|
|
155
|
+
mapBigIntLiteral(node) {
|
|
156
|
+
const newNode = this.mapSimpleLiteral(node);
|
|
157
|
+
const bigint = node.bigint // estree spec is to not have a trailing `n` on this property
|
|
158
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
159
|
+
.replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
|
|
160
|
+
.replace(/_/, '');
|
|
161
|
+
return { ...newNode,
|
|
162
|
+
// coerce the string to a bigint value if supported by the environment
|
|
163
|
+
value: typeof BigInt === 'function' ? BigInt(bigint) : null,
|
|
164
|
+
bigint
|
|
165
|
+
};
|
|
166
|
+
}
|
|
96
167
|
|
|
97
|
-
|
|
98
|
-
|
|
168
|
+
mapNullLiteral(node) {
|
|
169
|
+
return { ...this.mapSimpleLiteral(node),
|
|
170
|
+
value: null
|
|
171
|
+
};
|
|
172
|
+
}
|
|
99
173
|
|
|
100
|
-
|
|
101
|
-
|
|
174
|
+
mapRegExpLiteral(node) {
|
|
175
|
+
const {
|
|
176
|
+
pattern,
|
|
177
|
+
flags
|
|
178
|
+
} = node; // Create RegExp value if possible. This can fail when the flags are invalid.
|
|
102
179
|
|
|
103
|
-
|
|
104
|
-
return this.mapExportDefaultDeclaration(node);
|
|
180
|
+
let value;
|
|
105
181
|
|
|
106
|
-
|
|
107
|
-
|
|
182
|
+
try {
|
|
183
|
+
value = new RegExp(pattern, flags);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
value = null;
|
|
186
|
+
}
|
|
108
187
|
|
|
109
|
-
|
|
110
|
-
|
|
188
|
+
return { ...this.mapSimpleLiteral(node),
|
|
189
|
+
value,
|
|
190
|
+
regex: {
|
|
191
|
+
pattern,
|
|
192
|
+
flags
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
111
196
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
197
|
+
mapBigIntLiteralTypeAnnotation(node) {
|
|
198
|
+
node.value = null;
|
|
199
|
+
return node;
|
|
200
|
+
}
|
|
115
201
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return node;
|
|
126
|
-
}
|
|
127
|
-
}, {
|
|
128
|
-
key: "mapSimpleLiteral",
|
|
129
|
-
value: function mapSimpleLiteral(node) {
|
|
130
|
-
node.type = 'Literal';
|
|
131
|
-
node.raw = this.code.slice(node.range[0], node.range[1]);
|
|
132
|
-
return node;
|
|
133
|
-
}
|
|
134
|
-
}, {
|
|
135
|
-
key: "mapNullLiteral",
|
|
136
|
-
value: function mapNullLiteral(node) {
|
|
137
|
-
node.type = 'Literal';
|
|
138
|
-
node.value = null;
|
|
139
|
-
node.raw = this.code.slice(node.range[0], node.range[1]);
|
|
140
|
-
return node;
|
|
141
|
-
}
|
|
142
|
-
}, {
|
|
143
|
-
key: "mapRegExpLiteral",
|
|
144
|
-
value: function mapRegExpLiteral(node) {
|
|
145
|
-
var pattern = node.pattern,
|
|
146
|
-
flags = node.flags; // Create RegExp value if possible. This can fail when the flags are invalid.
|
|
147
|
-
|
|
148
|
-
var value;
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
value = new RegExp(pattern, flags);
|
|
152
|
-
} catch (e) {
|
|
153
|
-
value = null;
|
|
202
|
+
mapTemplateElement(node) {
|
|
203
|
+
return {
|
|
204
|
+
type: 'TemplateElement',
|
|
205
|
+
loc: node.loc,
|
|
206
|
+
range: node.range,
|
|
207
|
+
tail: node.tail,
|
|
208
|
+
value: {
|
|
209
|
+
cooked: node.cooked,
|
|
210
|
+
raw: node.raw
|
|
154
211
|
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
155
214
|
|
|
215
|
+
mapGenericTypeAnnotation(node) {
|
|
216
|
+
// Convert simple `this` generic type to ThisTypeAnnotation
|
|
217
|
+
if (node.typeParameters == null && node.id.type === 'Identifier' && node.id.name === 'this') {
|
|
156
218
|
return {
|
|
157
|
-
type: '
|
|
158
|
-
loc: node.loc,
|
|
159
|
-
range: node.range,
|
|
160
|
-
value: value,
|
|
161
|
-
raw: this.code.slice(node.range[0], node.range[1]),
|
|
162
|
-
regex: {
|
|
163
|
-
pattern: pattern,
|
|
164
|
-
flags: flags
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
}, {
|
|
169
|
-
key: "mapTemplateElement",
|
|
170
|
-
value: function mapTemplateElement(node) {
|
|
171
|
-
return {
|
|
172
|
-
type: 'TemplateElement',
|
|
219
|
+
type: 'ThisTypeAnnotation',
|
|
173
220
|
loc: node.loc,
|
|
174
|
-
range: node.range
|
|
175
|
-
tail: node.tail,
|
|
176
|
-
value: {
|
|
177
|
-
cooked: node.cooked,
|
|
178
|
-
raw: node.raw
|
|
179
|
-
}
|
|
221
|
+
range: node.range
|
|
180
222
|
};
|
|
181
223
|
}
|
|
182
|
-
}, {
|
|
183
|
-
key: "mapGenericTypeAnnotation",
|
|
184
|
-
value: function mapGenericTypeAnnotation(node) {
|
|
185
|
-
// Convert simple `this` generic type to ThisTypeAnnotation
|
|
186
|
-
if (node.typeParameters === null && node.id.type === 'Identifier' && node.id.name === 'this') {
|
|
187
|
-
return {
|
|
188
|
-
type: 'ThisTypeAnnotation',
|
|
189
|
-
loc: node.loc,
|
|
190
|
-
range: node.range
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
224
|
|
|
194
|
-
|
|
225
|
+
return this.mapNodeDefault(node);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
mapProperty(nodeUnprocessed) {
|
|
229
|
+
const node = this.mapNodeDefault(nodeUnprocessed);
|
|
230
|
+
|
|
231
|
+
if (node.value.type === 'FunctionExpression') {
|
|
232
|
+
node.value.loc.start = node.key.loc.end;
|
|
233
|
+
node.value.range[0] = node.key.range[1];
|
|
195
234
|
}
|
|
196
|
-
}, {
|
|
197
|
-
key: "mapComment",
|
|
198
|
-
value: function mapComment(node) {
|
|
199
|
-
if (node.type === 'CommentBlock') {
|
|
200
|
-
node.type = 'Block';
|
|
201
|
-
} else if (node.type === 'CommentLine') {
|
|
202
|
-
node.type = 'Line';
|
|
203
|
-
}
|
|
204
235
|
|
|
205
|
-
|
|
236
|
+
return node;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
mapComment(node) {
|
|
240
|
+
if (node.type === 'CommentBlock') {
|
|
241
|
+
node.type = 'Block';
|
|
242
|
+
} else if (node.type === 'CommentLine') {
|
|
243
|
+
node.type = 'Line';
|
|
206
244
|
}
|
|
207
|
-
}]);
|
|
208
245
|
|
|
209
|
-
|
|
210
|
-
}
|
|
246
|
+
return node;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
}
|
|
211
250
|
|
|
212
|
-
|
|
251
|
+
exports.default = HermesToESTreeAdapter;
|
|
@@ -0,0 +1,233 @@
|
|
|
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
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
This class does some very "javascripty" things in the name of
|
|
13
|
+
performance which are ultimately impossible to soundly type.
|
|
14
|
+
|
|
15
|
+
So instead of adding strict types and a large number of suppression
|
|
16
|
+
comments, instead it is left untyped and subclasses are strictly
|
|
17
|
+
typed via a separate flow declaration file.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {HermesNode} from './HermesAST';
|
|
21
|
+
import type {ParserOptions} from './ParserOptions';
|
|
22
|
+
|
|
23
|
+
import HermesASTAdapter from './HermesASTAdapter';
|
|
24
|
+
|
|
25
|
+
declare var BigInt: ?(value: $FlowFixMe) => mixed;
|
|
26
|
+
|
|
27
|
+
export default class HermesToESTreeAdapter extends HermesASTAdapter {
|
|
28
|
+
+code: string;
|
|
29
|
+
|
|
30
|
+
constructor(options: ParserOptions, code: string) {
|
|
31
|
+
super(options);
|
|
32
|
+
this.code = code;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fixSourceLocation(node: HermesNode): void {
|
|
36
|
+
const loc = node.loc;
|
|
37
|
+
if (loc == null) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
node.loc = {
|
|
42
|
+
source: this.sourceFilename ?? null,
|
|
43
|
+
start: loc.start,
|
|
44
|
+
end: loc.end,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
node.range = [loc.rangeStart, loc.rangeEnd];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
mapNode(node: HermesNode): HermesNode {
|
|
51
|
+
this.fixSourceLocation(node);
|
|
52
|
+
switch (node.type) {
|
|
53
|
+
case 'Program':
|
|
54
|
+
return this.mapProgram(node);
|
|
55
|
+
case 'NullLiteral':
|
|
56
|
+
return this.mapNullLiteral(node);
|
|
57
|
+
case 'BooleanLiteral':
|
|
58
|
+
case 'StringLiteral':
|
|
59
|
+
case 'NumericLiteral':
|
|
60
|
+
case 'JSXStringLiteral':
|
|
61
|
+
return this.mapSimpleLiteral(node);
|
|
62
|
+
case 'BigIntLiteral':
|
|
63
|
+
return this.mapBigIntLiteral(node);
|
|
64
|
+
case 'RegExpLiteral':
|
|
65
|
+
return this.mapRegExpLiteral(node);
|
|
66
|
+
case 'Empty':
|
|
67
|
+
return this.mapEmpty(node);
|
|
68
|
+
case 'TemplateElement':
|
|
69
|
+
return this.mapTemplateElement(node);
|
|
70
|
+
case 'BigIntLiteralTypeAnnotation':
|
|
71
|
+
return this.mapBigIntLiteralTypeAnnotation(node);
|
|
72
|
+
case 'GenericTypeAnnotation':
|
|
73
|
+
return this.mapGenericTypeAnnotation(node);
|
|
74
|
+
case 'ImportDeclaration':
|
|
75
|
+
return this.mapImportDeclaration(node);
|
|
76
|
+
case 'ImportSpecifier':
|
|
77
|
+
return this.mapImportSpecifier(node);
|
|
78
|
+
case 'ExportDefaultDeclaration':
|
|
79
|
+
return this.mapExportDefaultDeclaration(node);
|
|
80
|
+
case 'ExportNamedDeclaration':
|
|
81
|
+
return this.mapExportNamedDeclaration(node);
|
|
82
|
+
case 'ExportAllDeclaration':
|
|
83
|
+
return this.mapExportAllDeclaration(node);
|
|
84
|
+
case 'PrivateName':
|
|
85
|
+
case 'ClassPrivateProperty':
|
|
86
|
+
return this.mapPrivateProperty(node);
|
|
87
|
+
case 'Property':
|
|
88
|
+
return this.mapProperty(node);
|
|
89
|
+
default:
|
|
90
|
+
return this.mapNodeDefault(node);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
mapProgram(node: HermesNode): HermesNode {
|
|
95
|
+
node = this.mapNodeDefault(node);
|
|
96
|
+
node.sourceType = this.getSourceType();
|
|
97
|
+
|
|
98
|
+
return node;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
mapSimpleLiteral(node: HermesNode): HermesNode {
|
|
102
|
+
return {
|
|
103
|
+
type: 'Literal',
|
|
104
|
+
loc: node.loc,
|
|
105
|
+
range: node.range,
|
|
106
|
+
value: node.value,
|
|
107
|
+
raw: this.code.slice(node.range[0], node.range[1]),
|
|
108
|
+
literalType: (() => {
|
|
109
|
+
switch (node.type) {
|
|
110
|
+
case 'NullLiteral':
|
|
111
|
+
return 'null';
|
|
112
|
+
|
|
113
|
+
case 'BooleanLiteral':
|
|
114
|
+
return 'boolean';
|
|
115
|
+
|
|
116
|
+
case 'StringLiteral':
|
|
117
|
+
case 'JSXStringLiteral':
|
|
118
|
+
return 'string';
|
|
119
|
+
|
|
120
|
+
case 'NumericLiteral':
|
|
121
|
+
return 'numeric';
|
|
122
|
+
|
|
123
|
+
case 'BigIntLiteral':
|
|
124
|
+
return 'bigint';
|
|
125
|
+
|
|
126
|
+
case 'RegExpLiteral':
|
|
127
|
+
return 'regexp';
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
})(),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
mapBigIntLiteral(node: HermesNode): HermesNode {
|
|
135
|
+
const newNode = this.mapSimpleLiteral(node);
|
|
136
|
+
const bigint = node.bigint
|
|
137
|
+
// estree spec is to not have a trailing `n` on this property
|
|
138
|
+
// https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
|
|
139
|
+
.replace(/n$/, '')
|
|
140
|
+
// `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
|
|
141
|
+
.replace(/_/, '');
|
|
142
|
+
return {
|
|
143
|
+
...newNode,
|
|
144
|
+
// coerce the string to a bigint value if supported by the environment
|
|
145
|
+
value: typeof BigInt === 'function' ? BigInt(bigint) : null,
|
|
146
|
+
bigint,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
mapNullLiteral(node: HermesNode): HermesNode {
|
|
151
|
+
return {
|
|
152
|
+
...this.mapSimpleLiteral(node),
|
|
153
|
+
value: null,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
mapRegExpLiteral(node: HermesNode): HermesNode {
|
|
158
|
+
const {pattern, flags} = node;
|
|
159
|
+
|
|
160
|
+
// Create RegExp value if possible. This can fail when the flags are invalid.
|
|
161
|
+
let value;
|
|
162
|
+
try {
|
|
163
|
+
value = new RegExp(pattern, flags);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
value = null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
...this.mapSimpleLiteral(node),
|
|
170
|
+
value,
|
|
171
|
+
regex: {
|
|
172
|
+
pattern,
|
|
173
|
+
flags,
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
mapBigIntLiteralTypeAnnotation(node: HermesNode): HermesNode {
|
|
179
|
+
node.value = null;
|
|
180
|
+
return node;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
mapTemplateElement(node: HermesNode): HermesNode {
|
|
184
|
+
return {
|
|
185
|
+
type: 'TemplateElement',
|
|
186
|
+
loc: node.loc,
|
|
187
|
+
range: node.range,
|
|
188
|
+
tail: node.tail,
|
|
189
|
+
value: {
|
|
190
|
+
cooked: node.cooked,
|
|
191
|
+
raw: node.raw,
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
mapGenericTypeAnnotation(node: HermesNode): HermesNode {
|
|
197
|
+
// Convert simple `this` generic type to ThisTypeAnnotation
|
|
198
|
+
if (
|
|
199
|
+
node.typeParameters == null &&
|
|
200
|
+
node.id.type === 'Identifier' &&
|
|
201
|
+
node.id.name === 'this'
|
|
202
|
+
) {
|
|
203
|
+
return {
|
|
204
|
+
type: 'ThisTypeAnnotation',
|
|
205
|
+
loc: node.loc,
|
|
206
|
+
range: node.range,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return this.mapNodeDefault(node);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
mapProperty(nodeUnprocessed: HermesNode): HermesNode {
|
|
214
|
+
const node = this.mapNodeDefault(nodeUnprocessed);
|
|
215
|
+
|
|
216
|
+
if (node.value.type === 'FunctionExpression') {
|
|
217
|
+
node.value.loc.start = node.key.loc.end;
|
|
218
|
+
node.value.range[0] = node.key.range[1];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return node;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
mapComment(node: HermesNode): HermesNode {
|
|
225
|
+
if (node.type === 'CommentBlock') {
|
|
226
|
+
node.type = 'Block';
|
|
227
|
+
} else if (node.type === 'CommentLine') {
|
|
228
|
+
node.type = 'Line';
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return node;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
export type ParserOptions = {
|
|
12
|
+
allowReturnOutsideFunction?: boolean,
|
|
13
|
+
babel?: boolean,
|
|
14
|
+
flow?: 'all' | 'detect',
|
|
15
|
+
sourceFilename?: string,
|
|
16
|
+
sourceType?: 'module' | 'script' | 'unambiguous',
|
|
17
|
+
tokens?: boolean,
|
|
18
|
+
};
|