source: frontend/node_modules/@svgr/babel-plugin-add-jsx-attribute/lib/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.2 KB
Line 
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5const positionMethod = {
6 start: 'unshiftContainer',
7 end: 'pushContainer'
8};
9
10const addJSXAttribute = ({
11 types: t,
12 template
13}, opts) => {
14 function getAttributeValue({
15 literal,
16 value
17 }) {
18 if (typeof value === 'boolean') {
19 return t.jsxExpressionContainer(t.booleanLiteral(value));
20 }
21
22 if (typeof value === 'number') {
23 return t.jsxExpressionContainer(t.numericLiteral(value));
24 }
25
26 if (typeof value === 'string' && literal) {
27 return t.jsxExpressionContainer(template.ast(value).expression);
28 }
29
30 if (typeof value === 'string') {
31 return t.stringLiteral(value);
32 }
33
34 return null;
35 }
36
37 function getAttribute({
38 spread,
39 name,
40 value,
41 literal
42 }) {
43 if (spread) {
44 return t.jsxSpreadAttribute(t.identifier(name));
45 }
46
47 return t.jsxAttribute(t.jsxIdentifier(name), getAttributeValue({
48 value,
49 literal
50 }));
51 }
52
53 return {
54 visitor: {
55 JSXOpeningElement(path) {
56 if (!opts.elements.includes(path.node.name.name)) return;
57 opts.attributes.forEach(({
58 name,
59 value = null,
60 spread = false,
61 literal = false,
62 position = 'end'
63 }) => {
64 const method = positionMethod[position];
65 const newAttribute = getAttribute({
66 spread,
67 name,
68 value,
69 literal
70 });
71 const attributes = path.get('attributes');
72
73 const isEqualAttribute = attribute => {
74 if (spread) {
75 return attribute.get('argument').isIdentifier({
76 name
77 });
78 }
79
80 return attribute.get('name').isJSXIdentifier({
81 name
82 });
83 };
84
85 const replaced = attributes.some(attribute => {
86 if (!isEqualAttribute(attribute)) return false;
87 attribute.replaceWith(newAttribute);
88 return true;
89 });
90
91 if (!replaced) {
92 path[method]('attributes', newAttribute);
93 }
94 });
95 }
96
97 }
98 };
99};
100
101var _default = addJSXAttribute;
102exports.default = _default;
Note: See TracBrowser for help on using the repository browser.