metro-source-map 0.63.0 → 0.64.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro-source-map",
3
- "version": "0.63.0",
3
+ "version": "0.64.0",
4
4
  "description": "🚇 Source map generator for Metro.",
5
5
  "main": "src/source-map.js",
6
6
  "repository": {
@@ -15,8 +15,9 @@
15
15
  "@babel/traverse": "^7.0.0",
16
16
  "@babel/types": "^7.0.0",
17
17
  "invariant": "^2.2.4",
18
- "metro-symbolicate": "0.63.0",
19
- "ob1": "0.63.0",
18
+ "metro-symbolicate": "0.64.0",
19
+ "nullthrows": "^1.1.1",
20
+ "ob1": "0.64.0",
20
21
  "source-map": "^0.5.6",
21
22
  "vlq": "^1.0.0"
22
23
  },
@@ -13,9 +13,8 @@ const invariant = require("invariant");
13
13
 
14
14
  const _require = require("./constants"),
15
15
  GENERATED_ORDER = _require.GENERATED_ORDER,
16
- iterationOrderToString = _require.iterationOrderToString;
16
+ iterationOrderToString = _require.iterationOrderToString; // Implementation details shared between MappingsConsumer and SectionsConsumer
17
17
 
18
- // Implementation details shared between MappingsConsumer and SectionsConsumer
19
18
  class AbstractConsumer {
20
19
  constructor(sourceMap) {
21
20
  this._sourceMap = sourceMap;
@@ -20,6 +20,7 @@ function _defineProperty(obj, key, value) {
20
20
  } else {
21
21
  obj[key] = value;
22
22
  }
23
+
23
24
  return obj;
24
25
  }
25
26
 
@@ -30,12 +31,12 @@ const _require = require("./constants"),
30
31
  ORIGINAL_ORDER = _require.ORIGINAL_ORDER,
31
32
  GREATEST_LOWER_BOUND = _require.GREATEST_LOWER_BOUND,
32
33
  LEAST_UPPER_BOUND = _require.LEAST_UPPER_BOUND;
33
-
34
34
  /**
35
35
  * A source map consumer that supports both "basic" and "indexed" source maps.
36
36
  * Uses `MappingsConsumer` and `SectionsConsumer` under the hood (via
37
37
  * `createConsumer`).
38
38
  */
39
+
39
40
  class DelegatingConsumer {
40
41
  constructor(sourceMap) {
41
42
  this._rootConsumer = createConsumer(sourceMap);
@@ -60,7 +60,9 @@ function _unsupportedIterableToArray(o, minLen) {
60
60
 
61
61
  function _arrayLikeToArray(arr, len) {
62
62
  if (len == null || len > arr.length) len = arr.length;
63
+
63
64
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
65
+
64
66
  return arr2;
65
67
  }
66
68
 
@@ -71,6 +73,7 @@ function _iterableToArrayLimit(arr, i) {
71
73
  var _n = true;
72
74
  var _d = false;
73
75
  var _e = undefined;
76
+
74
77
  try {
75
78
  for (
76
79
  var _i = arr[Symbol.iterator](), _s;
@@ -78,6 +81,7 @@ function _iterableToArrayLimit(arr, i) {
78
81
  _n = true
79
82
  ) {
80
83
  _arr.push(_s.value);
84
+
81
85
  if (i && _arr.length === i) break;
82
86
  }
83
87
  } catch (err) {
@@ -90,6 +94,7 @@ function _iterableToArrayLimit(arr, i) {
90
94
  if (_d) throw _e;
91
95
  }
92
96
  }
97
+
93
98
  return _arr;
94
99
  }
95
100
 
@@ -99,6 +104,7 @@ function _arrayWithHoles(arr) {
99
104
 
100
105
  function ownKeys(object, enumerableOnly) {
101
106
  var keys = Object.keys(object);
107
+
102
108
  if (Object.getOwnPropertySymbols) {
103
109
  var symbols = Object.getOwnPropertySymbols(object);
104
110
  if (enumerableOnly)
@@ -107,12 +113,14 @@ function ownKeys(object, enumerableOnly) {
107
113
  });
108
114
  keys.push.apply(keys, symbols);
109
115
  }
116
+
110
117
  return keys;
111
118
  }
112
119
 
113
120
  function _objectSpread(target) {
114
121
  for (var i = 1; i < arguments.length; i++) {
115
122
  var source = arguments[i] != null ? arguments[i] : {};
123
+
116
124
  if (i % 2) {
117
125
  ownKeys(Object(source), true).forEach(function(key) {
118
126
  _defineProperty(target, key, source[key]);
@@ -129,6 +137,7 @@ function _objectSpread(target) {
129
137
  });
130
138
  }
131
139
  }
140
+
132
141
  return target;
133
142
  }
134
143
 
@@ -143,6 +152,7 @@ function _defineProperty(obj, key, value) {
143
152
  } else {
144
153
  obj[key] = value;
145
154
  }
155
+
146
156
  return obj;
147
157
  }
148
158
 
@@ -171,11 +181,11 @@ const _require3 = require("ob1"),
171
181
 
172
182
  const _require4 = require("vlq"),
173
183
  decodeVlq = _require4.decode;
174
-
175
184
  /**
176
185
  * A source map consumer that supports "basic" source maps (that have a
177
186
  * `mappings` field and no sections).
178
187
  */
188
+
179
189
  class MappingsConsumer extends AbstractConsumer {
180
190
  constructor(sourceMap) {
181
191
  super(sourceMap);
@@ -11,6 +11,7 @@
11
11
 
12
12
  function ownKeys(object, enumerableOnly) {
13
13
  var keys = Object.keys(object);
14
+
14
15
  if (Object.getOwnPropertySymbols) {
15
16
  var symbols = Object.getOwnPropertySymbols(object);
16
17
  if (enumerableOnly)
@@ -19,12 +20,14 @@ function ownKeys(object, enumerableOnly) {
19
20
  });
20
21
  keys.push.apply(keys, symbols);
21
22
  }
23
+
22
24
  return keys;
23
25
  }
24
26
 
25
27
  function _objectSpread(target) {
26
28
  for (var i = 1; i < arguments.length; i++) {
27
29
  var source = arguments[i] != null ? arguments[i] : {};
30
+
28
31
  if (i % 2) {
29
32
  ownKeys(Object(source), true).forEach(function(key) {
30
33
  _defineProperty(target, key, source[key]);
@@ -41,6 +44,7 @@ function _objectSpread(target) {
41
44
  });
42
45
  }
43
46
  }
47
+
44
48
  return target;
45
49
  }
46
50
 
@@ -55,6 +59,7 @@ function _defineProperty(obj, key, value) {
55
59
  } else {
56
60
  obj[key] = value;
57
61
  }
62
+
58
63
  return obj;
59
64
  }
60
65
 
@@ -85,7 +90,9 @@ function _unsupportedIterableToArray(o, minLen) {
85
90
 
86
91
  function _arrayLikeToArray(arr, len) {
87
92
  if (len == null || len > arr.length) len = arr.length;
93
+
88
94
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
95
+
89
96
  return arr2;
90
97
  }
91
98
 
@@ -96,6 +103,7 @@ function _iterableToArrayLimit(arr, i) {
96
103
  var _n = true;
97
104
  var _d = false;
98
105
  var _e = undefined;
106
+
99
107
  try {
100
108
  for (
101
109
  var _i = arr[Symbol.iterator](), _s;
@@ -103,6 +111,7 @@ function _iterableToArrayLimit(arr, i) {
103
111
  _n = true
104
112
  ) {
105
113
  _arr.push(_s.value);
114
+
106
115
  if (i && _arr.length === i) break;
107
116
  }
108
117
  } catch (err) {
@@ -115,6 +124,7 @@ function _iterableToArrayLimit(arr, i) {
115
124
  if (_d) throw _e;
116
125
  }
117
126
  }
127
+
118
128
  return _arr;
119
129
  }
120
130
 
@@ -144,11 +154,11 @@ const _require4 = require("ob1"),
144
154
  add0 = _require4.add0,
145
155
  sub1 = _require4.sub1,
146
156
  sub = _require4.sub;
147
-
148
157
  /**
149
158
  * A source map consumer that supports "indexed" source maps (that have a
150
159
  * `sections` field and no top-level mappings).
151
160
  */
161
+
152
162
  class SectionsConsumer extends AbstractConsumer {
153
163
  constructor(sourceMap) {
154
164
  super(sourceMap);
@@ -11,6 +11,7 @@
11
11
 
12
12
  function ownKeys(object, enumerableOnly) {
13
13
  var keys = Object.keys(object);
14
+
14
15
  if (Object.getOwnPropertySymbols) {
15
16
  var symbols = Object.getOwnPropertySymbols(object);
16
17
  if (enumerableOnly)
@@ -19,12 +20,14 @@ function ownKeys(object, enumerableOnly) {
19
20
  });
20
21
  keys.push.apply(keys, symbols);
21
22
  }
23
+
22
24
  return keys;
23
25
  }
24
26
 
25
27
  function _objectSpread(target) {
26
28
  for (var i = 1; i < arguments.length; i++) {
27
29
  var source = arguments[i] != null ? arguments[i] : {};
30
+
28
31
  if (i % 2) {
29
32
  ownKeys(Object(source), true).forEach(function(key) {
30
33
  _defineProperty(target, key, source[key]);
@@ -41,6 +44,7 @@ function _objectSpread(target) {
41
44
  });
42
45
  }
43
46
  }
47
+
44
48
  return target;
45
49
  }
46
50
 
@@ -55,6 +59,7 @@ function _defineProperty(obj, key, value) {
55
59
  } else {
56
60
  obj[key] = value;
57
61
  }
62
+
58
63
  return obj;
59
64
  }
60
65
 
@@ -7,7 +7,4 @@
7
7
  *
8
8
  * @format
9
9
  */
10
- // NOTE: The linter and formatter are fighting over the following lint rule.
11
-
12
- /* eslint-disable flowtype/object-type-delimiter */
13
10
  "use strict";
@@ -8,9 +8,6 @@
8
8
  * @format
9
9
  */
10
10
 
11
- // NOTE: The linter and formatter are fighting over the following lint rule.
12
- /* eslint-disable flowtype/object-type-delimiter */
13
-
14
11
  'use strict';
15
12
 
16
13
  import type {IterationOrder, LookupBias} from './constants';
package/src/Generator.js CHANGED
@@ -11,6 +11,7 @@
11
11
 
12
12
  function ownKeys(object, enumerableOnly) {
13
13
  var keys = Object.keys(object);
14
+
14
15
  if (Object.getOwnPropertySymbols) {
15
16
  var symbols = Object.getOwnPropertySymbols(object);
16
17
  if (enumerableOnly)
@@ -19,12 +20,14 @@ function ownKeys(object, enumerableOnly) {
19
20
  });
20
21
  keys.push.apply(keys, symbols);
21
22
  }
23
+
22
24
  return keys;
23
25
  }
24
26
 
25
27
  function _objectSpread(target) {
26
28
  for (var i = 1; i < arguments.length; i++) {
27
29
  var source = arguments[i] != null ? arguments[i] : {};
30
+
28
31
  if (i % 2) {
29
32
  ownKeys(Object(source), true).forEach(function(key) {
30
33
  _defineProperty(target, key, source[key]);
@@ -41,6 +44,7 @@ function _objectSpread(target) {
41
44
  });
42
45
  }
43
46
  }
47
+
44
48
  return target;
45
49
  }
46
50
 
@@ -55,11 +59,11 @@ function _defineProperty(obj, key, value) {
55
59
  } else {
56
60
  obj[key] = value;
57
61
  }
62
+
58
63
  return obj;
59
64
  }
60
65
 
61
66
  const B64Builder = require("./B64Builder");
62
-
63
67
  /**
64
68
  * Generates a source map from raw mappings.
65
69
  *
@@ -71,6 +75,7 @@ const B64Builder = require("./B64Builder");
71
75
  *
72
76
  * Mappings have to be passed in the order appearance in the generated source.
73
77
  */
78
+
74
79
  class Generator {
75
80
  constructor() {
76
81
  this.builder = new B64Builder();
@@ -14,9 +14,8 @@ const Consumer = require("./Consumer"); // TODO(t67648443): Bypass the `sort-req
14
14
  Consumer;
15
15
 
16
16
  const _require = require("source-map"),
17
- SourceMapGenerator = _require.SourceMapGenerator;
17
+ SourceMapGenerator = _require.SourceMapGenerator; // Originally based on https://github.com/jakobwesthoff/source-map-merger
18
18
 
19
- // Originally based on https://github.com/jakobwesthoff/source-map-merger
20
19
  function composeSourceMaps(maps) {
21
20
  // NOTE: require() here to break dependency cycle
22
21
  const SourceMetadataMapConsumer = require("metro-symbolicate/src/SourceMetadataMapConsumer");
@@ -11,8 +11,14 @@
11
11
 
12
12
  var _traverse = _interopRequireDefault(require("@babel/traverse"));
13
13
 
14
+ var _types = require("@babel/types");
15
+
14
16
  function _interopRequireDefault(obj) {
15
- return obj && obj.__esModule ? obj : { default: obj };
17
+ return obj && obj.__esModule
18
+ ? obj
19
+ : {
20
+ default: obj
21
+ };
16
22
  }
17
23
 
18
24
  function _slicedToArray(arr, i) {
@@ -37,6 +43,7 @@ function _iterableToArrayLimit(arr, i) {
37
43
  var _n = true;
38
44
  var _d = false;
39
45
  var _e = undefined;
46
+
40
47
  try {
41
48
  for (
42
49
  var _i = arr[Symbol.iterator](), _s;
@@ -44,6 +51,7 @@ function _iterableToArrayLimit(arr, i) {
44
51
  _n = true
45
52
  ) {
46
53
  _arr.push(_s.value);
54
+
47
55
  if (i && _arr.length === i) break;
48
56
  }
49
57
  } catch (err) {
@@ -56,6 +64,7 @@ function _iterableToArrayLimit(arr, i) {
56
64
  if (_d) throw _e;
57
65
  }
58
66
  }
67
+
59
68
  return _arr;
60
69
  }
61
70
 
@@ -99,7 +108,9 @@ function _arrayWithoutHoles(arr) {
99
108
 
100
109
  function _arrayLikeToArray(arr, len) {
101
110
  if (len == null || len > arr.length) len = arr.length;
111
+
102
112
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
113
+
103
114
  return arr2;
104
115
  }
105
116
 
@@ -107,8 +118,9 @@ const B64Builder = require("./B64Builder");
107
118
 
108
119
  const fsPath = require("path");
109
120
 
110
- const t = require("@babel/types");
121
+ const nullthrows = require("nullthrows");
111
122
 
123
+ const t = require("@babel/types");
112
124
  /**
113
125
  * Generate a map of source positions to function names. The names are meant to
114
126
  * describe the stack frame in an error trace and may contain more contextual
@@ -117,6 +129,7 @@ const t = require("@babel/types");
117
129
  * The output is encoded for use in a source map. For details about the format,
118
130
  * see MappingEncoder below.
119
131
  */
132
+
120
133
  function generateFunctionMap(ast, context) {
121
134
  const encoder = new MappingEncoder();
122
135
  forEachMapping(ast, context, mapping => encoder.push(mapping));
@@ -193,22 +206,25 @@ function forEachMapping(ast, context, pushMapping) {
193
206
  const basename = context.filename
194
207
  ? fsPath.basename(context.filename).replace(/\..+$/, "")
195
208
  : null;
196
- (0, _traverse.default)(ast, {
197
- "Function|Program|Class": {
198
- enter(path) {
199
- let name = getNameForPath(path);
209
+ const visitor = {
210
+ enter(path) {
211
+ let name = getNameForPath(path);
200
212
 
201
- if (basename) {
202
- name = removeNamePrefix(name, basename);
203
- }
213
+ if (basename) {
214
+ name = removeNamePrefix(name, basename);
215
+ }
204
216
 
205
- pushFrame(name, path.node.loc);
206
- },
217
+ pushFrame(name, nullthrows(path.node.loc));
218
+ },
207
219
 
208
- exit(path) {
209
- popFrame();
210
- }
220
+ exit(path) {
221
+ popFrame();
211
222
  }
223
+ };
224
+ (0, _traverse.default)(ast, {
225
+ Function: visitor,
226
+ Program: visitor,
227
+ Class: visitor
212
228
  });
213
229
  }
214
230
 
@@ -224,20 +240,22 @@ function getNameForPath(path) {
224
240
  parent = path.parent,
225
241
  parentPath = path.parentPath;
226
242
 
227
- if (t.isProgram(node)) {
243
+ if ((0, _types.isProgram)(node)) {
228
244
  return "<global>";
229
245
  }
230
246
 
231
- let id = path.id; // has an `id` so we don't need to infer one
247
+ let _ref = path,
248
+ id = _ref.id; // has an `id` so we don't need to infer one
232
249
 
233
250
  if (node.id) {
251
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
234
252
  return node.id.name;
235
253
  }
236
254
 
237
255
  let propertyPath;
238
256
  let kind = "";
239
257
 
240
- if (t.isObjectMethod(node) || t.isClassMethod(node)) {
258
+ if ((0, _types.isObjectMethod)(node) || (0, _types.isClassMethod)(node)) {
241
259
  id = node.key;
242
260
 
243
261
  if (node.kind !== "method" && node.kind !== "constructor") {
@@ -245,30 +263,55 @@ function getNameForPath(path) {
245
263
  }
246
264
 
247
265
  propertyPath = path;
248
- } else if (t.isObjectProperty(parent) || t.isClassProperty(parent)) {
266
+ } else if (
267
+ (0, _types.isObjectProperty)(parent) ||
268
+ (0, _types.isClassProperty)(parent)
269
+ ) {
249
270
  // { foo() {} };
250
271
  id = parent.key;
251
272
  propertyPath = parentPath;
252
- } else if (t.isVariableDeclarator(parent)) {
273
+ } else if ((0, _types.isVariableDeclarator)(parent)) {
253
274
  // let foo = function () {};
254
275
  id = parent.id;
255
- } else if (t.isAssignmentExpression(parent)) {
276
+ } else if ((0, _types.isAssignmentExpression)(parent)) {
256
277
  // foo = function () {};
257
278
  id = parent.left;
258
- } else if (t.isJSXExpressionContainer(parent)) {
259
- if (t.isJSXElement(parentPath.parentPath.node)) {
279
+ } else if ((0, _types.isJSXExpressionContainer)(parent)) {
280
+ var _parentPath$parentPat;
281
+
282
+ const grandParentNode =
283
+ parentPath === null || parentPath === void 0
284
+ ? void 0
285
+ : (_parentPath$parentPat = parentPath.parentPath) === null ||
286
+ _parentPath$parentPat === void 0
287
+ ? void 0
288
+ : _parentPath$parentPat.node;
289
+
290
+ if ((0, _types.isJSXElement)(grandParentNode)) {
260
291
  // <foo>{function () {}}</foo>
261
- const openingElement = parentPath.parentPath.node.openingElement;
262
- id = t.JSXMemberExpression(
263
- t.JSXMemberExpression(openingElement.name, t.JSXIdentifier("props")),
264
- t.JSXIdentifier("children")
292
+ const openingElement = grandParentNode.openingElement;
293
+ id = t.jsxMemberExpression(
294
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
295
+ t.jsxMemberExpression(openingElement.name, t.jsxIdentifier("props")),
296
+ t.jsxIdentifier("children")
265
297
  );
266
- } else if (t.isJSXAttribute(parentPath.parentPath.node)) {
267
- // <foo bar={function () {}} />
268
- const openingElement = parentPath.parentPath.parentPath.node;
269
- const prop = parentPath.parentPath.node;
270
- id = t.JSXMemberExpression(
271
- t.JSXMemberExpression(openingElement.name, t.JSXIdentifier("props")),
298
+ } else if ((0, _types.isJSXAttribute)(grandParentNode)) {
299
+ var _parentPath$parentPat2, _parentPath$parentPat3; // <foo bar={function () {}} />
300
+
301
+ const openingElement =
302
+ parentPath === null || parentPath === void 0
303
+ ? void 0
304
+ : (_parentPath$parentPat2 = parentPath.parentPath) === null ||
305
+ _parentPath$parentPat2 === void 0
306
+ ? void 0
307
+ : (_parentPath$parentPat3 = _parentPath$parentPat2.parentPath) ===
308
+ null || _parentPath$parentPat3 === void 0
309
+ ? void 0
310
+ : _parentPath$parentPat3.node;
311
+ const prop = grandParentNode;
312
+ id = t.jsxMemberExpression(
313
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
314
+ t.jsxMemberExpression(openingElement.name, t.jsxIdentifier("props")), // $FlowFixMe Flow error uncovered by typing Babel more strictly
272
315
  prop.name
273
316
  );
274
317
  }
@@ -277,7 +320,10 @@ function getNameForPath(path) {
277
320
  let name = getNameFromId(id);
278
321
 
279
322
  if (name == null) {
280
- if (t.isCallExpression(parent) || t.isNewExpression(parent)) {
323
+ if (
324
+ (0, _types.isCallExpression)(parent) ||
325
+ (0, _types.isNewExpression)(parent)
326
+ ) {
281
327
  // foo(function () {})
282
328
  const argIndex = parent.arguments.indexOf(node);
283
329
 
@@ -285,7 +331,7 @@ function getNameForPath(path) {
285
331
  const calleeName = getNameFromId(parent.callee); // var f = Object.freeze(function () {})
286
332
 
287
333
  if (CALLEES_TO_SKIP.indexOf(calleeName) !== -1) {
288
- return getNameForPath(parentPath);
334
+ return getNameForPath(nullthrows(parentPath));
289
335
  }
290
336
 
291
337
  if (calleeName) {
@@ -294,6 +340,13 @@ function getNameForPath(path) {
294
340
  }
295
341
  }
296
342
 
343
+ if (
344
+ (0, _types.isTypeCastExpression)(parent) &&
345
+ parent.expression === node
346
+ ) {
347
+ return getNameForPath(nullthrows(parentPath));
348
+ }
349
+
297
350
  return ANONYMOUS_NAME;
298
351
  }
299
352
 
@@ -302,15 +355,17 @@ function getNameForPath(path) {
302
355
  }
303
356
 
304
357
  if (propertyPath) {
305
- if (t.isClassBody(propertyPath.parent)) {
358
+ if ((0, _types.isClassBody)(propertyPath.parent)) {
359
+ // $FlowFixMe Disvoered when typing babel-traverse
306
360
  const className = getNameForPath(propertyPath.parentPath.parentPath);
307
361
 
308
362
  if (className !== ANONYMOUS_NAME) {
363
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
309
364
  const separator = propertyPath.node.static ? "." : "#";
310
365
  name = className + separator + name;
311
366
  }
312
- } else if (t.isObjectExpression(propertyPath.parent)) {
313
- const objectName = getNameForPath(propertyPath.parentPath);
367
+ } else if ((0, _types.isObjectExpression)(propertyPath.parent)) {
368
+ const objectName = getNameForPath(nullthrows(propertyPath.parentPath));
314
369
 
315
370
  if (objectName !== ANONYMOUS_NAME) {
316
371
  name = objectName + "." + name;
@@ -322,11 +377,13 @@ function getNameForPath(path) {
322
377
  }
323
378
 
324
379
  function isAnyMemberExpression(node) {
325
- return t.isMemberExpression(node) || t.isJSXMemberExpression(node);
380
+ return (
381
+ node.type === "MemberExpression" || node.type === "JSXMemberExpression"
382
+ );
326
383
  }
327
384
 
328
385
  function isAnyIdentifier(node) {
329
- return t.isIdentifier(node) || t.isJSXIdentifier(node);
386
+ return (0, _types.isIdentifier)(node) || (0, _types.isJSXIdentifier)(node);
330
387
  }
331
388
 
332
389
  function getNameFromId(id) {
@@ -356,11 +413,11 @@ function getNamePartsFromId(id) {
356
413
  return [];
357
414
  }
358
415
 
359
- if (t.isCallExpression(id) || t.isNewExpression(id)) {
416
+ if ((0, _types.isCallExpression)(id) || (0, _types.isNewExpression)(id)) {
360
417
  return getNamePartsFromId(id.callee);
361
418
  }
362
419
 
363
- if (t.isTypeCastExpression(id)) {
420
+ if ((0, _types.isTypeCastExpression)(id)) {
364
421
  return getNamePartsFromId(id.expression);
365
422
  }
366
423
 
@@ -368,21 +425,25 @@ function getNamePartsFromId(id) {
368
425
 
369
426
  if (isAnyIdentifier(id)) {
370
427
  name = id.name;
371
- } else if (t.isNullLiteral(id)) {
428
+ } else if ((0, _types.isNullLiteral)(id)) {
372
429
  name = "null";
373
- } else if (t.isRegExpLiteral(id)) {
374
- name = `_${id.pattern}_${id.flags}`;
375
- } else if (t.isTemplateLiteral(id)) {
430
+ } else if ((0, _types.isRegExpLiteral)(id)) {
431
+ var _id$flags;
432
+
433
+ name = `_${id.pattern}_${
434
+ (_id$flags = id.flags) !== null && _id$flags !== void 0 ? _id$flags : ""
435
+ }`;
436
+ } else if ((0, _types.isTemplateLiteral)(id)) {
376
437
  name = id.quasis.map(quasi => quasi.value.raw).join("");
377
- } else if (t.isLiteral(id) && id.value != null) {
378
- name = id.value + "";
438
+ } else if ((0, _types.isLiteral)(id) && id.value != null) {
439
+ name = String(id.value);
379
440
  }
380
441
 
381
442
  if (name != null) {
382
443
  return [t.toBindingIdentifierName(name)];
383
444
  }
384
445
 
385
- if (t.isImport(id)) {
446
+ if ((0, _types.isImport)(id)) {
386
447
  name = "import";
387
448
  }
388
449
 
@@ -394,7 +455,7 @@ function getNamePartsFromId(id) {
394
455
  if (
395
456
  isAnyIdentifier(id.object) &&
396
457
  id.object.name === "Symbol" &&
397
- (isAnyIdentifier(id.property) || t.isLiteral(id.property))
458
+ (isAnyIdentifier(id.property) || (0, _types.isLiteral)(id.property))
398
459
  ) {
399
460
  const propertyName = getNameFromId(id.property);
400
461
 
@@ -436,9 +497,9 @@ function removeNamePrefix(name, namePrefix) {
436
497
 
437
498
  const shortenedName = name.substr(namePrefix.length);
438
499
 
439
- const _ref = shortenedName.match(DELIMITER_START_RE) || [],
440
- _ref2 = _slicedToArray(_ref, 1),
441
- delimiterMatch = _ref2[0];
500
+ const _ref2 = shortenedName.match(DELIMITER_START_RE) || [],
501
+ _ref3 = _slicedToArray(_ref2, 1),
502
+ delimiterMatch = _ref3[0];
442
503
 
443
504
  if (delimiterMatch) {
444
505
  return shortenedName.substr(delimiterMatch.length) || name;
@@ -488,9 +549,9 @@ class MappingEncoder {
488
549
  };
489
550
  }
490
551
 
491
- push(_ref3) {
492
- let name = _ref3.name,
493
- start = _ref3.start;
552
+ push(_ref4) {
553
+ let name = _ref4.name,
554
+ start = _ref4.start;
494
555
 
495
556
  let nameIndex = this._namesMap.get(name);
496
557
 
@@ -13,12 +13,38 @@
13
13
  const B64Builder = require('./B64Builder');
14
14
 
15
15
  const fsPath = require('path');
16
+ const nullthrows = require('nullthrows');
16
17
  const t = require('@babel/types');
17
18
 
18
19
  import type {FBSourceFunctionMap} from './source-map';
19
- import type {Ast} from '@babel/core';
20
20
  import traverse from '@babel/traverse';
21
- import type {Path} from '@babel/traverse';
21
+ import type {NodePath} from '@babel/traverse';
22
+ import {
23
+ isProgram,
24
+ isIdentifier,
25
+ isJSXIdentifier,
26
+ isCallExpression,
27
+ isNewExpression,
28
+ isTypeCastExpression,
29
+ isRegExpLiteral,
30
+ isTemplateLiteral,
31
+ isLiteral,
32
+ isObjectMethod,
33
+ isClassMethod,
34
+ isObjectProperty,
35
+ isClassProperty,
36
+ isVariableDeclarator,
37
+ isAssignmentExpression,
38
+ isJSXExpressionContainer,
39
+ isJSXElement,
40
+ isJSXAttribute,
41
+ isNullLiteral,
42
+ isImport,
43
+ isClassBody,
44
+ isObjectExpression,
45
+ } from '@babel/types';
46
+ import type {Node} from '@babel/types';
47
+
22
48
  type Position = {
23
49
  line: number,
24
50
  column: number,
@@ -39,7 +65,10 @@ type Context = {filename?: string, ...};
39
65
  * The output is encoded for use in a source map. For details about the format,
40
66
  * see MappingEncoder below.
41
67
  */
42
- function generateFunctionMap(ast: Ast, context?: Context): FBSourceFunctionMap {
68
+ function generateFunctionMap(
69
+ ast: BabelNode,
70
+ context?: Context,
71
+ ): FBSourceFunctionMap {
43
72
  const encoder = new MappingEncoder();
44
73
  forEachMapping(ast, context, mapping => encoder.push(mapping));
45
74
  return encoder.getResult();
@@ -52,7 +81,7 @@ function generateFunctionMap(ast: Ast, context?: Context): FBSourceFunctionMap {
52
81
  * Lines are 1-based and columns are 0-based.
53
82
  */
54
83
  function generateFunctionMappingsArray(
55
- ast: Ast,
84
+ ast: BabelNode,
56
85
  context?: Context,
57
86
  ): $ReadOnlyArray<RangeMapping> {
58
87
  const mappings = [];
@@ -67,7 +96,7 @@ function generateFunctionMappingsArray(
67
96
  * mappings, one at a time.
68
97
  */
69
98
  function forEachMapping(
70
- ast: Ast,
99
+ ast: BabelNode,
71
100
  context: ?Context,
72
101
  pushMapping: RangeMapping => void,
73
102
  ) {
@@ -89,7 +118,7 @@ function forEachMapping(
89
118
  tailPos = pos;
90
119
  }
91
120
 
92
- function pushFrame(name, loc) {
121
+ function pushFrame(name: string, loc: BabelNodeSourceLocation) {
93
122
  advanceToPos(loc.start);
94
123
  nameStack.unshift({name, loc});
95
124
  }
@@ -111,19 +140,25 @@ function forEachMapping(
111
140
  ? fsPath.basename(context.filename).replace(/\..+$/, '')
112
141
  : null;
113
142
 
114
- traverse(ast, {
115
- 'Function|Program|Class': {
116
- enter(path) {
117
- let name = getNameForPath(path);
118
- if (basename) {
119
- name = removeNamePrefix(name, basename);
120
- }
121
- pushFrame(name, path.node.loc);
122
- },
123
- exit(path) {
124
- popFrame();
125
- },
143
+ const visitor = {
144
+ enter(path) {
145
+ let name = getNameForPath(path);
146
+ if (basename) {
147
+ name = removeNamePrefix(name, basename);
148
+ }
149
+
150
+ pushFrame(name, nullthrows(path.node.loc));
126
151
  },
152
+
153
+ exit(path): void {
154
+ popFrame();
155
+ },
156
+ };
157
+
158
+ traverse(ast, {
159
+ Function: visitor,
160
+ Program: visitor,
161
+ Class: visitor,
127
162
  });
128
163
  }
129
164
 
@@ -134,48 +169,54 @@ const CALLEES_TO_SKIP = ['Object.freeze'];
134
169
  * Derive a contextual name for the given AST node (Function, Program, Class or
135
170
  * ObjectExpression).
136
171
  */
137
- function getNameForPath(path: Path): string {
172
+ function getNameForPath(path: NodePath<>): string {
138
173
  const {node, parent, parentPath} = path;
139
- if (t.isProgram(node)) {
174
+ if (isProgram(node)) {
140
175
  return '<global>';
141
176
  }
142
- let {id} = path;
177
+
178
+ let {id} = (path: any);
143
179
  // has an `id` so we don't need to infer one
144
180
  if (node.id) {
181
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
145
182
  return node.id.name;
146
183
  }
147
184
  let propertyPath;
148
185
  let kind = '';
149
- if (t.isObjectMethod(node) || t.isClassMethod(node)) {
186
+ if (isObjectMethod(node) || isClassMethod(node)) {
150
187
  id = node.key;
151
188
  if (node.kind !== 'method' && node.kind !== 'constructor') {
152
189
  kind = node.kind;
153
190
  }
154
191
  propertyPath = path;
155
- } else if (t.isObjectProperty(parent) || t.isClassProperty(parent)) {
192
+ } else if (isObjectProperty(parent) || isClassProperty(parent)) {
156
193
  // { foo() {} };
157
194
  id = parent.key;
158
195
  propertyPath = parentPath;
159
- } else if (t.isVariableDeclarator(parent)) {
196
+ } else if (isVariableDeclarator(parent)) {
160
197
  // let foo = function () {};
161
198
  id = parent.id;
162
- } else if (t.isAssignmentExpression(parent)) {
199
+ } else if (isAssignmentExpression(parent)) {
163
200
  // foo = function () {};
164
201
  id = parent.left;
165
- } else if (t.isJSXExpressionContainer(parent)) {
166
- if (t.isJSXElement(parentPath.parentPath.node)) {
202
+ } else if (isJSXExpressionContainer(parent)) {
203
+ const grandParentNode = parentPath?.parentPath?.node;
204
+ if (isJSXElement(grandParentNode)) {
167
205
  // <foo>{function () {}}</foo>
168
- const openingElement = parentPath.parentPath.node.openingElement;
169
- id = t.JSXMemberExpression(
170
- t.JSXMemberExpression(openingElement.name, t.JSXIdentifier('props')),
171
- t.JSXIdentifier('children'),
206
+ const openingElement = grandParentNode.openingElement;
207
+ id = t.jsxMemberExpression(
208
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
209
+ t.jsxMemberExpression(openingElement.name, t.jsxIdentifier('props')),
210
+ t.jsxIdentifier('children'),
172
211
  );
173
- } else if (t.isJSXAttribute(parentPath.parentPath.node)) {
212
+ } else if (isJSXAttribute(grandParentNode)) {
174
213
  // <foo bar={function () {}} />
175
- const openingElement = parentPath.parentPath.parentPath.node;
176
- const prop = parentPath.parentPath.node;
177
- id = t.JSXMemberExpression(
178
- t.JSXMemberExpression(openingElement.name, t.JSXIdentifier('props')),
214
+ const openingElement = parentPath?.parentPath?.parentPath?.node;
215
+ const prop = grandParentNode;
216
+ id = t.jsxMemberExpression(
217
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
218
+ t.jsxMemberExpression(openingElement.name, t.jsxIdentifier('props')),
219
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
179
220
  prop.name,
180
221
  );
181
222
  }
@@ -184,20 +225,23 @@ function getNameForPath(path: Path): string {
184
225
  let name = getNameFromId(id);
185
226
 
186
227
  if (name == null) {
187
- if (t.isCallExpression(parent) || t.isNewExpression(parent)) {
228
+ if (isCallExpression(parent) || isNewExpression(parent)) {
188
229
  // foo(function () {})
189
230
  const argIndex = parent.arguments.indexOf(node);
190
231
  if (argIndex !== -1) {
191
232
  const calleeName = getNameFromId(parent.callee);
192
233
  // var f = Object.freeze(function () {})
193
234
  if (CALLEES_TO_SKIP.indexOf(calleeName) !== -1) {
194
- return getNameForPath(parentPath);
235
+ return getNameForPath(nullthrows(parentPath));
195
236
  }
196
237
  if (calleeName) {
197
238
  return `${calleeName}$argument_${argIndex}`;
198
239
  }
199
240
  }
200
241
  }
242
+ if (isTypeCastExpression(parent) && parent.expression === node) {
243
+ return getNameForPath(nullthrows(parentPath));
244
+ }
201
245
  return ANONYMOUS_NAME;
202
246
  }
203
247
 
@@ -206,14 +250,16 @@ function getNameForPath(path: Path): string {
206
250
  }
207
251
 
208
252
  if (propertyPath) {
209
- if (t.isClassBody(propertyPath.parent)) {
253
+ if (isClassBody(propertyPath.parent)) {
254
+ // $FlowFixMe Disvoered when typing babel-traverse
210
255
  const className = getNameForPath(propertyPath.parentPath.parentPath);
211
256
  if (className !== ANONYMOUS_NAME) {
257
+ // $FlowFixMe Flow error uncovered by typing Babel more strictly
212
258
  const separator = propertyPath.node.static ? '.' : '#';
213
259
  name = className + separator + name;
214
260
  }
215
- } else if (t.isObjectExpression(propertyPath.parent)) {
216
- const objectName = getNameForPath(propertyPath.parentPath);
261
+ } else if (isObjectExpression(propertyPath.parent)) {
262
+ const objectName = getNameForPath(nullthrows(propertyPath.parentPath));
217
263
  if (objectName !== ANONYMOUS_NAME) {
218
264
  name = objectName + '.' + name;
219
265
  }
@@ -223,15 +269,17 @@ function getNameForPath(path: Path): string {
223
269
  return name;
224
270
  }
225
271
 
226
- function isAnyMemberExpression(node: Ast): boolean {
227
- return t.isMemberExpression(node) || t.isJSXMemberExpression(node);
272
+ function isAnyMemberExpression(node: Node): boolean %checks {
273
+ return (
274
+ node.type === 'MemberExpression' || node.type === 'JSXMemberExpression'
275
+ );
228
276
  }
229
277
 
230
- function isAnyIdentifier(node: Ast): boolean {
231
- return t.isIdentifier(node) || t.isJSXIdentifier(node);
278
+ function isAnyIdentifier(node: Node): boolean %checks {
279
+ return isIdentifier(node) || isJSXIdentifier(node);
232
280
  }
233
281
 
234
- function getNameFromId(id: Ast): ?string {
282
+ function getNameFromId(id: Node): ?string {
235
283
  const parts = getNamePartsFromId(id);
236
284
 
237
285
  if (!parts.length) {
@@ -251,16 +299,16 @@ function getNameFromId(id: Ast): ?string {
251
299
  return parts.join('.');
252
300
  }
253
301
 
254
- function getNamePartsFromId(id: Ast): $ReadOnlyArray<string> {
302
+ function getNamePartsFromId(id: Node): $ReadOnlyArray<string> {
255
303
  if (!id) {
256
304
  return [];
257
305
  }
258
306
 
259
- if (t.isCallExpression(id) || t.isNewExpression(id)) {
307
+ if (isCallExpression(id) || isNewExpression(id)) {
260
308
  return getNamePartsFromId(id.callee);
261
309
  }
262
310
 
263
- if (t.isTypeCastExpression(id)) {
311
+ if (isTypeCastExpression(id)) {
264
312
  return getNamePartsFromId(id.expression);
265
313
  }
266
314
 
@@ -268,21 +316,21 @@ function getNamePartsFromId(id: Ast): $ReadOnlyArray<string> {
268
316
 
269
317
  if (isAnyIdentifier(id)) {
270
318
  name = id.name;
271
- } else if (t.isNullLiteral(id)) {
319
+ } else if (isNullLiteral(id)) {
272
320
  name = 'null';
273
- } else if (t.isRegExpLiteral(id)) {
274
- name = `_${id.pattern}_${id.flags}`;
275
- } else if (t.isTemplateLiteral(id)) {
321
+ } else if (isRegExpLiteral(id)) {
322
+ name = `_${id.pattern}_${id.flags ?? ''}`;
323
+ } else if (isTemplateLiteral(id)) {
276
324
  name = id.quasis.map(quasi => quasi.value.raw).join('');
277
- } else if (t.isLiteral(id) && id.value != null) {
278
- name = id.value + '';
325
+ } else if (isLiteral(id) && id.value != null) {
326
+ name = String(id.value);
279
327
  }
280
328
 
281
329
  if (name != null) {
282
330
  return [t.toBindingIdentifierName(name)];
283
331
  }
284
332
 
285
- if (t.isImport(id)) {
333
+ if (isImport(id)) {
286
334
  name = 'import';
287
335
  }
288
336
 
@@ -294,7 +342,7 @@ function getNamePartsFromId(id: Ast): $ReadOnlyArray<string> {
294
342
  if (
295
343
  isAnyIdentifier(id.object) &&
296
344
  id.object.name === 'Symbol' &&
297
- (isAnyIdentifier(id.property) || t.isLiteral(id.property))
345
+ (isAnyIdentifier(id.property) || isLiteral(id.property))
298
346
  ) {
299
347
  const propertyName = getNameFromId(id.property);
300
348
  if (propertyName) {
package/src/source-map.js CHANGED
@@ -17,6 +17,7 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
17
17
  reject(error);
18
18
  return;
19
19
  }
20
+
20
21
  if (info.done) {
21
22
  resolve(value);
22
23
  } else {
@@ -30,12 +31,15 @@ function _asyncToGenerator(fn) {
30
31
  args = arguments;
31
32
  return new Promise(function(resolve, reject) {
32
33
  var gen = fn.apply(self, args);
34
+
33
35
  function _next(value) {
34
36
  asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
35
37
  }
38
+
36
39
  function _throw(err) {
37
40
  asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
38
41
  }
42
+
39
43
  _next(undefined);
40
44
  });
41
45
  };
@@ -45,9 +49,8 @@ const Consumer = require("./Consumer");
45
49
 
46
50
  const Generator = require("./Generator");
47
51
 
48
- const SourceMap = require("source-map");
52
+ const SourceMap = require("source-map"); // We need to export this for `metro-symbolicate`
49
53
 
50
- // We need to export this for `metro-symbolicate`
51
54
  const normalizeSourcePath = require("./Consumer/normalizeSourcePath");
52
55
 
53
56
  const composeSourceMaps = require("./composeSourceMaps");