Skip to content

Commit

Permalink
Add support for start and stop ignore hints
Browse files Browse the repository at this point in the history
Adds the ability to do `/* istanbul ignore start */` paired with `/* istanbul ignore end */` to ignore ranges of code rather than AST subtrees.

Functionally this allows for ignore flags at the same points within the tree and just provides syntax that doesn’t require annotating every AST node with the ignore flags.

When the flags partially cover a AST node, the AST is considered NOT skip, but any children that are fully covered will be skipped. Tests for both of these cases are included.

Fixes gotwarlost#413
  • Loading branch information
kpdecker committed Nov 13, 2015
1 parent 237a658 commit 2a55a14
Show file tree
Hide file tree
Showing 3 changed files with 415 additions and 6 deletions.
9 changes: 9 additions & 0 deletions ignoring-code-for-coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ the source code
* A part of a logical expression in which case that part of the expression is ignored for branch coverage
6. It is up to the caller to scope this as narrowly as possible. For example, if you have a source file that is wrapped
in a function expression, adding `/* istanbul ignore next */` at the top of the file will ignore the whole file!
7. Ranges of code may be skipped using `/* istanbul ignore start */` and `/* istanbul ignore end */`. When defined, these only ignore locations that they completely cover. Ex:

```
/* istanbul ignore start */
if (foo || bar /* istanbul ignore end */) {
}
```

Will only ignore the `foo || bar` portions of the code and the `if` will continue to be covered.

### How it works

Expand Down
49 changes: 43 additions & 6 deletions lib/instrumenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
crypto = isNode ? require('crypto') : null,
LEADER_WRAP = '(function () { ',
TRAILER_WRAP = '\n}());',
COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/,
COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next|start|end)(?=\W|$)/,
astgen,
preconditions,
cond,
Expand Down Expand Up @@ -467,11 +467,12 @@
}
return this.instrumentASTSync(program, filename, code);
},
filterHints: function (comments) {
filterHints: function (comments, program) {
var ret = [],
i,
comment,
groups;
groups,
lastHint;
if (!(comments && isArray(comments))) {
return ret;
}
Expand All @@ -481,18 +482,54 @@
if (comment && comment.value && comment.range && isArray(comment.range)) {
groups = String(comment.value).match(COMMENT_RE);
if (groups) {
ret.push({ type: groups[1], start: comment.range[0], end: comment.range[1] });
if (lastHint && lastHint.type === 'start') {
// If we have the end flag then finalize the range
if (groups[1] === 'end') {
lastHint.type = 'range';
lastHint.rangeEnd = comment.range[1];
}
// Otherwise ignore the nested ignore
} else {
lastHint = { type: groups[1], start: comment.range[0], end: comment.range[1] };
ret.push(lastHint);
}
}
}
}

// If there is a start without an end, then expand the range to cover the remainder of the file
if (lastHint && lastHint.type === 'start') {
lastHint.type = 'range';
var programEnd = program.body[program.body.length - 1];
lastHint.rangeEnd = programEnd.range[1] + 1;
}

return ret;
},
extractCurrentHint: function (node) {
if (!node.range) { return; }
var i = this.currentState.lastHintPosition + 1,
hints = this.currentState.hints,
nodeStart = node.range[0],
hint;
hint = this.currentState.currentHint;

// If we are still in a range, keep it running
if (hint) {
if (hint.type === 'range' && hint.rangeEnd > node.range[1]) {
return;
}
}

// Check if we are part of a partial range overlap that didn't
// cover our parents completely
hint = hints[i - 1];
if (hint && hint.type === 'range' && hint.rangeEnd > node.range[1]) {
this.currentState.currentHint = hint;
this.currentState.lastHintPosition = i - 1;
return;
}

// Otherwise check for the next range
this.currentState.currentHint = null;
while (i < hints.length) {
hint = hints[i];
Expand Down Expand Up @@ -540,7 +577,7 @@
branch: 0,
variable: 0,
statement: 0,
hints: this.filterHints(program.comments),
hints: this.filterHints(program.comments, program),
currentHint: null,
lastHintPosition: -1,
ignoring: 0
Expand Down
Loading

0 comments on commit 2a55a14

Please sign in to comment.