ensure that invalid identifier JSX attribute keys are quoted when transforming to calls - fixes #2675

This commit is contained in:
Sebastian McKenzie 2015-10-30 17:16:47 +00:00
parent 5bda4d9744
commit 309a7556ff
3 changed files with 12 additions and 3 deletions

View File

@ -0,0 +1 @@
<button data-value='a value'>Button</button>;

View File

@ -0,0 +1,5 @@
React.createElement(
'button',
{ 'data-value': 'a value' },
'Button'
);

View File

@ -1,6 +1,5 @@
/* @flow */
import isString from "lodash/lang/isString";
import esutils from "esutils";
import * as t from "babel-types";
@ -66,11 +65,15 @@ export default function (opts) {
function convertAttribute(node) {
let value = convertAttributeValue(node.value || t.booleanLiteral(true));
if (t.isLiteral(value) && isString(value.value)) {
if (t.isStringLiteral(value)) {
value.value = value.value.replace(/\n\s+/g, " ");
}
node.name.type = "Identifier";
if (t.isValidIdentifier(node.name.name)) {
node.name.type = "Identifier";
} else {
node.name = t.stringLiteral(node.name.name);
}
return t.inherits(t.objectProperty(node.name, value), node);
}