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

WIP: feat: add platform-conditional ignore hints #619

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 28 additions & 3 deletions packages/istanbul-lib-instrument/src/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { SourceCoverage } = require('./source-coverage');
const { SHA, MAGIC_KEY, MAGIC_VALUE } = require('./constants');

// pattern for istanbul to ignore a section
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/;
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?:\s+platform(!?)=([a-z0-9]))?(?=\W|$)/;
// pattern for istanbul to ignore the whole file
const COMMENT_FILE_RE = /^\s*istanbul\s+ignore\s+(file)(?=\W|$)/;
const COMMENT_FILE_RE = /^\s*istanbul\s+ignore\s+(file)(?:\s+platform(!?)=([a-z0-9]))?(?=\W|$)/;
// source map URL pattern
const SOURCE_MAP_RE = /[#@]\s*sourceMappingURL=(.*)\s*$/m;

Expand Down Expand Up @@ -56,6 +56,16 @@ class VisitState {
).trim();
const groups = v.match(COMMENT_RE);
if (groups) {
if (groups[3]) {
const negate = groups[2] === '!'
const platformMatch = groups[3] === process.platform
if (platformMatch === negate) {
// either platform!=windows and we are windows,
// or platform=win32 and we're not windows
// do not add this hint.
return
}
}
hint = groups[1];
}
});
Expand Down Expand Up @@ -567,7 +577,22 @@ function alreadyInstrumented(path, visitState) {
function shouldIgnoreFile(programNode) {
return (
programNode.parent &&
programNode.parent.comments.some(c => COMMENT_FILE_RE.test(c.value))
programNode.parent.comments.some(c => {
const groups = c.value.test(COMMENT_FILE_RE)
if (groups) {
if (groups[3]) {
const negate = groups[2] === '!'
const platformMatch = groups[3] === process.platform
if (platformMatch === negate) {
// either platform!=windows and we are windows,
// or platform=win32 and we're not windows
// do not add this hint.
return false
}
}
return true
}
})
);
}

Expand Down