Skip to content

Commit 415f237

Browse files
committed
fix: fix extraction when styled and css tags are used together. fixes #301
1 parent 9d49177 commit 415f237

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

src/__tests__/__snapshots__/babel.test.js.snap

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,33 @@ Dependencies: NA
279279
280280
`;
281281
282+
exports[`supports both css and styled tags 1`] = `
283+
"import { css } from 'linaria';
284+
import { styled } from 'linaria/react';
285+
const Title =
286+
/*#__PURE__*/
287+
styled(\\"h1\\")({
288+
name: \\"Title\\",
289+
class: \\"th6xni0\\"
290+
});
291+
const title = \\"t1u0rrat\\";"
292+
`;
293+
294+
exports[`supports both css and styled tags 2`] = `
295+
296+
CSS:
297+
298+
.th6xni0 {
299+
font-size: 14px;
300+
}
301+
.t1u0rrat {
302+
color: blue;
303+
}
304+
305+
Dependencies: NA
306+
307+
`;
308+
282309
exports[`throws when contains dynamic expression without evaluate: true in css tag 1`] = `
283310
"<<DIRNAME>>/app/index.js: The CSS cannot contain JavaScript expressions when using the 'css' tag. To evaluate the expressions at build time, pass 'evaluate: true' to the babel plugin.
284311
  2 | 

src/__tests__/babel.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,23 @@ it('throws when contains dynamic expression without evaluate: true in css tag',
342342
expect(e.message.replace(__dirname, '<<DIRNAME>>')).toMatchSnapshot();
343343
}
344344
});
345+
346+
it('supports both css and styled tags', async () => {
347+
const { code, metadata } = await transpile(
348+
dedent`
349+
import { css } from 'linaria';
350+
import { styled } from 'linaria/react';
351+
352+
const Title = styled.h1\`
353+
font-size: 14px;
354+
\`;
355+
356+
const title = css\`
357+
color: blue;
358+
\`;
359+
`
360+
);
361+
362+
expect(code).toMatchSnapshot();
363+
expect(metadata).toMatchSnapshot();
364+
});

src/babel/extract.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ module.exports = function extract(babel: any, options: Options) {
212212
let css;
213213

214214
if (
215+
t.isCallExpression(tag) &&
216+
t.isIdentifier(tag.callee) &&
217+
tag.arguments.length === 1 &&
218+
tag.callee.name === 'styled' &&
215219
imports(
216220
t,
217221
path.scope,
@@ -220,23 +224,23 @@ module.exports = function extract(babel: any, options: Options) {
220224
'linaria/react'
221225
)
222226
) {
223-
if (
224-
t.isCallExpression(tag) &&
225-
t.isIdentifier(tag.callee) &&
226-
tag.arguments.length === 1 &&
227-
tag.callee.name === 'styled'
228-
) {
229-
styled = { component: path.get('tag').get('arguments')[0] };
230-
} else if (
231-
t.isMemberExpression(tag) &&
232-
t.isIdentifier(tag.object) &&
233-
t.isIdentifier(tag.property) &&
234-
tag.object.name === 'styled'
235-
) {
236-
styled = {
237-
component: { node: t.stringLiteral(tag.property.name) },
238-
};
239-
}
227+
styled = { component: path.get('tag').get('arguments')[0] };
228+
} else if (
229+
t.isMemberExpression(tag) &&
230+
t.isIdentifier(tag.object) &&
231+
t.isIdentifier(tag.property) &&
232+
tag.object.name === 'styled' &&
233+
imports(
234+
t,
235+
path.scope,
236+
state.file.opts.filename,
237+
'styled',
238+
'linaria/react'
239+
)
240+
) {
241+
styled = {
242+
component: { node: t.stringLiteral(tag.property.name) },
243+
};
240244
} else if (
241245
imports(t, path.scope, state.file.opts.filename, 'css', 'linaria')
242246
) {

0 commit comments

Comments
 (0)