'use strict'; var path = require('path'); /** @import { ConcatNode, MdNode, StackBuckets } from './types' */ /** @param {string} stack */ function stackSplit(stack) { var pattern = /^\s\s\s\sat\s/; var stackLines = stack.split('\n'); return { frame: stackLines.filter(function (stackLine) { return !pattern.test(stackLine); }), lines: stackLines.filter(function (stackLine) { return pattern.test(stackLine); }), }; } /** * join the stack frame with the lines * @param {StackBuckets} stack */ function stackJoin(stack) { return [ stack.frame.join('\n'), stack.lines.join('\n'), ].join('\n'); } /** * @param {readonly MdNode[]} nodes * @param {number | undefined} line */ function findErrorNode(nodes, line) { return nodes.find(function (node) { return Number(node.startLine) <= Number(line) && Number(node.endLine) >= Number(line); }); } /** * @param {unknown} e * @returns {e is Error & { loc: { line: number, column: number } }} */ var isAcornError = function (e) { return e instanceof Error && 'loc' in e && typeof e.loc === 'object' && e.loc !== null && 'line' in e.loc && typeof e.loc.line === 'number' && 'column' in e.loc && typeof e.loc.column === 'number'; }; /** * @param {readonly MdNode[]} nodes * @param {string} filePath */ function acornError(nodes, filePath) { return /** @param {unknown} e */ function (e) { if (!isAcornError(e) || !e.stack || !e.loc) { return e; } var stack = stackSplit(e.stack); var lineChar = [ e.loc.line, ':', e.loc.column, ].join(''); var errorNode = findErrorNode(nodes, e.loc.line); var absFilePath = path.resolve(filePath); var line = [ ' at ', absFilePath, ':', lineChar, errorNode ? " {block ".concat(errorNode.id, "}") : '', ].join(''); stack.lines = [line]; return stackJoin(stack); }; } /** @param {string | Error} s */ function parseLineChar(s) { var str = s instanceof Error ? s.message : s; var patternLineChar = /:(\d+):(\d+)/; var patternLine = /:(\d+)/; var matchLineChar = str.match(patternLineChar); if (matchLineChar) { return { lineChar: matchLineChar[0], line: parseInt(matchLineChar[1], 10), char: parseInt(matchLineChar[2], 10), }; } var matchLine = str.match(patternLine); if (matchLine) { return { lineChar: parseInt(matchLine[1], 10), line: parseInt(matchLine[1], 10), char: false, }; } return false; } /** * @param {string[]} incLines * @param {ConcatNode} nodes * @param {string} absFilePath * @param {boolean} frame */ function getCleanLines(incLines, nodes, absFilePath, frame) { var lines = incLines.map(function (line) { var lineChar = parseLineChar(line); var matchNodes = nodes.find(function (node) { if (!node.fileEvalHashPath) { return false; } return line.match(node.fileEvalHashPath); }); var matchNode = (function () { if (!nodes.fileEvalHashPath) { return false; } var match = line.match(nodes.fileEvalHashPath); if (!match) { return false; } var errorNode = findErrorNode(nodes, lineChar ? lineChar.line : undefined); return { id: errorNode && errorNode.id ? errorNode.id : nodes.id, fileEval: nodes.fileEval, }; }()); var match = matchNodes || matchNode || false; /** @type {string | false} */ var replacement = false; if (match) { replacement = ''; if (!frame) { replacement += ' at '; } replacement += absFilePath; if (lineChar && lineChar.line && lineChar.char) { replacement += ":".concat(lineChar.line, ":").concat(lineChar.char); } if (lineChar && lineChar.line && !lineChar.char) { replacement += ":".concat(lineChar.line); } if (match.id && !match.fileEval) { replacement += " {block ".concat(match.id, "}"); } if (match.id && match.fileEval) { replacement += " {block ".concat(match.id, " (").concat(match.fileEval, ")}"); } } return { line: line, replacement: replacement, }; }); lines = lines.reverse(); var matchFound = false; if (!frame) { lines = lines.filter(function (line) { if (line.replacement) { matchFound = true; } return matchFound; }); } var cleanLines = lines.map(function (line) { if (typeof line.replacement === 'string') { return line.replacement; } return line.line; }); return cleanLines.reverse(); } module.exports = { stackSplit: stackSplit, stackJoin: stackJoin, findErrorNode: findErrorNode, isAcornError: isAcornError, acornError: acornError, parseLineChar: parseLineChar, getCleanLines: getCleanLines, };