diff options
author | Joel Kronqvist <joel.h.kronqvist@gmail.com> | 2022-03-05 19:02:27 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.h.kronqvist@gmail.com> | 2022-03-05 19:02:27 +0200 |
commit | 5d309ff52cd399a6b71968a6b9a70c8ac0b98981 (patch) | |
tree | 360f7eb50f956e2367ef38fa1fc6ac7ac5258042 /node_modules/istanbul-lib-instrument/src/read-coverage.js | |
parent | b500a50f1b97d93c98b36ed9a980f8188d648147 (diff) | |
download | LYLLRuoka-5d309ff52cd399a6b71968a6b9a70c8ac0b98981.tar.gz LYLLRuoka-5d309ff52cd399a6b71968a6b9a70c8ac0b98981.zip |
Added node_modules for the updating to work properly.
Diffstat (limited to 'node_modules/istanbul-lib-instrument/src/read-coverage.js')
-rw-r--r-- | node_modules/istanbul-lib-instrument/src/read-coverage.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/node_modules/istanbul-lib-instrument/src/read-coverage.js b/node_modules/istanbul-lib-instrument/src/read-coverage.js new file mode 100644 index 0000000..5b76dbb --- /dev/null +++ b/node_modules/istanbul-lib-instrument/src/read-coverage.js @@ -0,0 +1,77 @@ +const { parseSync, traverse } = require('@babel/core'); +const { defaults } = require('@istanbuljs/schema'); +const { MAGIC_KEY, MAGIC_VALUE } = require('./constants'); + +function getAst(code) { + if (typeof code === 'object' && typeof code.type === 'string') { + // Assume code is already a babel ast. + return code; + } + + if (typeof code !== 'string') { + throw new Error('Code must be a string'); + } + + // Parse as leniently as possible + return parseSync(code, { + babelrc: false, + configFile: false, + parserOpts: { + allowAwaitOutsideFunction: true, + allowImportExportEverywhere: true, + allowReturnOutsideFunction: true, + allowSuperOutsideMethod: true, + sourceType: 'script', + plugins: defaults.instrumenter.parserPlugins + } + }); +} + +module.exports = function readInitialCoverage(code) { + const ast = getAst(code); + + let covScope; + traverse(ast, { + ObjectProperty(path) { + const { node } = path; + if ( + !node.computed && + path.get('key').isIdentifier() && + node.key.name === MAGIC_KEY + ) { + const magicValue = path.get('value').evaluate(); + if (!magicValue.confident || magicValue.value !== MAGIC_VALUE) { + return; + } + covScope = + path.scope.getFunctionParent() || + path.scope.getProgramParent(); + path.stop(); + } + } + }); + + if (!covScope) { + return null; + } + + const result = {}; + + for (const key of ['path', 'hash', 'gcv', 'coverageData']) { + const binding = covScope.getOwnBinding(key); + if (!binding) { + return null; + } + const valuePath = binding.path.get('init'); + const value = valuePath.evaluate(); + if (!value.confident) { + return null; + } + result[key] = value.value; + } + + delete result.coverageData[MAGIC_KEY]; + delete result.coverageData.hash; + + return result; +}; |