| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 | const positionMethod = {
|
|---|
| 6 | start: 'unshiftContainer',
|
|---|
| 7 | end: 'pushContainer'
|
|---|
| 8 | };
|
|---|
| 9 |
|
|---|
| 10 | const 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 |
|
|---|
| 101 | var _default = addJSXAttribute;
|
|---|
| 102 | exports.default = _default; |
|---|