ensure that invalid identifier JSX attribute keys are quoted when transforming to calls - fixes #2675
This commit is contained in:
parent
5bda4d9744
commit
309a7556ff
1
packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js
vendored
Normal file
1
packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
<button data-value='a value'>Button</button>;
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
React.createElement(
|
||||||
|
'button',
|
||||||
|
{ 'data-value': 'a value' },
|
||||||
|
'Button'
|
||||||
|
);
|
||||||
@ -1,6 +1,5 @@
|
|||||||
/* @flow */
|
/* @flow */
|
||||||
|
|
||||||
import isString from "lodash/lang/isString";
|
|
||||||
import esutils from "esutils";
|
import esutils from "esutils";
|
||||||
import * as t from "babel-types";
|
import * as t from "babel-types";
|
||||||
|
|
||||||
@ -66,11 +65,15 @@ export default function (opts) {
|
|||||||
function convertAttribute(node) {
|
function convertAttribute(node) {
|
||||||
let value = convertAttributeValue(node.value || t.booleanLiteral(true));
|
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, " ");
|
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);
|
return t.inherits(t.objectProperty(node.name, value), node);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user