hermes-parser 0.5.0 → 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,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -73,6 +73,8 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
73
73
  return this.mapRestElement(node);
74
74
  case 'ImportExpression':
75
75
  return this.mapImportExpression(node);
76
+ case 'JSXStringLiteral':
77
+ return this.mapJSXStringLiteral(node);
76
78
  case 'PrivateName':
77
79
  case 'ClassPrivateProperty':
78
80
  return this.mapPrivateProperty(node);
@@ -198,7 +200,7 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
198
200
  mapGenericTypeAnnotation(node: HermesNode): HermesNode {
199
201
  // Convert simple `this` generic type to ThisTypeAnnotation
200
202
  if (
201
- node.typeParameters === null &&
203
+ node.typeParameters == null &&
202
204
  node.id.type === 'Identifier' &&
203
205
  node.id.name === 'this'
204
206
  ) {
@@ -343,6 +345,19 @@ export default class HermesToBabelAdapter extends HermesASTAdapter {
343
345
  };
344
346
  }
345
347
 
348
+ mapJSXStringLiteral(node: HermesNode): HermesNode {
349
+ // Babel expects StringLiterals in JSX,
350
+ // but Hermes uses JSXStringLiteral to attach the raw value without
351
+ // having to internally attach it to every single string literal.
352
+ return {
353
+ type: 'StringLiteral',
354
+ loc: node.loc,
355
+ start: node.start,
356
+ end: node.end,
357
+ value: node.value,
358
+ };
359
+ }
360
+
346
361
  mapFunction(node: HermesNode): HermesNode {
347
362
  // Remove the first parameter if it is a this-type annotation,
348
363
  // which is not recognized by Babel.
@@ -1,216 +1,237 @@
1
1
  "use strict";
2
2
 
3
- 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); }
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
- exports["default"] = void 0;
9
-
10
- var _HermesASTAdapter2 = _interopRequireDefault(require("./HermesASTAdapter"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
-
14
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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
+
12
+ /**
13
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
14
+ *
15
+ * This source code is licensed under the MIT license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ *
19
+ * @format
20
+ */
21
+
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
+ }
15
36
 
16
- 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); } }
37
+ fixSourceLocation(node) {
38
+ var _this$sourceFilename;
17
39
 
18
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
40
+ const loc = node.loc;
19
41
 
20
- 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); }
42
+ if (loc == null) {
43
+ return;
44
+ }
21
45
 
22
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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
+ }
23
53
 
24
- 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); }; }
54
+ mapNode(node) {
55
+ this.fixSourceLocation(node);
25
56
 
26
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
57
+ switch (node.type) {
58
+ case 'Program':
59
+ return this.mapProgram(node);
27
60
 
28
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
61
+ case 'NullLiteral':
62
+ return this.mapNullLiteral(node);
29
63
 
30
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
64
+ case 'BooleanLiteral':
65
+ case 'StringLiteral':
66
+ case 'NumericLiteral':
67
+ case 'JSXStringLiteral':
68
+ return this.mapSimpleLiteral(node);
31
69
 
32
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
70
+ case 'BigIntLiteral':
71
+ return this.mapBigIntLiteral(node);
33
72
 
34
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
73
+ case 'RegExpLiteral':
74
+ return this.mapRegExpLiteral(node);
35
75
 
36
- var HermesToESTreeAdapter = /*#__PURE__*/function (_HermesASTAdapter) {
37
- _inherits(HermesToESTreeAdapter, _HermesASTAdapter);
76
+ case 'Empty':
77
+ return this.mapEmpty(node);
38
78
 
39
- var _super = _createSuper(HermesToESTreeAdapter);
79
+ case 'TemplateElement':
80
+ return this.mapTemplateElement(node);
40
81
 
41
- function HermesToESTreeAdapter(options, code) {
42
- var _this;
82
+ case 'BigIntLiteralTypeAnnotation':
83
+ return this.mapBigIntLiteralTypeAnnotation(node);
43
84
 
44
- _classCallCheck(this, HermesToESTreeAdapter);
85
+ case 'GenericTypeAnnotation':
86
+ return this.mapGenericTypeAnnotation(node);
45
87
 
46
- _this = _super.call(this, options);
88
+ case 'ImportDeclaration':
89
+ return this.mapImportDeclaration(node);
47
90
 
48
- _defineProperty(_assertThisInitialized(_this), "code", void 0);
91
+ case 'ImportSpecifier':
92
+ return this.mapImportSpecifier(node);
49
93
 
50
- _this.code = code;
51
- return _this;
52
- }
94
+ case 'ExportDefaultDeclaration':
95
+ return this.mapExportDefaultDeclaration(node);
53
96
 
54
- _createClass(HermesToESTreeAdapter, [{
55
- key: "fixSourceLocation",
56
- value: function fixSourceLocation(node) {
57
- var _this$sourceFilename;
97
+ case 'ExportNamedDeclaration':
98
+ return this.mapExportNamedDeclaration(node);
58
99
 
59
- var loc = node.loc;
100
+ case 'ExportAllDeclaration':
101
+ return this.mapExportAllDeclaration(node);
60
102
 
61
- if (loc == null) {
62
- return;
63
- }
103
+ case 'PrivateName':
104
+ case 'ClassPrivateProperty':
105
+ return this.mapPrivateProperty(node);
64
106
 
65
- node.loc = {
66
- source: (_this$sourceFilename = this.sourceFilename) !== null && _this$sourceFilename !== void 0 ? _this$sourceFilename : null,
67
- start: loc.start,
68
- end: loc.end
69
- };
70
- node.range = [loc.rangeStart, loc.rangeEnd];
107
+ default:
108
+ return this.mapNodeDefault(node);
71
109
  }
72
- }, {
73
- key: "mapNode",
74
- value: function mapNode(node) {
75
- this.fixSourceLocation(node);
76
-
77
- switch (node.type) {
78
- case 'Program':
79
- return this.mapProgram(node);
80
-
81
- case 'NullLiteral':
82
- return this.mapNullLiteral(node);
83
-
84
- case 'BooleanLiteral':
85
- case 'StringLiteral':
86
- case 'NumericLiteral':
87
- return this.mapSimpleLiteral(node);
110
+ }
88
111
 
89
- case 'RegExpLiteral':
90
- return this.mapRegExpLiteral(node);
112
+ mapProgram(node) {
113
+ node = this.mapNodeDefault(node);
114
+ node.sourceType = this.getSourceType();
115
+ return node;
116
+ }
91
117
 
92
- case 'Empty':
93
- 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
+ }
94
146
 
95
- case 'TemplateElement':
96
- return this.mapTemplateElement(node);
147
+ return null;
148
+ })()
149
+ };
150
+ }
97
151
 
98
- case 'GenericTypeAnnotation':
99
- 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
+ }
100
164
 
101
- case 'ImportDeclaration':
102
- return this.mapImportDeclaration(node);
165
+ mapNullLiteral(node) {
166
+ return { ...this.mapSimpleLiteral(node),
167
+ value: null
168
+ };
169
+ }
103
170
 
104
- case 'ImportSpecifier':
105
- 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.
106
176
 
107
- case 'ExportDefaultDeclaration':
108
- return this.mapExportDefaultDeclaration(node);
177
+ let value;
109
178
 
110
- case 'ExportNamedDeclaration':
111
- return this.mapExportNamedDeclaration(node);
179
+ try {
180
+ value = new RegExp(pattern, flags);
181
+ } catch (e) {
182
+ value = null;
183
+ }
112
184
 
113
- case 'ExportAllDeclaration':
114
- return this.mapExportAllDeclaration(node);
185
+ return { ...this.mapSimpleLiteral(node),
186
+ value,
187
+ regex: {
188
+ pattern,
189
+ flags
190
+ }
191
+ };
192
+ }
115
193
 
116
- case 'PrivateName':
117
- case 'ClassPrivateProperty':
118
- return this.mapPrivateProperty(node);
194
+ mapBigIntLiteralTypeAnnotation(node) {
195
+ node.value = null;
196
+ return node;
197
+ }
119
198
 
120
- default:
121
- return this.mapNodeDefault(node);
122
- }
123
- }
124
- }, {
125
- key: "mapProgram",
126
- value: function mapProgram(node) {
127
- node = this.mapNodeDefault(node);
128
- node.sourceType = this.getSourceType();
129
- return node;
130
- }
131
- }, {
132
- key: "mapSimpleLiteral",
133
- value: function mapSimpleLiteral(node) {
134
- node.type = 'Literal';
135
- node.raw = this.code.slice(node.range[0], node.range[1]);
136
- return node;
137
- }
138
- }, {
139
- key: "mapNullLiteral",
140
- value: function mapNullLiteral(node) {
141
- node.type = 'Literal';
142
- node.value = null;
143
- node.raw = this.code.slice(node.range[0], node.range[1]);
144
- return node;
145
- }
146
- }, {
147
- key: "mapRegExpLiteral",
148
- value: function mapRegExpLiteral(node) {
149
- var pattern = node.pattern,
150
- flags = node.flags; // Create RegExp value if possible. This can fail when the flags are invalid.
151
-
152
- var value;
153
-
154
- try {
155
- value = new RegExp(pattern, flags);
156
- } catch (e) {
157
- 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
158
208
  }
209
+ };
210
+ }
159
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') {
160
215
  return {
161
- type: 'Literal',
162
- loc: node.loc,
163
- range: node.range,
164
- value: value,
165
- raw: this.code.slice(node.range[0], node.range[1]),
166
- regex: {
167
- pattern: pattern,
168
- flags: flags
169
- }
170
- };
171
- }
172
- }, {
173
- key: "mapTemplateElement",
174
- value: function mapTemplateElement(node) {
175
- return {
176
- type: 'TemplateElement',
216
+ type: 'ThisTypeAnnotation',
177
217
  loc: node.loc,
178
- range: node.range,
179
- tail: node.tail,
180
- value: {
181
- cooked: node.cooked,
182
- raw: node.raw
183
- }
218
+ range: node.range
184
219
  };
185
220
  }
186
- }, {
187
- key: "mapGenericTypeAnnotation",
188
- value: function mapGenericTypeAnnotation(node) {
189
- // Convert simple `this` generic type to ThisTypeAnnotation
190
- if (node.typeParameters === null && node.id.type === 'Identifier' && node.id.name === 'this') {
191
- return {
192
- type: 'ThisTypeAnnotation',
193
- loc: node.loc,
194
- range: node.range
195
- };
196
- }
197
221
 
198
- return this.mapNodeDefault(node);
199
- }
200
- }, {
201
- key: "mapComment",
202
- value: function mapComment(node) {
203
- if (node.type === 'CommentBlock') {
204
- node.type = 'Block';
205
- } else if (node.type === 'CommentLine') {
206
- node.type = 'Line';
207
- }
222
+ return this.mapNodeDefault(node);
223
+ }
208
224
 
209
- return node;
225
+ mapComment(node) {
226
+ if (node.type === 'CommentBlock') {
227
+ node.type = 'Block';
228
+ } else if (node.type === 'CommentLine') {
229
+ node.type = 'Line';
210
230
  }
211
- }]);
212
231
 
213
- return HermesToESTreeAdapter;
214
- }(_HermesASTAdapter2["default"]);
232
+ return node;
233
+ }
234
+
235
+ }
215
236
 
216
- exports["default"] = HermesToESTreeAdapter;
237
+ exports.default = HermesToESTreeAdapter;
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
@@ -22,6 +22,8 @@ import type {ParserOptions} from './ParserOptions';
22
22
 
23
23
  import HermesASTAdapter from './HermesASTAdapter';
24
24
 
25
+ declare var BigInt: ?(value: $FlowFixMe) => mixed;
26
+
25
27
  export default class HermesToESTreeAdapter extends HermesASTAdapter {
26
28
  +code: string;
27
29
 
@@ -55,13 +57,18 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
55
57
  case 'BooleanLiteral':
56
58
  case 'StringLiteral':
57
59
  case 'NumericLiteral':
60
+ case 'JSXStringLiteral':
58
61
  return this.mapSimpleLiteral(node);
62
+ case 'BigIntLiteral':
63
+ return this.mapBigIntLiteral(node);
59
64
  case 'RegExpLiteral':
60
65
  return this.mapRegExpLiteral(node);
61
66
  case 'Empty':
62
67
  return this.mapEmpty(node);
63
68
  case 'TemplateElement':
64
69
  return this.mapTemplateElement(node);
70
+ case 'BigIntLiteralTypeAnnotation':
71
+ return this.mapBigIntLiteralTypeAnnotation(node);
65
72
  case 'GenericTypeAnnotation':
66
73
  return this.mapGenericTypeAnnotation(node);
67
74
  case 'ImportDeclaration':
@@ -90,18 +97,59 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
90
97
  }
91
98
 
92
99
  mapSimpleLiteral(node: HermesNode): HermesNode {
93
- node.type = 'Literal';
94
- node.raw = this.code.slice(node.range[0], node.range[1]);
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';
95
110
 
96
- return node;
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
+ };
97
130
  }
98
131
 
99
- mapNullLiteral(node: HermesNode): HermesNode {
100
- node.type = 'Literal';
101
- node.value = null;
102
- node.raw = this.code.slice(node.range[0], node.range[1]);
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
+ }
103
147
 
104
- return node;
148
+ mapNullLiteral(node: HermesNode): HermesNode {
149
+ return {
150
+ ...this.mapSimpleLiteral(node),
151
+ value: null,
152
+ };
105
153
  }
106
154
 
107
155
  mapRegExpLiteral(node: HermesNode): HermesNode {
@@ -116,11 +164,8 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
116
164
  }
117
165
 
118
166
  return {
119
- type: 'Literal',
120
- loc: node.loc,
121
- range: node.range,
167
+ ...this.mapSimpleLiteral(node),
122
168
  value,
123
- raw: this.code.slice(node.range[0], node.range[1]),
124
169
  regex: {
125
170
  pattern,
126
171
  flags,
@@ -128,6 +173,11 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
128
173
  };
129
174
  }
130
175
 
176
+ mapBigIntLiteralTypeAnnotation(node: HermesNode): HermesNode {
177
+ node.value = null;
178
+ return node;
179
+ }
180
+
131
181
  mapTemplateElement(node: HermesNode): HermesNode {
132
182
  return {
133
183
  type: 'TemplateElement',
@@ -144,7 +194,7 @@ export default class HermesToESTreeAdapter extends HermesASTAdapter {
144
194
  mapGenericTypeAnnotation(node: HermesNode): HermesNode {
145
195
  // Convert simple `this` generic type to ThisTypeAnnotation
146
196
  if (
147
- node.typeParameters === null &&
197
+ node.typeParameters == null &&
148
198
  node.id.type === 'Identifier' &&
149
199
  node.id.name === 'this'
150
200
  ) {
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.