@@ -8,7 +8,7 @@ var childProcess = require('child_process');
88var phantomjs = require ( 'phantomjs-prebuilt' ) ;
99var commander = require ( 'commander' ) ;
1010var SourceMapConsumer = require ( 'source-map' ) . SourceMapConsumer ;
11- var cssParse = require ( 'css-parse ' ) ;
11+ var cssTree = require ( 'css-tree ' ) ;
1212
1313
1414function parseFileName ( filePath ) {
@@ -50,12 +50,21 @@ if (commander.css) {
5050}
5151
5252var CSS_STR = fs . readFileSync ( commander . css , 'utf8' ) ;
53- var ast = cssParse ( CSS_STR , { source : commander . css } ) ;
54-
55- var RULES = ast . stylesheet . rules ;
56- var cssForPhantom = RULES . map ( function ( rule ) {
57- return rule . selectors ;
58- } ) ;
53+ var ast = cssTree . parse ( CSS_STR , { filename : commander . css , positions : true } ) ;
54+
55+ var cssForPhantom = [ ] ;
56+ cssTree . walkRules ( ast , ( rule ) => {
57+ if ( 'Atrule' === rule . type ) {
58+ // ignore
59+ } else if ( 'Rule' === rule . type ) {
60+ var converted = rule . prelude . children . map ( ( selector ) => {
61+ return cssTree . translate ( selector )
62+ } )
63+ cssForPhantom . push ( converted )
64+ } else {
65+ throw new Error ( 'BUG: Forgot to handle this rule subtype: ' + rule . type )
66+ }
67+ } )
5968
6069// Check if there is a sourceMappingURL
6170var sourceMapConsumer = null ;
@@ -114,7 +123,7 @@ function generateLcovStr(coverageOutput) {
114123 // coverageOutput is of the form:
115124 // [[1, ['body']], [400, ['div.foo']]]
116125 // where each entry is a pair of count, selectors
117- var expected = RULES . length ;
126+ var expected = cssForPhantom . length ;
118127 var actual = coverageOutput . length ;
119128 if ( expected !== actual ) {
120129 throw new Error ( 'BUG: count lengths do not match. Expected: ' + expected + ' Actual: ' + actual ) ;
@@ -133,26 +142,24 @@ function generateLcovStr(coverageOutput) {
133142 files [ fileName ] . push ( { startLine : startLine , endLine : endLine , count : count } ) ;
134143 }
135144
136- RULES . forEach ( function ( rule , i ) {
137- var count = coverageOutput [ i ] [ 0 ] ;
138-
139- // Skip CSS comments
140- if ( ! rule . selectors ) {
141- // if (commander.verbose) {
142- // console.error('Skipping CSS comment');
143- // }
144- return ;
145+ var i = - 1 ;
146+ cssTree . walkRules ( ast , ( rule , item , list ) => {
147+ if ( 'Rule' !== rule . type ) {
148+ return // Skip AtRules
145149 }
146150
151+ i += 1 ;
152+
153+ var count = coverageOutput [ i ] [ 0 ] ;
147154 var fileName ;
148155 var startLine ;
149156 var endLine ;
150157 // Look up the source map (if available)
151158 if ( sourceMapConsumer ) {
152159 // From https://github.com/mozilla/source-map#sourcemapconsumerprototypeoriginalpositionforgeneratedposition
153160 // Could have been {line: rule.position.start.line, column: rule.positoin.start.column}
154- var origStart = rule . position . start ;
155- var origEnd = rule . position . end ;
161+ var origStart = rule . loc . start ;
162+ var origEnd = rule . loc . end ;
156163
157164 if ( commander . coverDeclarations ) {
158165
@@ -211,9 +218,9 @@ function generateLcovStr(coverageOutput) {
211218 } else {
212219 // No sourceMap available
213220 fileName = commander . css ;
214- startLine = rule . position . start . line ;
221+ startLine = rule . loc . start . line ;
215222 if ( commander . coverDeclarations ) {
216- endLine = rule . position . end . line ;
223+ endLine = rule . loc . end . line ;
217224 } else {
218225 endLine = startLine ; // Just do the selector (startLine)
219226 }
@@ -226,7 +233,8 @@ function generateLcovStr(coverageOutput) {
226233
227234 var nonZero = 0 ; // For summary info
228235 var allCounter = 0 ;
229- ret . push ( 'SF:' + path . resolve ( path . dirname ( sourceMapPath ) , fileName ) ) ;
236+ var fileNamePrefix = sourceMapPath ? path . dirname ( sourceMapPath ) : '' ;
237+ ret . push ( 'SF:' + path . resolve ( fileNamePrefix , fileName ) ) ;
230238
231239 files [ fileName ] . forEach ( function ( entry ) {
232240 var startLine = entry . startLine ;
0 commit comments