Skip to content

Commit

Permalink
feat: support comment c8 ignore start/stop (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 22, 2020
1 parent 08c4c2c commit 591126b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -60,6 +60,16 @@ if (process.platform === 'win32') {
}
```

### ignoring all lines until told

```js
/* c8 ignore start */
function dontMindMe() {
// ...
}
/* c8 ignore stop */
```

### ignoring the same line as the comment

```js
Expand Down
57 changes: 42 additions & 15 deletions lib/source.js
Expand Up @@ -14,35 +14,62 @@ module.exports = class CovSource {
_buildLines (source) {
let position = 0
let ignoreCount = 0
let ignoreAll = false
for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
const line = new CovLine(i + 1, position, lineStr)
if (ignoreCount > 0) {
line.ignore = true
ignoreCount--
} else {
ignoreCount = this._parseIgnoreNext(lineStr, line)
} else if (ignoreAll) {
line.ignore = true
}
this.lines.push(line)
position += lineStr.length
}
}

_parseIgnoreNext (lineStr, line) {
const testIgnoreNextLines = lineStr.match(/^\W*\/\* c8 ignore next (?<count>[0-9]+)? *\*\/\W*$/)
if (testIgnoreNextLines) {
const ignoreToken = this._parseIgnore(lineStr)
if (!ignoreToken) continue

line.ignore = true
if (testIgnoreNextLines.groups.count) {
return Number(testIgnoreNextLines.groups.count)
} else {
return 1
if (ignoreToken.count !== undefined) {
ignoreCount = ignoreToken.count
}
} else {
if (lineStr.match(/\/\* c8 ignore next \*\//)) {
line.ignore = true
if (ignoreToken.start || ignoreToken.stop) {
ignoreAll = ignoreToken.start
ignoreCount = 0
}
}
}

return 0
/**
* Parses for comments:
* c8 ignore next
* c8 ignore next 3
* c8 ignore start
* c8 ignore stop
* @param {string} lineStr
* @return {{count?: number, start?: boolean, stop?: boolean}|undefined}
*/
_parseIgnore (lineStr) {
const testIgnoreNextLines = lineStr.match(/^\W*\/\* c8 ignore next (?<count>[0-9]+) *\*\/\W*$/)
if (testIgnoreNextLines) {
return { count: Number(testIgnoreNextLines.groups.count) }
}

// Check if comment is on its own line.
if (lineStr.match(/^\W*\/\* c8 ignore next *\*\/\W*$/)) {
return { count: 1 }
}

if (lineStr.match(/\/\* c8 ignore next \*\//)) {
// Won't ignore successive lines, but the current line will be ignored.
return { count: 0 }
}

const testIgnoreStartStop = lineStr.match(/\/\* c8 ignore (?<mode>start|stop) *\*\//)
if (testIgnoreStartStop) {
if (testIgnoreStartStop.groups.mode === 'start') return { start: true }
if (testIgnoreStartStop.groups.mode === 'stop') return { stop: true }
}
}

// given a start column and end column in absolute offsets within
Expand Down
24 changes: 24 additions & 0 deletions test/source.js
Expand Up @@ -74,5 +74,29 @@ describe('Source', () => {
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(false)
})

it('ignores lines between start and stop', () => {
const sourceRaw = `
/* c8 ignore start */
function ignoreMe() {
// ...
}
/* c8 ignore stop */
function doNotIgnoreMe() {
// ...
}
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(true)
source.lines[3].ignore.should.equal(true)
source.lines[4].ignore.should.equal(true)
source.lines[5].ignore.should.equal(true)
source.lines[6].ignore.should.equal(false)
source.lines[7].ignore.should.equal(false)
source.lines[8].ignore.should.equal(false)
source.lines[9].ignore.should.equal(false)
})
})
})

0 comments on commit 591126b

Please sign in to comment.