-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransform-jsx-syntax.js
33 lines (26 loc) · 1.22 KB
/
transform-jsx-syntax.js
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
module.exports = function(babel) {
const { types: t } = babel;
const supportedElements = ['CLILabel', 'CLIButton', 'CLIPanel', 'CLICheckbox', 'CLIRadio', 'CLIImage', 'CLITextbox', 'CLIWebview', 'CLICanvas', 'CLICustom'];
return {
name: 'transform-jsx-syntax',
visitor: {
JSXElement(path) {
const elementName = path.node.openingElement.name.name;
if (supportedElements.includes(elementName)) {
const pathAttr = path.node.openingElement.attributes;
const pathProps = pathAttr.map((attr) => {
const key = attr.name.name;
let value;
if (t.isJSXExpressionContainer(attr.value)) value = attr.value.expression;
else value = attr.value;
return t.objectProperty(t.stringLiteral(key), value);
});
const newElement = t.newExpression(t.identifier(elementName), [
t.objectExpression(pathProps),
]);
path.replaceWith(newElement);
}
},
},
};
};