From 309a7556ff7031f23c9b1419a005eec50f86963c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 30 Oct 2015 17:16:47 +0000 Subject: [PATCH] ensure that invalid identifier JSX attribute keys are quoted when transforming to calls - fixes #2675 --- .../react/should-quote-jsx-attributes/actual.js | 1 + .../react/should-quote-jsx-attributes/expected.js | 5 +++++ packages/babel-helper-builder-react-jsx/src/index.js | 9 ++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js create mode 100644 packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/expected.js diff --git a/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js b/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js new file mode 100644 index 0000000000..c794650e94 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/actual.js @@ -0,0 +1 @@ +; diff --git a/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/expected.js b/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/expected.js new file mode 100644 index 0000000000..1f01a22149 --- /dev/null +++ b/packages/babel-core/test/fixtures/transformation/react/should-quote-jsx-attributes/expected.js @@ -0,0 +1,5 @@ +React.createElement( + 'button', + { 'data-value': 'a value' }, + 'Button' +); diff --git a/packages/babel-helper-builder-react-jsx/src/index.js b/packages/babel-helper-builder-react-jsx/src/index.js index 645c44a6da..558d6f789a 100644 --- a/packages/babel-helper-builder-react-jsx/src/index.js +++ b/packages/babel-helper-builder-react-jsx/src/index.js @@ -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); }