diff --git a/lib/source.js b/lib/source.js index d8ebc215..a7edfc1a 100644 --- a/lib/source.js +++ b/lib/source.js @@ -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 (?[0-9]+)/) + const testIgnoreNextLines = lineStr.match(/^\W*\/\* (?:[cv]8|node:coverage) ignore next (?[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 } } @@ -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 (?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 @@ -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(/(?#!.*)/) if (match) { return match.groups.shebang.length } } else { + /* c8 ignore stop - platform-specific */ return 0 } } diff --git a/test/source.js b/test/source.js index 2e5b877d..c4eea834 100644 --- a/test/source.js +++ b/test/source.js @@ -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) + }) + }) })