Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
oguimbal committed Oct 30, 2020
1 parent f8f74c6 commit 01acda1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
2 changes: 2 additions & 0 deletions packages/istanbul-lib-instrument/api.md
Expand Up @@ -42,6 +42,7 @@ instead.
- `opts.autoWrap` **[boolean][15]** set to true to allow `return` statements outside of functions. (optional, default `false`)
- `opts.produceSourceMap` **[boolean][15]** set to true to produce a source map for the instrumented code. (optional, default `false`)
- `opts.ignoreClassMethods` **[Array][16]** set to array of class method names to ignore for coverage. (optional, default `[]`)
- `opts.ignoreGuardStatements` **[Array][16]** ignore all guard statements. Can contain any of 'returns', 'literalReturns', 'identifierReturns', 'voidReturns', 'throws', 'continues', 'breaks' (optional, default `[]`)
- `opts.sourceMapUrlCallback` **[Function][17]** a callback function that is called when a source map URL
is found in the original code. This function is called with the source file name and the source map URL. (optional, default `null`)
- `opts.debug` **[boolean][15]** turn debugging on (optional, default `false`)
Expand Down Expand Up @@ -114,6 +115,7 @@ The exit function returns an object that currently has the following keys:
- `opts.coverageGlobalScope` **[string][14]** the global coverage variable scope. (optional, default `this`)
- `opts.coverageGlobalScopeFunc` **[boolean][15]** use an evaluated function to find coverageGlobalScope. (optional, default `true`)
- `opts.ignoreClassMethods` **[Array][16]** names of methods to ignore by default on classes. (optional, default `[]`)
- `opts.ignoreGuardStatements` **[Array][16]** ignore all guard statements. Can contain any of 'returns', 'literalReturns', 'identifierReturns', 'voidReturns', 'throws', 'continues', 'breaks' (optional, default `[]`)
- `opts.inputSourceMap` **[object][13]** the input source map, that maps the uninstrumented code back to the
original code. (optional, default `undefined`)

Expand Down
42 changes: 26 additions & 16 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -411,13 +411,17 @@ function convertArrowExpression(path) {
}
}

function isIgnoredGuard(n) {
if (!this.ignoreGuardStatements
|| n.type !== 'BlockStatement'
|| !Array.isArray(this.ignoreGuardStatements)
|| !this.ignoreGuardStatements.length) {
function ignoreGuard(n) {
if (!this.ignoreGuardStatements || n.type !== 'BlockStatement') {
return false;
}
if (!Array.isArray(this.ignoreGuardStatements)) {
return false;
}
if (!this.ignoreGuardStatements.length) {
return false;
}

if (n.body.length !== 1) {
// only ignore simple bodies (signle statement)
return false;
Expand All @@ -432,31 +436,37 @@ function isIgnoredGuard(n) {
return this.ignoreGuardStatements.includes('continues');
}
if (stm.type === 'BreakStatement') {
// ignore continues
// ignore breaks
return this.ignoreGuardStatements.includes('breaks');
}
if (stm.type === 'ReturnStatement') {
if (!this.ignoreGuardStatements) {
return false;
}
if (this.ignoreGuardStatements.includes('returns')) {
// ignore all returns
return true;
}
if (!stm.argument) {
return this.ignoreGuardStatements.includes('literalReturns')
|| this.ignoreGuardStatements.includes('voidReturns');
// ignore void returns
return (
this.ignoreGuardStatements.includes('literalReturns') ||
this.ignoreGuardStatements.includes('voidReturns')
);
}
switch (stm.argument.type) {
case 'NumericLiteral':
case 'BooleanLiteral':
case 'StringLiteral':
case 'NullLiteral':
// ignore constant literal returns
return this.ignoreGuardStatements.includes('literalReturns');
case 'Identifier':
return stm.argument.name === 'undefined' && this.ignoreGuardStatements.includes('literalReturns')
|| this.ignoreGuardStatements.includes('identifierReturns');

if (this.ignoreGuardStatements.includes('identifierReturns')) {
return true;
}
// ignore 'return undefined'
return (
stm.argument.name === 'undefined' &&
this.ignoreGuardStatements.includes('literalReturns')
);
}
}
return false;
Expand All @@ -465,8 +475,8 @@ function isIgnoredGuard(n) {
function coverIfBranches(path) {
const n = path.node;
const hint = this.hintFor(n);
const ignoreIf = hint === 'if' || isIgnoredGuard.call(this, n.consequent);
const ignoreElse = hint === 'else' || isIgnoredGuard.call(this, n.alternate);
const ignoreIf = hint === 'if' || ignoreGuard.call(this, n.consequent);
const ignoreElse = hint === 'else' || ignoreGuard.call(this, n.alternate);
const branch = this.cov.newBranch('if', n.loc);

if (ignoreIf) {
Expand Down

0 comments on commit 01acda1

Please sign in to comment.