Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: readInitialCoverage - extracts empty coverage data from an inst…
…rumented file In support of finally fixing istanbuljs/babel-plugin-istanbul#4. Blocked on a babel-traverse release that includes babel/babel#4746
- Loading branch information
Showing
5 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createHash} from 'crypto'; | ||
|
||
// function to use for creating hashes | ||
const SHA = 'sha1'; | ||
|
||
// generate a variable name from hashing the supplied file path | ||
export default function genVar(filename) { | ||
var hash = createHash(SHA); | ||
hash.update(filename); | ||
return 'cov_' + parseInt(hash.digest('hex').substr(0, 12), 16).toString(36); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import genVar from './gen-var'; | ||
import { parse } from 'babylon'; | ||
import traverse from 'babel-traverse'; | ||
import * as t from 'babel-types'; | ||
|
||
export default function readInitialCoverage (code, filename) { | ||
if (typeof code !== 'string') { | ||
throw new Error('Code must be a string'); | ||
} | ||
|
||
// Parse as leniently as possible | ||
const ast = parse(code, { | ||
allowImportExportEverywhere: true, | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
sourceType: "script", // I think ? | ||
plugins: ["*"] | ||
}); | ||
|
||
const varName = genVar(filename); | ||
let covPath; | ||
traverse(ast, { | ||
VariableDeclarator: function (path) { | ||
if (t.isIdentifier(path.node.id) && path.node.id.name === varName) { | ||
covPath = path.get('init'); | ||
path.stop(); | ||
} | ||
} | ||
}); | ||
|
||
if (!covPath) { | ||
return null; | ||
} | ||
|
||
const binding = covPath.get('callee.body').scope.getOwnBinding("coverageData"); | ||
if (!binding) { | ||
return null; | ||
} | ||
|
||
const dataPath = binding.path.get('init'); | ||
const coverageData = dataPath.evaluate(); | ||
if (!coverageData.confident) { | ||
return null; | ||
} | ||
|
||
return coverageData.value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters