insin / babel-plugin-react-html-attrs

Babel plugin which transforms HTML and SVG attributes on JSX host elements into React-compatible attributes

Home Page:https://astexplorer.net/#/gist/0008d677e8156dab437be51d1fee1757/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inlining elements

ostness opened this issue · comments

commented

Coupled with babel-plugin-transform-react-inline-elements, this doesn't work:

echo '<a class="link">b</a>' |
babel --presets react --plugins react-html-attrs,babel-plugin-transform-react-inline-elements

Output:

_jsx("a", {
  "class": "link"
}, void 0, "b");

No substitutions (for "class") observed. Expected:

_jsx("a", {
  className: "link"
}, void 0, "b");

I've tinkered with it to make it work, but I'm not much of an expert in babel / plugins / visitors mechanics not to break other use cases.

transform-react-inline-elements doesn't touch JSXAttribute, so replacing it (although leaving it doesn't change a thing) with JSXElement entry does the trick -- with or without inlining.

--- a/lib/index.js
+++ b/lib/index.js
@@ -8,9 +8,12 @@ var TRANSLATIONS = {
 module.exports = function() {
   return {
     visitor: {
-      JSXAttribute: function(node) {
-        if (node.node.name.name in TRANSLATIONS) {
-          node.node.name.name = TRANSLATIONS[node.node.name.name]
+      JSXElement: function(path, file) {
+        for (var i in path.node.openingElement.attributes) {
+          var name = path.node.openingElement.attributes[i].name
+          if (name.name in TRANSLATIONS) {
+            name.name = TRANSLATIONS[name.name]
+          }
         }
       }
     }