-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (31 loc) · 1.06 KB
/
index.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
export default function ({types: t}) {
return {
visitor: {
JSXElement: function (path) {
let { node } = path;
let ifAttr = node.openingElement.attributes
.find(({type, name}) => type === 'JSXAttribute' && name.name === 'r-if');
if (ifAttr == null) {
return;
}
let jsxOpeningElement = t.JSXOpeningElement(
node.openingElement.name,
node.openingElement.attributes
? node.openingElement.attributes.filter((attr)=> attr !== ifAttr)
: null
);
let jsxElement = t.JSXElement(
jsxOpeningElement,
node.closingElement,
node.children
);
let expression = t.conditionalExpression(
ifAttr.value.expression,
jsxElement,
t.nullLiteral()
);
path.replaceWith(expression);
},
}
}
}