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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) Facebook, Inc. and its affiliates.
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -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.
@@ -33,6 +33,7 @@ export type HermesToken = {
33
33
  | 'Keyword'
34
34
  | 'Null'
35
35
  | 'Numeric'
36
+ | 'BigInt'
36
37
  | 'Punctuator'
37
38
  | 'String'
38
39
  | 'RegularExpression'
@@ -3,30 +3,37 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports["default"] = void 0;
6
+ exports.default = void 0;
7
7
 
8
8
  var _visitorKeys = require("./generated/visitor-keys");
9
9
 
10
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
-
12
- 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); } }
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
+ */
13
19
 
14
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
20
+ /*
21
+ This class does some very "javascripty" things in the name of
22
+ performance which are ultimately impossible to soundly type.
15
23
 
16
- 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; }
24
+ So instead of adding strict types and a large number of suppression
25
+ comments, instead it is left untyped and subclasses are strictly
26
+ typed via a separate flow declaration file.
27
+ */
17
28
 
18
29
  /**
19
30
  * The base class for transforming the Hermes AST to the desired output format.
20
31
  * Extended by concrete adapters which output an ESTree or Babel AST.
21
32
  */
22
- var HermesASTAdapter = /*#__PURE__*/function () {
23
- function HermesASTAdapter(options) {
24
- _classCallCheck(this, HermesASTAdapter);
25
-
26
- _defineProperty(this, "sourceFilename", void 0);
27
-
28
- _defineProperty(this, "sourceType", void 0);
29
-
33
+ class HermesASTAdapter {
34
+ constructor(options) {
35
+ this.sourceFilename = void 0;
36
+ this.sourceType = void 0;
30
37
  this.sourceFilename = options.sourceFilename;
31
38
  this.sourceType = options.sourceType;
32
39
  }
@@ -36,162 +43,144 @@ var HermesASTAdapter = /*#__PURE__*/function () {
36
43
  */
37
44
 
38
45
 
39
- _createClass(HermesASTAdapter, [{
40
- key: "transform",
41
- value: function transform(program) {
42
- // Comments are not traversed via visitor keys
43
- var comments = program.comments;
46
+ transform(program) {
47
+ // Comments are not traversed via visitor keys
48
+ const comments = program.comments;
44
49
 
45
- for (var i = 0; i < comments.length; i++) {
46
- var comment = comments[i];
47
- this.fixSourceLocation(comment);
48
- comments[i] = this.mapComment(comment);
49
- } // The first comment may be an interpreter directive and is stored directly on the program node
50
+ for (let i = 0; i < comments.length; i++) {
51
+ const comment = comments[i];
52
+ this.fixSourceLocation(comment);
53
+ comments[i] = this.mapComment(comment);
54
+ } // The first comment may be an interpreter directive and is stored directly on the program node
50
55
 
51
56
 
52
- program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
57
+ program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
53
58
 
54
- var tokens = program.tokens;
59
+ const tokens = program.tokens;
55
60
 
56
- if (tokens) {
57
- for (var _i = 0; _i < tokens.length; _i++) {
58
- this.fixSourceLocation(tokens[_i]);
59
- }
61
+ if (tokens) {
62
+ for (let i = 0; i < tokens.length; i++) {
63
+ this.fixSourceLocation(tokens[i]);
60
64
  }
61
-
62
- return this.mapNode(program);
63
- }
64
- /**
65
- * Transform a Hermes AST node to the output AST format.
66
- *
67
- * This may modify the input node in-place and return that same node, or a completely
68
- * new node may be constructed and returned. Overriden in child classes.
69
- */
70
-
71
- }, {
72
- key: "mapNode",
73
- value: function mapNode(_node) {
74
- throw new Error('Implemented in subclasses');
75
65
  }
76
- }, {
77
- key: "mapNodeDefault",
78
- value: function mapNodeDefault(node) {
79
- var visitorKeys = _visitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
80
66
 
81
- for (var key in visitorKeys) {
82
- var childType = visitorKeys[key];
67
+ return this.mapNode(program);
68
+ }
69
+ /**
70
+ * Transform a Hermes AST node to the output AST format.
71
+ *
72
+ * This may modify the input node in-place and return that same node, or a completely
73
+ * new node may be constructed and returned. Overriden in child classes.
74
+ */
83
75
 
84
- if (childType === _visitorKeys.NODE_CHILD) {
85
- var child = node[key];
86
76
 
87
- if (child != null) {
88
- node[key] = this.mapNode(child);
89
- }
90
- } else if (childType === _visitorKeys.NODE_LIST_CHILD) {
91
- var children = node[key];
77
+ mapNode(_node) {
78
+ throw new Error('Implemented in subclasses');
79
+ }
92
80
 
93
- for (var i = 0; i < children.length; i++) {
94
- var _child = children[i];
81
+ mapNodeDefault(node) {
82
+ const visitorKeys = _visitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
95
83
 
96
- if (_child != null) {
97
- children[i] = this.mapNode(_child);
98
- }
99
- }
84
+ for (const key in visitorKeys) {
85
+ const childType = visitorKeys[key];
86
+
87
+ if (childType === _visitorKeys.NODE_CHILD) {
88
+ const child = node[key];
89
+
90
+ if (child != null) {
91
+ node[key] = this.mapNode(child);
100
92
  }
101
- }
93
+ } else if (childType === _visitorKeys.NODE_LIST_CHILD) {
94
+ const children = node[key];
102
95
 
103
- return node;
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
-
110
- }, {
111
- key: "fixSourceLocation",
112
- value: function fixSourceLocation(_node) {
113
- throw new Error('Implemented in subclasses');
114
- }
115
- }, {
116
- key: "getSourceType",
117
- value: function getSourceType() {
118
- var _this$sourceType;
96
+ for (let i = 0; i < children.length; i++) {
97
+ const child = children[i];
119
98
 
120
- return (_this$sourceType = this.sourceType) !== null && _this$sourceType !== void 0 ? _this$sourceType : 'script';
121
- }
122
- }, {
123
- key: "setModuleSourceType",
124
- value: function setModuleSourceType() {
125
- if (this.sourceType == null) {
126
- this.sourceType = 'module';
99
+ if (child != null) {
100
+ children[i] = this.mapNode(child);
101
+ }
102
+ }
127
103
  }
128
104
  }
129
- }, {
130
- key: "mapComment",
131
- value: function mapComment(node) {
132
- return node;
133
- }
134
- }, {
135
- key: "mapEmpty",
136
- value: function mapEmpty(_node) {
137
- // $FlowExpectedError
138
- return null;
139
- }
140
- }, {
141
- key: "mapImportDeclaration",
142
- value: function mapImportDeclaration(node) {
143
- if (node.importKind === 'value') {
144
- this.setModuleSourceType();
145
- }
146
105
 
147
- return this.mapNodeDefault(node);
148
- }
149
- }, {
150
- key: "mapImportSpecifier",
151
- value: function mapImportSpecifier(node) {
152
- if (node.importKind === 'value') {
153
- node.importKind = null;
154
- }
106
+ return node;
107
+ }
108
+ /**
109
+ * Update the source location for this node depending on the output AST format.
110
+ * This can modify the input node in-place. Overriden in child classes.
111
+ */
155
112
 
156
- return this.mapNodeDefault(node);
113
+
114
+ fixSourceLocation(_node) {
115
+ throw new Error('Implemented in subclasses');
116
+ }
117
+
118
+ getSourceType() {
119
+ var _this$sourceType;
120
+
121
+ return (_this$sourceType = this.sourceType) != null ? _this$sourceType : 'script';
122
+ }
123
+
124
+ setModuleSourceType() {
125
+ if (this.sourceType == null) {
126
+ this.sourceType = 'module';
157
127
  }
158
- }, {
159
- key: "mapExportDefaultDeclaration",
160
- value: function mapExportDefaultDeclaration(node) {
128
+ }
129
+
130
+ mapComment(node) {
131
+ return node;
132
+ }
133
+
134
+ mapEmpty(_node) {
135
+ // $FlowExpectedError
136
+ return null;
137
+ }
138
+
139
+ mapImportDeclaration(node) {
140
+ if (node.importKind === 'value') {
161
141
  this.setModuleSourceType();
162
- return this.mapNodeDefault(node);
163
142
  }
164
- }, {
165
- key: "mapExportNamedDeclaration",
166
- value: function mapExportNamedDeclaration(node) {
167
- if (node.exportKind === 'value') {
168
- this.setModuleSourceType();
169
- }
170
143
 
171
- return this.mapNodeDefault(node);
172
- }
173
- }, {
174
- key: "mapExportAllDeclaration",
175
- value: function mapExportAllDeclaration(node) {
176
- if (node.exportKind === 'value') {
177
- this.setModuleSourceType();
178
- }
144
+ return this.mapNodeDefault(node);
145
+ }
179
146
 
180
- return this.mapNodeDefault(node);
147
+ mapImportSpecifier(node) {
148
+ if (node.importKind === 'value') {
149
+ node.importKind = null;
181
150
  }
182
- }, {
183
- key: "mapPrivateProperty",
184
- value: function mapPrivateProperty(node) {
185
- throw new SyntaxError(this.formatError(node, 'Private properties are not supported'));
151
+
152
+ return this.mapNodeDefault(node);
153
+ }
154
+
155
+ mapExportDefaultDeclaration(node) {
156
+ this.setModuleSourceType();
157
+ return this.mapNodeDefault(node);
158
+ }
159
+
160
+ mapExportNamedDeclaration(node) {
161
+ if (node.exportKind === 'value') {
162
+ this.setModuleSourceType();
186
163
  }
187
- }, {
188
- key: "formatError",
189
- value: function formatError(node, message) {
190
- return "".concat(message, " (").concat(node.loc.start.line, ":").concat(node.loc.start.column, ")");
164
+
165
+ return this.mapNodeDefault(node);
166
+ }
167
+
168
+ mapExportAllDeclaration(node) {
169
+ if (node.exportKind === 'value') {
170
+ this.setModuleSourceType();
191
171
  }
192
- }]);
193
172
 
194
- return HermesASTAdapter;
195
- }();
173
+ return this.mapNodeDefault(node);
174
+ }
175
+
176
+ mapPrivateProperty(node) {
177
+ throw new SyntaxError(this.formatError(node, 'Private properties are not supported'));
178
+ }
179
+
180
+ formatError(node, message) {
181
+ return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
182
+ }
183
+
184
+ }
196
185
 
197
- exports["default"] = HermesASTAdapter;
186
+ exports.default = HermesASTAdapter;
@@ -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.
@@ -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.
@@ -18,36 +18,36 @@ var _HermesParserDeserializer = _interopRequireDefault(require("./HermesParserDe
18
18
 
19
19
  var _HermesParserWASM = _interopRequireDefault(require("./HermesParserWASM"));
20
20
 
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
22
 
23
- var hermesParse = _HermesParserWASM["default"].cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
23
+ const hermesParse = _HermesParserWASM.default.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
24
24
 
25
- var hermesParseResult_free = _HermesParserWASM["default"].cwrap('hermesParseResult_free', 'void', ['number']);
25
+ const hermesParseResult_free = _HermesParserWASM.default.cwrap('hermesParseResult_free', 'void', ['number']);
26
26
 
27
- var hermesParseResult_getError = _HermesParserWASM["default"].cwrap('hermesParseResult_getError', 'string', ['number']);
27
+ const hermesParseResult_getError = _HermesParserWASM.default.cwrap('hermesParseResult_getError', 'string', ['number']);
28
28
 
29
- var hermesParseResult_getErrorLine = _HermesParserWASM["default"].cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
29
+ const hermesParseResult_getErrorLine = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
30
30
 
31
- var hermesParseResult_getErrorColumn = _HermesParserWASM["default"].cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
31
+ const hermesParseResult_getErrorColumn = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
32
32
 
33
- var hermesParseResult_getProgramBuffer = _HermesParserWASM["default"].cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
33
+ const hermesParseResult_getProgramBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
34
34
 
35
- var hermesParseResult_getPositionBuffer = _HermesParserWASM["default"].cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
35
+ const hermesParseResult_getPositionBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
36
36
 
37
- var hermesParseResult_getPositionBufferSize = _HermesParserWASM["default"].cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
37
+ const hermesParseResult_getPositionBufferSize = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
38
38
 
39
39
 
40
40
  function copyToHeap(buffer, addr) {
41
- _HermesParserWASM["default"].HEAP8.set(buffer, addr);
41
+ _HermesParserWASM.default.HEAP8.set(buffer, addr);
42
42
 
43
- _HermesParserWASM["default"].HEAP8[addr + buffer.length] = 0;
43
+ _HermesParserWASM.default.HEAP8[addr + buffer.length] = 0;
44
44
  }
45
45
 
46
46
  function parse(source, options) {
47
47
  // Allocate space on heap for source text
48
- var sourceBuffer = Buffer.from(source, 'utf8');
48
+ const sourceBuffer = Buffer.from(source, 'utf8');
49
49
 
50
- var sourceAddr = _HermesParserWASM["default"]._malloc(sourceBuffer.length + 1);
50
+ const sourceAddr = _HermesParserWASM.default._malloc(sourceBuffer.length + 1);
51
51
 
52
52
  if (!sourceAddr) {
53
53
  throw new Error('Parser out of memory');
@@ -56,14 +56,14 @@ function parse(source, options) {
56
56
  try {
57
57
  // Copy source text onto WASM heap
58
58
  copyToHeap(sourceBuffer, sourceAddr);
59
- var parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.tokens, options.allowReturnOutsideFunction);
59
+ const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.tokens, options.allowReturnOutsideFunction);
60
60
 
61
61
  try {
62
62
  // Extract and throw error from parse result if parsing failed
63
- var err = hermesParseResult_getError(parseResult);
63
+ const err = hermesParseResult_getError(parseResult);
64
64
 
65
65
  if (err) {
66
- var syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
66
+ const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
67
67
 
68
68
  syntaxError.loc = {
69
69
  line: hermesParseResult_getErrorLine(parseResult),
@@ -72,12 +72,12 @@ function parse(source, options) {
72
72
  throw syntaxError;
73
73
  }
74
74
 
75
- var deserializer = new _HermesParserDeserializer["default"](hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), _HermesParserWASM["default"], options);
75
+ const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), _HermesParserWASM.default, options);
76
76
  return deserializer.deserialize();
77
77
  } finally {
78
78
  hermesParseResult_free(parseResult);
79
79
  }
80
80
  } finally {
81
- _HermesParserWASM["default"]._free(sourceAddr);
81
+ _HermesParserWASM.default._free(sourceAddr);
82
82
  }
83
83
  }
@@ -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.
@@ -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.
@@ -20,16 +20,16 @@
20
20
  Object.defineProperty(exports, "__esModule", {
21
21
  value: true
22
22
  });
23
- exports["default"] = HermesParserDecodeUTF8String;
23
+ exports.default = HermesParserDecodeUTF8String;
24
24
 
25
25
  function HermesParserDecodeUTF8String(ptrIn, length, heap) {
26
- var ptr = ptrIn;
27
- var endPtr = ptr + length;
28
- var str = '';
26
+ let ptr = ptrIn;
27
+ const endPtr = ptr + length;
28
+ let str = '';
29
29
 
30
30
  while (ptr < endPtr) {
31
31
  // ASCII characters fit in single byte code point
32
- var u0 = heap[ptr++];
32
+ let u0 = heap[ptr++];
33
33
 
34
34
  if (!(u0 & 0x80)) {
35
35
  str += String.fromCharCode(u0);
@@ -37,16 +37,16 @@ function HermesParserDecodeUTF8String(ptrIn, length, heap) {
37
37
  } // Two byte code point
38
38
 
39
39
 
40
- var u1 = heap[ptr++] & 0x3f;
40
+ const u1 = heap[ptr++] & 0x3f;
41
41
 
42
- if ((u0 & 0xe0) == 0xc0) {
42
+ if ((u0 & 0xe0) === 0xc0) {
43
43
  str += String.fromCharCode((u0 & 0x1f) << 6 | u1);
44
44
  continue;
45
45
  }
46
46
 
47
- var u2 = heap[ptr++] & 0x3f;
47
+ const u2 = heap[ptr++] & 0x3f;
48
48
 
49
- if ((u0 & 0xf0) == 0xe0) {
49
+ if ((u0 & 0xf0) === 0xe0) {
50
50
  // Three byte code point
51
51
  u0 = (u0 & 0x0f) << 12 | u1 << 6 | u2;
52
52
  } else {
@@ -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.
@@ -37,13 +37,13 @@ export default function HermesParserDecodeUTF8String(
37
37
 
38
38
  // Two byte code point
39
39
  const u1 = heap[ptr++] & 0x3f;
40
- if ((u0 & 0xe0) == 0xc0) {
40
+ if ((u0 & 0xe0) === 0xc0) {
41
41
  str += String.fromCharCode(((u0 & 0x1f) << 6) | u1);
42
42
  continue;
43
43
  }
44
44
 
45
45
  const u2 = heap[ptr++] & 0x3f;
46
- if ((u0 & 0xf0) == 0xe0) {
46
+ if ((u0 & 0xf0) === 0xe0) {
47
47
  // Three byte code point
48
48
  u0 = ((u0 & 0x0f) << 12) | (u1 << 6) | u2;
49
49
  } else {