Skip to content

Commit

Permalink
Consider embedded input source map in file coverage
Browse files Browse the repository at this point in the history
When locating the source map, use the source map included in the
file coverage whenever possible. Closes SitePen#2 but requires
istanbuljs-archived-repos/istanbul-lib-instrument#23
  • Loading branch information
Micha Reiser committed Nov 7, 2016
1 parent 72ddb4b commit 96911f6
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions lib/remap.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,38 +231,43 @@ define([
else {
codeIsArray = false;
}
var match = sourceMapRegEx.exec(jsText);
var sourceMapDir = path.dirname(filePath);
var rawSourceMap;

if (!match && !codeFromFile) {
codeIsArray = false;
jsText = readFile(filePath);
match = sourceMapRegEx.exec(jsText);
}

if (match) {
if (match[1]) {
rawSourceMap = JSON.parse((new Buffer(match[2], 'base64').toString('utf8')));
var rawSourceMap;
var sourceMapDir = path.dirname(filePath);
if (fileCoverage.inputSourceMap) {
rawSourceMap = fileCoverage.inputSourceMap;
} else {
var match = sourceMapRegEx.exec(jsText);

if (!match && !codeFromFile) {
codeIsArray = false;
jsText = readFile(filePath);
match = sourceMapRegEx.exec(jsText);
}
else {
var sourceMapPath = path.join(sourceMapDir, match[2]);
rawSourceMap = readJSON(sourceMapPath);
sourceMapDir = path.dirname(sourceMapPath);

if (match) {
if (match[1]) {
rawSourceMap = JSON.parse((new Buffer(match[2], 'base64').toString('utf8')));
}
else {
var sourceMapPath = path.join(sourceMapDir, match[2]);
rawSourceMap = readJSON(sourceMapPath);
sourceMapDir = path.dirname(sourceMapPath);
}
}
}

if (!match || !rawSourceMap) {
/* We couldn't find a source map, so will copy coverage after warning. */
warn(new Error('Could not find source map for: "' + filePath + '"'));
try {
fileCoverage.code = String(fs.readFileSync(filePath)).split('\n');
}
catch (error) {
warn(new Error('Could find source for : "' + filePath + '"'));
if (!match || !rawSourceMap) {
/* We couldn't find a source map, so will copy coverage after warning. */
warn(new Error('Could not find source map for: "' + filePath + '"'));
try {
fileCoverage.code = String(fs.readFileSync(filePath)).split('\n');
}
catch (error) {
warn(new Error('Could find source for : "' + filePath + '"'));
}
srcCoverage[filePath] = fileCoverage;
return;
}
srcCoverage[filePath] = fileCoverage;
return;
}

sourceMapDir = options.basePath || sourceMapDir;
Expand Down

0 comments on commit 96911f6

Please sign in to comment.