Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: hoist css.named calls as well #121

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/babel/hoist-css/__tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ var _article = css\`color: \${colors.red};\`;
var _article2 = css\`font-size: 12px;\`;"
`;

exports[`hoist-css babel plugin should hoist named CSS from JSX from styles 1`] = `
"var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function MyComponent() {
return React.createElement(\\"article\\", _extends({
id: \\"post\\"
}, styles(_article, _article2)));
}

var _article = css.named('test')\`color: \${colors.red};\`;

var _article2 = css\`font-size: 12px;\`;"
`;

exports[`hoist-css babel plugin should noop if no styles or class present 1`] = `
"
function MyComponent() {
Expand Down
15 changes: 15 additions & 0 deletions src/babel/hoist-css/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ describe('hoist-css babel plugin', () => {
expect(code).toMatchSnapshot();
});

it('should hoist named CSS from JSX from styles', () => {
const { code } = transpile(`
function MyComponent() {
return (
<article
id="post"
{...styles(css.named('test')\`color: \${colors.red};\`, css\`font-size: 12px;\`)}
/>
);
}
`);

expect(code).toMatchSnapshot();
});

it('should not affect other template literals in styles', () => {
const { code } = transpile(`
function MyComponent() {
Expand Down
6 changes: 5 additions & 1 deletion src/babel/hoist-css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ const transformTaggedTemplate = (
) => {
if (
t.isTaggedTemplateExpression(expression) &&
expression.tag.name === 'css'
((t.isIdentifier(expression.tag) && expression.tag.name === 'css') ||
(t.isCallExpression(expression.tag) &&
t.isMemberExpression(expression.tag.callee) &&
expression.tag.callee.object.name === 'css' &&
expression.tag.callee.property.name === 'named'))
) {
const className = path.scope.generateUidIdentifier(path.node.name.name);
state.programPath.node.body.push(
Expand Down