Support `//` comments
- Version: 12.16.2
- Platform: Darwin Kernel Version 19.4.0
I'd like to see support for the simpler // c8 ignore next comments.
Also // c8 ignore if and // c8 ignore else (even for ternary if possible), as it is nice not to be overaggressive in suppressing missing coverage.
Thanks!
@brettz9 is probably not possible without c8 parsing the AST of the JavaScript test coverage is being collected for (I would like to not do this with c8, the goal being to use V8's built in coverage tracking.).
You can add support for // c8 ignore next comments by yourself if you don't mind launching c8 with an additional preload module. That means instead of running c8 simply with
c8 npm test
pass a preload module to the node executable
node --require=./patch-cov-source.js c8 npm test
And the file patch-cov-source.js would look like this:
'use strict';
const { resolve } = require;
const COV_SOURCE_MODULE_ID = 'v8-to-istanbul/lib/source';
const c8Paths = resolve.paths('c8');
const covSourcePath = resolve(COV_SOURCE_MODULE_ID, { paths: c8Paths });
const CovSource = require(covSourcePath);
CovSource.prototype._parseIgnoreNext =
(lineStr, line) =>
{
const testIgnoreNextLines =
lineStr.match(/^\W*\/\* c8 ignore next (?<count>[0-9]+)? *\*\/\W*$/) ||
lineStr.match(/(?<!\/)\/\/ c8 ignore next(?: (?<count>[0-9]+))?\s*$/);
if (testIgnoreNextLines)
{
line.ignore = true;
const { count } = testIgnoreNextLines.groups;
return count ? Number(count) : 1;
}
else if (lineStr.match(/\/\* c8 ignore next \*\//))
line.ignore = true;
return 0;
};
This basically patches the function _parseIgnoreNext defined in subpackage v8-to-istanbul with an additional regex that recognizes single line comments (with or without a line count). Of course, this can trigger false positives, but so can the original regex for multiline comments, and since the code is not parsed there will never be 100% accuracy.
I did something similar in a project of mine where the generated code has all comments stripped off - including c8 ignore next stuff - so I had to find a way to tell c8 to ignore certain lines depending on other criteria. Luckily, the relevant code is organized in classed and single functions are easy to access at runtime.
Bump, I have a similar issue, we build using esbuild and it does remove all comments. The only way to keep them is to use legal comments like /* c8 ignore next 2 -- @preserve */ but c8 does not seem to allow this tho istanbul does.