@@ -8,18 +8,19 @@ describe("styled-components", () => {
8
8
const file = require . resolve ( "./fixtures/styled" ) ;
9
9
let code = fs . readFileSync ( file ) ;
10
10
11
- const root = syntax . parse ( code , {
11
+ const document = syntax . parse ( code , {
12
12
from : file ,
13
13
} ) ;
14
14
15
15
code = code . toString ( ) ;
16
- expect ( root . toString ( ) , code ) ;
16
+ expect ( document . toString ( ) , code ) ;
17
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
17
18
18
- expect ( root . nodes ) . to . have . lengthOf ( 1 ) ;
19
- expect ( root . first . nodes ) . to . have . lengthOf ( 8 ) ;
19
+ expect ( document . nodes ) . to . have . lengthOf ( 1 ) ;
20
+ expect ( document . first . nodes ) . to . have . lengthOf ( 8 ) ;
20
21
21
22
const lines = code . match ( / ^ .+ $ / gm) . slice ( 3 ) . map ( line => ( line . replace ( / ^ \s * ( .+ ?) ; ? \s * $ / , "$1" ) ) ) ;
22
- root . first . nodes . forEach ( ( decl , i ) => {
23
+ document . first . nodes . forEach ( ( decl , i ) => {
23
24
if ( i ) {
24
25
expect ( decl ) . to . have . property ( "type" , "decl" ) ;
25
26
} else {
@@ -37,37 +38,40 @@ describe("styled-components", () => {
37
38
"}" ,
38
39
"" ,
39
40
] . join ( "\n" ) ;
40
- const root = syntax . parse ( code , {
41
+ const document = syntax . parse ( code , {
41
42
from : "empty_template_literal.js" ,
42
43
} ) ;
43
- expect ( root . toString ( ) ) . to . equal ( code ) ;
44
- expect ( root . nodes ) . to . have . lengthOf ( 0 ) ;
44
+ expect ( document . toString ( ) ) . to . equal ( code ) ;
45
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
46
+ expect ( document . nodes ) . to . have . lengthOf ( 0 ) ;
45
47
} ) ;
46
48
47
49
it ( "skip javascript syntax error" , ( ) => {
48
50
const code = "\\`" ;
49
- const root = syntax . parse ( code , {
51
+ const document = syntax . parse ( code , {
50
52
from : "syntax_error.js" ,
51
53
} ) ;
52
- expect ( root . toString ( ) ) . to . equal ( code ) ;
53
- expect ( root . nodes ) . to . have . lengthOf ( 0 ) ;
54
+ expect ( document . toString ( ) ) . to . equal ( code ) ;
55
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
56
+ expect ( document . nodes ) . to . have . lengthOf ( 0 ) ;
54
57
} ) ;
55
58
56
59
it ( "illegal template literal" , ( ) => {
57
60
const code = [
58
61
"const styled = require(\"styled-components\");" ,
59
62
"styled.div`$\n{display: block}\n${g} {}`" ,
60
63
] . join ( "\n" ) ;
61
- const root = syntax . parse ( code , {
64
+ const document = syntax . parse ( code , {
62
65
from : "illegal_template_literal.js" ,
63
66
} ) ;
64
- expect ( root . toString ( ) ) . to . equal ( code ) ;
65
- expect ( root . nodes ) . to . have . lengthOf ( 1 ) ;
66
- expect ( root . first . nodes ) . to . have . lengthOf ( 2 ) ;
67
- expect ( root . first . first ) . have . property ( "type" , "rule" ) ;
68
- expect ( root . first . first ) . have . property ( "selector" , "$" ) ;
69
- expect ( root . last . last ) . have . property ( "type" , "rule" ) ;
70
- expect ( root . last . last ) . have . property ( "selector" , "${g}" ) ;
67
+ expect ( document . toString ( ) ) . to . equal ( code ) ;
68
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
69
+ expect ( document . nodes ) . to . have . lengthOf ( 1 ) ;
70
+ expect ( document . first . nodes ) . to . have . lengthOf ( 2 ) ;
71
+ expect ( document . first . first ) . have . property ( "type" , "rule" ) ;
72
+ expect ( document . first . first ) . have . property ( "selector" , "$" ) ;
73
+ expect ( document . last . last ) . have . property ( "type" , "rule" ) ;
74
+ expect ( document . last . last ) . have . property ( "selector" , "${g}" ) ;
71
75
} ) ;
72
76
73
77
it ( "styled.img" , ( ) => {
@@ -85,16 +89,17 @@ describe("styled-components", () => {
85
89
expect ( root . toString ( ) ) . to . equal ( code ) ;
86
90
} ) ;
87
91
88
- it ( "skip CSS syntax error" , ( ) => {
92
+ it ( "throw CSS syntax error" , ( ) => {
89
93
const code = [
90
94
"const styled = require(\"styled-components\");" ,
91
95
"styled.div`a{`;" ,
92
96
] . join ( "\n" ) ;
93
- const root = syntax . parse ( code , {
94
- from : "css_syntax_error.js" ,
95
- } ) ;
96
- expect ( root . toString ( ) ) . to . equal ( code ) ;
97
- expect ( root . nodes ) . to . have . lengthOf ( 0 ) ;
97
+
98
+ expect ( ( ) => {
99
+ syntax . parse ( code , {
100
+ from : "css_syntax_error.js" ,
101
+ } ) ;
102
+ } ) . to . throw ( "css_syntax_error.js:2:12: Unclosed block" ) ;
98
103
} ) ;
99
104
100
105
it ( "skip empty template literal" , ( ) => {
@@ -114,38 +119,54 @@ describe("styled-components", () => {
114
119
"const styled = require(\"styled-components\");" ,
115
120
"styled.div`a{`;" ,
116
121
] . join ( "\n" ) ;
117
- const root = syntax ( {
122
+ const document = syntax ( {
118
123
css : "safe-parser" ,
119
124
} ) . parse ( code , {
120
125
from : "postcss-safe-parser.js" ,
121
126
} ) ;
122
- expect ( root . toString ( ) ) . to . equal ( [
127
+ expect ( document . toString ( ) ) . to . equal ( [
123
128
"const styled = require(\"styled-components\");" ,
124
129
"styled.div`a{}`;" ,
125
130
] . join ( "\n" ) ) ;
126
- expect ( root . nodes ) . to . have . lengthOf ( 1 ) ;
127
- expect ( root . first . nodes ) . to . have . lengthOf ( 1 ) ;
128
- expect ( root . first . first ) . have . property ( "type" , "rule" ) ;
129
- expect ( root . first . first ) . have . property ( "selector" , "a" ) ;
131
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
132
+ expect ( document . nodes ) . to . have . lengthOf ( 1 ) ;
133
+ expect ( document . first . nodes ) . to . have . lengthOf ( 1 ) ;
134
+ expect ( document . first . first ) . have . property ( "type" , "rule" ) ;
135
+ expect ( document . first . first ) . have . property ( "selector" , "a" ) ;
130
136
} ) ;
131
137
132
138
it ( "fix styled syntax error" , ( ) => {
133
139
const code = [
134
140
"const styled = require(\"styled-components\");" ,
135
141
"styled.div`${ a } {`" ,
136
142
] . join ( "\n" ) ;
137
- const root = syntax ( {
143
+ const document = syntax ( {
138
144
css : "safe-parser" ,
139
145
} ) . parse ( code , {
140
146
from : "styled-safe-parse.js" ,
141
147
} ) ;
142
- expect ( root . toString ( ) ) . to . equal ( [
148
+ expect ( document . toString ( ) ) . to . equal ( [
143
149
"const styled = require(\"styled-components\");" ,
144
150
"styled.div`${ a } {}`" ,
145
151
] . join ( "\n" ) ) ;
146
- expect ( root . nodes ) . to . have . lengthOf ( 1 ) ;
147
- expect ( root . first . nodes ) . to . have . lengthOf ( 1 ) ;
148
- expect ( root . first . first ) . have . property ( "type" , "rule" ) ;
149
- expect ( root . first . first ) . have . property ( "selector" , "${ a }" ) ;
152
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
153
+ expect ( document . nodes ) . to . have . lengthOf ( 1 ) ;
154
+ expect ( document . first . nodes ) . to . have . lengthOf ( 1 ) ;
155
+ expect ( document . first . first ) . have . property ( "type" , "rule" ) ;
156
+ expect ( document . first . first ) . have . property ( "selector" , "${ a }" ) ;
157
+ } ) ;
158
+
159
+ it ( "template literal in prop" , ( ) => {
160
+ const code = [
161
+ "const styled = require(\"styled-components\");" ,
162
+ "styled.div`margin-${/* sc-custom 'left' */ rtlSwitch}: 12.5px;`" ,
163
+ ] . join ( "\n" ) ;
164
+ const document = syntax . parse ( code , {
165
+ from : "template_literal_in_prop.js" ,
166
+ } ) ;
167
+ expect ( document . toString ( ) ) . to . equal ( code ) ;
168
+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
169
+ expect ( document . nodes ) . to . have . lengthOf ( 1 ) ;
170
+ expect ( document . first . first ) . to . haveOwnProperty ( "prop" , "margin-${/* sc-custom 'left' */ rtlSwitch}" ) ;
150
171
} ) ;
151
172
} ) ;
0 commit comments