Skip to content

Commit bf91314

Browse files
committed
Add support for CSS-in-JS
1 parent 024c986 commit bf91314

22 files changed

+495
-13
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/fixtures/*.js

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/fixtures/*.js

jest-setup.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const plugin = require('./');
44
const postcss = require('postcss');
5+
const jsx = require('postcss-jsx');
56

67
global.groupTest = function(testGroups) {
78
testGroups.forEach(group => {
@@ -51,7 +52,10 @@ global.runTest = function(input, opts, dirname) {
5152
}
5253

5354
return postcss([plugin(opts)])
54-
.process(inputCSS, { from: inputPath })
55+
.process(inputCSS, {
56+
from: inputPath,
57+
syntax: inputExt === 'js' ? jsx : undefined,
58+
})
5559
.then(result => {
5660
const actualCSS = result.css;
5761

lib/getContainingNode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = function getContainingNode(node) {
2+
// For styled-components declarations are children of Root node
3+
if (
4+
node.type !== 'rule' &&
5+
node.type !== 'atrule' &&
6+
node.parent.document &&
7+
node.parent.document.nodes &&
8+
node.parent.document.nodes.some(item => item.type === 'root')
9+
) {
10+
return node.parent;
11+
}
12+
13+
return node;
14+
};

lib/isRuleWithNodes.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const atRuleExcludes = ['function', 'if', 'else', 'for', 'each', 'while'];
22

33
module.exports = function isRuleWithNodes(node) {
4-
return (
5-
(node.type === 'rule' || node.type === 'atrule') &&
6-
node.nodes &&
7-
node.nodes.length &&
8-
atRuleExcludes.indexOf(node.name) === -1
9-
);
4+
if (node.type === 'atrule' && atRuleExcludes.includes(node.name)) {
5+
return false;
6+
}
7+
8+
return node.nodes && node.nodes.length && !node.nodes.some(item => item.type === 'literal');
109
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const Component1 = styled.div`
2+
a {
3+
color: blue;
4+
}
5+
color: tomato;
6+
${props => props.great && 'color: red'};
7+
8+
@media screen {
9+
color: black;
10+
}
11+
`;
12+
13+
const Component3 = styled.div`
14+
${props => props.great && 'color: red'};
15+
div {
16+
color: tomato;
17+
a {
18+
color: blue;
19+
}
20+
}
21+
`;
22+
23+
const Component5 = styled.div`
24+
div {
25+
${props => props.great && 'color: red'};
26+
span {
27+
}
28+
29+
display: none;
30+
31+
@media (min-width: 100px) {
32+
}
33+
34+
div {
35+
}
36+
}
37+
`;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const Component1 = styled.div`
2+
a {
3+
color: blue;
4+
}
5+
color: tomato;
6+
${props => props.great && 'color: red'};
7+
8+
@media screen {
9+
color: black;
10+
}
11+
`;
12+
13+
const Component3 = styled.div`
14+
${props => props.great && 'color: red'};
15+
div {
16+
a {
17+
color: blue;
18+
}
19+
color: tomato;
20+
}
21+
`;
22+
23+
const Component5 = styled.div`
24+
div {
25+
${props => props.great && 'color: red'};
26+
span {
27+
}
28+
29+
display: none;
30+
31+
@media (min-width: 100px) {
32+
}
33+
34+
div {
35+
}
36+
}
37+
`;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const Component1 = styled.div`
2+
color: tomato;
3+
a {
4+
color: blue;
5+
}
6+
7+
@media screen {
8+
color: black;
9+
}
10+
`;
11+
12+
const Component2 = styled.div`
13+
color: tomato;
14+
${Button} {
15+
color: blue;
16+
}
17+
@media screen {
18+
color: black;
19+
}
20+
`;
21+
22+
const Component3 = styled.div`
23+
div {
24+
color: tomato;
25+
a {
26+
color: blue;
27+
}
28+
}
29+
`;
30+
31+
const Component4 = styled.div`
32+
33+
display: none;
34+
span {
35+
}
36+
37+
div {
38+
}
39+
40+
@media (min-width: 100px) {
41+
}
42+
`;
43+
44+
const Component5 = styled.div`
45+
div {
46+
47+
display: none;
48+
span {
49+
}
50+
51+
div {
52+
}
53+
54+
@media (min-width: 100px) {
55+
}
56+
}
57+
`;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const Component1 = styled.div`
2+
a {
3+
color: blue;
4+
}
5+
color: tomato;
6+
7+
@media screen {
8+
color: black;
9+
}
10+
`;
11+
12+
const Component2 = styled.div`
13+
@media screen {
14+
color: black;
15+
}
16+
${Button} {
17+
color: blue;
18+
}
19+
color: tomato;
20+
`;
21+
22+
const Component3 = styled.div`
23+
div {
24+
a {
25+
color: blue;
26+
}
27+
color: tomato;
28+
}
29+
`;
30+
31+
const Component4 = styled.div`
32+
span {
33+
}
34+
35+
display: none;
36+
37+
@media (min-width: 100px) {
38+
}
39+
40+
div {
41+
}
42+
`;
43+
44+
const Component5 = styled.div`
45+
div {
46+
span {
47+
}
48+
49+
display: none;
50+
51+
@media (min-width: 100px) {
52+
}
53+
54+
div {
55+
}
56+
}
57+
`;

lib/order/__tests__/order.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,21 @@ groupTest([
595595
],
596596
},
597597
]);
598+
599+
test('Should sort by keywords (styled)', () =>
600+
runTest(
601+
'keywords.js',
602+
{
603+
order: ['declarations', 'rules', 'at-rules'],
604+
},
605+
__dirname
606+
));
607+
608+
test('Ignore nodes with template literals (styled)', () =>
609+
runTest(
610+
'ignore-template-literals.js',
611+
{
612+
order: ['declarations', 'rules', 'at-rules'],
613+
},
614+
__dirname
615+
));

0 commit comments

Comments
 (0)