From 63157159ab3424b3681b624323c20f86a868d992 Mon Sep 17 00:00:00 2001 From: mtpc Date: Tue, 9 Jan 2018 06:24:02 +0100 Subject: [PATCH] Preserve namespaced attributes when throwIfNamespace is false (#7173) Previously that part would throw since it expects a JSXIdentifier but it (logically) gets a JSXNamespacedName when using a namespaced attribute. --- packages/babel-helper-builder-react-jsx/src/index.js | 6 +++++- .../react/should-support-xml-namespaces-if-flag/actual.js | 2 +- .../react/should-support-xml-namespaces-if-flag/expected.js | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/babel-helper-builder-react-jsx/src/index.js b/packages/babel-helper-builder-react-jsx/src/index.js index 6307db4608..e0b4f5498e 100644 --- a/packages/babel-helper-builder-react-jsx/src/index.js +++ b/packages/babel-helper-builder-react-jsx/src/index.js @@ -92,7 +92,11 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`, if (t.isValidIdentifier(node.name.name)) { node.name.type = "Identifier"; } else { - node.name = t.stringLiteral(node.name.name); + node.name = t.stringLiteral( + t.isJSXNamespacedName(node.name) + ? node.name.namespace.name + ":" + node.name.name.name + : node.name.name, + ); } return t.inherits(t.objectProperty(node.name, value), node); diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/actual.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/actual.js index 4888b2b705..48d051a951 100644 --- a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/actual.js +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/actual.js @@ -1 +1 @@ -; +; diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/expected.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/expected.js index 84038ebb97..24a57b8f23 100644 --- a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/expected.js +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/should-support-xml-namespaces-if-flag/expected.js @@ -1 +1,3 @@ -h("f:image", null); +h("f:image", { + "n:attr": true +});