From 591126b102244465b27906cdb8cdb82df1f4e760 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 21 Dec 2020 18:32:59 -0600 Subject: [PATCH] feat: support comment c8 ignore start/stop (#130) --- README.md | 10 +++++++++ lib/source.js | 57 +++++++++++++++++++++++++++++++++++++------------- test/source.js | 24 +++++++++++++++++++++ 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cb593961..5c8c6791 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/source.js b/lib/source.js index e573d1b4..4d8f1cb9 100644 --- a/lib/source.js +++ b/lib/source.js @@ -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 (?[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 (?[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 (?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 diff --git a/test/source.js b/test/source.js index 512ed98e..8a74df82 100644 --- a/test/source.js +++ b/test/source.js @@ -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) + }) }) })