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