metro-source-map 0.66.2 → 0.67.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.
Files changed (37) hide show
  1. package/package.json +3 -3
  2. package/src/B64Builder.js +1 -1
  3. package/src/B64Builder.js.flow +1 -1
  4. package/src/BundleBuilder.js +7 -7
  5. package/src/BundleBuilder.js.flow +5 -4
  6. package/src/Consumer/AbstractConsumer.js +3 -4
  7. package/src/Consumer/AbstractConsumer.js.flow +6 -7
  8. package/src/Consumer/DelegatingConsumer.js +5 -5
  9. package/src/Consumer/DelegatingConsumer.js.flow +11 -12
  10. package/src/Consumer/MappingsConsumer.js +14 -14
  11. package/src/Consumer/MappingsConsumer.js.flow +14 -16
  12. package/src/Consumer/SectionsConsumer.js +11 -8
  13. package/src/Consumer/SectionsConsumer.js.flow +10 -12
  14. package/src/Consumer/constants.js +3 -3
  15. package/src/Consumer/constants.js.flow +3 -3
  16. package/src/Consumer/createConsumer.js +1 -1
  17. package/src/Consumer/createConsumer.js.flow +3 -3
  18. package/src/Consumer/index.js +1 -1
  19. package/src/Consumer/index.js.flow +1 -1
  20. package/src/Consumer/normalizeSourcePath.js +1 -1
  21. package/src/Consumer/normalizeSourcePath.js.flow +1 -1
  22. package/src/Consumer/positionMath.js +6 -6
  23. package/src/Consumer/positionMath.js.flow +3 -3
  24. package/src/Consumer/search.js +2 -2
  25. package/src/Consumer/search.js.flow +1 -1
  26. package/src/Consumer/types.flow.js +1 -1
  27. package/src/Consumer/types.flow.js.flow +1 -1
  28. package/src/Generator.js +7 -7
  29. package/src/Generator.js.flow +4 -4
  30. package/src/composeSourceMaps.js +16 -17
  31. package/src/composeSourceMaps.js.flow +6 -7
  32. package/src/encode.js +6 -65
  33. package/src/encode.js.flow +6 -65
  34. package/src/generateFunctionMap.js +35 -23
  35. package/src/generateFunctionMap.js.flow +64 -36
  36. package/src/source-map.js +17 -18
  37. package/src/source-map.js.flow +30 -15
@@ -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.
@@ -10,41 +10,39 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const B64Builder = require('./B64Builder');
14
-
15
- const fsPath = require('path');
16
- const nullthrows = require('nullthrows');
17
- const t = require('@babel/types');
18
-
19
13
  import type {FBSourceFunctionMap} from './source-map';
20
- import traverse from '@babel/traverse';
21
14
  import type {NodePath} from '@babel/traverse';
15
+ import type {Node} from '@babel/types';
16
+
17
+ import traverse from '@babel/traverse';
22
18
  import {
23
- isProgram,
19
+ isAssignmentExpression,
20
+ isClassBody,
21
+ isClassMethod,
22
+ isClassProperty,
23
+ isExportDefaultDeclaration,
24
24
  isIdentifier,
25
+ isImport,
26
+ isJSXAttribute,
27
+ isJSXElement,
28
+ isJSXExpressionContainer,
25
29
  isJSXIdentifier,
26
- isCallExpression,
27
- isNewExpression,
28
- isTypeCastExpression,
29
- isRegExpLiteral,
30
- isTemplateLiteral,
31
30
  isLiteral,
31
+ isNullLiteral,
32
+ isObjectExpression,
32
33
  isObjectMethod,
33
- isClassMethod,
34
34
  isObjectProperty,
35
- isClassProperty,
35
+ isProgram,
36
+ isRegExpLiteral,
37
+ isTemplateLiteral,
38
+ isTypeCastExpression,
36
39
  isVariableDeclarator,
37
- isAssignmentExpression,
38
- isJSXExpressionContainer,
39
- isJSXElement,
40
- isJSXAttribute,
41
- isNullLiteral,
42
- isImport,
43
- isClassBody,
44
- isObjectExpression,
45
- isExportDefaultDeclaration,
46
40
  } from '@babel/types';
47
- import type {Node} from '@babel/types';
41
+
42
+ const B64Builder = require('./B64Builder');
43
+ const t = require('@babel/types');
44
+ const nullthrows = require('nullthrows');
45
+ const fsPath = require('path');
48
46
 
49
47
  type Position = {
50
48
  line: number,
@@ -105,7 +103,7 @@ function forEachMapping(
105
103
  let tailPos = {line: 1, column: 0};
106
104
  let tailName = null;
107
105
 
108
- function advanceToPos(pos) {
106
+ function advanceToPos(pos: {column: number, line: number}) {
109
107
  if (tailPos && positionGreater(pos, tailPos)) {
110
108
  const name = nameStack[0].name; // We always have at least Program
111
109
  if (name !== tailName) {
@@ -142,7 +140,12 @@ function forEachMapping(
142
140
  : null;
143
141
 
144
142
  const visitor = {
145
- enter(path) {
143
+ enter(
144
+ path:
145
+ | NodePath<BabelNodeProgram>
146
+ | NodePath<BabelNodeFunction>
147
+ | NodePath<BabelNodeClass>,
148
+ ) {
146
149
  let name = getNameForPath(path);
147
150
  if (basename) {
148
151
  name = removeNamePrefix(name, basename);
@@ -151,7 +154,12 @@ function forEachMapping(
151
154
  pushFrame(name, nullthrows(path.node.loc));
152
155
  },
153
156
 
154
- exit(path): void {
157
+ exit(
158
+ path:
159
+ | NodePath<BabelNodeProgram>
160
+ | NodePath<BabelNodeFunction>
161
+ | NodePath<BabelNodeClass>,
162
+ ): void {
155
163
  popFrame();
156
164
  },
157
165
  };
@@ -164,7 +172,6 @@ function forEachMapping(
164
172
  }
165
173
 
166
174
  const ANONYMOUS_NAME = '<anonymous>';
167
- const CALLEES_TO_SKIP = ['Object.freeze'];
168
175
 
169
176
  /**
170
177
  * Derive a contextual name for the given AST node (Function, Program, Class or
@@ -236,13 +243,20 @@ function getNameForPath(path: NodePath<>): string {
236
243
 
237
244
  if (name == null) {
238
245
  // We couldn't find a name directly. Try the parent in certain cases.
239
- if (isCallExpression(parent) || isNewExpression(parent)) {
246
+ if (isAnyCallExpression(parent)) {
240
247
  // foo(function () {})
241
248
  const argIndex = parent.arguments.indexOf(node);
242
249
  if (argIndex !== -1) {
243
250
  const calleeName = getNameFromId(parent.callee);
244
251
  // var f = Object.freeze(function () {})
245
- if (CALLEES_TO_SKIP.indexOf(calleeName) !== -1) {
252
+ if (argIndex === 0 && calleeName === 'Object.freeze') {
253
+ return getNameForPath(nullthrows(parentPath));
254
+ }
255
+ // var f = useCallback(function () {})
256
+ if (
257
+ argIndex === 0 &&
258
+ (calleeName === 'useCallback' || calleeName === 'React.useCallback')
259
+ ) {
246
260
  return getNameForPath(nullthrows(parentPath));
247
261
  }
248
262
  if (calleeName) {
@@ -276,7 +290,6 @@ function getNameForPath(path: NodePath<>): string {
276
290
  name = className + separator + name;
277
291
  }
278
292
  } else if (isObjectExpression(propertyPath.parent)) {
279
- // $FlowFixMe[incompatible-use]
280
293
  const objectName = getNameForPath(nullthrows(propertyPath.parentPath));
281
294
  if (objectName !== ANONYMOUS_NAME) {
282
295
  name = objectName + '.' + name;
@@ -287,9 +300,19 @@ function getNameForPath(path: NodePath<>): string {
287
300
  return name;
288
301
  }
289
302
 
303
+ function isAnyCallExpression(node: Node): boolean %checks {
304
+ return (
305
+ node.type === 'CallExpression' ||
306
+ node.type === 'NewExpression' ||
307
+ node.type === 'OptionalCallExpression'
308
+ );
309
+ }
310
+
290
311
  function isAnyMemberExpression(node: Node): boolean %checks {
291
312
  return (
292
- node.type === 'MemberExpression' || node.type === 'JSXMemberExpression'
313
+ node.type === 'MemberExpression' ||
314
+ node.type === 'JSXMemberExpression' ||
315
+ node.type === 'OptionalMemberExpression'
293
316
  );
294
317
  }
295
318
 
@@ -322,7 +345,7 @@ function getNamePartsFromId(id: Node): $ReadOnlyArray<string> {
322
345
  return [];
323
346
  }
324
347
 
325
- if (isCallExpression(id) || isNewExpression(id)) {
348
+ if (isAnyCallExpression(id)) {
326
349
  return getNamePartsFromId(id.callee);
327
350
  }
328
351
 
@@ -488,7 +511,12 @@ class RelativeValue {
488
511
  }
489
512
  }
490
513
 
491
- function positionGreater(x, y) {
514
+ function positionGreater(
515
+ x: {column: number, line: number},
516
+ y:
517
+ | {column: number, line: number}
518
+ | $TEMPORARY$object<{column: number, line: number}>,
519
+ ) {
492
520
  return x.line > y.line || (x.line === y.line && x.column > y.column);
493
521
  }
494
522
 
package/src/source-map.js CHANGED
@@ -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.
@@ -9,20 +9,19 @@
9
9
  */
10
10
  "use strict";
11
11
 
12
- const Consumer = require("./Consumer");
12
+ const { BundleBuilder, createIndexMap } = require("./BundleBuilder");
13
13
 
14
- const Generator = require("./Generator");
14
+ const composeSourceMaps = require("./composeSourceMaps");
15
15
 
16
- const SourceMap = require("source-map");
16
+ const Consumer = require("./Consumer"); // We need to export this for `metro-symbolicate`
17
17
 
18
- // We need to export this for `metro-symbolicate`
19
18
  const normalizeSourcePath = require("./Consumer/normalizeSourcePath");
20
19
 
21
- const composeSourceMaps = require("./composeSourceMaps");
20
+ const { generateFunctionMap } = require("./generateFunctionMap");
22
21
 
23
- const { createIndexMap, BundleBuilder } = require("./BundleBuilder");
22
+ const Generator = require("./Generator");
24
23
 
25
- const { generateFunctionMap } = require("./generateFunctionMap");
24
+ const SourceMap = require("source-map");
26
25
 
27
26
  function fromRawMappingsImpl(isBlocking, onDone, modules, offsetLines) {
28
27
  const modulesToProcess = modules.slice();
@@ -91,7 +90,7 @@ function fromRawMappings(modules, offsetLines = 0) {
91
90
  let generator;
92
91
  fromRawMappingsImpl(
93
92
  true,
94
- g => {
93
+ (g) => {
95
94
  generator = g;
96
95
  },
97
96
  modules,
@@ -106,7 +105,7 @@ function fromRawMappings(modules, offsetLines = 0) {
106
105
  }
107
106
 
108
107
  async function fromRawMappingsNonBlocking(modules, offsetLines = 0) {
109
- return new Promise(resolve => {
108
+ return new Promise((resolve) => {
110
109
  fromRawMappingsImpl(false, resolve, modules, offsetLines);
111
110
  });
112
111
  }
@@ -117,18 +116,18 @@ async function fromRawMappingsNonBlocking(modules, offsetLines = 0) {
117
116
 
118
117
  function toBabelSegments(sourceMap) {
119
118
  const rawMappings = [];
120
- new SourceMap.SourceMapConsumer(sourceMap).eachMapping(map => {
119
+ new SourceMap.SourceMapConsumer(sourceMap).eachMapping((map) => {
121
120
  rawMappings.push({
122
121
  generated: {
123
122
  line: map.generatedLine,
124
- column: map.generatedColumn
123
+ column: map.generatedColumn,
125
124
  },
126
125
  original: {
127
126
  line: map.originalLine,
128
- column: map.originalColumn
127
+ column: map.originalColumn,
129
128
  },
130
129
  source: map.source,
131
- name: map.name
130
+ name: map.name,
132
131
  });
133
132
  });
134
133
  return rawMappings;
@@ -184,9 +183,9 @@ function addMapping(generator, mapping, carryOver) {
184
183
  }
185
184
  }
186
185
 
187
- function countLines(string) {
188
- return string.split("\n").length;
189
- }
186
+ const newline = /\r\n?|\n|\u2028|\u2029/g;
187
+
188
+ const countLines = (string) => (string.match(newline) || []).length + 1;
190
189
 
191
190
  module.exports = {
192
191
  BundleBuilder,
@@ -198,5 +197,5 @@ module.exports = {
198
197
  fromRawMappingsNonBlocking,
199
198
  normalizeSourcePath,
200
199
  toBabelSegments,
201
- toSegmentTuple
200
+ toSegmentTuple,
202
201
  };
@@ -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.
@@ -10,21 +10,19 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- const Consumer = require('./Consumer');
14
- const Generator = require('./Generator');
15
- const SourceMap = require('source-map');
16
-
17
13
  import type {IConsumer} from './Consumer/types.flow';
18
- export type {IConsumer};
14
+ import type {BabelSourceMapSegment} from '@babel/generator';
19
15
 
16
+ const {BundleBuilder, createIndexMap} = require('./BundleBuilder');
17
+ const composeSourceMaps = require('./composeSourceMaps');
18
+ const Consumer = require('./Consumer');
20
19
  // We need to export this for `metro-symbolicate`
21
20
  const normalizeSourcePath = require('./Consumer/normalizeSourcePath');
22
-
23
- const composeSourceMaps = require('./composeSourceMaps');
24
- const {createIndexMap, BundleBuilder} = require('./BundleBuilder');
25
21
  const {generateFunctionMap} = require('./generateFunctionMap');
22
+ const Generator = require('./Generator');
23
+ const SourceMap = require('source-map');
26
24
 
27
- import type {BabelSourceMapSegment} from '@babel/generator';
25
+ export type {IConsumer};
28
26
 
29
27
  type GeneratedCodeMapping = [number, number];
30
28
  type SourceMapping = [number, number, number, number];
@@ -241,7 +239,19 @@ function toSegmentTuple(
241
239
  return [line, column, original.line, original.column, name];
242
240
  }
243
241
 
244
- function addMappingsForFile(generator, mappings, module, carryOver) {
242
+ function addMappingsForFile(
243
+ generator: Generator,
244
+ mappings: Array<MetroSourceMapSegmentTuple>,
245
+ module: {
246
+ +code: string,
247
+ +functionMap: ?FBSourceFunctionMap,
248
+ +map: ?Array<MetroSourceMapSegmentTuple>,
249
+ +path: string,
250
+ +source: string,
251
+ ...
252
+ },
253
+ carryOver: number,
254
+ ) {
245
255
  generator.startFile(module.path, module.source, module.functionMap);
246
256
 
247
257
  for (let i = 0, n = mappings.length; i < n; ++i) {
@@ -251,7 +261,11 @@ function addMappingsForFile(generator, mappings, module, carryOver) {
251
261
  generator.endFile();
252
262
  }
253
263
 
254
- function addMapping(generator, mapping, carryOver) {
264
+ function addMapping(
265
+ generator: Generator,
266
+ mapping: MetroSourceMapSegmentTuple,
267
+ carryOver: number,
268
+ ) {
255
269
  const n = mapping.length;
256
270
  const line = mapping[0] + carryOver;
257
271
  // lines start at 1, columns start at 0
@@ -277,9 +291,10 @@ function addMapping(generator, mapping, carryOver) {
277
291
  }
278
292
  }
279
293
 
280
- function countLines(string) {
281
- return string.split('\n').length;
282
- }
294
+ const newline = /\r\n?|\n|\u2028|\u2029/g;
295
+
296
+ const countLines = (string: string): number =>
297
+ (string.match(newline) || []).length + 1;
283
298
 
284
299
  module.exports = {
285
300
  BundleBuilder,