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

resolve link cannot be located in the source code #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function retrieveFile(path) {
}

if (/^\S+:\/\//.test(path) && !/^http(s)?:\/\//.test(path)) {
return null
return null;
}

try {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, false);
xhr.send(null);
var contents = null
var contents = null;
if (xhr.readyState === 4 && xhr.status === 200) {
contents = xhr.responseText
contents = xhr.responseText;
}

} catch (e) {
Expand All @@ -44,9 +44,11 @@ function retrieveFile(path) {
// Support URLs relative to a directory, but be careful about a protocol prefix
// in case we are in the browser (i.e. directories may start with "http://")
function supportRelativeURL(file, url) {
var regx = /^\w+:\/\/[^\/]*/;
if (regx.test(url)) return url;
if (!file) return url;
var dir = path.dirname(file);
var match = /^\w+:\/\/[^\/]*/.exec(dir);
var match = regx.exec(dir);
var protocol = match ? match[0] : '';
return protocol + path.resolve(dir.slice(protocol.length), url);
}
Expand All @@ -62,12 +64,11 @@ function retrieveSourceMapURL(source, position) {
fileData = xhr.responseText;
fileContentsCache[source] = fileData;
}
if (xhr.status !== 200) return null
} catch (e) { return null }
if (xhr.status !== 200) return null;
} catch (e) { return null; }

// Support providing a sourceMappingURL via the SourceMap header
var sourceMapHeader = xhr.getResponseHeader('SourceMap') ||
xhr.getResponseHeader('X-SourceMap');
var sourceMapHeader = xhr.getResponseHeader('SourceMap') || xhr.getResponseHeader('X-SourceMap');
if (sourceMapHeader) {
return sourceMapHeader;
}
Expand All @@ -80,7 +81,7 @@ function retrieveSourceMapURL(source, position) {
while (match = re.exec(fileData)) lastMatch = match; // eslint-disable-line
if (!lastMatch) {
var line = fileData.split(/(?:\r\n|\r|\n)/)[position.line - 1];
line = line.replace(/^.*sourceMappingURL=/, '').replace(/"\s*\);\s*$/,'')
line = line.replace(/^.*sourceMappingURL=/, '').replace(/"\s*\);\s*$/,'');
if (reSourceMap.test(line)) return line;
return null;
}
Expand All @@ -103,8 +104,8 @@ function retrieveSourceMap(source, position) {
// Support source map URL as a data url
var rawData = sourceMappingURL.slice(sourceMappingURL.indexOf(',') + 1);
sourceMapData = new Buffer(rawData, 'base64').toString();
sourceMapData = JSON.parse(sourceMapData)
source = sourceMapData.sources[0]
sourceMapData = JSON.parse(sourceMapData);
source = sourceMapData.sources[0];
sourceMappingURL = null;
} else {
// Support source map URLs relative to the source URL
Expand All @@ -128,8 +129,8 @@ function mapSourcePosition(position) {
// Call the (overrideable) retrieveSourceMap function to get the source map.
var urlAndMap = retrieveSourceMap(position.source, position);
if (urlAndMap) {
var source = urlAndMap.url ? position.source : urlAndMap.map.sources[0]
position.source = source
var source = urlAndMap.url ? position.source : urlAndMap.map.sources[0];
position.source = source;
sourceMap = sourceMapCache[source] = {
url: urlAndMap.url,
map: new SourceMapConsumer(urlAndMap.map)
Expand All @@ -156,17 +157,17 @@ function mapSourcePosition(position) {

// Resolve the source URL relative to the URL of the source map
if (sourceMap && sourceMap.map) {
var pos
var pos;
if (position.eval) {
pos = {
line: position.eval[0],
column: position.eval[1]
}
};
} else {
pos = {
line: position.line,
column: position.column
}
};
}

var originalPosition = sourceMap.map.originalPositionFor(pos);
Expand Down Expand Up @@ -200,7 +201,7 @@ function getEvalPosition(origin, ln, cn) {
eval:[ln, cn]
});

return position
return position;
}

// Parse nested eval() calls using recursion
Expand Down Expand Up @@ -231,8 +232,8 @@ function CallSiteToString() {
fileLocation += ', '; // Expecting source position to follow.
}
if (fileName && !/^\w+:\/\//.test(fileName)) {
var root = location.protocol + '//' + location.host
fileName = root + (/^\//.test(fileName) ? fileName : '/' + fileName)
var root = location.protocol + '//' + location.host;
fileName = root + (/^\//.test(fileName) ? fileName : '/' + fileName);
}

if (fileName) {
Expand Down Expand Up @@ -299,9 +300,9 @@ function wrapCallSite(frame) {
// Most call sites will return the source file from getFileName(), but code
// passed to eval() ending in "//# sourceURL=..." will return the source file
// from getScriptNameOrSourceURL() instead
if (frame.isNative()) return frame
if (frame.isNative()) return frame;
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
var position
var position;
if (source) {
var line = frame.getLineNumber();
var column = frame.getColumnNumber() - 1;
Expand All @@ -322,15 +323,15 @@ function wrapCallSite(frame) {
var origin = frame.isEval() && frame.getEvalOrigin();
if (origin) {
// ln and cn in eval
var ln = frame.getLineNumber()
var cn = frame.getColumnNumber()
var ln = frame.getLineNumber();
var cn = frame.getColumnNumber();
position = getEvalPosition(origin, ln, cn);
frame = cloneCallSite(frame);
frame.getScriptNameOrSourceURL = function() { return position.source.replace(/\?.*$/, ''); };
frame.getLineNumber = function() { return position.line; };
frame.getColumnNumber = function() { return position.column ? position.column + 1: cn; };
frame.getEvalOrigin = function() { return origin; };
frame.getFileName = function () { return position.source}
frame.getFileName = function () { return position.source;};
return frame;
}

Expand All @@ -347,8 +348,8 @@ function prepareStackTrace(error, stack) {
}
stack = stack.filter(function (frame) {
var source = frame.getFileName() || frame.getScriptNameOrSourceURL();
return !/node_modules\/mocha/.test(source)
})
return !/node_modules\/mocha/.test(source);
});
return error + stack.map(function(frame) {
return '\n at ' + wrapCallSite(frame);
}).join('');
Expand All @@ -370,8 +371,7 @@ function getErrorSource(error) {
if (contents) {
var code = contents.split(/(?:\r\n|\r|\n)/)[line - 1];
if (code) {
return source + ':' + line + '\n' + code + '\n' +
new Array(column).join(' ') + '^';
return source + ':' + line + '\n' + code + '\n' + new Array(column).join(' ') + '^';
}
}
}
Expand All @@ -384,12 +384,12 @@ exports.mapSourcePosition = mapSourcePosition;
exports.retrieveSourceMap = retrieveSourceMap;

module.exports = function (option) {
option = option || {}
option = option || {};
if (/^file/i.test(location.protocol)) {
console.warn('stack-source-map not works on file protocol')
console.warn('stack-source-map not works on file protocol');
} else {
Error.prepareStackTrace = prepareStackTrace;
}
if (option.hasOwnProperty('empty')) emptyCacheBetweenOperations = option.empty
}
if (option.hasOwnProperty('empty')) emptyCacheBetweenOperations = option.empty;
};