From ab7acaeec799968e1f9deb8bb58c6fa1a758b290 Mon Sep 17 00:00:00 2001 From: Jack Frain Date: Tue, 15 Oct 2024 15:17:12 -0400 Subject: [PATCH] fix(aos): fix error line number in aos --- src/index.js | 2 ++ src/services/errors.js | 40 +++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index c158adea..14cc1564 100644 --- a/src/index.js +++ b/src/index.js @@ -532,6 +532,8 @@ async function doEvaluate(line, id, jwk, spinner, rl, loadedModules, dryRunMode) if (result?.Error || result?.error) { const error = parseError(result.Error || result.error) if (error) { + // When loading, '\n\n' is prepended to the file. This creates a 2 line offset. Fix it here. + error.lineNumber -= 2 // get what file the error comes from, // if the line was loaded const errorOrigin = getErrorOrigin(loadedModules, error.lineNumber) diff --git a/src/services/errors.js b/src/services/errors.js index 469a5516..370b83d9 100644 --- a/src/services/errors.js +++ b/src/services/errors.js @@ -67,15 +67,15 @@ export function getErrorOrigin(loadedModules, lineNumber) { } let currentLine = 0 + let totalLines = 0 for (let i = 0; i < loadedModules.length; i++) { - // get module line count - const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1 - + // get module line count, add 2 for '\n\n' offset + const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1 + 2 if (currentLine + lineCount >= lineNumber) { return { file: loadedModules[i].path, - line: lineNumber - currentLine - i * 2 + line: lineNumber - currentLine - (i + 1) * 2 } } @@ -95,15 +95,25 @@ export function outputError(line, error, origin) { const lineNumber = origin?.line || error.lineNumber const lineNumberPlaceholder = ' '.repeat(lineNumber.toString().length) - console.log( - '\n' + - chalk.bold(error.errorMessage) + - '\n' + - (origin ? chalk.dim(` in ${origin.file}\n`) : "") + - chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) + - line.split('\n')[error.lineNumber - 1] + - '\n' + - chalk.blue(` ${lineNumberPlaceholder} |\n`) + - chalk.dim('This error occurred while aos was evaluating the submitted code.') - ) + if (origin) { + console.log( + '\n' + + chalk.bold(error.errorMessage) + + '\n' + + (origin ? chalk.dim(` in ${origin.file}\n`) : "") + + chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) + + // Add 2 lines back as the line does not includes the '\n\n' offset + line.split('\n')[error.lineNumber - 1 + 2] + + '\n' + + chalk.blue(` ${lineNumberPlaceholder} |\n`) + + chalk.dim('This error occurred while aos was evaluating the submitted code.') + ) + } else { + console.log( + '\n' + + chalk.bold(`Error on line ${lineNumber}: ${error.errorMessage}`) + + '\n' + + chalk.dim('This error occurred while aos was evaluating the submitted code.') + ) + } }