22const traverse = require ( "@babel/traverse" ) . default ;
33const t = require ( "@babel/types" ) . default ;
44const parse = require ( "babylon" ) . parse ;
5+ const getTemplate = require ( "./get-template" ) ;
56
67const isTopLevelExpression = path =>
78 path . isObjectExpression ( ) && ! path . findParent ( p => p . isObjectExpression ( ) ) ;
@@ -64,15 +65,15 @@ function getOptions (opts, attribute) {
6465 } ;
6566}
6667
67- function literalParser ( source , opts ) {
68+ function literalParser ( source , opts , styles ) {
6869 let ast ;
6970 try {
7071 ast = parse ( source , getOptions ( opts ) ) ;
7172 } catch ( ex ) {
7273 if ( opts . from && / \. (?: m ? [ j t ] s x ? | e s \d * | p a c ) ( \? .* | $ ) / i. test ( opts . from ) ) {
73- return [ ] ;
74+ return styles || [ ] ;
7475 }
75- return ;
76+ return styles ;
7677 }
7778
7879 let glamorousImport ;
@@ -133,30 +134,34 @@ function literalParser (source, opts) {
133134
134135 objects = objects . map ( path => {
135136 const objectSyntax = require ( "./object-syntax" ) ;
136- const root = objectSyntax . parse ( path . node , source , opts ) ;
137- const startIndex = root . first . raws . node . start ;
138- const endIndex = root . last . raws . node . end ;
137+ const endNode = path . node ;
138+ const syntax = objectSyntax ( endNode ) ;
139+ let startNode = endNode ;
140+ if ( startNode . leadingComments && startNode . leadingComments . length ) {
141+ startNode = startNode . leadingComments [ 0 ] ;
142+ }
139143 return {
140- root,
141- startIndex,
142- endIndex,
143- syntax : objectSyntax ,
144+ startIndex : startNode . start ,
145+ endIndex : endNode . end ,
146+ skipConvert : true ,
147+ content : source ,
148+ syntax,
144149 lang : "object-literal" ,
145150 } ;
146151 } ) ;
147152
148- let styledSyntax ;
153+ let templateSyntax ;
149154
150- function getStyledSyntax ( ) {
151- if ( ! styledSyntax ) {
152- const getSyntax = require ( "postcss-syntax/lib/ get-syntax" ) ;
155+ function getTemplateSyntax ( ) {
156+ if ( ! templateSyntax ) {
157+ const getSyntax = require ( "postcss-syntax/get-syntax" ) ;
153158 const cssSyntax = getSyntax ( "css" , opts ) ;
154- styledSyntax = {
155- parse : require ( cssSyntax . parse . name === "safeParse" ? "./template- safe-parse" : "./template- parse" ) ,
159+ templateSyntax = {
160+ parse : require ( "postcss-styled/template-" + ( cssSyntax . parse . name === "safeParse" ? "safe-parse" : "parse" ) ) ,
156161 stringify : cssSyntax . stringify ,
157162 } ;
158163 }
159- return styledSyntax ;
164+ return templateSyntax ;
160165 }
161166
162167 tpls = tpls . filter ( path => (
@@ -165,28 +170,28 @@ function literalParser (source, opts) {
165170 ) )
166171 ) ) . map ( path => {
167172 const quasis = path . node . quasis ;
168- const startIndex = quasis [ 0 ] . start ;
169- const strSource = source . slice ( startIndex , quasis [ quasis . length - 1 ] . end ) ;
173+ const value = getTemplate ( path . node , source ) ;
170174
171- if ( ! strSource . trim ( ) ) {
175+ if ( value . length === 1 && ! value [ 0 ] . trim ( ) ) {
172176 return ;
173177 }
174178
175179 const style = {
176- startIndex : startIndex ,
177- content : strSource ,
180+ startIndex : quasis [ 0 ] . start ,
181+ endIndex : quasis [ quasis . length - 1 ] . end ,
182+ content : value . join ( "" ) ,
178183 ignoreErrors : true ,
179184 } ;
180- if ( / ( ^ | \s | \{ | \} | ; | : ) \$ \{ / m . test ( strSource ) ) {
181- style . syntax = getStyledSyntax ( ) ;
185+ if ( value . length > 1 ) {
186+ style . syntax = getTemplateSyntax ( ) ;
182187 style . lang = "template-literal" ;
183188 } else {
184189 style . lang = "css" ;
185190 }
186191 return style ;
187192 } ) . filter ( Boolean ) ;
188193
189- return objects . concat ( tpls ) ;
194+ return ( styles || [ ] ) . concat ( objects ) . concat ( tpls ) ;
190195} ;
191196
192197module . exports = literalParser ;
0 commit comments