testplane
Version:
Tests framework based on mocha and wdio
46 lines • 2.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveLocationWithSourceMap = exports.extractSourceMaps = void 0;
const source_map_js_1 = require("source-map-js");
const url_1 = __importDefault(require("url"));
const constants_1 = require("./constants");
const utils_1 = require("./utils");
const fs_1 = require("../utils/fs");
const typescript_1 = require("../utils/typescript");
const extractSourceMaps = async (fileContents, fileName) => {
const hasNoSourceMaps = fileContents.indexOf(constants_1.JS_SOURCE_MAP_URL_COMMENT) === -1;
const isEsmFile = fileName.startsWith("file://");
if (hasNoSourceMaps && !isEsmFile) {
fileContents = (0, typescript_1.transformCode)(fileContents, { sourceFile: fileName, sourceMaps: true });
}
const sourceMapsStartIndex = fileContents.lastIndexOf(constants_1.JS_SOURCE_MAP_URL_COMMENT);
const sourceMapsEndIndex = fileContents.indexOf("\n", sourceMapsStartIndex);
if (sourceMapsStartIndex === -1) {
return null;
}
const sourceMapUrl = sourceMapsEndIndex === -1
? fileContents.slice(sourceMapsStartIndex + constants_1.JS_SOURCE_MAP_URL_COMMENT.length)
: fileContents.slice(sourceMapsStartIndex + constants_1.JS_SOURCE_MAP_URL_COMMENT.length, sourceMapsEndIndex);
const sourceMaps = await (0, utils_1.getSourceCodeFile)(url_1.default.resolve(fileName, sourceMapUrl));
const rawSourceMaps = JSON.parse(sourceMaps);
rawSourceMaps.file = rawSourceMaps.file || fileName;
return new source_map_js_1.SourceMapConsumer(rawSourceMaps);
};
exports.extractSourceMaps = extractSourceMaps;
const resolveLocationWithSourceMap = (stackFrame, sourceMaps) => {
const positions = sourceMaps.originalPositionFor({ line: stackFrame.lineNumber, column: stackFrame.columnNumber });
const source = positions.source ? sourceMaps.sourceContentFor(positions.source) : null;
const location = { line: positions.line, column: positions.column };
if (!source) {
throw new Error("File source code could not be evaluated from the source map");
}
if (!location.line || !location.column) {
throw new Error("Line and column could not be evaluated from the source map");
}
return { file: (0, fs_1.softFileURLToPath)(sourceMaps.file), source, location };
};
exports.resolveLocationWithSourceMap = resolveLocationWithSourceMap;
//# sourceMappingURL=source-maps.js.map