File size: 1,223 Bytes
a053984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';

var core = require('@babel/core');

const addJSXAttribute = (api, opts) => {
  const getAttributeValue = (value, literal) => {
    if (typeof value === "string" && literal) {
      return core.types.jsxExpressionContainer(
        core.template.ast(value).expression
      );
    }
    if (typeof value === "string") {
      return core.types.stringLiteral(value);
    }
    if (typeof value === "boolean") {
      return core.types.jsxExpressionContainer(core.types.booleanLiteral(value));
    }
    if (typeof value === "number") {
      return core.types.jsxExpressionContainer(core.types.numericLiteral(value));
    }
    return null;
  };
  return {
    visitor: {
      JSXAttribute(path) {
        const valuePath = path.get("value");
        if (!valuePath.isStringLiteral())
          return;
        opts.values.forEach(({ value, newValue, literal }) => {
          if (!valuePath.isStringLiteral({ value }))
            return;
          const attributeValue = getAttributeValue(newValue, literal);
          if (attributeValue) {
            valuePath.replaceWith(attributeValue);
          }
        });
      }
    }
  };
};

module.exports = addJSXAttribute;
//# sourceMappingURL=index.js.map