Skip to content

Commit

Permalink
fix: ignore empty lines and JavaScript comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-redFox committed Feb 7, 2024
1 parent 521dee7 commit 7f2ddb3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 832 deletions.
3 changes: 2 additions & 1 deletion lib/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = class CovLine {
this.count = 1

// set by source.js during parsing, if /* c8 ignore next */ is found.
this.ignore = false
// also can be comment or empty line.
this.ignore = /^\s*\/{2}.+\s*$|^\s*$|^\s*\/\*.+\*\/\s*$/.test(lineStr)
}

toIstanbul () {
Expand Down
7 changes: 5 additions & 2 deletions lib/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = class CovSource {
this.lines.push(line)
position += lineStr.length

const ignoreToken = this._parseIgnore(lineStr)
const ignoreToken = this._parseIgnore(lineStr, ignoreAll)
if (!ignoreToken) continue

line.ignore = true
Expand All @@ -51,7 +51,7 @@ module.exports = class CovSource {
* @param {string} lineStr
* @return {{count?: number, start?: boolean, stop?: boolean}|undefined}
*/
_parseIgnore (lineStr) {
_parseIgnore (lineStr, ignoreAll) {
const testIgnoreNextLines = lineStr.match(/^\W*\/\* [c|v]8 ignore next (?<count>[0-9]+)/)
if (testIgnoreNextLines) {
return { count: Number(testIgnoreNextLines.groups.count) }
Expand All @@ -72,6 +72,9 @@ module.exports = class CovSource {
if (testIgnoreStartStop.groups.mode === 'start') return { start: true }
if (testIgnoreStartStop.groups.mode === 'stop') return { stop: true }
}

if (/^\s*\/\*.+(?<!\*\/)\s*$/.test(lineStr)) return { start: true }
if (ignoreAll && /^.+?\*\/\s*$/.test(lineStr)) return { stop: true }
}

// given a start column and end column in absolute offsets within
Expand Down
4 changes: 1 addition & 3 deletions lib/v8-to-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,7 @@ module.exports = class V8ToIstanbul {
s: {}
}
source.lines.forEach((line, index) => {
if (line.ignore) {
return;
}
if (line.ignore) return
statements.statementMap[`${index}`] = line.toIstanbul()
statements.s[`${index}`] = line.count
})
Expand Down
Loading

0 comments on commit 7f2ddb3

Please sign in to comment.