Skip to content

Commit

Permalink
feat: respect node:coverage comments (#252)
Browse files Browse the repository at this point in the history
This respects the `node:coverage` comments as used by the node:test
coverage reporter.

- `node:coverage disable` -> `c8 ignore start`
- `node:coverage enable` -> `c8 ignore stop`
- `node:coverage ignore next` -> `c8 ignore ignore next`
- `node:coverage ignore next N` -> `c8 ignore ignore next N`
  • Loading branch information
isaacs authored Jun 22, 2024
1 parent fcc2e35 commit 92d593e
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ module.exports = class CovSource {
* @return {{count?: number, start?: boolean, stop?: boolean}|undefined}
*/
_parseIgnore (lineStr) {
const testIgnoreNextLines = lineStr.match(/^\W*\/\* [c|v]8 ignore next (?<count>[0-9]+)/)
const testIgnoreNextLines = lineStr.match(/^\W*\/\* (?:[cv]8|node:coverage) ignore next (?<count>[0-9]+)/)
if (testIgnoreNextLines) {
return { count: Number(testIgnoreNextLines.groups.count) }
}

// Check if comment is on its own line.
if (lineStr.match(/^\W*\/\* [c|v]8 ignore next/)) {
if (lineStr.match(/^\W*\/\* (?:[cv]8|node:coverage) ignore next/)) {
return { count: 1 }
}

if (lineStr.match(/\/\* [c|v]8 ignore next/)) {
if (lineStr.match(/\/\* ([cv]8|node:coverage) ignore next/)) {
// Won't ignore successive lines, but the current line will be ignored.
return { count: 0 }
}
Expand All @@ -72,6 +72,12 @@ module.exports = class CovSource {
if (testIgnoreStartStop.groups.mode === 'start') return { start: true }
if (testIgnoreStartStop.groups.mode === 'stop') return { stop: true }
}

const testNodeIgnoreStartStop = lineStr.match(/\/\* node:coverage (?<mode>enable|disable)/)
if (testNodeIgnoreStartStop) {
if (testNodeIgnoreStartStop.groups.mode === 'disable') return { start: true }
if (testNodeIgnoreStartStop.groups.mode === 'enable') return { stop: true }
}
}

// given a start column and end column in absolute offsets within
Expand Down Expand Up @@ -235,12 +241,14 @@ function originalPositionTryBoth (sourceMap, line, column) {
// Not required since Node 12, see: https://github.com/nodejs/node/pull/27375
const isPreNode12 = /^v1[0-1]\./u.test(process.version)
function getShebangLength (source) {
/* c8 ignore start - platform-specific */
if (isPreNode12 && source.indexOf('#!') === 0) {
const match = source.match(/(?<shebang>#!.*)/)
if (match) {
return match.groups.shebang.length
}
} else {
/* c8 ignore stop - platform-specific */
return 0
}
}
98 changes: 98 additions & 0 deletions test/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,102 @@ describe('Source', () => {
})
})
}

describe('ignore hint using node:coverage', () => {
it('ignores the next line if /* node:coverage ignore next */ is on its own line', () => {
const sourceRaw = `
const a = 33
/* node:coverage ignore next */
const a = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(false)
source.lines[2].ignore.should.equal(true)
source.lines[3].ignore.should.equal(true)
})

it('ignores the next N lines if /* node:coverage ignore next N */ is used', () => {
const sourceRaw = `
/* node:coverage ignore next 2 */
const a = 33
const a = 99
`
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)
})

it('ignores a line that contains /* node:coverage ignore next */', () => {
const sourceRaw = `
const a = foo ? true /* node:coverage ignore next */ : false
const b = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(false)
})

it('ignores lines between disable and enable', () => {
const sourceRaw = `
/* node:coverage disable */
function ignoreMe() {
// ...
}
/* node:coverage enable */
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)
})

it('ignore hint accepts other text content', () => {
const sourceRaw = `
const a = 33
/* node:coverage ignore next -- reasoning why this is ignored */
const b = 99
/* node:coverage disable: reasoning here */
function ignoreMe() {
// ...
}
/* node:coverage enable -- @preserve */
const c = a ? true /* node:coverage ignore next reasoning here */ : false
/* node:coverage ignore next 2 -- ignores next two lines */
const a = 33
const a = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(false)
source.lines[2].ignore.should.equal(false)
source.lines[3].ignore.should.equal(true)
source.lines[4].ignore.should.equal(true)
source.lines[5].ignore.should.equal(false)
source.lines[6].ignore.should.equal(true)
source.lines[7].ignore.should.equal(true)
source.lines[8].ignore.should.equal(true)
source.lines[9].ignore.should.equal(true)
source.lines[10].ignore.should.equal(true)
source.lines[11].ignore.should.equal(false)
source.lines[12].ignore.should.equal(true)
source.lines[13].ignore.should.equal(false)
source.lines[14].ignore.should.equal(true)
source.lines[15].ignore.should.equal(true)
source.lines[16].ignore.should.equal(true)
})
})
})

0 comments on commit 92d593e

Please sign in to comment.