@sentry/babel-plugin-component-annotate 4.6.2 → 4.8.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/dist/cjs/index.js CHANGED
@@ -144,6 +144,461 @@ var KNOWN_INCOMPATIBLE_PLUGINS = [
144
144
  "@react-navigation"];
145
145
  var DEFAULT_IGNORED_ELEMENTS = ["a", "abbr", "address", "area", "article", "aside", "audio", "b", "base", "bdi", "bdo", "blockquote", "body", "br", "button", "canvas", "caption", "cite", "code", "col", "colgroup", "data", "datalist", "dd", "del", "details", "dfn", "dialog", "div", "dl", "dt", "em", "embed", "fieldset", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", "i", "iframe", "img", "input", "ins", "kbd", "keygen", "label", "legend", "li", "link", "main", "map", "mark", "menu", "menuitem", "meter", "nav", "noscript", "object", "ol", "optgroup", "option", "output", "p", "param", "pre", "progress", "q", "rb", "rp", "rt", "rtc", "ruby", "s", "samp", "script", "section", "select", "small", "source", "span", "strong", "style", "sub", "summary", "sup", "table", "tbody", "td", "template", "textarea", "tfoot", "th", "thead", "time", "title", "tr", "track", "u", "ul", "var", "video", "wbr"];
146
146
 
147
+ /**
148
+ * MIT License
149
+ *
150
+ * Copyright (c) 2020 Engineering at FullStory
151
+ *
152
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
153
+ * of this software and associated documentation files (the "Software"), to deal
154
+ * in the Software without restriction, including without limitation the rights
155
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
156
+ * copies of the Software, and to permit persons to whom the Software is
157
+ * furnished to do so, subject to the following conditions:
158
+ *
159
+ * The above copyright notice and this permission notice shall be included in all
160
+ * copies or substantial portions of the Software.
161
+ *
162
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
165
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
166
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
167
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
168
+ * SOFTWARE.
169
+ *
170
+ */
171
+
172
+ /**
173
+ * The following code is based on the FullStory Babel plugin, but has been modified to work
174
+ * with Sentry products:
175
+ *
176
+ * - Added `sentry` to data properties, i.e `data-sentry-component`
177
+ * - Converted to TypeScript
178
+ * - Code cleanups
179
+ * - Highly modified to inject the data attributes into the root HTML elements of a component.
180
+ */
181
+
182
+ var REACT_NATIVE_ELEMENTS = ["Image", "Text", "View", "ScrollView", "TextInput", "TouchableOpacity", "TouchableHighlight", "TouchableWithoutFeedback", "FlatList", "SectionList", "ActivityIndicator", "Button", "Switch", "Modal", "SafeAreaView", "StatusBar", "KeyboardAvoidingView", "RefreshControl", "Picker", "Slider"];
183
+
184
+ // Shared context object for all JSX processing functions
185
+
186
+ // We must export the plugin as default, otherwise the Babel loader will not be able to resolve it when configured using its string identifier
187
+ function experimentalComponentNameAnnotatePlugin(_ref) {
188
+ var t = _ref.types;
189
+ return {
190
+ visitor: {
191
+ Program: {
192
+ enter: function enter(path, state) {
193
+ var fragmentContext = collectFragmentContext$1(path);
194
+ state.sentryFragmentContext = fragmentContext;
195
+ }
196
+ },
197
+ FunctionDeclaration: function FunctionDeclaration(path, state) {
198
+ if (!path.node.id || !path.node.id.name) {
199
+ return;
200
+ }
201
+ var context = createJSXProcessingContext$1(state, t, path.node.id.name);
202
+ functionBodyPushAttributes$1(context, path);
203
+ },
204
+ ArrowFunctionExpression: function ArrowFunctionExpression(path, state) {
205
+ // We're expecting a `VariableDeclarator` like `const MyComponent =`
206
+ var parent = path.parent;
207
+ if (!parent || !("id" in parent) || !parent.id || !("name" in parent.id) || !parent.id.name) {
208
+ return;
209
+ }
210
+ var context = createJSXProcessingContext$1(state, t, parent.id.name);
211
+ functionBodyPushAttributes$1(context, path);
212
+ },
213
+ ClassDeclaration: function ClassDeclaration(path, state) {
214
+ var _name$node;
215
+ var name = path.get("id");
216
+ var properties = path.get("body").get("body");
217
+ var render = properties.find(function (prop) {
218
+ return prop.isClassMethod() && prop.get("key").isIdentifier({
219
+ name: "render"
220
+ });
221
+ });
222
+ if (!render || !render.traverse) {
223
+ return;
224
+ }
225
+ var context = createJSXProcessingContext$1(state, t, ((_name$node = name.node) === null || _name$node === void 0 ? void 0 : _name$node.name) || "");
226
+ render.traverse({
227
+ ReturnStatement: function ReturnStatement(returnStatement) {
228
+ var arg = returnStatement.get("argument");
229
+ if (!arg.isJSXElement() && !arg.isJSXFragment()) {
230
+ return;
231
+ }
232
+ processJSX$1(context, arg);
233
+ }
234
+ });
235
+ }
236
+ }
237
+ };
238
+ }
239
+
240
+ /**
241
+ * Checks if an element name represents an HTML element (as opposed to a React component).
242
+ * HTML elements include standard lowercase HTML tags and React Native elements.
243
+ */
244
+ function isHtmlElement(elementName) {
245
+ // Unknown elements are not HTML elements
246
+ if (elementName === UNKNOWN_ELEMENT_NAME$1) {
247
+ return false;
248
+ }
249
+
250
+ // Check for lowercase first letter (standard HTML elements)
251
+ if (elementName.length > 0 && elementName.charAt(0) === elementName.charAt(0).toLowerCase()) {
252
+ return true;
253
+ }
254
+
255
+ // React Native elements typically start with uppercase but are still "native" elements
256
+ // We consider them HTML-like elements for annotation purposes
257
+ if (REACT_NATIVE_ELEMENTS.includes(elementName)) {
258
+ return true;
259
+ }
260
+
261
+ // Otherwise, assume it's a React component (PascalCase)
262
+ return false;
263
+ }
264
+
265
+ /**
266
+ * Creates a JSX processing context from the plugin state
267
+ */
268
+ function createJSXProcessingContext$1(state, t, componentName) {
269
+ var _state$opts$ignoredCo;
270
+ return {
271
+ t: t,
272
+ componentName: componentName,
273
+ attributeName: attributeNamesFromState$1(state),
274
+ ignoredComponents: (_state$opts$ignoredCo = state.opts.ignoredComponents) !== null && _state$opts$ignoredCo !== void 0 ? _state$opts$ignoredCo : [],
275
+ fragmentContext: state.sentryFragmentContext
276
+ };
277
+ }
278
+
279
+ /**
280
+ * Processes the body of a function to add Sentry tracking attributes to JSX elements.
281
+ * Handles various function body structures including direct JSX returns, conditional expressions,
282
+ * and nested JSX elements.
283
+ */
284
+ function functionBodyPushAttributes$1(context, path) {
285
+ var jsxNode;
286
+ var functionBody = path.get("body").get("body");
287
+ if (!("length" in functionBody) && functionBody.parent && (functionBody.parent.type === "JSXElement" || functionBody.parent.type === "JSXFragment")) {
288
+ var maybeJsxNode = functionBody.find(function (c) {
289
+ return c.type === "JSXElement" || c.type === "JSXFragment";
290
+ });
291
+ if (!maybeJsxNode) {
292
+ return;
293
+ }
294
+ jsxNode = maybeJsxNode;
295
+ } else {
296
+ var returnStatement = functionBody.find(function (c) {
297
+ return c.type === "ReturnStatement";
298
+ });
299
+ if (!returnStatement) {
300
+ return;
301
+ }
302
+ var arg = returnStatement.get("argument");
303
+ if (!arg) {
304
+ return;
305
+ }
306
+ if (Array.isArray(arg)) {
307
+ return;
308
+ }
309
+
310
+ // Handle the case of a function body returning a ternary operation.
311
+ // `return (maybeTrue ? '' : (<SubComponent />))`
312
+ if (arg.isConditionalExpression()) {
313
+ var consequent = arg.get("consequent");
314
+ if (consequent.isJSXFragment() || consequent.isJSXElement()) {
315
+ processJSX$1(context, consequent);
316
+ }
317
+ var alternate = arg.get("alternate");
318
+ if (alternate.isJSXFragment() || alternate.isJSXElement()) {
319
+ processJSX$1(context, alternate);
320
+ }
321
+ return;
322
+ }
323
+ if (!arg.isJSXFragment() && !arg.isJSXElement()) {
324
+ return;
325
+ }
326
+ jsxNode = arg;
327
+ }
328
+ if (!jsxNode) {
329
+ return;
330
+ }
331
+ processJSX$1(context, jsxNode);
332
+ }
333
+
334
+ /**
335
+ * Recursively processes JSX elements to add Sentry tracking attributes.
336
+ * Handles both JSX elements and fragments, applying appropriate attributes
337
+ * based on configuration and component context.
338
+ */
339
+ function processJSX$1(context, jsxNode) {
340
+ if (!jsxNode) {
341
+ return;
342
+ }
343
+
344
+ // NOTE: I don't know of a case where `openingElement` would have more than one item,
345
+ // but it's safer to always iterate
346
+ var paths = jsxNode.get("openingElement");
347
+ var openingElements = Array.isArray(paths) ? paths : [paths];
348
+ var hasInjectedAttributes = openingElements.reduce(function (prev, openingElement) {
349
+ return prev || applyAttributes$1(context, openingElement, context.componentName);
350
+ }, false);
351
+ if (hasInjectedAttributes) {
352
+ return;
353
+ }
354
+ var children = jsxNode.get("children");
355
+ // TODO: See why `Array.isArray` doesn't have correct behaviour here
356
+ if (children && !("length" in children)) {
357
+ // A single child was found, maybe a bit of static text
358
+ children = [children];
359
+ }
360
+ children.forEach(function (child) {
361
+ // Happens for some node types like plain text
362
+ if (!child.node) {
363
+ return;
364
+ }
365
+
366
+ // If the current element is a fragment, children are still considered at root level
367
+ // Otherwise, children are not at root level
368
+ var openingElement = child.get("openingElement");
369
+ // TODO: Improve this. We never expect to have multiple opening elements
370
+ // but if it's possible, this should work
371
+ if (Array.isArray(openingElement)) {
372
+ return;
373
+ }
374
+ processJSX$1(context, child);
375
+ });
376
+ }
377
+
378
+ /**
379
+ * Applies Sentry tracking attributes to a JSX opening element.
380
+ * Adds component name, element name, and source file attributes while
381
+ * respecting ignore lists and fragment detection.
382
+ */
383
+ function applyAttributes$1(context, openingElement, componentName) {
384
+ var t = context.t,
385
+ componentAttributeName = context.attributeName,
386
+ ignoredComponents = context.ignoredComponents,
387
+ fragmentContext = context.fragmentContext;
388
+
389
+ // e.g., Raw JSX text like the `A` in `<h1>a</h1>`
390
+ if (!openingElement.node) {
391
+ return false;
392
+ }
393
+
394
+ // Check if this is a React fragment - if so, skip attribute addition entirely
395
+ var isFragment = isReactFragment$1(t, openingElement, fragmentContext);
396
+ if (isFragment) {
397
+ return false;
398
+ }
399
+ if (!openingElement.node.attributes) {
400
+ openingElement.node.attributes = [];
401
+ }
402
+ var elementName = getPathName$1(t, openingElement);
403
+ if (!isHtmlElement(elementName)) {
404
+ return false;
405
+ }
406
+ var isAnIgnoredComponent = ignoredComponents.some(function (ignoredComponent) {
407
+ return ignoredComponent === componentName || ignoredComponent === elementName;
408
+ });
409
+
410
+ // Add a stable attribute for the component name (only for root elements)
411
+ if (!isAnIgnoredComponent && !hasAttributeWithName$1(openingElement, componentAttributeName)) {
412
+ if (componentAttributeName) {
413
+ openingElement.node.attributes.push(t.jSXAttribute(t.jSXIdentifier(componentAttributeName), t.stringLiteral(componentName)));
414
+ }
415
+ }
416
+ return true;
417
+ }
418
+ function attributeNamesFromState$1(state) {
419
+ if (state.opts["native"]) {
420
+ return "dataSentryComponent";
421
+ }
422
+ return "data-sentry-component";
423
+ }
424
+ function collectFragmentContext$1(programPath) {
425
+ var fragmentAliases = new Set();
426
+ var reactNamespaceAliases = new Set(["React"]); // Default React namespace
427
+
428
+ programPath.traverse({
429
+ ImportDeclaration: function ImportDeclaration(importPath) {
430
+ var source = importPath.node.source.value;
431
+
432
+ // Handle React imports
433
+ if (source === "react" || source === "React") {
434
+ importPath.node.specifiers.forEach(function (spec) {
435
+ if (spec.type === "ImportSpecifier" && spec.imported.type === "Identifier") {
436
+ // Detect aliased React.Fragment imports (e.g., `Fragment as F`)
437
+ // so we can later identify <F> as a fragment in JSX.
438
+ if (spec.imported.name === "Fragment") {
439
+ fragmentAliases.add(spec.local.name);
440
+ }
441
+ } else if (spec.type === "ImportDefaultSpecifier" || spec.type === "ImportNamespaceSpecifier") {
442
+ // import React from 'react' -> React OR
443
+ // import * as React from 'react' -> React
444
+ reactNamespaceAliases.add(spec.local.name);
445
+ }
446
+ });
447
+ }
448
+ },
449
+ // Handle simple variable assignments only (avoid complex cases)
450
+ VariableDeclarator: function VariableDeclarator(varPath) {
451
+ if (varPath.node.init) {
452
+ var init = varPath.node.init;
453
+
454
+ // Handle identifier assignments: const MyFragment = Fragment
455
+ if (varPath.node.id.type === "Identifier") {
456
+ // Handle: const MyFragment = Fragment (only if Fragment is a known alias)
457
+ if (init.type === "Identifier" && fragmentAliases.has(init.name)) {
458
+ fragmentAliases.add(varPath.node.id.name);
459
+ }
460
+
461
+ // Handle: const MyFragment = React.Fragment (only for known React namespaces)
462
+ if (init.type === "MemberExpression" && init.object.type === "Identifier" && init.property.type === "Identifier" && init.property.name === "Fragment" && reactNamespaceAliases.has(init.object.name)) {
463
+ fragmentAliases.add(varPath.node.id.name);
464
+ }
465
+ }
466
+
467
+ // Handle destructuring assignments: const { Fragment } = React
468
+ if (varPath.node.id.type === "ObjectPattern") {
469
+ if (init.type === "Identifier" && reactNamespaceAliases.has(init.name)) {
470
+ var properties = varPath.node.id.properties;
471
+ var _iterator = _createForOfIteratorHelper(properties),
472
+ _step;
473
+ try {
474
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
475
+ var prop = _step.value;
476
+ if (prop.type === "ObjectProperty" && prop.key && prop.key.type === "Identifier" && prop.value && prop.value.type === "Identifier" && prop.key.name === "Fragment") {
477
+ fragmentAliases.add(prop.value.name);
478
+ }
479
+ }
480
+ } catch (err) {
481
+ _iterator.e(err);
482
+ } finally {
483
+ _iterator.f();
484
+ }
485
+ }
486
+ }
487
+ }
488
+ }
489
+ });
490
+ return {
491
+ fragmentAliases: fragmentAliases,
492
+ reactNamespaceAliases: reactNamespaceAliases
493
+ };
494
+ }
495
+ function isReactFragment$1(t, openingElement, context) {
496
+ // Handle JSX fragments (<>)
497
+ if (openingElement.isJSXFragment()) {
498
+ return true;
499
+ }
500
+ var elementName = getPathName$1(t, openingElement);
501
+
502
+ // Direct fragment references
503
+ if (elementName === "Fragment" || elementName === "React.Fragment") {
504
+ return true;
505
+ }
506
+
507
+ // TODO: All these objects are typed as unknown, maybe an oversight in Babel types?
508
+
509
+ // Check if the element name is a known fragment alias
510
+ if (context && elementName && context.fragmentAliases.has(elementName)) {
511
+ return true;
512
+ }
513
+
514
+ // Handle JSXMemberExpression
515
+ if (openingElement.node && "name" in openingElement.node && openingElement.node.name && _typeof(openingElement.node.name) === "object" && "type" in openingElement.node.name && openingElement.node.name.type === "JSXMemberExpression") {
516
+ var nodeName = openingElement.node.name;
517
+ if (_typeof(nodeName) !== "object" || !nodeName) {
518
+ return false;
519
+ }
520
+ if ("object" in nodeName && "property" in nodeName) {
521
+ var nodeNameObject = nodeName.object;
522
+ var nodeNameProperty = nodeName.property;
523
+ if (_typeof(nodeNameObject) !== "object" || _typeof(nodeNameProperty) !== "object") {
524
+ return false;
525
+ }
526
+ if (!nodeNameObject || !nodeNameProperty) {
527
+ return false;
528
+ }
529
+ var objectName = "name" in nodeNameObject && nodeNameObject.name;
530
+ var propertyName = "name" in nodeNameProperty && nodeNameProperty.name;
531
+
532
+ // React.Fragment check
533
+ if (objectName === "React" && propertyName === "Fragment") {
534
+ return true;
535
+ }
536
+
537
+ // Enhanced checks using context
538
+ if (context) {
539
+ // Check React.Fragment pattern with known React namespaces
540
+ if (context.reactNamespaceAliases.has(objectName) && propertyName === "Fragment") {
541
+ return true;
542
+ }
543
+
544
+ // Check MyFragment.Fragment pattern
545
+ if (context.fragmentAliases.has(objectName) && propertyName === "Fragment") {
546
+ return true;
547
+ }
548
+ }
549
+ }
550
+ }
551
+ return false;
552
+ }
553
+ function hasAttributeWithName$1(openingElement, name) {
554
+ if (!name) {
555
+ return false;
556
+ }
557
+ return openingElement.node.attributes.some(function (node) {
558
+ if (node.type === "JSXAttribute") {
559
+ return node.name.name === name;
560
+ }
561
+ return false;
562
+ });
563
+ }
564
+ function getPathName$1(t, path) {
565
+ if (!path.node) return UNKNOWN_ELEMENT_NAME$1;
566
+ if (!("name" in path.node)) {
567
+ return UNKNOWN_ELEMENT_NAME$1;
568
+ }
569
+ var name = path.node.name;
570
+ if (typeof name === "string") {
571
+ return name;
572
+ }
573
+ if (t.isIdentifier(name) || t.isJSXIdentifier(name)) {
574
+ return name.name;
575
+ }
576
+ if (t.isJSXNamespacedName(name)) {
577
+ return name.name.name;
578
+ }
579
+
580
+ // Handle JSX member expressions like Tab.Group
581
+ if (t.isJSXMemberExpression(name)) {
582
+ var objectName = getJSXMemberExpressionObjectName$1(t, name.object);
583
+ var propertyName = name.property.name;
584
+ return "".concat(objectName, ".").concat(propertyName);
585
+ }
586
+ return UNKNOWN_ELEMENT_NAME$1;
587
+ }
588
+
589
+ // Recursively handle nested member expressions (e.g. Components.UI.Header)
590
+ function getJSXMemberExpressionObjectName$1(t, object) {
591
+ if (t.isJSXIdentifier(object)) {
592
+ return object.name;
593
+ }
594
+ if (t.isJSXMemberExpression(object)) {
595
+ var objectName = getJSXMemberExpressionObjectName$1(t, object.object);
596
+ return "".concat(objectName, ".").concat(object.property.name);
597
+ }
598
+ return UNKNOWN_ELEMENT_NAME$1;
599
+ }
600
+ var UNKNOWN_ELEMENT_NAME$1 = "unknown";
601
+
147
602
  var webComponentName = "data-sentry-component";
148
603
  var webElementName = "data-sentry-element";
149
604
  var webSourceFileName = "data-sentry-source-file";
@@ -151,8 +606,6 @@ var nativeComponentName = "dataSentryComponent";
151
606
  var nativeElementName = "dataSentryElement";
152
607
  var nativeSourceFileName = "dataSentrySourceFile";
153
608
 
154
- // Shared context object for all JSX processing functions
155
-
156
609
  // We must export the plugin as default, otherwise the Babel loader will not be able to resolve it when configured using its string identifier
157
610
  function componentNameAnnotatePlugin(_ref) {
158
611
  var t = _ref.types;
@@ -614,4 +1067,5 @@ function getJSXMemberExpressionObjectName(t, object) {
614
1067
  var UNKNOWN_ELEMENT_NAME = "unknown";
615
1068
 
616
1069
  exports["default"] = componentNameAnnotatePlugin;
1070
+ exports.experimentalComponentNameAnnotatePlugin = experimentalComponentNameAnnotatePlugin;
617
1071
  //# sourceMappingURL=index.js.map