hermes-parser 0.4.6 → 0.6.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.
@@ -1,212 +1,237 @@
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) Facebook, Inc. and its affiliates.
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
21
 
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); }
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
+ }
12
36
 
13
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
37
+ fixSourceLocation(node) {
38
+ var _this$sourceFilename;
14
39
 
15
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
40
+ const loc = node.loc;
16
41
 
17
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
42
+ if (loc == null) {
43
+ return;
44
+ }
18
45
 
19
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
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
+ }
20
53
 
21
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
54
+ mapNode(node) {
55
+ this.fixSourceLocation(node);
22
56
 
23
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
57
+ switch (node.type) {
58
+ case 'Program':
59
+ return this.mapProgram(node);
24
60
 
25
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
61
+ case 'NullLiteral':
62
+ return this.mapNullLiteral(node);
26
63
 
27
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
64
+ case 'BooleanLiteral':
65
+ case 'StringLiteral':
66
+ case 'NumericLiteral':
67
+ case 'JSXStringLiteral':
68
+ return this.mapSimpleLiteral(node);
28
69
 
29
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
70
+ case 'BigIntLiteral':
71
+ return this.mapBigIntLiteral(node);
30
72
 
31
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
73
+ case 'RegExpLiteral':
74
+ return this.mapRegExpLiteral(node);
32
75
 
33
- var HermesASTAdapter = require('./HermesASTAdapter');
76
+ case 'Empty':
77
+ return this.mapEmpty(node);
34
78
 
35
- var HermesToESTreeAdapter = /*#__PURE__*/function (_HermesASTAdapter) {
36
- _inherits(HermesToESTreeAdapter, _HermesASTAdapter);
79
+ case 'TemplateElement':
80
+ return this.mapTemplateElement(node);
37
81
 
38
- var _super = _createSuper(HermesToESTreeAdapter);
82
+ case 'BigIntLiteralTypeAnnotation':
83
+ return this.mapBigIntLiteralTypeAnnotation(node);
39
84
 
40
- function HermesToESTreeAdapter(options, code) {
41
- var _this;
85
+ case 'GenericTypeAnnotation':
86
+ return this.mapGenericTypeAnnotation(node);
42
87
 
43
- _classCallCheck(this, HermesToESTreeAdapter);
88
+ case 'ImportDeclaration':
89
+ return this.mapImportDeclaration(node);
44
90
 
45
- _this = _super.call(this, options);
46
- _this.code = code;
47
- return _this;
48
- }
91
+ case 'ImportSpecifier':
92
+ return this.mapImportSpecifier(node);
49
93
 
50
- _createClass(HermesToESTreeAdapter, [{
51
- key: "fixSourceLocation",
52
- value: function fixSourceLocation(node) {
53
- var _this$sourceFilename;
94
+ case 'ExportDefaultDeclaration':
95
+ return this.mapExportDefaultDeclaration(node);
54
96
 
55
- var loc = node.loc;
97
+ case 'ExportNamedDeclaration':
98
+ return this.mapExportNamedDeclaration(node);
56
99
 
57
- if (loc == null) {
58
- return;
59
- }
100
+ case 'ExportAllDeclaration':
101
+ return this.mapExportAllDeclaration(node);
60
102
 
61
- node.loc = {
62
- source: (_this$sourceFilename = this.sourceFilename) !== null && _this$sourceFilename !== void 0 ? _this$sourceFilename : null,
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);
103
+ case 'PrivateName':
104
+ case 'ClassPrivateProperty':
105
+ return this.mapPrivateProperty(node);
72
106
 
73
- switch (node.type) {
74
- case 'Program':
75
- return this.mapProgram(node);
76
-
77
- case 'NullLiteral':
78
- return this.mapNullLiteral(node);
79
-
80
- case 'BooleanLiteral':
81
- case 'StringLiteral':
82
- case 'NumericLiteral':
83
- return this.mapSimpleLiteral(node);
107
+ default:
108
+ return this.mapNodeDefault(node);
109
+ }
110
+ }
84
111
 
85
- case 'RegExpLiteral':
86
- return this.mapRegExpLiteral(node);
112
+ mapProgram(node) {
113
+ node = this.mapNodeDefault(node);
114
+ node.sourceType = this.getSourceType();
115
+ return node;
116
+ }
87
117
 
88
- case 'Empty':
89
- return this.mapEmpty(node);
118
+ mapSimpleLiteral(node) {
119
+ return {
120
+ type: 'Literal',
121
+ loc: node.loc,
122
+ range: node.range,
123
+ value: node.value,
124
+ raw: this.code.slice(node.range[0], node.range[1]),
125
+ literalType: (() => {
126
+ switch (node.type) {
127
+ case 'NullLiteral':
128
+ return 'null';
129
+
130
+ case 'BooleanLiteral':
131
+ return 'boolean';
132
+
133
+ case 'StringLiteral':
134
+ case 'JSXStringLiteral':
135
+ return 'string';
136
+
137
+ case 'NumericLiteral':
138
+ return 'numeric';
139
+
140
+ case 'BigIntLiteral':
141
+ return 'bigint';
142
+
143
+ case 'RegExpLiteral':
144
+ return 'regexp';
145
+ }
90
146
 
91
- case 'TemplateElement':
92
- return this.mapTemplateElement(node);
147
+ return null;
148
+ })()
149
+ };
150
+ }
93
151
 
94
- case 'GenericTypeAnnotation':
95
- return this.mapGenericTypeAnnotation(node);
152
+ mapBigIntLiteral(node) {
153
+ const newNode = this.mapSimpleLiteral(node);
154
+ const bigint = node.bigint // estree spec is to not have a trailing `n` on this property
155
+ // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
156
+ .replace(/n$/, '') // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
157
+ .replace(/_/, '');
158
+ return { ...newNode,
159
+ // coerce the string to a bigint value if supported by the environment
160
+ value: typeof BigInt === 'function' ? BigInt(bigint) : null,
161
+ bigint
162
+ };
163
+ }
96
164
 
97
- case 'ImportDeclaration':
98
- return this.mapImportDeclaration(node);
165
+ mapNullLiteral(node) {
166
+ return { ...this.mapSimpleLiteral(node),
167
+ value: null
168
+ };
169
+ }
99
170
 
100
- case 'ImportSpecifier':
101
- return this.mapImportSpecifier(node);
171
+ mapRegExpLiteral(node) {
172
+ const {
173
+ pattern,
174
+ flags
175
+ } = node; // Create RegExp value if possible. This can fail when the flags are invalid.
102
176
 
103
- case 'ExportDefaultDeclaration':
104
- return this.mapExportDefaultDeclaration(node);
177
+ let value;
105
178
 
106
- case 'ExportNamedDeclaration':
107
- return this.mapExportNamedDeclaration(node);
179
+ try {
180
+ value = new RegExp(pattern, flags);
181
+ } catch (e) {
182
+ value = null;
183
+ }
108
184
 
109
- case 'ExportAllDeclaration':
110
- return this.mapExportAllDeclaration(node);
185
+ return { ...this.mapSimpleLiteral(node),
186
+ value,
187
+ regex: {
188
+ pattern,
189
+ flags
190
+ }
191
+ };
192
+ }
111
193
 
112
- case 'PrivateName':
113
- case 'ClassPrivateProperty':
114
- return this.mapPrivateProperty(node);
194
+ mapBigIntLiteralTypeAnnotation(node) {
195
+ node.value = null;
196
+ return node;
197
+ }
115
198
 
116
- default:
117
- return this.mapNodeDefault(node);
118
- }
119
- }
120
- }, {
121
- key: "mapProgram",
122
- value: function mapProgram(node) {
123
- node = this.mapNodeDefault(node);
124
- node.sourceType = this.getSourceType();
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;
199
+ mapTemplateElement(node) {
200
+ return {
201
+ type: 'TemplateElement',
202
+ loc: node.loc,
203
+ range: node.range,
204
+ tail: node.tail,
205
+ value: {
206
+ cooked: node.cooked,
207
+ raw: node.raw
154
208
  }
209
+ };
210
+ }
155
211
 
212
+ mapGenericTypeAnnotation(node) {
213
+ // Convert simple `this` generic type to ThisTypeAnnotation
214
+ if (node.typeParameters == null && node.id.type === 'Identifier' && node.id.name === 'this') {
156
215
  return {
157
- type: 'Literal',
216
+ type: 'ThisTypeAnnotation',
158
217
  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
- }
218
+ range: node.range
166
219
  };
167
220
  }
168
- }, {
169
- key: "mapTemplateElement",
170
- value: function mapTemplateElement(node) {
171
- return {
172
- type: 'TemplateElement',
173
- loc: node.loc,
174
- range: node.range,
175
- tail: node.tail,
176
- value: {
177
- cooked: node.cooked,
178
- raw: node.raw
179
- }
180
- };
181
- }
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
221
 
194
- return this.mapNodeDefault(node);
195
- }
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
- }
222
+ return this.mapNodeDefault(node);
223
+ }
204
224
 
205
- return node;
225
+ mapComment(node) {
226
+ if (node.type === 'CommentBlock') {
227
+ node.type = 'Block';
228
+ } else if (node.type === 'CommentLine') {
229
+ node.type = 'Line';
206
230
  }
207
- }]);
208
231
 
209
- return HermesToESTreeAdapter;
210
- }(HermesASTAdapter);
232
+ return node;
233
+ }
234
+
235
+ }
211
236
 
212
- module.exports = HermesToESTreeAdapter;
237
+ exports.default = HermesToESTreeAdapter;
@@ -0,0 +1,220 @@
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
+ default:
88
+ return this.mapNodeDefault(node);
89
+ }
90
+ }
91
+
92
+ mapProgram(node: HermesNode): HermesNode {
93
+ node = this.mapNodeDefault(node);
94
+ node.sourceType = this.getSourceType();
95
+
96
+ return node;
97
+ }
98
+
99
+ mapSimpleLiteral(node: HermesNode): HermesNode {
100
+ return {
101
+ type: 'Literal',
102
+ loc: node.loc,
103
+ range: node.range,
104
+ value: node.value,
105
+ raw: this.code.slice(node.range[0], node.range[1]),
106
+ literalType: (() => {
107
+ switch (node.type) {
108
+ case 'NullLiteral':
109
+ return 'null';
110
+
111
+ case 'BooleanLiteral':
112
+ return 'boolean';
113
+
114
+ case 'StringLiteral':
115
+ case 'JSXStringLiteral':
116
+ return 'string';
117
+
118
+ case 'NumericLiteral':
119
+ return 'numeric';
120
+
121
+ case 'BigIntLiteral':
122
+ return 'bigint';
123
+
124
+ case 'RegExpLiteral':
125
+ return 'regexp';
126
+ }
127
+ return null;
128
+ })(),
129
+ };
130
+ }
131
+
132
+ mapBigIntLiteral(node: HermesNode): HermesNode {
133
+ const newNode = this.mapSimpleLiteral(node);
134
+ const bigint = node.bigint
135
+ // estree spec is to not have a trailing `n` on this property
136
+ // https://github.com/estree/estree/blob/db962bb417a97effcfe9892f87fbb93c81a68584/es2020.md#bigintliteral
137
+ .replace(/n$/, '')
138
+ // `BigInt` doesn't accept numeric separator and `bigint` property should not include numeric separator
139
+ .replace(/_/, '');
140
+ return {
141
+ ...newNode,
142
+ // coerce the string to a bigint value if supported by the environment
143
+ value: typeof BigInt === 'function' ? BigInt(bigint) : null,
144
+ bigint,
145
+ };
146
+ }
147
+
148
+ mapNullLiteral(node: HermesNode): HermesNode {
149
+ return {
150
+ ...this.mapSimpleLiteral(node),
151
+ value: null,
152
+ };
153
+ }
154
+
155
+ mapRegExpLiteral(node: HermesNode): HermesNode {
156
+ const {pattern, flags} = node;
157
+
158
+ // Create RegExp value if possible. This can fail when the flags are invalid.
159
+ let value;
160
+ try {
161
+ value = new RegExp(pattern, flags);
162
+ } catch (e) {
163
+ value = null;
164
+ }
165
+
166
+ return {
167
+ ...this.mapSimpleLiteral(node),
168
+ value,
169
+ regex: {
170
+ pattern,
171
+ flags,
172
+ },
173
+ };
174
+ }
175
+
176
+ mapBigIntLiteralTypeAnnotation(node: HermesNode): HermesNode {
177
+ node.value = null;
178
+ return node;
179
+ }
180
+
181
+ mapTemplateElement(node: HermesNode): HermesNode {
182
+ return {
183
+ type: 'TemplateElement',
184
+ loc: node.loc,
185
+ range: node.range,
186
+ tail: node.tail,
187
+ value: {
188
+ cooked: node.cooked,
189
+ raw: node.raw,
190
+ },
191
+ };
192
+ }
193
+
194
+ mapGenericTypeAnnotation(node: HermesNode): HermesNode {
195
+ // Convert simple `this` generic type to ThisTypeAnnotation
196
+ if (
197
+ node.typeParameters == null &&
198
+ node.id.type === 'Identifier' &&
199
+ node.id.name === 'this'
200
+ ) {
201
+ return {
202
+ type: 'ThisTypeAnnotation',
203
+ loc: node.loc,
204
+ range: node.range,
205
+ };
206
+ }
207
+
208
+ return this.mapNodeDefault(node);
209
+ }
210
+
211
+ mapComment(node: HermesNode): HermesNode {
212
+ if (node.type === 'CommentBlock') {
213
+ node.type = 'Block';
214
+ } else if (node.type === 'CommentLine') {
215
+ node.type = 'Line';
216
+ }
217
+
218
+ return node;
219
+ }
220
+ }
@@ -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
+ };