-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
32 lines (32 loc) · 1.24 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 JsxDisplayIf({types: t}) {
return {
visitor: {
JSXElement: function transform(path) {
let { node } = path;
let ifAttributes = node.openingElement.attributes
.filter(({type, name}) => type === 'JSXAttribute' && name.name === 'display-if');
if (!ifAttributes.length) {
return;
}
let ifAttribute = ifAttributes[0];
let newJsxOpeningElement = t.JSXOpeningElement(
node.openingElement.name,
node.openingElement.attributes
? node.openingElement.attributes.filter((attr)=> attr !== ifAttribute)
: null
);
let newJsxElement = t.JSXElement(
newJsxOpeningElement,
node.closingElement,
node.children
);
let conditionalExpression = t.conditionalExpression(
ifAttribute.value.expression,
newJsxElement,
t.nullLiteral()
);
path.replaceWith(conditionalExpression);
},
}
}
}