@@ -8,18 +8,19 @@ describe("styled-components", () => {
88 const file = require . resolve ( "./fixtures/styled" ) ;
99 let code = fs . readFileSync ( file ) ;
1010
11- const root = syntax . parse ( code , {
11+ const document = syntax . parse ( code , {
1212 from : file ,
1313 } ) ;
1414
1515 code = code . toString ( ) ;
16- expect ( root . toString ( ) , code ) ;
16+ expect ( document . toString ( ) , code ) ;
17+ expect ( document . source ) . to . haveOwnProperty ( "lang" , "jsx" ) ;
1718
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 ) ;
2021
2122 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 ) => {
2324 if ( i ) {
2425 expect ( decl ) . to . have . property ( "type" , "decl" ) ;
2526 } else {
@@ -37,37 +38,40 @@ describe("styled-components", () => {
3738 "}" ,
3839 "" ,
3940 ] . join ( "\n" ) ;
40- const root = syntax . parse ( code , {
41+ const document = syntax . parse ( code , {
4142 from : "empty_template_literal.js" ,
4243 } ) ;
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 ) ;
4547 } ) ;
4648
4749 it ( "skip javascript syntax error" , ( ) => {
4850 const code = "\\`" ;
49- const root = syntax . parse ( code , {
51+ const document = syntax . parse ( code , {
5052 from : "syntax_error.js" ,
5153 } ) ;
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 ) ;
5457 } ) ;
5558
5659 it ( "illegal template literal" , ( ) => {
5760 const code = [
5861 "const styled = require(\"styled-components\");" ,
5962 "styled.div`$\n{display: block}\n${g} {}`" ,
6063 ] . join ( "\n" ) ;
61- const root = syntax . parse ( code , {
64+ const document = syntax . parse ( code , {
6265 from : "illegal_template_literal.js" ,
6366 } ) ;
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}" ) ;
7175 } ) ;
7276
7377 it ( "styled.img" , ( ) => {
@@ -85,16 +89,17 @@ describe("styled-components", () => {
8589 expect ( root . toString ( ) ) . to . equal ( code ) ;
8690 } ) ;
8791
88- it ( "skip CSS syntax error" , ( ) => {
92+ it ( "throw CSS syntax error" , ( ) => {
8993 const code = [
9094 "const styled = require(\"styled-components\");" ,
9195 "styled.div`a{`;" ,
9296 ] . 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" ) ;
98103 } ) ;
99104
100105 it ( "skip empty template literal" , ( ) => {
@@ -114,38 +119,54 @@ describe("styled-components", () => {
114119 "const styled = require(\"styled-components\");" ,
115120 "styled.div`a{`;" ,
116121 ] . join ( "\n" ) ;
117- const root = syntax ( {
122+ const document = syntax ( {
118123 css : "safe-parser" ,
119124 } ) . parse ( code , {
120125 from : "postcss-safe-parser.js" ,
121126 } ) ;
122- expect ( root . toString ( ) ) . to . equal ( [
127+ expect ( document . toString ( ) ) . to . equal ( [
123128 "const styled = require(\"styled-components\");" ,
124129 "styled.div`a{}`;" ,
125130 ] . 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" ) ;
130136 } ) ;
131137
132138 it ( "fix styled syntax error" , ( ) => {
133139 const code = [
134140 "const styled = require(\"styled-components\");" ,
135141 "styled.div`${ a } {`" ,
136142 ] . join ( "\n" ) ;
137- const root = syntax ( {
143+ const document = syntax ( {
138144 css : "safe-parser" ,
139145 } ) . parse ( code , {
140146 from : "styled-safe-parse.js" ,
141147 } ) ;
142- expect ( root . toString ( ) ) . to . equal ( [
148+ expect ( document . toString ( ) ) . to . equal ( [
143149 "const styled = require(\"styled-components\");" ,
144150 "styled.div`${ a } {}`" ,
145151 ] . 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}" ) ;
150171 } ) ;
151172} ) ;
0 commit comments