Skip to content

Commit

Permalink
🐛 Added support for attributes returning JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
deamme committed Oct 13, 2017
1 parent 7323a76 commit 6c14faa
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-transform-inferno",
"version": "0.4.0",
"version": "0.5.0",
"description": "A typescript transformer for InfernoJS",
"bugs": {
"url": "https://github.com/deamme/ts-transform-inferno/issues"
Expand Down
20 changes: 7 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,16 @@ export default function (CONFIG?: Config) {
} else {
let propName = astProp.name.text;

// if (propName.type === 'JSXIdentifier') {
// propName = propName.name;
// } else if (propName.type === 'JSXNamespacedName') {
// propName = propName.namespace.name + ':' + propName.name.name;
// }

if (
!isComponent &&
(propName === "className" || propName === "class")
) {
className = getValue(initializer);
className = getValue(initializer, visitor);
} else if (!isComponent && propName === "htmlFor") {
props.push(
ts.createPropertyAssignment(
getName("for"),
getValue(initializer)
getValue(initializer, visitor)
)
);
} else if (propName.substr(0, 11) === "onComponent" && isComponent) {
Expand All @@ -257,15 +251,15 @@ export default function (CONFIG?: Config) {
ref.properties.push(
ts.createObjectLiteral(
getName(propName),
getValue(initializer)
getValue(initializer, visitor)
)
);
} else if (!isComponent && propName in svgAttributes) {
// React compatibility for SVG Attributes
props.push(
ts.createPropertyAssignment(
getName(svgAttributes[propName]),
getValue(initializer)
getValue(initializer, visitor)
)
);
} else {
Expand All @@ -280,16 +274,16 @@ export default function (CONFIG?: Config) {
hasKeyedChildren = true;
break;
case "ref":
ref = getValue(initializer);
ref = getValue(initializer, visitor);
break;
case "key":
key = getValue(initializer);
key = getValue(initializer, visitor);
break;
default:
props.push(
ts.createPropertyAssignment(
getName(propName),
initializer ? getValue(initializer) : ts.createTrue()
initializer ? getValue(initializer, visitor) : ts.createTrue()
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/getValue.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as ts from "typescript";

export default function getValue(node) {
export default function getValue(node, visitor) {
if (node.kind === ts.SyntaxKind.StringLiteral) {
return ts.createLiteral(node.text);
}
if (node.kind === ts.SyntaxKind.JsxExpression) {
return node.expression;
return visitor(node.expression);
}
}
1 change: 1 addition & 0 deletions tests/cases/divWithAttributeReturningJSX.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div foo={() => (<div bar />)} />
3 changes: 3 additions & 0 deletions tests/references/divWithAttributeReturningJSX.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var Inferno = require("inferno");
var createVNode = Inferno.createVNode;
createVNode(2, "div", null, null, { "foo": function () { return (createVNode(2, "div", null, null, { "bar": true })); } });

0 comments on commit 6c14faa

Please sign in to comment.