metro-source-map 0.76.6 → 0.76.7

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.76.6",
3
+ "version": "0.76.7",
4
4
  "description": "🚇 Source map generator for Metro.",
5
5
  "main": "src/source-map.js",
6
6
  "repository": {
@@ -15,9 +15,9 @@
15
15
  "@babel/traverse": "^7.20.0",
16
16
  "@babel/types": "^7.20.0",
17
17
  "invariant": "^2.2.4",
18
- "metro-symbolicate": "0.76.6",
18
+ "metro-symbolicate": "0.76.7",
19
19
  "nullthrows": "^1.1.1",
20
- "ob1": "0.76.6",
20
+ "ob1": "0.76.7",
21
21
  "source-map": "^0.5.6",
22
22
  "vlq": "^1.0.0"
23
23
  },
@@ -18,6 +18,7 @@ function _interopRequireDefault(obj) {
18
18
  }
19
19
  const B64Builder = require("./B64Builder");
20
20
  const t = require("@babel/types");
21
+ const invariant = require("invariant");
21
22
  const nullthrows = require("nullthrows");
22
23
  const fsPath = require("path");
23
24
  /**
@@ -47,12 +48,51 @@ function generateFunctionMappingsArray(ast, context) {
47
48
  });
48
49
  return mappings;
49
50
  }
51
+ function functionMapBabelPlugin() {
52
+ return {
53
+ // Eagerly traverse the tree on `pre`, before any visitors have run, so
54
+ // that regardless of plugin order we're dealing with the AST before any
55
+ // mutations.
56
+ visitor: {},
57
+ pre: ({ path, metadata, opts }) => {
58
+ const { filename } = nullthrows(opts);
59
+ const encoder = new MappingEncoder();
60
+ const visitor = getFunctionMapVisitor(
61
+ {
62
+ filename,
63
+ },
64
+ (mapping) => encoder.push(mapping)
65
+ );
66
+ invariant(
67
+ path && t.isProgram(path.node),
68
+ "path missing or not a program node"
69
+ );
70
+ // $FlowFixMe[prop-missing] checked above
71
+ // $FlowFixMe[incompatible-type-arg] checked above
72
+ const programPath = path;
73
+ visitor.enter(programPath);
74
+ programPath.traverse({
75
+ Function: visitor,
76
+ Class: visitor,
77
+ });
78
+ visitor.exit(programPath);
50
79
 
51
- /**
52
- * Traverses a Babel AST and calls the supplied callback with function name
53
- * mappings, one at a time.
54
- */
55
- function forEachMapping(ast, context, pushMapping) {
80
+ // $FlowFixMe[prop-missing] Babel `File` is not generically typed
81
+ const metroMetadata = metadata;
82
+ const functionMap = encoder.getResult();
83
+
84
+ // Set the result on a metadata property
85
+ if (!metroMetadata.metro) {
86
+ metroMetadata.metro = {
87
+ functionMap,
88
+ };
89
+ } else {
90
+ metroMetadata.metro.functionMap = functionMap;
91
+ }
92
+ },
93
+ };
94
+ }
95
+ function getFunctionMapVisitor(context, pushMapping) {
56
96
  const nameStack = [];
57
97
  let tailPos = {
58
98
  line: 1,
@@ -96,7 +136,7 @@ function forEachMapping(ast, context, pushMapping) {
96
136
  const basename = context.filename
97
137
  ? fsPath.basename(context.filename).replace(/\..+$/, "")
98
138
  : null;
99
- const visitor = {
139
+ return {
100
140
  enter(path) {
101
141
  let name = getNameForPath(path);
102
142
  if (basename) {
@@ -108,6 +148,14 @@ function forEachMapping(ast, context, pushMapping) {
108
148
  popFrame();
109
149
  },
110
150
  };
151
+ }
152
+
153
+ /**
154
+ * Traverses a Babel AST and calls the supplied callback with function name
155
+ * mappings, one at a time.
156
+ */
157
+ function forEachMapping(ast, context, pushMapping) {
158
+ const visitor = getFunctionMapVisitor(context, pushMapping);
111
159
 
112
160
  // Traversing populates/pollutes the path cache (`traverse.cache.path`) with
113
161
  // values missing the `hub` property needed by Babel transformation, so we
@@ -440,6 +488,7 @@ function positionGreater(x, y) {
440
488
  return x.line > y.line || (x.line === y.line && x.column > y.column);
441
489
  }
442
490
  module.exports = {
491
+ functionMapBabelPlugin,
443
492
  generateFunctionMap,
444
493
  generateFunctionMappingsArray,
445
494
  };
@@ -11,7 +11,9 @@
11
11
 
12
12
  'use strict';
13
13
 
14
+ import type {MetroBabelFileMetadata} from 'metro-babel-transformer';
14
15
  import type {FBSourceFunctionMap} from './source-map';
16
+ import type {PluginObj} from '@babel/core';
15
17
  import type {NodePath} from '@babel/traverse';
16
18
  import type {Node} from '@babel/types';
17
19
 
@@ -42,6 +44,7 @@ import {
42
44
 
43
45
  const B64Builder = require('./B64Builder');
44
46
  const t = require('@babel/types');
47
+ const invariant = require('invariant');
45
48
  const nullthrows = require('nullthrows');
46
49
  const fsPath = require('path');
47
50
 
@@ -55,7 +58,21 @@ type RangeMapping = {
55
58
  start: Position,
56
59
  ...
57
60
  };
58
- export type Context = {filename?: string, ...};
61
+ type FunctionMapVisitor = {
62
+ enter: (
63
+ path:
64
+ | NodePath<BabelNodeProgram>
65
+ | NodePath<BabelNodeFunction>
66
+ | NodePath<BabelNodeClass>,
67
+ ) => void,
68
+ exit: (
69
+ path:
70
+ | NodePath<BabelNodeProgram>
71
+ | NodePath<BabelNodeFunction>
72
+ | NodePath<BabelNodeClass>,
73
+ ) => void,
74
+ };
75
+ export type Context = {filename?: ?string, ...};
59
76
 
60
77
  /**
61
78
  * Generate a map of source positions to function names. The names are meant to
@@ -91,15 +108,52 @@ function generateFunctionMappingsArray(
91
108
  return mappings;
92
109
  }
93
110
 
94
- /**
95
- * Traverses a Babel AST and calls the supplied callback with function name
96
- * mappings, one at a time.
97
- */
98
- function forEachMapping(
99
- ast: BabelNode,
111
+ function functionMapBabelPlugin(): PluginObj<> {
112
+ return {
113
+ // Eagerly traverse the tree on `pre`, before any visitors have run, so
114
+ // that regardless of plugin order we're dealing with the AST before any
115
+ // mutations.
116
+ visitor: {},
117
+ pre: ({path, metadata, opts}) => {
118
+ const {filename} = nullthrows(opts);
119
+ const encoder = new MappingEncoder();
120
+ const visitor = getFunctionMapVisitor({filename}, mapping =>
121
+ encoder.push(mapping),
122
+ );
123
+ invariant(
124
+ path && t.isProgram(path.node),
125
+ 'path missing or not a program node',
126
+ );
127
+ // $FlowFixMe[prop-missing] checked above
128
+ // $FlowFixMe[incompatible-type-arg] checked above
129
+ const programPath: NodePath<BabelNodeProgram> = path;
130
+
131
+ visitor.enter(programPath);
132
+ programPath.traverse({
133
+ Function: visitor,
134
+ Class: visitor,
135
+ });
136
+ visitor.exit(programPath);
137
+
138
+ // $FlowFixMe[prop-missing] Babel `File` is not generically typed
139
+ const metroMetadata: MetroBabelFileMetadata = metadata;
140
+
141
+ const functionMap = encoder.getResult();
142
+
143
+ // Set the result on a metadata property
144
+ if (!metroMetadata.metro) {
145
+ metroMetadata.metro = {functionMap};
146
+ } else {
147
+ metroMetadata.metro.functionMap = functionMap;
148
+ }
149
+ },
150
+ };
151
+ }
152
+
153
+ function getFunctionMapVisitor(
100
154
  context: ?Context,
101
155
  pushMapping: RangeMapping => void,
102
- ) {
156
+ ): FunctionMapVisitor {
103
157
  const nameStack: Array<{loc: BabelNodeSourceLocation, name: string}> = [];
104
158
  let tailPos = {line: 1, column: 0};
105
159
  let tailName = null;
@@ -140,30 +194,31 @@ function forEachMapping(
140
194
  ? fsPath.basename(context.filename).replace(/\..+$/, '')
141
195
  : null;
142
196
 
143
- const visitor = {
144
- enter(
145
- path:
146
- | NodePath<BabelNodeProgram>
147
- | NodePath<BabelNodeFunction>
148
- | NodePath<BabelNodeClass>,
149
- ) {
197
+ return {
198
+ enter(path) {
150
199
  let name = getNameForPath(path);
151
200
  if (basename) {
152
201
  name = removeNamePrefix(name, basename);
153
202
  }
154
-
155
203
  pushFrame(name, nullthrows(path.node.loc));
156
204
  },
157
205
 
158
- exit(
159
- path:
160
- | NodePath<BabelNodeProgram>
161
- | NodePath<BabelNodeFunction>
162
- | NodePath<BabelNodeClass>,
163
- ): void {
206
+ exit(path) {
164
207
  popFrame();
165
208
  },
166
209
  };
210
+ }
211
+
212
+ /**
213
+ * Traverses a Babel AST and calls the supplied callback with function name
214
+ * mappings, one at a time.
215
+ */
216
+ function forEachMapping(
217
+ ast: BabelNode,
218
+ context: ?Context,
219
+ pushMapping: RangeMapping => void,
220
+ ) {
221
+ const visitor = getFunctionMapVisitor(context, pushMapping);
167
222
 
168
223
  // Traversing populates/pollutes the path cache (`traverse.cache.path`) with
169
224
  // values missing the `hub` property needed by Babel transformation, so we
@@ -525,4 +580,8 @@ function positionGreater(x: Position, y: Position) {
525
580
  return x.line > y.line || (x.line === y.line && x.column > y.column);
526
581
  }
527
582
 
528
- module.exports = {generateFunctionMap, generateFunctionMappingsArray};
583
+ module.exports = {
584
+ functionMapBabelPlugin,
585
+ generateFunctionMap,
586
+ generateFunctionMappingsArray,
587
+ };
package/src/source-map.js CHANGED
@@ -16,7 +16,10 @@ const composeSourceMaps = require("./composeSourceMaps");
16
16
  const Consumer = require("./Consumer");
17
17
  // We need to export this for `metro-symbolicate`
18
18
  const normalizeSourcePath = require("./Consumer/normalizeSourcePath");
19
- const { generateFunctionMap } = require("./generateFunctionMap");
19
+ const {
20
+ functionMapBabelPlugin,
21
+ generateFunctionMap,
22
+ } = require("./generateFunctionMap");
20
23
  const Generator = require("./Generator");
21
24
  // $FlowFixMe[untyped-import] - source-map
22
25
  const SourceMap = require("source-map");
@@ -182,6 +185,7 @@ module.exports = {
182
185
  generateFunctionMap,
183
186
  fromRawMappings,
184
187
  fromRawMappingsNonBlocking,
188
+ functionMapBabelPlugin,
185
189
  normalizeSourcePath,
186
190
  toBabelSegments,
187
191
  toSegmentTuple,
@@ -19,7 +19,10 @@ const composeSourceMaps = require('./composeSourceMaps');
19
19
  const Consumer = require('./Consumer');
20
20
  // We need to export this for `metro-symbolicate`
21
21
  const normalizeSourcePath = require('./Consumer/normalizeSourcePath');
22
- const {generateFunctionMap} = require('./generateFunctionMap');
22
+ const {
23
+ functionMapBabelPlugin,
24
+ generateFunctionMap,
25
+ } = require('./generateFunctionMap');
23
26
  const Generator = require('./Generator');
24
27
  // $FlowFixMe[untyped-import] - source-map
25
28
  const SourceMap = require('source-map');
@@ -338,6 +341,7 @@ module.exports = {
338
341
  generateFunctionMap,
339
342
  fromRawMappings,
340
343
  fromRawMappingsNonBlocking,
344
+ functionMapBabelPlugin,
341
345
  normalizeSourcePath,
342
346
  toBabelSegments,
343
347
  toSegmentTuple,