Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: exclude lines from processing #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,25 @@ const myVariable = 99
const os = process.platform === 'darwin' ? 'OSXy' /* c8 ignore next */ : 'Windowsy'
```

## Excluding Lines

Sometimes you might find yourself wanting to exclude lines
in your application (for example, perhaps you have your unit tests inside of a file
you wish to check coverage of, but
you don't want to include the tests in the report).

To exclude lines, use the special comments `/* c8 exclude start */` and `/* c8 exclude stop */`.
Comment on lines +80 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different from /* c8 ignore start */ + /* c8 ignore end */? I guess the ignore marks lines as covered, but this exclude would not include them in the report at all - like istanbul instrumented code does?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. It excludes the lines from the report.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my opinion but I think this will be complex for users. This would introduce a second set of ignore hint syntax where:

  • ignore would mark lines as covered even if they are not
  • exclude would remove lines from coverage report - exactly as istanbul instrumented coverage does for /* istanbul ignore ... */ hints

And this new exclude hint does not cover all the same cases as ignore does.

I'm not sure why exactly the ignore syntax has been implemented so that it works differently when compared to istanbul instrumented code. I would rather see it work like your exclude hint here works so that it would completely remove the ignored parts from coverage report.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you about the fact that ignore should work as exclude does.


### Excluding all lines until told

```js
/* c8 exclude start */
function excludeMe() {
// ...
}
/* c8 exclude stop */
```

## Testing

To execute tests, simply run:
Expand Down
24 changes: 23 additions & 1 deletion lib/source.js
Expand Up @@ -16,15 +16,22 @@ module.exports = class CovSource {
let position = 0
let ignoreCount = 0
let ignoreAll = false
let excludeAll = false
for (const [i, lineStr] of source.split(/(?<=\r?\n)/u).entries()) {
const line = new CovLine(i + 1, position, lineStr)
const excludeToken = this._parseExclude(lineStr)
if (excludeToken) {
excludeAll = excludeToken?.start
}
if (ignoreCount > 0) {
line.ignore = true
ignoreCount--
} else if (ignoreAll) {
line.ignore = true
}
this.lines.push(line)
if (!excludeAll && !excludeToken?.stop) {
this.lines.push(line)
}
position += lineStr.length

const ignoreToken = this._parseIgnore(lineStr)
Expand Down Expand Up @@ -73,6 +80,21 @@ module.exports = class CovSource {
}
}

/**
* Parses for comments:
* c8 exclude start
* c8 exclude stop
* @param {string} lineStr
* @return {{start?: boolean, stop?: boolean}|undefined}
*/
_parseExclude (lineStr) {
const testExcludeStartStop = lineStr.match(/\/\* c8 exclude (?<mode>start|stop)/)
if (testExcludeStartStop) {
if (testExcludeStartStop.groups.mode === 'start') return { start: true }
if (testExcludeStartStop.groups.mode === 'stop') return { stop: true }
}
}

// given a start column and end column in absolute offsets within
// a source file (0 - EOF), returns the relative line column positions.
offsetToOriginalRelative (sourceMap, startCol, endCol) {
Expand Down