Skip to content

Commit eab13c0

Browse files
committed
use const instead of var
when declaring variables
1 parent c878103 commit eab13c0

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "css-coverage",
33
"version": "0.2.6",
44
"scripts": {
5-
"test": "standard --fix"
5+
"test": "./bin/css-coverage --cover-declarations --html ./test/test.html --css ./test/test.css --lcov ./test/test.lcov",
6+
"posttest": "standard --fix"
67
},
78
"dependencies": {
89
"bunyan": "^1.8.12",

src/runCoverage.js

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var fs = require('fs')
2-
var path = require('path')
3-
var puppeteer = require('puppeteer')
4-
var commander = require('commander')
5-
var SourceMapConsumer = require('source-map').SourceMapConsumer
6-
var cssTree = require('css-tree')
1+
const fs = require('fs')
2+
const path = require('path')
3+
const puppeteer = require('puppeteer')
4+
const commander = require('commander')
5+
const SourceMapConsumer = require('source-map').SourceMapConsumer
6+
const cssTree = require('css-tree')
77
require('dotenv').config()
88

99
const bunyan = require('bunyan')
@@ -58,8 +58,8 @@ if (commander.css) {
5858
process.exit(STATUS_CODE.ERROR)
5959
}
6060

61-
var CSS_STR = fs.readFileSync(commander.css, 'utf8')
62-
var ast
61+
const CSS_STR = fs.readFileSync(commander.css, 'utf8')
62+
let ast
6363
try {
6464
ast = cssTree.parse(CSS_STR, { filename: commander.css, positions: true })
6565
} catch (e) {
@@ -68,12 +68,12 @@ try {
6868
throw e
6969
}
7070

71-
var cssRules = []
71+
const cssRules = []
7272
cssTree.walkRules(ast, (rule) => {
7373
if (rule.type === 'Atrule') {
7474
// ignore
7575
} else if (rule.type === 'Rule') {
76-
var converted = rule.prelude.children.map((selector) => {
76+
const converted = rule.prelude.children.map((selector) => {
7777
return cssTree.translate(selector)
7878
})
7979
cssRules.push(converted)
@@ -83,15 +83,16 @@ cssTree.walkRules(ast, (rule) => {
8383
})
8484

8585
// Check if there is a sourceMappingURL
86-
var sourceMapConsumer = null
86+
let sourceMapConsumer = null
87+
let sourceMapPath
8788
if (!commander.ignoreSourceMap && /sourceMappingURL=([^ ]*)/.exec(CSS_STR)) {
88-
var sourceMapPath = /sourceMappingURL=([^ ]*)/.exec(CSS_STR)[1]
89+
sourceMapPath = /sourceMappingURL=([^ ]*)/.exec(CSS_STR)[1]
8990
sourceMapPath = path.resolve(path.dirname(commander.css), sourceMapPath)
9091
if (commander.verbose) {
9192
console.error('Using sourceMappingURL at ' + sourceMapPath)
9293
}
93-
var sourceMapStr = fs.readFileSync(sourceMapPath)
94-
var sourceMap = JSON.parse(sourceMapStr)
94+
const sourceMapStr = fs.readFileSync(sourceMapPath)
95+
const sourceMap = JSON.parse(sourceMapStr)
9596
sourceMapConsumer = new SourceMapConsumer(sourceMap)
9697

9798
// sourceMapConsumer.eachMapping(function (m) { console.log(m.generatedLine, m.source); });
@@ -151,10 +152,10 @@ async function runCoverage () {
151152
const coverageOutput = await page.evaluate(cssRules => {
152153
// This is the meat of the code. It runs inside the browser
153154
console.log(`Starting evaluation`)
154-
var rules = cssRules
155+
const rules = cssRules
155156

156157
// Add default do-nothing for selectors used in cnx-easybake
157-
var PSEUDOS = ['deferred', 'pass', 'match', 'after', 'before', 'outside']
158+
const PSEUDOS = ['deferred', 'pass', 'match', 'after', 'before', 'outside']
158159
PSEUDOS.forEach(function (pseudo) {
159160
window.Sizzle.selectors.match[pseudo] = RegExp(':?:?' + pseudo)
160161
window.Sizzle.selectors.find[pseudo] = function (match, context, isXML) { return context }
@@ -165,7 +166,7 @@ async function runCoverage () {
165166
rules.forEach(function (selectors) {
166167
console.log(`Checking selector: "${JSON.stringify(selectors)}"`)
167168

168-
var count = 0
169+
let count = 0
169170
// selectors could be null (maybe if it's a comment?)
170171
if (selectors) {
171172
selectors.forEach(function (selector) {
@@ -179,7 +180,7 @@ async function runCoverage () {
179180
})
180181

181182
try {
182-
var matches = window.Sizzle(selector)
183+
const matches = window.Sizzle(selector)
183184
count += matches.length
184185
} catch (e) {
185186
// If we cannot select it then we cannot cover it
@@ -203,7 +204,7 @@ async function runCoverage () {
203204
log.debug('Finished evaluating selectors')
204205
log.info('Generating LCOV string...')
205206

206-
var lcovStr = generateLcovStr(coverageOutput)
207+
const lcovStr = generateLcovStr(coverageOutput)
207208
if (commander.lcov) {
208209
fs.writeFileSync(commander.lcov, lcovStr)
209210
} else {
@@ -223,16 +224,16 @@ function generateLcovStr (coverageOutput) {
223224
// coverageOutput is of the form:
224225
// [[1, ['body']], [400, ['div.foo']]]
225226
// where each entry is a pair of count, selectors
226-
var expected = cssRules.length
227-
var actual = coverageOutput.length
227+
const expected = cssRules.length
228+
const actual = coverageOutput.length
228229
if (expected !== actual) {
229230
throw new Error('BUG: count lengths do not match. Expected: ' + expected + ' Actual: ' + actual)
230231
}
231232

232-
var files = {} // key is filename, value is [{startLine, endLine, count}]
233-
var ret = [] // each line in the lcov file. Joined at the end of the function
233+
const files = {} // key is filename, value is [{startLine, endLine, count}]
234+
const ret = [] // each line in the lcov file. Joined at the end of the function
234235

235-
var cssLines = CSS_STR.split('\n')
236+
const cssLines = CSS_STR.split('\n')
236237

237238
function addCoverage (fileName, count, startLine, endLine) {
238239
// add it to the files
@@ -242,33 +243,33 @@ function generateLcovStr (coverageOutput) {
242243
files[fileName].push({startLine: startLine, endLine: endLine, count: count})
243244
}
244245

245-
var i = -1
246+
let i = -1
246247
cssTree.walkRules(ast, (rule, item, list) => {
247248
if (rule.type !== 'Rule') {
248249
return // Skip AtRules
249250
}
250251

251252
i += 1
252253

253-
var count = coverageOutput[i][0]
254-
var fileName
255-
var startLine
256-
var endLine
254+
const count = coverageOutput[i][0]
255+
let fileName
256+
let startLine
257+
let endLine
257258
// Look up the source map (if available)
258259
if (sourceMapConsumer) {
259260
// From https://github.com/mozilla/source-map#sourcemapconsumerprototypeoriginalpositionforgeneratedposition
260261
// Could have been {line: rule.position.start.line, column: rule.positoin.start.column}
261-
var origStart = rule.loc.start
262-
var origEnd = rule.loc.end
262+
const origStart = rule.loc.start
263+
const origEnd = rule.loc.end
263264

264265
if (commander.coverDeclarations) {
265266
// Loop over every character between origStart and origEnd to make sure they are covered
266267
// TODO: Do not duplicate-count lines just because this code runs character-by-character
267-
var parseColumn = origStart.column
268-
for (var parseLine = origStart.line; parseLine <= origEnd.line; parseLine++) {
269-
var curLineText = cssLines[parseLine - 1]
270-
for (var curColumn = parseColumn - 1; curColumn < curLineText.length; curColumn++) {
271-
var info = sourceMapConsumer.originalPositionFor({line: parseLine, column: curColumn})
268+
let parseColumn = origStart.column
269+
for (let parseLine = origStart.line; parseLine <= origEnd.line; parseLine++) {
270+
const curLineText = cssLines[parseLine - 1]
271+
for (let curColumn = parseColumn - 1; curColumn < curLineText.length; curColumn++) {
272+
const info = sourceMapConsumer.originalPositionFor({line: parseLine, column: curColumn})
272273
// stop processing when we hit origEnd
273274
if (parseLine === origEnd.line && curColumn >= origEnd.column) {
274275
break
@@ -292,8 +293,8 @@ function generateLcovStr (coverageOutput) {
292293
}
293294
} else {
294295
// Just cover the selectors
295-
var startInfo = sourceMapConsumer.originalPositionFor({line: origStart.line, column: origStart.column - 1})
296-
// var endInfo = sourceMapConsumer.originalPositionFor({line: origEnd.line, column: origEnd.column - 2})
296+
const startInfo = sourceMapConsumer.originalPositionFor({line: origStart.line, column: origStart.column - 1})
297+
// const endInfo = sourceMapConsumer.originalPositionFor({line: origEnd.line, column: origEnd.column - 2})
297298

298299
// When there is no match, startInfo.source is null
299300
if (!startInfo.source /* || startInfo.source !== endInfo.source */) {
@@ -323,18 +324,18 @@ function generateLcovStr (coverageOutput) {
323324
}
324325
})
325326

326-
for (var fileName in files) {
327-
var nonZero = 0 // For summary info
328-
var allCounter = 0
329-
var fileNamePrefix = sourceMapPath ? path.dirname(sourceMapPath) : ''
327+
for (const fileName in files) {
328+
let nonZero = 0 // For summary info
329+
let allCounter = 0
330+
const fileNamePrefix = sourceMapPath ? path.dirname(sourceMapPath) : ''
330331
ret.push('SF:' + path.resolve(fileNamePrefix, fileName))
331332

332333
files[fileName].forEach(function (entry) {
333-
var startLine = entry.startLine
334-
var endLine = entry.endLine
335-
var count = entry.count
334+
const startLine = entry.startLine
335+
const endLine = entry.endLine
336+
const count = entry.count
336337

337-
for (var line = startLine; line <= endLine; line++) {
338+
for (let line = startLine; line <= endLine; line++) {
338339
ret.push('DA:' + line + ',' + count)
339340
if (count > 0) {
340341
nonZero += 1

0 commit comments

Comments
 (0)