React inlining: Fix transform for computed key

(This was broken before my last change too.)
This commit is contained in:
Ben Alpert
2015-11-11 15:23:59 -08:00
parent 0b7b2e4a3a
commit 07a5bcc04a
3 changed files with 13 additions and 3 deletions

View File

@@ -12,6 +12,13 @@ export default function ({ types: t }) {
return t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name, { name: name });
}
function getAttributeValue(attr) {
let value = attr.value;
if (!value) return t.identifier("true");
if (t.isJSXExpressionContainer(value)) value = value.expression;
return value;
}
return {
visitor: {
JSXElement(path, file) {
@@ -33,18 +40,17 @@ export default function ({ types: t }) {
}
function pushProp(objProps, key, value) {
if (t.isJSXExpressionContainer(value)) value = value.expression;
objProps.push(t.objectProperty(key, value));
}
// props
for (let attr of (open.attributes: Array<Object>)) {
if (isJSXAttributeOfName(attr, "key")) {
key = attr.value;
key = getAttributeValue(attr);
} else {
let name = attr.name.name;
let propertyKey = t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name);
pushProp(props.properties, propertyKey, attr.value || t.identifier("true"));
pushProp(props.properties, propertyKey, getAttributeValue(attr));
}
}