Skip to content

Commit

Permalink
Revert "remove jsx transforms"
Browse files Browse the repository at this point in the history
This reverts commit 5a1d3a1.
  • Loading branch information
danielberndt committed Aug 6, 2018
1 parent 127e82f commit aa04ca3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
6 changes: 6 additions & 0 deletions __tests__/__fixtures__/02-jsx/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import g from "glamorous";

const GDot1 = props => <g.Span {...props}>Hi, I'm a Span!</g.Span>;
const GDot2 = props => <g.Div css={{marginTop: 5}}/>;
const GDot3 = props => <g.Div marginTop={5}/>;
const GDot4 = props => <g.Div marginTop={5} css={{marginTop: 10}} marginBottom="5" onClick={handler}/>;
15 changes: 15 additions & 0 deletions __tests__/__fixtures__/02-jsx/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const GDot1 = props => <span {...props}>Hi, I'm a Span!</span>;

const GDot2 = props => <div css={{
marginTop: 5
}} />;

const GDot3 = props => <div css={{
marginTop: 5
}} />;

const GDot4 = props => <div css={{
marginTop: 10,
marginTop: 5,
marginBottom: "5"
}} onClick={handler} />;
50 changes: 46 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ module.exports = function(babel) {
});
};

const transformJSXAttributes = jsxAttrs => {
if (!jsxAttrs) return [];
const appendToCss = [];
let cssAttr = null;
const newAttrs = jsxAttrs.filter(attr => {
if (t.isJSXSpreadAttribute(attr)) return true;
const {value, name: jsxKey} = attr;
if (jsxKey.name === "css") {
cssAttr = attr;
} else {
// TODO: be smarter about finding out which properties
// are CSS properties and which ones are not

// event handlers are no CSS Props!
if (jsxKey.name.indexOf("on") === 0) return true;

appendToCss.push({
name: t.identifier(jsxKey.name),
value: t.isJSXExpressionContainer(value) ? value.expression : value,
});
return false;
}
return true;
});
if (appendToCss.length > 0) {
if (!cssAttr) {
cssAttr = t.jsxAttribute(
t.jsxIdentifier("css"),
t.jsxExpressionContainer(t.objectExpression([]))
);
newAttrs.push(cssAttr);
} else if (!t.isObjectExpression(cssAttr.value.expression)) {
// turn <span css={obj} .../> into <span css={{...obj}} .../>
// so we can add more properties to this css attribute
cssAttr.value.expression = t.objectExpression([t.spreadElement(cssAttr.value.expression)]);
}
appendToCss.forEach(({name, value}) => {
cssAttr.value.expression.properties.push(t.objectProperty(name, value));
});
cssAttr.value.expression.properties;
}
return newAttrs;
};

const glamorousVisitor = {
// for each reference to an identifier...
ReferencedIdentifier(path, {getNewName, oldName}) {
Expand Down Expand Up @@ -76,10 +120,8 @@ module.exports = function(babel) {
case "JSXMemberExpression": {
const grandParent = path.parentPath.parent;
grandParent.name = t.identifier(grandParent.name.property.name.toLowerCase());
if (t.isJSXOpeningElement(grandParent) && grandParent.attributes) {
console.warn(
"The current version of the codemod has only weak support of the '<glamorous.Div>' syntax. It won't transform any attributes."
);
if (t.isJSXOpeningElement(grandParent)) {
grandParent.attributes = transformJSXAttributes(grandParent.attributes);
}
break;
}
Expand Down

0 comments on commit aa04ca3

Please sign in to comment.