Merge branch 'master' of github.com:babel/babel

This commit is contained in:
Sebastian McKenzie 2015-11-11 23:24:53 -08:00
commit 67b8f10eba
8 changed files with 19 additions and 9 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));
}
}
@ -61,7 +67,7 @@ export default function ({ types: t }) {
if (props.properties.length) {
props = t.callExpression(file.addHelper("defaultProps"), [defProps, props]);
} else {
props = defProps;
props = t.logicalExpression("||", defProps, props);
}
}

View File

@ -1 +1 @@
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});

View File

@ -0,0 +1 @@
<Foo key={"foo" + "baz"} data-value="bar" />

View File

@ -0,0 +1,3 @@
babelHelpers.createRawReactElement(Foo, "foo" + "baz", babelHelpers.defaultProps(Foo.defaultProps, {
"data-value": "bar"
}));

View File

@ -1 +1 @@
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});

View File

@ -1,4 +1,4 @@
babelHelpers.createRawReactElement(Foo, null, babelHelpers.defaultProps(Foo.defaultProps, {
className: "foo",
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps)]
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps || {})]
}));

View File

@ -1,4 +1,4 @@
babelHelpers.createRawReactElement("div", null, {
className: "foo",
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps)]
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps || {})]
});

View File

@ -1 +1 @@
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});