Merge branch 'master' of github.com:babel/babel
This commit is contained in:
commit
67b8f10eba
@ -12,6 +12,13 @@ export default function ({ types: t }) {
|
|||||||
return t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name, { name: name });
|
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 {
|
return {
|
||||||
visitor: {
|
visitor: {
|
||||||
JSXElement(path, file) {
|
JSXElement(path, file) {
|
||||||
@ -33,18 +40,17 @@ export default function ({ types: t }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function pushProp(objProps, key, value) {
|
function pushProp(objProps, key, value) {
|
||||||
if (t.isJSXExpressionContainer(value)) value = value.expression;
|
|
||||||
objProps.push(t.objectProperty(key, value));
|
objProps.push(t.objectProperty(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
// props
|
// props
|
||||||
for (let attr of (open.attributes: Array<Object>)) {
|
for (let attr of (open.attributes: Array<Object>)) {
|
||||||
if (isJSXAttributeOfName(attr, "key")) {
|
if (isJSXAttributeOfName(attr, "key")) {
|
||||||
key = attr.value;
|
key = getAttributeValue(attr);
|
||||||
} else {
|
} else {
|
||||||
let name = attr.name.name;
|
let name = attr.name.name;
|
||||||
let propertyKey = t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(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) {
|
if (props.properties.length) {
|
||||||
props = t.callExpression(file.addHelper("defaultProps"), [defProps, props]);
|
props = t.callExpression(file.addHelper("defaultProps"), [defProps, props]);
|
||||||
} else {
|
} else {
|
||||||
props = defProps;
|
props = t.logicalExpression("||", defProps, props);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
|
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});
|
||||||
@ -0,0 +1 @@
|
|||||||
|
<Foo key={"foo" + "baz"} data-value="bar" />
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
babelHelpers.createRawReactElement(Foo, "foo" + "baz", babelHelpers.defaultProps(Foo.defaultProps, {
|
||||||
|
"data-value": "bar"
|
||||||
|
}));
|
||||||
@ -1 +1 @@
|
|||||||
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
|
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});
|
||||||
@ -1,4 +1,4 @@
|
|||||||
babelHelpers.createRawReactElement(Foo, null, babelHelpers.defaultProps(Foo.defaultProps, {
|
babelHelpers.createRawReactElement(Foo, null, babelHelpers.defaultProps(Foo.defaultProps, {
|
||||||
className: "foo",
|
className: "foo",
|
||||||
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps)]
|
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps || {})]
|
||||||
}));
|
}));
|
||||||
@ -1,4 +1,4 @@
|
|||||||
babelHelpers.createRawReactElement("div", null, {
|
babelHelpers.createRawReactElement("div", null, {
|
||||||
className: "foo",
|
className: "foo",
|
||||||
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps)]
|
children: [bar, babelHelpers.createRawReactElement(Baz, "baz", Baz.defaultProps || {})]
|
||||||
});
|
});
|
||||||
@ -1 +1 @@
|
|||||||
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps);
|
babelHelpers.createRawReactElement(Baz, null, Baz.defaultProps || {});
|
||||||
Loading…
x
Reference in New Issue
Block a user