Skip to content

Commit ee4a4f5

Browse files
committed
fix(aos): fix error line number in aos
1 parent 6bd8ead commit ee4a4f5

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ async function doEvaluate(line, id, jwk, spinner, rl, loadedModules, dryRunMode)
535535
// get what file the error comes from,
536536
// if the line was loaded
537537
const errorOrigin = getErrorOrigin(loadedModules, error.lineNumber)
538-
539538
// print error
540539
outputError(line, error, errorOrigin)
541540
} else {

src/services/errors.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ export function getErrorOrigin(loadedModules, lineNumber) {
6969
let currentLine = 0
7070

7171
for (let i = 0; i < loadedModules.length; i++) {
72-
// get module line count
73-
const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1
74-
72+
// get module line count, add 2 for '\n\n' offset
73+
const lineCount = (loadedModules[i].content.match(/\r?\n/g)?.length || 0) + 1 + 2
7574
if (currentLine + lineCount >= lineNumber) {
7675
return {
7776
file: loadedModules[i].path,
78-
line: lineNumber - currentLine - i * 2
77+
line: lineNumber - currentLine - (i + 1) * 2
7978
}
8079
}
8180

@@ -92,18 +91,28 @@ export function getErrorOrigin(loadedModules, lineNumber) {
9291
* @param {ErrorOrigin|undefined} origin
9392
*/
9493
export function outputError(line, error, origin) {
95-
const lineNumber = origin?.line || error.lineNumber
94+
// Subtract 2 lines as the line does not includes the '\n\n' offset
95+
const lineNumber = (origin?.line || error.lineNumber - 2)
9696
const lineNumberPlaceholder = ' '.repeat(lineNumber.toString().length)
9797

98-
console.log(
99-
'\n' +
100-
chalk.bold(error.errorMessage) +
101-
'\n' +
102-
(origin ? chalk.dim(` in ${origin.file}\n`) : "") +
103-
chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) +
104-
line.split('\n')[error.lineNumber - 1] +
105-
'\n' +
106-
chalk.blue(` ${lineNumberPlaceholder} |\n`) +
107-
chalk.dim('This error occurred while aos was evaluating the submitted code.')
108-
)
98+
if (origin) {
99+
console.log(
100+
'\n' +
101+
chalk.bold(error.errorMessage) +
102+
'\n' +
103+
(origin ? chalk.dim(` in ${origin.file}\n`) : "") +
104+
chalk.blue(` ${lineNumberPlaceholder} |\n ${lineNumber} | `) +
105+
line.split('\n')[lineNumber + 1] +
106+
'\n' +
107+
chalk.blue(` ${lineNumberPlaceholder} |\n`) +
108+
chalk.dim('This error occurred while aos was evaluating the submitted code.')
109+
)
110+
} else {
111+
console.log(
112+
'\n' +
113+
chalk.bold(`Error on line ${lineNumber}: ${error.errorMessage}`) +
114+
'\n' +
115+
chalk.dim('This error occurred while aos was evaluating the submitted code.')
116+
)
117+
}
109118
}

0 commit comments

Comments
 (0)