index.js
1.1 KB
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
45
46
47
48
49
50
51
52
"use strict";
exports.__esModule = true;
exports.default = void 0;
const addJSXAttribute = ({
types: t,
template
}, opts) => {
function getAttributeValue(value, literal) {
if (typeof value === 'string' && literal) {
return t.jsxExpressionContainer(template.ast(value).expression);
}
if (typeof value === 'string') {
return t.stringLiteral(value);
}
if (typeof value === 'boolean') {
return t.jsxExpressionContainer(t.booleanLiteral(value));
}
if (typeof value === 'number') {
return t.jsxExpressionContainer(t.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;
valuePath.replaceWith(getAttributeValue(newValue, literal));
});
}
}
};
};
var _default = addJSXAttribute;
exports.default = _default;